File: btl_portals_compat_utcp.c

package info (click to toggle)
openmpi 1.1-2.3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 39,124 kB
  • ctags: 22,534
  • sloc: ansic: 216,698; sh: 22,541; makefile: 6,921; cpp: 5,562; asm: 3,160; lex: 375; objc: 365; perl: 347; csh: 89; tcl: 12; f90: 5
file content (278 lines) | stat: -rw-r--r-- 10,443 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
 * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
 *                         University Research and Technology
 *                         Corporation.  All rights reserved.
 * Copyright (c) 2004-2005 The University of Tennessee and The University
 *                         of Tennessee Research Foundation.  All rights
 *                         reserved.
 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart, 
 *                         University of Stuttgart.  All rights reserved.
 * Copyright (c) 2004-2005 The Regents of the University of California.
 *                         All rights reserved.
 * $COPYRIGHT$
 * 
 * Additional copyrights may follow
 * 
 * $HEADER$
 */


#include "ompi_config.h"

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <netinet/in.h>

#include "ompi/constants.h"
#include "opal/util/output.h"
#include "ompi/proc/proc.h"
#include "ompi/mca/pml/base/pml_base_module_exchange.h"

#include "btl_portals.h"
#include "btl_portals_compat.h"

#include <p3api/debug.h>

/* how's this for source code diving? - find private method for
   getting interface */
extern int p3tcp_my_nid(const char *if_str, unsigned int *nid);

static bool use_modex = true;

int
mca_btl_portals_init_compat(mca_btl_portals_component_t *comp)
{
    ptl_process_id_t info;
    int ret, max_interfaces;

    /* if the environment variables for the utcp implementation are
       already set, assume the user is running without the full Open
       RTE and is doing RTE testing for a more tightly-coupled
       platform (like, say, Red Storm).  Otherwise, be nice and use
       the modex to setup everything for the user */
    if (NULL == getenv("PTL_MY_RID")) {
        use_modex = true;
    } else {
        use_modex = false;
    }

    if (use_modex) {
        unsigned int nid;

        p3tcp_my_nid(mca_btl_portals_component.portals_ifname, &nid);

        /* post our contact info in the registry */
        info.nid = htonl(nid);
        info.pid = htonl((ptl_pid_t) getpid());
        opal_output_verbose(100, mca_btl_portals_component.portals_output,
                            "contact info: %u, %u", ntohl(info.nid), 
                            ntohl(info.pid));

        ret = mca_pml_base_modex_send(&mca_btl_portals_component.super.btl_version,
                                      &info, sizeof(ptl_process_id_t));
        if (OMPI_SUCCESS != ret) {
            opal_output_verbose(10, mca_btl_portals_component.portals_output,
                                "mca_pml_base_modex_send failed: %d", ret);
            return ret;
        }
    } else {
        /*
         * Initialize Portals interface
         */
        ret = PtlInit(&max_interfaces);
        if (PTL_OK != ret) {
            opal_output_verbose(10, mca_btl_portals_component.portals_output,
                                "PtlInit failed, returning %d\n", ret);
            return OMPI_ERR_FATAL;
        }

        /* tell the UTCP runtime code to read the env variables */
        PtlSetRank(PTL_INVALID_HANDLE, -1, -1);

        /*
         * Initialize a network device
         */
        ret = PtlNIInit(PTL_IFACE_DEFAULT, /* interface to initialize */
                        PTL_PID_ANY,       /* let library assign our pid */
                        NULL,              /* no desired limits */
                        NULL,              /* no need to have limits around */
                        &mca_btl_portals_module.portals_ni_h  /* our interface handle */
                        );
        if (PTL_OK != ret) {
            opal_output_verbose(10, mca_btl_portals_component.portals_output,
                                "PtlNIInit failed, returning %d\n", ret);
            return OMPI_ERR_FATAL;
        }
    }

    return OMPI_SUCCESS;
}


