Debian Codegraph @

psm_ep.c

raw ◀ ▶
Source reconstructed at patch state 1 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /*
2 * Copyright (c) 2013. Intel Corporation. All rights reserved.
3 * Copyright (c) 2006-2012. QLogic Corporation. All rights reserved.
4 * Copyright (c) 2003-2006, PathScale, Inc. All rights reserved.
5 *
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33 */
34
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38 #include <sched.h> // cpu_set
39 #include <ctype.h> // isalpha
40
41 #include "psm_user.h"
42 #include "psm_mq_internal.h"
43 #include "psm_am_internal.h"
44
45 /*
46 * Endpoint management
47 */
48 psm_ep_t psmi_opened_endpoint = NULL;
49 int psmi_opened_endpoint_count = 0;
50
51 static psm_error_t psmi_ep_open_device(const psm_ep_t ep,
52 const struct psm_ep_open_opts *opts,
53 const psm_uuid_t unique_job_key,
54 struct psmi_context *context,
55 psm_epid_t *epid);
56
57 /*
58 * Device managment
59 *
60 * PSM uses "devices" as components to manage communication to self, to peers
61 * reachable via shared memory and finally to peers reachable only through
62 * ipath.
63 *
64 * By default, PSMI_DEVICES_DEFAULT establishes the bind order a component is
65 * tested for reachability to each peer. First self, then shm and finally
66 * ipath. The order should really only affect endpoints that happen to be on
67 * the same node. PSM will correctly detect that two endpoints are on the same
68 * node even though they may be using different host interfaces.
69 */
70
71 #define PSMI_DEVICES_DEFAULT "self,shm,ipath"
72 static psm_error_t psmi_parse_devices(int devices[PTL_MAX_INIT],
73 const char *devstr);
74 static int psmi_device_is_enabled(const int devices[PTL_MAX_INIT],
75 int devid);
76 int psmi_ep_device_is_enabled(const psm_ep_t ep, int devid);
77
78 psm_error_t
79 __psm_ep_num_devunits(uint32_t *num_units_o)
80 {
81 static int num_units = -1;
82
83 PSMI_ERR_UNLESS_INITIALIZED(NULL);
84
85 if (num_units == -1) {
86 num_units = ipath_get_num_units();
87 if (num_units == -1)
88 num_units = 0;
89 }
90
91 *num_units_o = (uint32_t) num_units;
92 return PSM_OK;
93 }
94 PSMI_API_DECL(psm_ep_num_devunits)
95
96 static int
97 cmpfunc(const void *p1, const void *p2)
98 {
99 uint64_t a = ((uint64_t *)p1)[0];
100 uint64_t b = ((uint64_t *)p2)[0];
101 if (a < b) return -1;
102 if (a == b) return 0;
103 return 1;
104 }
105 static psm_error_t
106 psmi_ep_multirail(int *num_rails, uint32_t *unit, uint16_t *port)
107 {
108 uint32_t num_units;
109 uint64_t gid_hi, gid_lo;
110 int i, j, ret, count=0;
111 char *env;
112 psm_error_t err = PSM_OK;
113 uint64_t gidh[IPATH_MAX_UNIT][3];
114
115 env = getenv("PSM_MULTIRAIL");
116 if (!env || atoi(env) == 0) {
117 *num_rails = 0;
118 return err;
119 }
120 #ifdef __MIC__
121 env = getenv("MPI_LOCALRANKID");
122 if (!env || atoi(env) == 0) {
123 _IPATH_INFO("PSM_MULTIRAIL is not supported and "
124 "ignored for this PSM mic version.\n");
125 }
126 *num_rails = 0;
127 return err;
128 #endif
129
130 /*
131 * map is in format: unit:port,unit:port,...
132 */
133 if ((env = getenv("PSM_MULTIRAIL_MAP"))) {
134 if (sscanf(env, "%d:%d", &i, &j) == 2) {
135 char *comma = strchr(env, ',');
136 unit[count] = i;
137 port[count] = j;
138 count++;
139 while (comma) {
140 if (sscanf(comma, ",%d:%d", &i, &j) != 2) {
141 break;
142 }
143 unit[count] = i;
144 port[count] = j;
145 count++;
146 if (count == IPATH_MAX_UNIT) break;
147 comma = strchr(comma+1, ',');
148 }
149 }
150 *num_rails = count;
151
152 /*
153 * Check if any of the port is not usable.
154 */
155 for (i = 0; i < count; i++) {
156 ret = ipath_get_port_lid(unit[i], port[i]);
157 if (ret == -1) {
158 err = psmi_handle_error(NULL, PSM_EP_DEVICE_FAILURE,
159 "Couldn't get lid for unit %d:%d",
160 unit[i], port[i]);
161 return err;
162 }
163 ret = ipath_get_port_gid(unit[i], port[i], &gid_hi, &gid_lo);
164 if (ret == -1) {
165 err = psmi_handle_error(NULL, PSM_EP_DEVICE_FAILURE,
166 "Couldn't get gid for unit %d:%d",
167 unit[i], port[i]);
168 return err;
169 }
170 }
171
172 return err;
173 }
174
175 if ((err = psm_ep_num_devunits(&num_units))) {
176 return err;
177 }
178 if (num_units > IPATH_MAX_UNIT) {
179 _IPATH_INFO("Found %d units, max %d units are supported, use %d\n",
180 num_units, IPATH_MAX_UNIT, IPATH_MAX_UNIT);
181 num_units = IPATH_MAX_UNIT;
182 }
183
184 /*
185 * Get all the ports with a valid lid and gid, one per unit.
186 * we don't know which number is a valid unit, we just loop
187 * over all supported numbers.
188 */
189 for (i = 0; i < IPATH_MAX_UNIT; i++) {
190 for (j = 1; j <= IPATH_MAX_PORT; j++) {
191 ret = ipath_get_port_lid(i, j);
192 if (ret == -1) continue;
193 ret = ipath_get_port_gid(i, j, &gid_hi, &gid_lo);
194 if (ret == -1) continue;
195
196 gidh[count][0] = gid_hi;
197 gidh[count][1] = i;
198 gidh[count][2] = j;
199 count++;
200 break;
201 }
202 if (count == num_units) break;
203 }
204
205 /*
206 * Sort all the ports with gidh from small to big.
207 * This is for multiple fabrics, and we use fabric with the
208 * smallest gid to make the master connection.
209 */
210 qsort(gidh, count, sizeof(uint64_t)*3, cmpfunc);
211
212 for (i = 0; i < count; i++) {
213 unit[i] = (uint32_t)gidh[i][1];
214 port[i] = (uint16_t)(uint32_t)gidh[i][2];
215 }
216 *num_rails = count;
217 return err;
218 }
219
220 static psm_error_t
221 psmi_ep_devlids(uint16_t **lids, uint32_t *num_lids_o,
222 uint64_t my_gid_hi, uint64_t my_gid_lo)
223 {
224 static uint16_t *ipath_lids = NULL;
225 static uint32_t nlids;
226 uint32_t num_units;
227 int i;
228 psm_error_t err = PSM_OK;
229
230 PSMI_ERR_UNLESS_INITIALIZED(NULL);
231
232 if (ipath_lids == NULL) {
233 if ((err = psm_ep_num_devunits(&num_units)))
234 goto fail;
235 ipath_lids = (uint16_t *)
236 psmi_calloc(PSMI_EP_NONE, UNDEFINED, num_units*IPATH_MAX_PORT,
237 sizeof(uint16_t));
238 if (ipath_lids == NULL) {
239 err = psmi_handle_error(NULL, PSM_NO_MEMORY,
240 "Couldn't allocate memory for dev_lids structure");
241 goto fail;
242 }
243
244 for (i = 0; i < IPATH_MAX_UNIT; i++) {
245 int j;
246 for (j = 1; j <= IPATH_MAX_PORT; j++) {
247 int lid = ipath_get_port_lid(i, j);
248 int ret;
249 uint64_t gid_hi = 0, gid_lo = 0;
250
251 if (lid == -1) continue;
252 ret = ipath_get_port_gid(i, j, &gid_hi, &gid_lo);
253 if (ret == -1)
254 continue;
255 else if (my_gid_hi != gid_hi) {
256 _IPATH_VDBG("LID %d, unit %d, port %d, "
257 "mismatched GID %llx:%llx and "
258 "%llx:%llx\n",
259 lid, i, j,
260 (unsigned long long) gid_hi,
261 (unsigned long long) gid_lo,
262 (unsigned long long) my_gid_hi,
263 (unsigned long long) my_gid_lo);
264 continue;
265 }
266 _IPATH_VDBG("LID %d, unit %d, port %d, "
267 "matching GID %llx:%llx and "
268 "%llx:%llx\n", lid, i, j,
269 (unsigned long long) gid_hi,
270 (unsigned long long) gid_lo,
271 (unsigned long long) my_gid_hi,
272 (unsigned long long) my_gid_lo);
273
274 ipath_lids[nlids++] = (uint16_t) lid;
275 }
276 }
277 if (nlids == 0) {
278 err = psmi_handle_error(NULL, PSM_EP_DEVICE_FAILURE,
279 "Couldn't get lid&gid from any unit/port");
280 goto fail;
281 }
282 }
283 *lids = ipath_lids;
284 *num_lids_o = nlids;
285
286 fail:
287 return err;
288 }
289
290 uint64_t
291 __psm_epid_nid(psm_epid_t epid)
292 {
293 return PSMI_EPID_GET_LID(epid);
294 }
295 PSMI_API_DECL(psm_epid_nid)
296
297 /* Currently not exposed to users, we don't acknowledge the existence of
298 * subcontexts */
299 uint64_t
300 psmi_epid_subcontext(psm_epid_t epid)
301 {
302 return PSMI_EPID_GET_SUBCONTEXT(epid);
303 }
304
305 /* Currently not exposed to users, we don't acknowledge the existence of
306 * service levels and HCA types encoding within epids. This may require
307 * changing to expose SLs
308 */
309 uint64_t
310 psmi_epid_hca_type(psm_epid_t epid)
311 {
312 return PSMI_EPID_GET_HCATYPE(epid);
313 }
314
315 uint64_t
316 psmi_epid_sl(psm_epid_t epid)
317 {
318 return PSMI_EPID_GET_SL(epid);
319 }
320
321 uint64_t
322 __psm_epid_context(psm_epid_t epid)
323 {
324 return PSMI_EPID_GET_CONTEXT(epid);
325 }
326 PSMI_API_DECL(psm_epid_context)
327
328 uint64_t
329 __psm_epid_port(psm_epid_t epid)
330 {
331 return __psm_epid_context(epid);
332 }
333 PSMI_API_DECL(psm_epid_port)
334
335 psm_error_t
336 __psm_ep_query (int *num_of_epinfo, psm_epinfo_t *array_of_epinfo)
337 {
338 psm_error_t err = PSM_OK;
339 int i;
340 psm_ep_t ep;
341
342 PSMI_ERR_UNLESS_INITIALIZED(NULL);
343
344 if (*num_of_epinfo <= 0) {
345 err = psmi_handle_error(NULL, PSM_PARAM_ERR,
346 "Invalid psm_ep_query parameters");
347 return err;
348 }
349
350 if (psmi_opened_endpoint == NULL) {
351 err = psmi_handle_error(NULL, PSM_EP_WAS_CLOSED,
352 "PSM Endpoint is closed or does not exist");
353 return err;
354 }
355
356 ep = psmi_opened_endpoint;
357 for (i = 0; i < *num_of_epinfo; i++) {
358 if (ep == NULL) break;
359 array_of_epinfo[i].ep = ep;
360 array_of_epinfo[i].epid = ep->epid;
361 memcpy(array_of_epinfo[i].uuid,
362 (void *) ep->key, sizeof(psm_uuid_t));
363 psmi_uuid_unparse(ep->key, array_of_epinfo[i].uuid_str);
364 ep = ep->user_ep_next;
365 }
366 *num_of_epinfo = i;
367
368 return err;
369 }
370 PSMI_API_DECL(psm_ep_query)
371
372 psm_error_t
373 __psm_ep_epid_lookup (psm_epid_t epid, psm_epconn_t *epconn)
374 {
375 psm_error_t err = PSM_OK;
376 psm_epaddr_t epaddr;
377 psm_ep_t ep;
378
379 PSMI_ERR_UNLESS_INITIALIZED(NULL);
380
381 /* Need to have an opened endpoint before we can resolve epids */
382 if (psmi_opened_endpoint == NULL) {
383 err = psmi_handle_error(NULL, PSM_EP_WAS_CLOSED,
384 "PSM Endpoint is closed or does not exist");
385 return err;
386 }
387
388 ep = psmi_opened_endpoint;
389 while (ep) {
390 epaddr = psmi_epid_lookup(ep, epid);
391 if (!epaddr) {
392 /* Search over SL values for bug 122239. Note that function
393 * ips_get_addr_from_epid() converts a base epid to an epaddr,
394 * which can then be used to get the correct epid for this flow.
395 * However, that function is at the IPS level and not accessible
396 * from here without breaking the layering. */
397 uint64_t lid, context, subcontext, hca_type, sl, try_sl;
398 psm_epid_t try_epid;
399 lid = PSMI_EPID_GET_LID(epid);
400 context = PSMI_EPID_GET_CONTEXT(epid);
401 subcontext = PSMI_EPID_GET_SUBCONTEXT(epid);
402 hca_type = PSMI_EPID_GET_HCATYPE(epid);
403 sl = PSMI_EPID_GET_SL(epid);
404 for (try_sl = 0; !epaddr && try_sl < 16; try_sl++) {
405 if (try_sl != sl) {
406 try_epid = PSMI_EPID_PACK_EXT(lid, context, subcontext,
407 hca_type, try_sl);
408 epaddr = psmi_epid_lookup(psmi_opened_endpoint, try_epid);
409 }
410 }
411
412 if (!epaddr) {
413 ep = ep->user_ep_next;
414 continue;
415 }
416 }
417
418 /* Found connection for epid. Return info about endpoint to caller. */
419 psmi_assert_always(epaddr->ep == ep);
420 epconn->addr = epaddr;
421 epconn->ep = epaddr->ep;
422 epconn->mq = epaddr->ep->mq;
423 return err;
424 }
425
426 err = psmi_handle_error(NULL, PSM_EPID_UNKNOWN,
427 "Endpoint connection status unknown");
428 return err;
429 }
430 PSMI_API_DECL(psm_ep_epid_lookup);
431
432 psm_error_t
433 __psm_ep_epid_share_memory(psm_ep_t ep, psm_epid_t epid, int *result_o)
434 {
435 uint32_t num_lids = 0;
436 uint16_t *lids = NULL;
437 int i;
438 uint16_t epid_lid;
439 int result = 0;
440 psm_error_t err;
441
442 psmi_assert_always(ep != NULL);
443 PSMI_ERR_UNLESS_INITIALIZED(ep);
444
445 epid_lid = (uint16_t) psm_epid_nid(epid);
446 /* If we're in non-ipath mode, done bother listing lids */
447 if (!psmi_ep_device_is_enabled(ep, PTL_DEVID_IPS)) {
448 uint64_t mylid = (uint16_t) psm_epid_nid(ep->epid);
449 if (mylid == epid_lid)
450 result = 1;
451 }
452 else {
453 err = psmi_ep_devlids(&lids, &num_lids, ep->gid_hi, ep->gid_lo);
454 if (err)
455 return err;
456 for (i = 0; i < num_lids; i++) {
457 if (epid_lid == lids[i]) {
458 result = 1;
459 break;
460 }
461 }
462 }
463 *result_o = result;
464 return PSM_OK;
465 }
466 PSMI_API_DECL(psm_ep_epid_share_memory)
467
468 #define PSMI_EP_OPEN_SHM_MBYTES_MIN 2
469 #define PSMI_EP_OPEN_PKEY_MASK 0x7fffULL
470
471 psm_error_t
472 __psm_ep_open_opts_get_defaults(struct psm_ep_open_opts *opts)
473 {
474 union psmi_envvar_val nSendBuf;
475 union psmi_envvar_val netPKey;
476 #if (PSM_VERNO >= 0x010d)
477 union psmi_envvar_val env_path_service_id;
478 union psmi_envvar_val env_path_res_type;
479 #endif
480 #if (PSM_VERNO >= 0x010e)
481 union psmi_envvar_val nSendDesc;
482 union psmi_envvar_val immSize;
483 #endif
484
485 PSMI_ERR_UNLESS_INITIALIZED(NULL);
486
487 /* Get number of default send buffers from environment */
488 psmi_getenv("PSM_NUM_SEND_BUFFERS",
489 "Number of send buffers to allocate [1024]",
490 PSMI_ENVVAR_LEVEL_USER,
491 PSMI_ENVVAR_TYPE_UINT,
492 (union psmi_envvar_val) 1024,
493 &nSendBuf);
494
495 /* Get network key from environment. MVAPICH and other vendor MPIs do not
496 * specify it on ep open and we may require it for vFabrics.
497 */
498 psmi_getenv("PSM_PKEY",
499 "Infiniband PKey to use for endpoint",
500 PSMI_ENVVAR_LEVEL_USER,
501 PSMI_ENVVAR_TYPE_ULONG,
502 (union psmi_envvar_val) IPATH_DEFAULT_P_KEY,
503 &netPKey);
504
505 #if (PSM_VERNO >= 0x010d)
506 /* Get Service ID from environment */
507 psmi_getenv("PSM_IB_SERVICE_ID",
508 "IB Service ID for path resolution",
509 PSMI_ENVVAR_LEVEL_USER,
510 PSMI_ENVVAR_TYPE_ULONG_ULONG,
511 (union psmi_envvar_val) IPATH_DEFAULT_SERVICE_ID,
512 &env_path_service_id);
513
514 /* Get Path resolution type from environment Possible choices are:
515 *
516 * NONE : Default same as previous instances. Utilizes static data.
517 * OPP : Use OFED Plus Plus library to do path record queries.
518 * UMAD : Use raw libibumad interface to form and process path records.
519 * ANY : Try all available path record mechanisms.
520 */
521 psmi_getenv("PSM_PATH_REC",
522 "Mechanism to query IB path record (default is no path query)",
523 PSMI_ENVVAR_LEVEL_USER, PSMI_ENVVAR_TYPE_STR,
524 (union psmi_envvar_val) "none", &env_path_res_type);
525 #endif
526
527 #if (PSM_VERNO >= 0x010e)
528 /* Get numner of send descriptors - by default this is 4 times the number
529 * of send buffers - mainly used for short/inlined messages.
530 */
531 psmi_getenv("PSM_NUM_SEND_DESCRIPTORS",
532 "Number of send descriptors to allocate [4096]",
533 PSMI_ENVVAR_LEVEL_USER,
534 PSMI_ENVVAR_TYPE_UINT,
535 (union psmi_envvar_val) (nSendBuf.e_uint << 2),
536 &nSendDesc);
537
538 /* Get immediate data size - transfers less than immediate data size do
539 * not consume a send buffer and require just a send descriptor.
540 */
541 psmi_getenv("PSM_SEND_IMMEDIATE_SIZE",
542 "Immediate data send size not requiring a buffer [128]",
543 PSMI_ENVVAR_LEVEL_USER,
544 PSMI_ENVVAR_TYPE_UINT,
545 (union psmi_envvar_val) 128,
546 &immSize);
547 #endif
548
549 opts->timeout = 30000000000LL; /* 30 sec */
550 opts->unit = IPATH_UNIT_ID_ANY;
551 opts->port = 0;
552 opts->outsl = PSMI_SL_DEFAULT;
553 #if (PSM_VERNO >= 0x0107) && (PSM_VERNO <= 0x010a)
554 opts->outvl = 0;
555 #endif
556 opts->affinity = PSM_EP_OPEN_AFFINITY_SET;
557 opts->shm_mbytes = 10;
558 opts->sendbufs_num = nSendBuf.e_uint;
559 opts->network_pkey = (uint64_t) netPKey.e_ulong;
560 #if (PSM_VERNO >= 0x010d)
561 opts->service_id = (uint64_t) env_path_service_id.e_ulonglong;
562
563 if (!strcasecmp(env_path_res_type.e_str, "none"))
564 opts->path_res_type = PSM_PATH_RES_NONE;
565 else if (!strcasecmp(env_path_res_type.e_str, "opp"))
566 opts->path_res_type = PSM_PATH_RES_OPP;
567 else if (!strcasecmp(env_path_res_type.e_str, "umad"))
568 opts->path_res_type = PSM_PATH_RES_UMAD;
569 else {
570 _IPATH_ERROR("Unknown path resolution type %s. Disabling use of path record query.\n", env_path_res_type.e_str);
571 opts->path_res_type = PSM_PATH_RES_NONE;
572 }
573 #endif
574 #if (PSM_VERNO >= 0x010e)
575 opts->senddesc_num = nSendDesc.e_uint;
576 opts->imm_size = immSize.e_uint;
577 #endif
578
579 return PSM_OK;
580 }
581 PSMI_API_DECL(psm_ep_open_opts_get_defaults)
582
583 psm_error_t psmi_poll_noop(ptl_t *ptl, int replyonly);
584
585 psm_error_t
586 __psm_ep_open_internal(psm_uuid_t const unique_job_key, int *devid_enabled,
587 struct psm_ep_open_opts const *opts_i, psm_mq_t mq,
588 psm_ep_t *epo, psm_epid_t *epido)
589 {
590 psm_ep_t ep = NULL;
591 uint32_t num_units;
592 size_t len;
593 psm_error_t err;
594 psm_epaddr_t epaddr = NULL;
595 char buf[128], *p, *e;
596 char *old_cpuaff = NULL, *old_unit = NULL;
597 union psmi_envvar_val yield_cnt, no_cpuaff, env_unit_id,
598 env_port_id, env_sl;
599 size_t ptl_sizes;
600 int default_cpuaff;
601 struct psm_ep_open_opts opts;
602 ptl_t *amsh_ptl, *ips_ptl, *self_ptl;
603 int i;
604
605 /* First get the set of default options, we overwrite with the user's
606 * desired values afterwards */
607 if ((err = psm_ep_open_opts_get_defaults(&opts)))
608 goto fail;
609
610 if (opts_i != NULL) {
611 if (opts_i->timeout != -1)
612 opts.timeout = opts_i->timeout;
613 if (opts_i->unit != -1)
614 opts.unit = opts_i->unit;
615 if (opts_i->affinity != -1)
616 opts.affinity = opts_i->affinity;
617 if (opts_i->shm_mbytes != -1)
618 opts.shm_mbytes = opts_i->shm_mbytes;
619 if (opts_i->sendbufs_num != -1)
620 opts.sendbufs_num = opts_i->sendbufs_num;
621 if (psmi_verno_client() >= PSMI_VERNO_MAKE(1,1)) {
622 if ((opts_i->network_pkey & PSMI_EP_OPEN_PKEY_MASK) !=
623 PSMI_EP_OPEN_PKEY_MASK)
624 opts.network_pkey = opts_i->network_pkey;
625 }
626 if (psmi_verno_client() >= PSMI_VERNO_MAKE(1,7)) {
627 /* these values are sanity checked below */
628 opts.port = opts_i->port;
629 opts.outsl = opts_i->outsl;
630 #if (PSM_VERNO >= 0x0107) && (PSM_VERNO <= 0x010a)
631 opts.outvl = opts_i->outvl;
632 #endif
633 }
634 #if (PSM_VERNO >= 0x010d)
635 /* Note: Environment variable specification for service ID and
636 * path resolition type takes precedence over ep_open defaults.
637 */
638 if (psmi_verno_client() >= 0x010d) {
639 if (opts_i->service_id)
640 opts.service_id = (uint64_t) opts_i->service_id;
641 if (opts.path_res_type == PSM_PATH_RES_NONE)
642 opts.path_res_type = opts_i->path_res_type;
643 }
644 #endif
645
646 #if (PSM_VERNO >= 0x010e)
647 if (psmi_verno_client() >= 0x010e) {
648 if (opts_i->senddesc_num)
649 opts.senddesc_num = opts_i->senddesc_num;
650 if (opts_i->imm_size)
651 opts.imm_size = opts_i->imm_size;
652 }
653 #endif
654 }
655
656 if (psmi_device_is_enabled(devid_enabled, PTL_DEVID_IPS)) {
657 if ((err = psm_ep_num_devunits(&num_units)) != PSM_OK)
658 goto fail;
659 } else num_units = 0;
660
661 /* do some error checking */
662 if (opts.timeout < -1) {
663 err = psmi_handle_error(NULL, PSM_PARAM_ERR,
664 "Invalid timeout value %lld",
665 (long long) opts.timeout);
666 goto fail;
667 } else if (num_units && (opts.unit < -1 || opts.unit >= IPATH_MAX_UNIT)) {
668 err = psmi_handle_error(NULL, PSM_PARAM_ERR,
669 "Invalid Device Unit ID %d (%d units found)",
670 opts.unit, num_units);
671 goto fail;
672 } else if (opts.affinity < 0 || opts.affinity > PSM_EP_OPEN_AFFINITY_FORCE) {
673 err = psmi_handle_error(NULL, PSM_PARAM_ERR,
674 "Invalid Affinity option: %d", opts.affinity);
675 goto fail;
676 } else if (opts.shm_mbytes < PSMI_EP_OPEN_SHM_MBYTES_MIN) {
677 err = psmi_handle_error(NULL, PSM_PARAM_ERR,
678 "Invalid shm_mbytes option at %d mbytes (minimum is %d)",
679 opts.shm_mbytes, PSMI_EP_OPEN_SHM_MBYTES_MIN);
680 goto fail;
681 }
682
683 /* Advertise in verbose env the fact that we parse the no-affinity
684 * variable. */
685 default_cpuaff = psmi_getenv("IPATH_NO_CPUAFFINITY",
686 "Prevent PSM from setting affinity",
687 PSMI_ENVVAR_LEVEL_USER,
688 PSMI_ENVVAR_TYPE_YESNO,
689 PSMI_ENVVAR_VAL_NO,
690 &no_cpuaff);
691
692 if (no_cpuaff.e_uint ||
693 (default_cpuaff && opts.affinity == PSM_EP_OPEN_AFFINITY_SKIP))
694 {
695 old_cpuaff = getenv("IPATH_NO_CPUAFFINITY");
696 setenv("IPATH_NO_CPUAFFINITY", "1", 1);
697 }
698
699 #ifdef __MIC__
700 /*
701 * On MIC, we always pick unit from /sys/class/qib/ipath/unit,
702 * but only do this if there is a HCA unit.
703 */
704 if (num_units > 0) {
705 char pathname[128];
706 struct stat st;
707 FILE *fp;
708
709 snprintf(pathname, sizeof(pathname),
710 "/sys/class/qib/ipath/unit");
711 fp = NULL;
712 if (stat(pathname, &st) || S_ISDIR(st.st_mode) ||
713 !(fp = fopen(pathname, "r")) || (fscanf(fp, "%d", &opts.unit) != 1)) {
714 err = psmi_handle_error(NULL, PSM_EP_DEVICE_FAILURE,
715 "Couldn't read from %s", pathname);
716 if (fp) fclose(fp);
717 goto fail;
718 }
719 fclose(fp);
720 psmi_assert(opts.unit != IPATH_UNIT_ID_ANY);
721 psmi_assert(opts.unit < IPATH_MAX_UNIT);
722 }
723 #else
724 /* If a specific unit is set in the environment, use that one. */
725 if (!psmi_getenv("IPATH_UNIT", "Device Unit number (-1 autodetects)",
726 PSMI_ENVVAR_LEVEL_USER, PSMI_ENVVAR_TYPE_LONG,
727 (union psmi_envvar_val) IPATH_UNIT_ID_ANY,
728 &env_unit_id)) {
729 opts.unit = env_unit_id.e_long;
730 /* set mock UNIT *just* for setaffinity */
731 if (opts.unit != IPATH_UNIT_ID_ANY) {
732 char buf[32];
733 snprintf(buf, sizeof buf - 1, "%d", (int) opts.unit);
734 buf[sizeof buf - 1] = '\0';
735 old_unit = getenv("IPATH_UNIT");
736 setenv("IPATH_UNIT", buf, 1);
737 }
738 else
739 unsetenv("IPATH_UNIT");
740 }
741 #endif
742
743 if (!psmi_getenv("IPATH_PORT", "IB Port number (<= 0 autodetects)",
744 PSMI_ENVVAR_LEVEL_USER, PSMI_ENVVAR_TYPE_LONG,
745 (union psmi_envvar_val)0,
746 &env_port_id)) {
747 opts.port = env_port_id.e_long;
748 }
749
750 if (!psmi_getenv("IPATH_SL", "IB outging ServiceLevel number (default 0)",
751 PSMI_ENVVAR_LEVEL_USER, PSMI_ENVVAR_TYPE_LONG,
752 (union psmi_envvar_val) PSMI_SL_DEFAULT,
753 &env_sl)) {
754 opts.outsl = env_sl.e_long;
755 }
756
757 #if (PSM_VERNO >= 0x0107) && (PSM_VERNO <= 0x010a)
758 {
759 union psmi_envvar_val env_vl;
760 if (!psmi_getenv("IPATH_VL", "IB outging VirtualLane (default 0)",
761 PSMI_ENVVAR_LEVEL_USER, PSMI_ENVVAR_TYPE_LONG,
762 (union psmi_envvar_val)0,
763 &env_vl)) {
764 opts.outvl = env_vl.e_long;
765 }
766 }
767 #endif
768
769 /* sanity check new capabilities, after both opts and env */
770 if (opts.port < 0 || opts.port > IPATH_MAX_PORT)
771 err = psmi_handle_error(NULL, PSM_PARAM_ERR,
772 "Invalid Port number: %lld",
773 (unsigned long long) opts.port);
774 if (opts.outsl < 0 || opts.outsl > 15)
775 err = psmi_handle_error(NULL, PSM_PARAM_ERR,
776 "Invalid SL number: %lld",
777 (unsigned long long) opts.outsl);
778
779 #if (PSM_VERNO >= 0x0107) && (PSM_VERNO <= 0x010a)
780 if (opts.outvl < 0 || opts.outvl > 7)
781 err = psmi_handle_error(NULL, PSM_PARAM_ERR,
782 "Invalid VL number: %lld",
783 (unsigned long long) opts.outvl);
784 #endif
785
786 ptl_sizes =
787 (psmi_device_is_enabled(devid_enabled, PTL_DEVID_SELF) ?
788 psmi_ptl_self.sizeof_ptl() : 0) +
789 (psmi_device_is_enabled(devid_enabled, PTL_DEVID_IPS) ?
790 psmi_ptl_ips.sizeof_ptl() : 0) +
791 (psmi_device_is_enabled(devid_enabled, PTL_DEVID_AMSH) ?
792 psmi_ptl_amsh.sizeof_ptl() : 0);
793 if (ptl_sizes == 0) return PSM_EP_NO_DEVICE;
794
795 ep = (psm_ep_t) psmi_calloc(PSMI_EP_NONE, UNDEFINED, 1,
796 sizeof(struct psm_ep) + ptl_sizes);
797 epaddr = (psm_epaddr_t) psmi_calloc(PSMI_EP_NONE, PER_PEER_ENDPOINT,
798 1, sizeof(struct psm_epaddr));
799 if (ep == NULL || epaddr == NULL) {
800 err = psmi_handle_error(NULL, PSM_NO_MEMORY,
801 "Couldn't allocate memory for %s structure",
802 ep == NULL ? "psm_ep" : "psm_epaddr");
803 goto fail;
804 }
805
806 /* Copy PTL enabled status */
807 for (i = 0; i < PTL_MAX_INIT; i++)
808 ep->devid_enabled[i] = devid_enabled[i];
809
810 /* Matched Queue initialization. We do this early because we have to
811 * make sure ep->mq exists and is valid before calling ips_do_work.
812 */
813 ep->mq = mq;
814
815 /* Get ready for PTL initialization */
816 memcpy(&ep->key, (void *) unique_job_key, sizeof(psm_uuid_t));
817 ep->epaddr = epaddr;
818 ep->shm_mbytes = opts.shm_mbytes;
819 ep->memmode = mq->memmode;
820 ep->ipath_num_sendbufs = opts.sendbufs_num;
821 ep->network_pkey = (uint16_t) opts.network_pkey & PSMI_EP_OPEN_PKEY_MASK;
822 #if (PSM_VERNO >= 0x010d)
823 ep->service_id = opts.service_id;
824 ep->path_res_type = opts.path_res_type;
825 #else
826 /* Select sane defaults with older PSM header */
827 ep->service_id = 0x1000117500000000ULL; /* Default service ID */
828 ep->path_res_type = 0; /* No path resolution */
829 #endif
830 #if (PSM_VERNO >= 0x010e)
831 ep->ipath_num_descriptors = opts.senddesc_num;
832 ep->ipath_imm_size = opts.imm_size;
833 #else
834 /* Default is 4 times more descriptors than buffers */
835 ep->ipath_num_descriptors = ep->ipath_num_sendbufs << 2;
836 ep->ipath_imm_size = 128;
837 #endif
838 ep->errh = psmi_errhandler_global; /* by default use the global one */
839 ep->ptl_amsh.ep_poll = psmi_poll_noop;
840 ep->ptl_ips.ep_poll = psmi_poll_noop;
841 ep->connections = 0;
842
843 /* Active message fields, used by psmi_shm_attach() */
844 ep->psmi_kassist_fd = -1;
845 ep->psmi_kassist_mode = 0;
846 ep->amsh_shmbase = 0;
847 ep->amsh_blockbase = 0;
848 ep->amsh_dirpage = NULL;
849 ep->amsh_keyname = NULL;
850 ep->amsh_shmfd = -1;
851 ep->amsh_shmidx = -1;
852 ep->amsh_max_idx = -1;
853
854 /* See how many iterations we want to spin before yielding */
855 psmi_getenv("PSM_YIELD_SPIN_COUNT",
856 "Spin poll iterations before yield",
857 PSMI_ENVVAR_LEVEL_HIDDEN,
858 PSMI_ENVVAR_TYPE_UINT,
859 (union psmi_envvar_val) PSMI_BLOCKUNTIL_POLLS_BEFORE_YIELD,
860 &yield_cnt);
861 ep->yield_spin_cnt = yield_cnt.e_uint;
862
863 ptl_sizes = 0;
864 amsh_ptl = ips_ptl = self_ptl = NULL;
865 if (psmi_ep_device_is_enabled(ep, PTL_DEVID_AMSH)) {
866 amsh_ptl = (ptl_t *) (ep->ptl_base_data + ptl_sizes);
867 ptl_sizes += psmi_ptl_amsh.sizeof_ptl();
868 }
869 if (psmi_ep_device_is_enabled(ep, PTL_DEVID_IPS)) {
870 ips_ptl = (ptl_t *) (ep->ptl_base_data + ptl_sizes);
871 ptl_sizes += psmi_ptl_ips.sizeof_ptl();
872 }
873 if (psmi_ep_device_is_enabled(ep, PTL_DEVID_SELF)) {
874 self_ptl = (ptl_t *) (ep->ptl_base_data + ptl_sizes);
875 ptl_sizes += psmi_ptl_self.sizeof_ptl();
876 }
877
878 if ((err = psmi_ep_open_device(ep, &opts, unique_job_key,
879 &(ep->context), &ep->epid)))
880 goto fail;
881
882 /* Restore old cpuaffinity and unit settings.
883 * TODO: PSM should really just implement its own affinity
884 * setting function */
885 if (old_cpuaff != NULL)
886 setenv("IPATH_NO_CPUAFFINITY", old_cpuaff, 1);
887 if (old_unit != NULL)
888 setenv("IPATH_UNIT", old_unit, 1);
889
890 psmi_assert_always(ep->epid != 0);
891 ep->epaddr->epid = ep->epid;
892
893 /* Set our new label as soon as we know what it is */
894 strncpy(buf, psmi_gethostname(), sizeof(buf) - 1);
895 buf[sizeof(buf) - 1] = '\0';
896
897 p = buf + strlen(buf);
898
899 /* If our rank is set, use it. If not, use context.subcontext notation */
900 if (((e = getenv("MPI_RANKID")) != NULL && *e) ||
901 ((e = getenv("PSC_MPI_RANK")) != NULL && *e))
902 len = snprintf(p, sizeof buf - strlen(buf), ":%d.", atoi(e));
903 else
904 len = snprintf(p, sizeof buf - strlen(buf), ":%d.%d.",
905 (uint32_t) psm_epid_context(ep->epid),
906 (uint32_t) psmi_epid_subcontext(ep->epid));
907 *(p + len) = '\0';
908 ep->context_mylabel = psmi_strdup(ep, buf);
909 if (ep->context_mylabel == NULL) {
910 err = PSM_NO_MEMORY;
911 goto fail;
912 }
913 //ipath_set_mylabel(ep->context_mylabel);
914
915 if ((err = psmi_epid_set_hostname(psm_epid_nid(ep->epid), buf, 0)))
916 goto fail;
917
918 /*
919 * Active Message initialization
920 */
921 if ((err = psmi_am_init_internal(ep)))
922 goto fail;
923
924 if (psmi_ep_device_is_enabled(ep, PTL_DEVID_SELF)) {
925 if ((err = psmi_ptl_self.init(ep, self_ptl, &ep->ptl_self)))
926 goto fail;
927 }
928 if (psmi_ep_device_is_enabled(ep, PTL_DEVID_IPS)) {
929 if ((err = psmi_ptl_ips.init(ep, ips_ptl, &ep->ptl_ips)))
930 goto fail;
931 }
932 /* If we're shm-only, this device is enabled above */
933 if (psmi_ep_device_is_enabled(ep, PTL_DEVID_AMSH)) {
934 if ((err = psmi_ptl_amsh.init(ep, amsh_ptl, &ep->ptl_amsh)))
935 goto fail;
936 }
937 else {
938 /* We may have pre-attached as part of getting our rank for enabling
939 * shared contexts. */
940 psmi_shm_detach(ep);
941 }
942
943 /*
944 * Keep only IPS since only IPS support multi-rail, other devices
945 * are only setup once. IPS device can come to this function again.
946 */
947 for (i = 0; i < PTL_MAX_INIT; i++) {
948 if (devid_enabled[i] != PTL_DEVID_IPS) {
949 devid_enabled[i] = -1;
950 }
951 }
952
953 *epido = ep->epid;
954 *epo = ep;
955
956 return PSM_OK;
957
958 fail:
959 if (ep != NULL) {
960 if (ep->context.fd != -1) close(ep->context.fd);
961 psmi_free(ep);
962 }
963 if (epaddr != NULL)
964 psmi_free(epaddr);
965 return err;
966 }
967
968 psm_error_t
969 __psm_ep_open(psm_uuid_t const unique_job_key, struct psm_ep_open_opts const *opts_i,
970 psm_ep_t *epo, psm_epid_t *epido)
971 {
972 psm_error_t err;
973 psm_mq_t mq;
974 psm_epid_t epid;
975 psm_ep_t ep, tmp;
976 uint32_t units[IPATH_MAX_UNIT];
977 uint16_t ports[IPATH_MAX_UNIT];
978 int i, num_rails = 0;
979 char *uname = "IPATH_UNIT";
980 char *pname = "IPATH_PORT";
981 char uvalue[4], pvalue[6];
982 int devid_enabled[PTL_MAX_INIT];
983 union psmi_envvar_val devs;
984
985 PSMI_ERR_UNLESS_INITIALIZED(NULL);
986
987 PSMI_PLOCK();
988
989 /* Matched Queue initialization. We do this early because we have to
990 * make sure ep->mq exists and is valid before calling ips_do_work.
991 */
992 err = psmi_mq_malloc(&mq);
993 if (err != PSM_OK) goto fail;
994
995 /* See which ptl devices we want to use for this ep to be opened */
996 psmi_getenv("PSM_DEVICES",
997 "Ordered list of PSM-level devices",
998 PSMI_ENVVAR_LEVEL_USER,
999 PSMI_ENVVAR_TYPE_STR,
1000 (union psmi_envvar_val) PSMI_DEVICES_DEFAULT,
1001 &devs);
1002
1003 if ((err = psmi_parse_devices(devid_enabled, devs.e_str)))
1004 goto fail;
1005
1006 if (psmi_device_is_enabled(devid_enabled, PTL_DEVID_IPS)) {
1007 err = psmi_ep_multirail(&num_rails, units, ports);
1008 if (err != PSM_OK) goto fail;
1009
1010 /* If multi-rail is used, set the first ep unit/port */
1011 if (num_rails > 0) {
1012 snprintf(uvalue, 4, "%1d", units[0]);
1013 snprintf(pvalue, 6, "%1d", ports[0]);
1014 setenv(uname, uvalue, 1);
1015 setenv(pname, pvalue, 1);
1016 }
1017 }
1018
1019 err = __psm_ep_open_internal(unique_job_key,
1020 devid_enabled, opts_i, mq, &ep, &epid);
1021 if (err != PSM_OK) goto fail;
1022
1023 if (psmi_opened_endpoint == NULL) {
1024 psmi_opened_endpoint = ep;
1025 } else {
1026 tmp = psmi_opened_endpoint;
1027 while (tmp->user_ep_next) tmp = tmp->user_ep_next;
1028 tmp->user_ep_next = ep;
1029 }
1030 psmi_opened_endpoint_count++;
1031 ep->mctxt_prev = ep->mctxt_next = ep;
1032 ep->mctxt_master = ep;
1033 mq->ep = ep;
1034
1035 *epo = ep;
1036 *epido = epid;
1037
1038 if (psmi_device_is_enabled(devid_enabled, PTL_DEVID_IPS)) {
1039 for (i = 1; i < num_rails; i++) {
1040 snprintf(uvalue, 4, "%1d", units[i]);
1041 snprintf(pvalue, 6, "%1d", ports[i]);
1042 setenv(uname, uvalue, 1);
1043 setenv(pname, pvalue, 1);
1044
1045 /* Create slave EP */
1046 err = __psm_ep_open_internal(unique_job_key,
1047 devid_enabled, opts_i, mq, &tmp, &epid);
1048 if (err) goto fail;
1049
1050 /* Link slave EP after master EP. */
1051 PSM_MCTXT_APPEND(ep, tmp);
1052 }
1053 }
1054
1055 /* Once we've initialized all devices, we can update the MQ with its
1056 * default values */
1057 if (err == PSM_OK) err = psmi_mq_initialize_defaults(mq);
1058
1059 fail:
1060 PSMI_PUNLOCK();
1061 return err;
1062 }
1063 PSMI_API_DECL(psm_ep_open)
1064
1065 psm_error_t
1066 __psm_ep_close(psm_ep_t ep, int mode, int64_t timeout_in)
1067 {
1068 psm_error_t err = PSM_OK;
1069 uint64_t t_start = get_cycles();
1070 union psmi_envvar_val timeout_intval;
1071 psm_ep_t tmp, mep;
1072
1073 PSMI_ERR_UNLESS_INITIALIZED(ep);
1074 psmi_assert_always(ep->mctxt_master == ep);
1075
1076 PSMI_PLOCK();
1077
1078 if (psmi_opened_endpoint == NULL) {
1079 err = psmi_handle_error(NULL, PSM_EP_WAS_CLOSED,
1080 "PSM Endpoint is closed or does not exist");
1081 return err;
1082 }
1083
1084 tmp = psmi_opened_endpoint;
1085 while (tmp && tmp != ep) {
1086 tmp = tmp->user_ep_next;
1087 }
1088 if (!tmp) {
1089 err = psmi_handle_error(NULL, PSM_EP_WAS_CLOSED,
1090 "PSM Endpoint is closed or does not exist");
1091 return err;
1092 }
1093
1094 psmi_getenv("PSM_CLOSE_TIMEOUT",
1095 "End-point close timeout over-ride.",
1096 PSMI_ENVVAR_LEVEL_USER, PSMI_ENVVAR_TYPE_UINT,
1097 (union psmi_envvar_val) 0,
1098 &timeout_intval);
1099
1100 if (getenv("PSM_CLOSE_TIMEOUT")) {
1101 timeout_in = timeout_intval.e_uint * SEC_ULL;
1102 }
1103 else if (timeout_in > 0) {
1104 /* The timeout parameter provides the minimum timeout. A heuristic
1105 * is used to scale up the timeout linearly with the number of
1106 * endpoints, and we allow one second per 100 endpoints. */
1107 timeout_in = max(timeout_in, (ep->connections * SEC_ULL) / 100);
1108 }
1109
1110 if (timeout_in > 0 && timeout_in < PSMI_MIN_EP_CLOSE_TIMEOUT)
1111 timeout_in = PSMI_MIN_EP_CLOSE_TIMEOUT;
1112
1113 /* Infinite and excessive close time-out are limited here to a max.
1114 * The "rationale" is that there is no point waiting around forever for
1115 * graceful termination. Normal (or forced) process termination should clean
1116 * up the context state correctly even if termination is not graceful. */
1117 if (timeout_in <= 0 || timeout_in < PSMI_MAX_EP_CLOSE_TIMEOUT)
1118 timeout_in = PSMI_MAX_EP_CLOSE_TIMEOUT;
1119 _IPATH_PRDBG("Closing endpoint %p with force=%s and to=%.2f seconds and "
1120 "%d connections\n",
1121 ep, mode == PSM_EP_CLOSE_FORCE ? "YES" : "NO",
1122 (double) timeout_in / 1e9, (int) ep->connections);
1123
1124 /* XXX We currently cheat in the sense that we leave each PTL the allowed
1125 * timeout. There's no good way to do this until we change the PTL
1126 * interface to allow asynchronous finalization
1127 */
1128 mep = ep;
1129 tmp = ep->mctxt_prev;
1130 do {
1131 ep = tmp;
1132 tmp = ep->mctxt_prev;
1133 PSM_MCTXT_REMOVE(ep);
1134 if (psmi_ep_device_is_enabled(ep, PTL_DEVID_AMSH))
1135 err = psmi_ptl_amsh.fini(ep->ptl_amsh.ptl, mode, timeout_in);
1136
1137 if ((err == PSM_OK || err == PSM_TIMEOUT) &&
1138 psmi_ep_device_is_enabled(ep, PTL_DEVID_IPS))
1139 err = psmi_ptl_ips.fini(ep->ptl_ips.ptl, mode, timeout_in);
1140
1141 /* If there's timeouts in the disconnect requests,
1142 * still make sure that we still get to close the
1143 *endpoint and mark it closed */
1144 if (psmi_ep_device_is_enabled(ep, PTL_DEVID_IPS))
1145 psmi_context_close(&ep->context);
1146
1147 psmi_free(ep->epaddr);
1148 psmi_free(ep->context_mylabel);
1149 /*
1150 * Before freeing the master ep itself,
1151 * remove it from the global linklist.
1152 * We do it here to let atexit handler in ptl_am directory
1153 * to search the global linklist and free the shared memory file.
1154 */
1155 if (ep == mep) {
1156 if (psmi_opened_endpoint == ep) {
1157 psmi_opened_endpoint = ep->user_ep_next;
1158 } else {
1159 tmp = psmi_opened_endpoint;
1160 while (tmp->user_ep_next != ep) {
1161 tmp = tmp->user_ep_next;
1162 }
1163 tmp->user_ep_next = ep->user_ep_next;
1164 }
1165 psmi_opened_endpoint_count--;
1166 }
1167 psmi_free(ep);
1168
1169 } while ((err == PSM_OK || err == PSM_TIMEOUT) && tmp != ep);
1170
1171 PSMI_PUNLOCK();
1172
1173 _IPATH_PRDBG("Closed endpoint in %.3f secs\n",
1174 (double) cycles_to_nanosecs(get_cycles() - t_start) / SEC_ULL);
1175 return err;
1176 }
1177 PSMI_API_DECL(psm_ep_close)
1178
1179 static
1180 psm_error_t
1181 psmi_ep_open_device(const psm_ep_t ep,
1182 const struct psm_ep_open_opts *opts,
1183 const psm_uuid_t unique_job_key,
1184 struct psmi_context *context,
1185 psm_epid_t *epid)
1186 {
1187 psm_error_t err = PSM_OK;
1188
1189 /* Skip affinity. No affinity if:
1190 * 1. User explicitly sets no-affinity=YES in environment.
1191 * 2. User doesn't set affinity in environment and PSM is opened with
1192 * option affinity skip.
1193 */
1194 if (psmi_ep_device_is_enabled(ep, PTL_DEVID_IPS)) {
1195 uint32_t lid;
1196
1197 ep->out_sl = opts->outsl;
1198
1199 if ((err = psmi_context_open(ep, opts->unit, opts->port, unique_job_key,
1200 opts->timeout, context)) != PSM_OK)
1201 goto fail;
1202
1203 _IPATH_DBG("[%d]use unit %d port %d\n", getpid(),
1204 context->base_info.spi_unit, context->base_info.spi_port);
1205
1206 if ((lid = ipath_get_port_lid(context->base_info.spi_unit,
1207 context->base_info.spi_port)) == -1) {
1208 err = psmi_handle_error(NULL,
1209 PSM_EP_DEVICE_FAILURE,
1210 "Can't get InfiniBand LID in psm_ep_open: is SMA running?");
1211 goto fail;
1212 }
1213
1214 if (context->base_info.spi_sw_version >= (1 << 16 | 5)) {
1215 uint32_t rcvthread_flags;
1216 union psmi_envvar_val env_rcvthread;
1217 static int norcvthread = 0; /* only for first rail */
1218
1219 /* See if we want to activate support for receive thread */
1220 psmi_getenv("PSM_RCVTHREAD", "Recv thread flags (0 disables thread)",
1221 PSMI_ENVVAR_LEVEL_USER, PSMI_ENVVAR_TYPE_UINT_FLAGS,
1222 (union psmi_envvar_val)(norcvthread++?0:PSMI_RCVTHREAD_FLAGS),
1223 &env_rcvthread);
1224 rcvthread_flags = env_rcvthread.e_uint;
1225
1226 /* If enabled, use the pollurg capability to implement a receive
1227 * interrupt thread that can handle urg packets */
1228 if (rcvthread_flags) {
1229 context->runtime_flags |= PSMI_RUNTIME_RCVTHREAD;
1230 #ifdef PSMI_PLOCK_IS_NOLOCK
1231 psmi_handle_error(PSMI_EP_NORETURN, PSM_INTERNAL_ERR,
1232 "#define PSMI_PLOCK_IS_NOLOCK not functional yet "
1233 "with RCVTHREAD on");
1234 #endif
1235 }
1236 context->rcvthread_flags = rcvthread_flags;
1237
1238 }
1239
1240 *epid = context->epid;
1241 }
1242 else {
1243 int rank, nranks;
1244 char *e;
1245 long nproc = sysconf(_SC_NPROCESSORS_ONLN);
1246
1247 if (psmi_ep_device_is_enabled(ep, PTL_DEVID_AMSH)) {
1248 /* In shm-only mode, we need to derive a valid epid based on our
1249 * rank. We try to get it from the environment if its available,
1250 * or resort to preattaching to the shared memory segment and use
1251 * our shared memory rank (shmidx) as the rank.
1252 */
1253 union psmi_envvar_val env_rankid;
1254
1255 if (psmi_getenv("MPI_LOCALRANKID", "Shared context rankid",
1256 PSMI_ENVVAR_LEVEL_HIDDEN, PSMI_ENVVAR_TYPE_INT,
1257 (union psmi_envvar_val) -1,
1258 &env_rankid)) {
1259 if (psmi_getenv("PSC_MPI_NODE_RANK", "Shared context rankid",
1260 PSMI_ENVVAR_LEVEL_HIDDEN, PSMI_ENVVAR_TYPE_INT,
1261 (union psmi_envvar_val) -1,
1262 &env_rankid)) {
1263 if ((err = psmi_shm_attach(ep, &rank)))
1264 goto fail;
1265 }
1266 else
1267 rank = env_rankid.e_int;
1268 }
1269 else
1270 rank = env_rankid.e_int;
1271 nranks = (int) nproc;
1272 }
1273 else {
1274 /* Self-only, meaning only 1 proc max */
1275 rank = 0;
1276 nranks = 1;
1277 }
1278
1279 e = getenv("IPATH_NO_CPUAFFINITY");
1280
1281 /* Now that we have a rank, set our affinity based on this rank */
1282 if (e == NULL || *e == '\0')
1283 {
1284 cpu_set_t cpuset;
1285 CPU_ZERO(&cpuset);
1286 /* First see if affinity is already set */
1287 if (sched_getaffinity(0, sizeof cpuset, &cpuset)) {
1288 _IPATH_PRDBG("Couldn't get processory affinity, assuming "
1289 "not set: %s\n", strerror(errno));
1290 }
1291 else {
1292 int i, num_set = 0;
1293 for (i = 0; i < CPU_SETSIZE; i++) {
1294 if (CPU_ISSET(i, &cpuset))
1295 num_set++;
1296 }
1297
1298 if (num_set > 0 && num_set < nproc)
1299 _IPATH_PRDBG("CPU affinity already set, leaving as is\n");
1300 else if (rank >= nranks || rank < 0)
1301 _IPATH_PRDBG("Skipping affinity, rank is %d and there are "
1302 "only %d processors.\n", rank, nranks);
1303 else {
1304 CPU_ZERO(&cpuset);
1305 CPU_SET(rank, &cpuset);
1306 if (sched_setaffinity(0,sizeof cpuset, &cpuset))
1307 _IPATH_PRDBG("Couldn't set affinity to processor %d: %s\n",
1308 rank, strerror(errno));
1309 else
1310 _IPATH_PRDBG("Set CPU affinity to %d out of %d processors\n",
1311 rank, nranks);
1312 }
1313 }
1314 }
1315
1316 /*
1317 * We use a random lid 0xffff which doesn't really matter since we're
1318 * closing ourselves to the outside world by explicitly disabling the
1319 * ipath device).
1320 */
1321 *epid = PSMI_EPID_PACK(0xffff, (rank>>2), rank);
1322 }
1323
1324 fail:
1325 return err;
1326 }
1327
1328 /* Get a list of PTLs we want to use. The order is important, it affects
1329 * whether node-local processes use shm or ips */
1330 static
1331 psm_error_t
1332 psmi_parse_devices(int devices[PTL_MAX_INIT], const char *devstring)
1333 {
1334 char *devstr = NULL;
1335 char *b_new, *e, *ee, *b;
1336 psm_error_t err = PSM_OK;
1337 int len;
1338 int i = 0;
1339
1340 psmi_assert_always(devstring != NULL);
1341 len = strlen(devstring)+1;
1342
1343 for (i = 0; i < PTL_MAX_INIT; i++)
1344 devices[i] = -1;
1345
1346 devstr = (char *) psmi_calloc(PSMI_EP_NONE, UNDEFINED, 2, len);
1347 if (devstr == NULL)
1348 goto fail;
1349
1350 b_new = (char *) devstr;
1351 e = b_new + len;
1352 strncpy(e, devstring, len-1);
1353 e[len-1] = '\0';
1354 ee = e + len;
1355 i = 0;
1356 while (e < ee && *e && i < PTL_MAX_INIT) {
1357 while (*e && !isalpha(*e))
1358 e++;
1359 b = e;
1360 while (*e && isalpha(*e))
1361 e++;
1362 *e = '\0';
1363 if (*b) {
1364 if (!strcasecmp(b, "self")) {
1365 devices[i++] = PTL_DEVID_SELF;
1366 b_new = strcpy(b_new, "self,");
1367 b_new += 5;
1368 } else if (!strcasecmp(b, "amsh")) {
1369 devices[i++] = PTL_DEVID_AMSH;
1370 strcpy(b_new, "amsh,");
1371 b_new += 5;
1372 } else if (!strcasecmp(b, "ips")) {
1373 devices[i++] = PTL_DEVID_IPS;
1374 strcpy(b_new, "ips,");
1375 b_new += 4;
1376 /* If shm or shmem is set, bind to amsh */
1377 } else if (!strcasecmp(b, "shm") || !strcasecmp(b, "shmem")) {
1378 devices[i++] = PTL_DEVID_AMSH;
1379 strcpy(b_new, "amsh,");
1380 b_new += 5;
1381 /* If shm or shmem is set, bind to ipath */
1382 } else if (!strcasecmp(b, "ipath") || !(strcasecmp(b, "infinipath"))) {
1383 devices[i++] = PTL_DEVID_IPS;
1384 strcpy(b_new, "ips,");
1385 b_new += 4;
1386 } else {
1387 err = psmi_handle_error(NULL, PSM_PARAM_ERR,
1388 "%s set in environment variable PSM_PTL_DEVICES=\"%s\" "
1389 "is not one of the recognized PTL devices (%s)",
1390 b, devstring, PSMI_DEVICES_DEFAULT);
1391 goto fail;
1392 }
1393 e++;
1394 }
1395 }
1396 if (b_new != devstr) /* we parsed something, remove trailing comma */
1397 b_new[strlen(b_new) - 1] = '\0';
1398
1399 _IPATH_PRDBG("PSM Device allocation order: %s\n", devstr);
1400 fail:
1401 if (devstr != NULL)
1402 psmi_free(devstr);
1403 return err;
1404
1405 }
1406
1407 static
1408 int
1409 psmi_device_is_enabled(const int devid_enabled[PTL_MAX_INIT], int devid)
1410 {
1411 int i;
1412 for (i = 0; i < PTL_MAX_INIT; i++)
1413 if (devid_enabled[i] == devid)
1414 return 1;
1415 return 0;
1416 }
1417
1418 int
1419 psmi_ep_device_is_enabled(const psm_ep_t ep, int devid)
1420 {
1421 return psmi_device_is_enabled(ep->devid_enabled, devid);
1422 }
1423