int
mca_btl_portals_add_procs_compat(struct mca_btl_portals_module_t* btl,
                                 size_t nprocs, struct ompi_proc_t **procs,
                                 ptl_process_id_t **portals_procs)
{
    int ret;

    if (use_modex) {
        int my_rid = 0;
        ptl_process_id_t *info;
        char *nidmap = NULL;
        char *pidmap = NULL;
        char *nid_str;
        char *pid_str;
        const size_t map_size = nprocs * 12 + 1; /* 12 is max length of long in decimal */
        size_t size, i;
        char *tmp;
        ompi_proc_t* proc_self = ompi_proc_local();
        int max_interfaces;

        /*
         * Do all the NID/PID map setup
         */
        /* each nid is a int, so need 10 there, plus the : */
        nidmap = malloc(map_size);
        pidmap = malloc(map_size);
        nid_str = malloc(12 + 1);
        pid_str = malloc(12 + 1);
        if (NULL == nidmap || NULL == pidmap || 
            NULL == nid_str || NULL == pid_str)
            return OMPI_ERROR;

        /* get space for the portals procs list */
        *portals_procs = calloc(nprocs, sizeof(ptl_process_id_t));
        if (NULL == *portals_procs) {
            opal_output_verbose(10, mca_btl_portals_component.portals_output,
                                "calloc(nprocs, sizeof(ptl_process_id_t)) failed");
            return OMPI_ERR_TEMP_OUT_OF_RESOURCE;
        }
         
        for (i = 0 ; i < nprocs ; ++i) {
            if (proc_self == procs[i]) my_rid = i;

            ret = mca_pml_base_modex_recv(&mca_btl_portals_component.super.btl_version, 
                                          procs[i], (void**) &info, &size);
            if (OMPI_SUCCESS != ret) {
                opal_output_verbose(10, mca_btl_portals_component.portals_output,
                                    "mca_pml_base_modex_recv failed: %d", ret);
                return ret;
            } else if (sizeof(ptl_process_id_t) != size) {
                opal_output_verbose(10, mca_btl_portals_component.portals_output,
                                    "mca_pml_base_modex_recv returned size %d, expected %d", 
                                    size, sizeof(ptl_process_id_t));
                return OMPI_ERROR;
            }

            if (i == 0) {
                snprintf(nidmap, map_size, "%u", ntohl(info->nid));
                snprintf(pidmap, map_size, "%u", ntohl(info->pid));
            } else {
                snprintf(nid_str, 12 + 1, ":%u", ntohl(info->nid));
                snprintf(pid_str, 12 + 1, ":%u", ntohl(info->pid));
                strncat(nidmap, nid_str, 12);
                strncat(pidmap, pid_str, 12);
            }

            /* update my local array of proc structs */
            (*portals_procs)[i].nid = ntohl(info->nid);
            (*portals_procs)[i].pid = ntohl(info->pid);

            free(info);
        }

        opal_output_verbose(100, mca_btl_portals_component.portals_output,
                            "my rid: %u", my_rid);
        opal_output_verbose(100, mca_btl_portals_component.portals_output,
                            "nid map: %s", nidmap);
        opal_output_verbose(100, mca_btl_portals_component.portals_output,
                            "pid map: %s", pidmap);
        opal_output_verbose(100, mca_btl_portals_component.portals_output,
                            "iface: %s",
                            mca_btl_portals_component.portals_ifname);

        asprintf(&tmp, "PTL_MY_RID=%u", my_rid);
        putenv(tmp);
        asprintf(&tmp, "PTL_NIDMAP=%s", nidmap);
        putenv(tmp);
        asprintf(&tmp, "PTL_PIDMAP=%s", pidmap);
        putenv(tmp);
        asprintf(&tmp, "PTL_IFACE=%s", mca_btl_portals_component.portals_ifname);
        putenv(tmp);

        free(pidmap);
        free(nidmap);
        free(pid_str);
        free(nid_str);

        /*
         * Initialize Portals
         */
        ret = PtlInit(&max_interfaces);
        if (PTL_OK != ret) {
            opal_output_verbose(10, mca_btl_portals_component.portals_output,
                                "PtlInit failed, returning %d\n", ret);
            return OMPI_ERR_FATAL;
        }

        /* tell the UTCP runtime code to read the env variables */
        PtlSetRank(PTL_INVALID_HANDLE, -1, -1);

        ret = PtlNIInit(PTL_IFACE_DEFAULT, /* interface to initialize */
                        PTL_PID_ANY,       /* let library assign our pid */
                        NULL,              /* no desired limits */
                        NULL,    /* save our limits somewhere */
                        &(mca_btl_portals_module.portals_ni_h)  /* our interface handle */
                        );
        if (PTL_OK != ret) {
            opal_output_verbose(10, mca_btl_portals_component.portals_output,
                                "PtlNIInit failed, returning %d\n", ret);
            return OMPI_ERR_FATAL;
        }
    } else { /* use_modex */
        unsigned int nptl_procs, rank, i;

        /*
         */
        ret = PtlGetRank(mca_btl_portals_module.portals_ni_h, &rank, &nptl_procs);
        if (ret != PTL_OK) {
            opal_output_verbose(10, mca_btl_portals_component.portals_output,
                                "PtlGetRank() returned %d", ret);
            return OMPI_ERR_FATAL;
        } else if (nptl_procs != nprocs) {
            opal_output_verbose(10, mca_btl_portals_component.portals_output,
                                "nptl_procs != nprocs (%d, %d)", nptl_procs,
                                nprocs);
            return OMPI_ERR_FATAL;
        }

        /* create enough space for all the proc info structs */
        *portals_procs = calloc(nprocs, sizeof(ptl_process_id_t));
        if (NULL == *portals_procs) {
            opal_output_verbose(10, mca_btl_portals_component.portals_output,
                                "calloc(nprocs, sizeof(ptl_process_id_t)) failed");
            return OMPI_ERR_TEMP_OUT_OF_RESOURCE;
        }
        /* fill in all the proc info structs */
        for (i = 0 ; i < nprocs ; ++i) {
            ret = PtlGetRankId(mca_btl_portals_module.portals_ni_h,
                               i, &((*portals_procs)[i]));
            if (PTL_OK != ret) {
                opal_output_verbose(10,
                                    mca_btl_portals_component.portals_output,
                                    "PtlGetRankId(%d) failed: %d\n", i, ret);
                return OMPI_ERR_FATAL;
            }
        }
    }

#if 0
    PtlNIDebug(mca_btl_portals_module.portals_ni_h, PTL_DBG_ALL);
#endif

    return OMPI_SUCCESS;
}