~ubuntu-branches/ubuntu/vivid/mpich/vivid-proposed

« back to all changes in this revision

Viewing changes to src/pm/hydra/tools/topo/hwloc/topo_hwloc.c

  • Committer: Package Import Robot
  • Author(s): Anton Gladky
  • Date: 2014-04-01 20:24:20 UTC
  • mfrom: (5.2.4 sid)
  • Revision ID: package-import@ubuntu.com-20140401202420-t5ey1ia2klt5dkq3
Tags: 3.1-4
* [c3e3398] Disable test_primitives, which is unreliable on some platforms.
            (Closes: #743047)
* [265a699] Add minimal autotest.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
 */
7
7
 
8
8
#include "hydra.h"
 
9
#include "topo.h"
9
10
#include "topo_hwloc.h"
10
11
 
11
12
#define MAP_LENGTH      (5)
 
13
#define DBG_STR_LENGTH  (128)
12
14
 
13
15
struct HYDT_topo_hwloc_info HYDT_topo_hwloc_info = { 0 };
14
16
 
23
25
 
24
26
    HYDU_FUNC_ENTER();
25
27
 
26
 
    HYDT_topo_hwloc_info.num_bitmaps = hwloc_get_nbobjs_by_type(topology, HWLOC_OBJ_PU);
27
 
    HYDU_MALLOC(HYDT_topo_hwloc_info.bitmap, hwloc_bitmap_t *,
28
 
                HYDT_topo_hwloc_info.num_bitmaps * sizeof(hwloc_bitmap_t), status);
29
 
 
30
 
    /* Initialize all values to map to all CPUs */
31
 
    for (i = 0; i < HYDT_topo_hwloc_info.num_bitmaps; i++) {
32
 
        HYDT_topo_hwloc_info.bitmap[i] = hwloc_bitmap_alloc();
33
 
        hwloc_bitmap_zero(HYDT_topo_hwloc_info.bitmap[i]);
34
 
    }
 
28
    HYDU_ASSERT(hwloc_initialized, status);
35
29
 
36
30
    num_bind_entries = 1;
37
31
    for (i = 0; binding[i]; i++)
68
62
    }
69
63
    bind_entries[j][k++] = 0;
70
64
 
 
65
    /* initialize bitmaps */
 
66
    HYDU_MALLOC(HYDT_topo_hwloc_info.bitmap, hwloc_bitmap_t *,
 
67
                num_bind_entries * sizeof(hwloc_bitmap_t), status);
71
68
 
72
69
    for (i = 0; i < num_bind_entries; i++) {
 
70
        HYDT_topo_hwloc_info.bitmap[i] = hwloc_bitmap_alloc();
 
71
        hwloc_bitmap_zero(HYDT_topo_hwloc_info.bitmap[i]);
73
72
        bindstr = strtok(bind_entries[i], "+");
74
73
        while (bindstr) {
75
 
            hwloc_bitmap_set(HYDT_topo_hwloc_info.bitmap[i],
76
 
                             atoi(bindstr) % HYDT_topo_hwloc_info.num_bitmaps);
 
74
            hwloc_bitmap_set(HYDT_topo_hwloc_info.bitmap[i], atoi(bindstr));
77
75
            bindstr = strtok(NULL, "+");
78
76
        }
79
77
    }
80
78
 
 
79
    HYDT_topo_hwloc_info.num_bitmaps = num_bind_entries;
 
80
    HYDT_topo_hwloc_info.user_binding = 1;
 
81
 
 
82
    /* free temporary memory */
 
83
    for (i = 0; i < num_bind_entries; i++) {
 
84
        HYDU_FREE(bind_entries[i]);
 
85
    }
 
86
    HYDU_FREE(bind_entries);
 
87
    HYDU_FREE(bind_entry_lengths);
 
88
 
81
89
  fn_exit:
82
90
    HYDU_FUNC_EXIT();
83
91
    return status;
93
101
 
94
102
    HYDU_FUNC_ENTER();
95
103
 
 
104
    HYDU_ASSERT(hwloc_initialized, status);
 
105
 
 
106
    /* initialize bitmaps */
96
107
    HYDT_topo_hwloc_info.num_bitmaps = hwloc_get_nbobjs_by_type(topology, HWLOC_OBJ_PU);
 
108
 
97
109
    HYDU_MALLOC(HYDT_topo_hwloc_info.bitmap, hwloc_bitmap_t *,
98
110
                HYDT_topo_hwloc_info.num_bitmaps * sizeof(hwloc_bitmap_t), status);
99
111
 
110
122
    goto fn_exit;
111
123
}
112
124
 
113
 
static hwloc_obj_t find_obj_containing_pu(hwloc_obj_type_t type, int idx, int cache_depth)
114
 
{
115
 
    int i;
116
 
    hwloc_obj_t obj;
117
 
 
118
 
    obj = hwloc_get_root_obj(topology);
119
 
    if (!obj || !hwloc_bitmap_isset(obj->cpuset, idx))
120
 
        return NULL;
121
 
 
122
 
    while (obj) {
123
 
        if (obj->type == type)
124
 
            if (type != HWLOC_OBJ_CACHE || obj->attr->cache.depth == cache_depth)
125
 
                break;
126
 
        for (i = 0; i < obj->arity; i++) {
127
 
            if (hwloc_bitmap_isset(obj->children[i]->cpuset, idx)) {
128
 
                obj = obj->children[i];
129
 
                break;
130
 
            }
131
 
        }
132
 
    }
133
 
 
134
 
    return obj;
135
 
}
136
 
 
137
 
static HYD_status get_nbobjs_by_type(hwloc_obj_type_t type, int *nbobjs,
138
 
                                     int *nbobjs_per_parent)
139
 
{
140
 
    int x, nb;
141
 
    hwloc_obj_type_t parent, t;
142
 
    HYD_status status = HYD_SUCCESS;
143
 
 
144
 
    HYDU_FUNC_ENTER();
145
 
 
146
 
    parent = HWLOC_OBJ_SYSTEM;
147
 
 
148
 
    if (type == HWLOC_OBJ_PU)
149
 
        parent = HWLOC_OBJ_CORE;
150
 
    else if (type == HWLOC_OBJ_CORE)
151
 
        parent = HWLOC_OBJ_SOCKET;
152
 
    else if (type == HWLOC_OBJ_SOCKET)
153
 
        parent = HWLOC_OBJ_NODE;
154
 
    else if (type == HWLOC_OBJ_NODE)
155
 
        parent = HWLOC_OBJ_MACHINE;
156
 
    else if (type == HWLOC_OBJ_MACHINE)
157
 
        parent = HWLOC_OBJ_MACHINE;
158
 
 
159
 
    HYDU_ASSERT(parent != HWLOC_OBJ_SYSTEM, status);
160
 
 
161
 
    nb = 0;
162
 
    t = type;
163
 
    while (1) {
164
 
        nb = hwloc_get_nbobjs_by_type(topology, t);
165
 
        if (nb)
166
 
            break;
167
 
        if (t == HWLOC_OBJ_SYSTEM)
168
 
            break;
169
 
        while (--t == HWLOC_OBJ_CACHE);
170
 
    }
171
 
    HYDU_ASSERT(nb, status);
172
 
    if (nbobjs)
173
 
        *nbobjs = nb;
174
 
 
175
 
    if (nbobjs_per_parent == NULL)
176
 
        goto fn_exit;
177
 
 
178
 
    x = 0;
179
 
    t = parent;
180
 
    while (1) {
181
 
        x = hwloc_get_nbobjs_by_type(topology, t);
182
 
        if (x)
183
 
            break;
184
 
        while (--t == HWLOC_OBJ_CACHE);
185
 
        if (t == HWLOC_OBJ_SYSTEM)
186
 
            break;
187
 
    }
188
 
    HYDU_ASSERT(x, status);
189
 
    HYDU_ASSERT(nb % x == 0, status);
190
 
 
191
 
    *nbobjs_per_parent = (nb / x);
192
 
 
193
 
  fn_exit:
194
 
    HYDU_FUNC_EXIT();
195
 
    return status;
196
 
 
197
 
  fn_fail:
198
 
    goto fn_exit;
199
 
}
200
 
 
201
125
static HYD_status split_count_field(const char *str, char **split_str, int *count)
202
126
{
203
127
    char *full_str = HYDU_strdup(str), *count_str;
240
164
    return atoi(t1);
241
165
}
242
166
 
243
 
static HYD_status cache_to_cpu_type(int cache_depth, hwloc_obj_type_t * cpu_type)
244
 
{
245
 
    hwloc_obj_t cache_obj, cpu_obj;
246
 
    HYD_status status = HYD_SUCCESS;
247
 
 
248
 
    HYDU_FUNC_ENTER();
249
 
 
250
 
    cache_obj = hwloc_get_root_obj(topology);
251
 
    HYDU_ASSERT(cache_obj, status);
252
 
 
253
 
    while (cache_obj && cache_obj->type != HWLOC_OBJ_CACHE &&
254
 
           cache_obj->attr->cache.depth != cache_depth)
255
 
        cache_obj = cache_obj->first_child;
256
 
    if (cache_obj == NULL) {
257
 
        HYDU_ERR_SETANDJUMP(status, HYD_INTERNAL_ERROR, "cache level %d not found\n",
258
 
                            cache_depth);
259
 
    }
260
 
 
261
 
    cpu_obj = hwloc_get_root_obj(topology);
262
 
    HYDU_ASSERT(cpu_obj, status);
263
 
 
264
 
    while (cpu_obj && cpu_obj->type == HWLOC_OBJ_CACHE &&
265
 
           !hwloc_bitmap_isequal(cpu_obj->cpuset, cache_obj->cpuset))
266
 
        cpu_obj = cpu_obj->first_child;
267
 
    if (cpu_obj == NULL) {
268
 
        HYDU_ERR_SETANDJUMP(status, HYD_INTERNAL_ERROR,
269
 
                            "could not find cpu type that maps cache\n");
270
 
    }
271
 
 
272
 
    *cpu_type = cpu_obj->type;
273
 
 
274
 
  fn_exit:
275
 
    HYDU_FUNC_EXIT();
276
 
    return status;
277
 
 
278
 
  fn_fail:
279
 
    goto fn_exit;
280
 
}
281
 
 
282
 
static HYD_status obj_type_to_map_str(hwloc_obj_type_t type, int cache_depth, char **map)
283
 
{
284
 
    hwloc_obj_type_t cpu_type;
285
 
    HYD_status status = HYD_SUCCESS;
286
 
 
287
 
    HYDU_FUNC_ENTER();
288
 
 
289
 
    if (type == HWLOC_OBJ_MACHINE) {
290
 
        *map = HYDU_strdup("BTCSN");
291
 
        goto fn_exit;
292
 
    }
293
 
    else if (type == HWLOC_OBJ_NODE) {
294
 
        *map = HYDU_strdup("NTCSB");
295
 
        goto fn_exit;
296
 
    }
297
 
    else if (type == HWLOC_OBJ_SOCKET) {
298
 
        *map = HYDU_strdup("STCNB");
299
 
        goto fn_exit;
300
 
    }
301
 
    else if (type == HWLOC_OBJ_CORE) {
302
 
        *map = HYDU_strdup("CTSNB");
303
 
        goto fn_exit;
304
 
    }
305
 
    else if (type == HWLOC_OBJ_PU) {
306
 
        *map = HYDU_strdup("TCSNB");
307
 
        goto fn_exit;
308
 
    }
309
 
 
310
 
    HYDU_ASSERT(type == HWLOC_OBJ_CACHE, status);
311
 
 
312
 
    status = cache_to_cpu_type(cache_depth, &cpu_type);
313
 
    HYDU_ERR_POP(status, "error while mapping cache to cpu object\n");
314
 
 
315
 
    status = obj_type_to_map_str(cpu_type, cache_depth, map);
316
 
    HYDU_ERR_POP(status, "error while mapping object to map string\n");
317
 
 
318
 
  fn_exit:
319
 
    HYDU_FUNC_EXIT();
320
 
    return status;
321
 
 
322
 
  fn_fail:
323
 
    goto fn_exit;
324
 
}
325
 
 
326
 
static int balance_obj_idx(int *obj_idx, int *nbobjs_per_parent)
327
 
{
328
 
    int i, ret;
329
 
 
330
 
    ret = 0;
331
 
    for (i = 0; i < MAP_LENGTH - 1; i++) {
332
 
        while (obj_idx[i] >= nbobjs_per_parent[i]) {
333
 
            obj_idx[i] -= nbobjs_per_parent[i];
334
 
            obj_idx[i + 1]++;
335
 
        }
336
 
        while (obj_idx[i] < 0) {
337
 
            obj_idx[i] += nbobjs_per_parent[i];
338
 
            obj_idx[i + 1]--;
339
 
        }
340
 
    }
341
 
    while (obj_idx[MAP_LENGTH - 1] >= nbobjs_per_parent[MAP_LENGTH - 1]) {
342
 
        obj_idx[MAP_LENGTH - 1] -= nbobjs_per_parent[MAP_LENGTH - 1];
343
 
        ret = 1;
344
 
    }
345
 
 
346
 
    return ret;
347
 
}
348
 
 
349
167
static HYD_status handle_bitmap_binding(const char *binding, const char *mapping)
350
168
{
351
 
    int i, j, k, idx, bind_count, map_count, cache_depth = 0;
352
 
    hwloc_obj_t obj;
353
 
    hwloc_obj_type_t bind_obj_type;
354
 
    int total_nbobjs[MAP_LENGTH], obj_idx[MAP_LENGTH], nbpu_per_obj[MAP_LENGTH];
355
 
    int nbobjs_per_parent[MAP_LENGTH];
 
169
    int i, j, k, bind_count, map_count, cache_depth = 0, bind_depth = 0, map_depth = 0;
 
170
    int total_map_objs, total_bind_objs, num_pus_in_map_domain, num_pus_in_bind_domain,
 
171
        total_map_domains;
 
172
    hwloc_obj_t map_obj, bind_obj, *start_pu;
 
173
    hwloc_cpuset_t *map_domains;
356
174
    char *bind_str, *map_str;
357
175
    HYD_status status = HYD_SUCCESS;
358
176
 
368
186
 
369
187
    /* get the binding object */
370
188
    if (!strcmp(bind_str, "board"))
371
 
        bind_obj_type = HWLOC_OBJ_MACHINE;
 
189
        bind_depth = hwloc_get_type_or_above_depth(topology, HWLOC_OBJ_MACHINE);
372
190
    else if (!strcmp(bind_str, "numa"))
373
 
        bind_obj_type = HWLOC_OBJ_NODE;
 
191
        bind_depth = hwloc_get_type_or_above_depth(topology, HWLOC_OBJ_NODE);
374
192
    else if (!strcmp(bind_str, "socket"))
375
 
        bind_obj_type = HWLOC_OBJ_SOCKET;
 
193
        bind_depth = hwloc_get_type_or_above_depth(topology, HWLOC_OBJ_SOCKET);
376
194
    else if (!strcmp(bind_str, "core"))
377
 
        bind_obj_type = HWLOC_OBJ_CORE;
 
195
        bind_depth = hwloc_get_type_or_above_depth(topology, HWLOC_OBJ_CORE);
378
196
    else if (!strcmp(bind_str, "hwthread"))
379
 
        bind_obj_type = HWLOC_OBJ_PU;
 
197
        bind_depth = hwloc_get_type_or_above_depth(topology, HWLOC_OBJ_PU);
380
198
    else {
381
199
        /* check if it's in the l*cache format */
382
200
        cache_depth = parse_cache_string(bind_str);
384
202
            HYDU_ERR_SETANDJUMP(status, HYD_INTERNAL_ERROR,
385
203
                                "unrecognized binding string \"%s\"\n", binding);
386
204
        }
387
 
        bind_obj_type = HWLOC_OBJ_CACHE;
388
 
    }
389
 
 
390
 
 
391
 
    /* get the mapping string */
392
 
    if (!strcmp(map_str, "board")) {
393
 
        HYDU_FREE(map_str);
394
 
        obj_type_to_map_str(HWLOC_OBJ_MACHINE, 0, &map_str);
395
 
    }
396
 
    else if (!strcmp(map_str, "numa")) {
397
 
        HYDU_FREE(map_str);
398
 
        obj_type_to_map_str(HWLOC_OBJ_NODE, 0, &map_str);
399
 
    }
400
 
    else if (!strcmp(map_str, "socket")) {
401
 
        HYDU_FREE(map_str);
402
 
        obj_type_to_map_str(HWLOC_OBJ_SOCKET, 0, &map_str);
403
 
    }
404
 
    else if (!strcmp(map_str, "core")) {
405
 
        HYDU_FREE(map_str);
406
 
        obj_type_to_map_str(HWLOC_OBJ_CORE, 0, &map_str);
407
 
    }
408
 
    else if (!strcmp(map_str, "hwthread")) {
409
 
        HYDU_FREE(map_str);
410
 
        obj_type_to_map_str(HWLOC_OBJ_PU, 0, &map_str);
411
 
    }
 
205
        bind_depth = hwloc_get_cache_type_depth(topology, cache_depth, -1);
 
206
    }
 
207
 
 
208
    /* get the mapping */
 
209
    if (!strcmp(map_str, "board"))
 
210
        map_depth = hwloc_get_type_or_above_depth(topology, HWLOC_OBJ_MACHINE);
 
211
    else if (!strcmp(map_str, "numa"))
 
212
        map_depth = hwloc_get_type_or_above_depth(topology, HWLOC_OBJ_NODE);
 
213
    else if (!strcmp(map_str, "socket"))
 
214
        map_depth = hwloc_get_type_or_above_depth(topology, HWLOC_OBJ_SOCKET);
 
215
    else if (!strcmp(map_str, "core"))
 
216
        map_depth = hwloc_get_type_or_above_depth(topology, HWLOC_OBJ_CORE);
 
217
    else if (!strcmp(map_str, "hwthread"))
 
218
        map_depth = hwloc_get_type_or_above_depth(topology, HWLOC_OBJ_PU);
412
219
    else {
413
220
        cache_depth = parse_cache_string(map_str);
414
 
        if (cache_depth) {
415
 
            HYDU_FREE(map_str);
416
 
            obj_type_to_map_str(HWLOC_OBJ_CACHE, cache_depth, &map_str);
417
 
        }
418
 
        else {
419
 
            for (i = 0; i < strlen(map_str); i++) {
420
 
                if (map_str[i] >= 'a' && map_str[i] <= 'z')
421
 
                    map_str[i] += ('A' - 'a');
422
 
 
423
 
                /* If any of the characters are not in the form, we
424
 
                 * want, return an error */
425
 
                if (map_str[i] != 'T' && map_str[i] != 'C' && map_str[i] != 'S' &&
426
 
                    map_str[i] != 'N' && map_str[i] != 'B') {
427
 
                    HYDU_ERR_SETANDJUMP(status, HYD_INTERNAL_ERROR,
428
 
                                        "unrecognized mapping string \"%s\"\n", mapping);
429
 
                }
430
 
            }
431
 
        }
432
 
    }
433
 
 
434
 
 
435
 
    for (i = 0; i < MAP_LENGTH; i++) {
436
 
        if (map_str[i] == 'T')
437
 
            status = get_nbobjs_by_type(HWLOC_OBJ_PU, &total_nbobjs[i], &nbobjs_per_parent[i]);
438
 
        else if (map_str[i] == 'C')
439
 
            status =
440
 
                get_nbobjs_by_type(HWLOC_OBJ_CORE, &total_nbobjs[i], &nbobjs_per_parent[i]);
441
 
        else if (map_str[i] == 'S')
442
 
            status =
443
 
                get_nbobjs_by_type(HWLOC_OBJ_SOCKET, &total_nbobjs[i], &nbobjs_per_parent[i]);
444
 
        else if (map_str[i] == 'N')
445
 
            status =
446
 
                get_nbobjs_by_type(HWLOC_OBJ_NODE, &total_nbobjs[i], &nbobjs_per_parent[i]);
447
 
        else if (map_str[i] == 'B')
448
 
            status =
449
 
                get_nbobjs_by_type(HWLOC_OBJ_MACHINE, &total_nbobjs[i], &nbobjs_per_parent[i]);
450
 
        HYDU_ERR_POP(status, "unable to get number of objects\n");
451
 
 
452
 
        nbpu_per_obj[i] = HYDT_topo_hwloc_info.num_bitmaps / total_nbobjs[i];
453
 
        obj_idx[i] = 0;
454
 
    }
455
 
 
 
221
        if (!cache_depth) {
 
222
            HYDU_ERR_SETANDJUMP(status, HYD_INTERNAL_ERROR,
 
223
                                "unrecognized mapping string \"%s\"\n", mapping);
 
224
        }
 
225
        map_depth = hwloc_get_cache_type_depth(topology, cache_depth, -1);
 
226
    }
 
227
 
 
228
    /*
 
229
     * Process Affinity Algorithm:
 
230
     *
 
231
     * The code below works in 3 stages. The end result is an array of all the possible
 
232
     * binding bitmaps for a system, based on the options specified.
 
233
     *
 
234
     * 1. Define all possible mapping "domains" in a system. A mapping domain is a group
 
235
     *    of hardware elements found by traversing the topology. Each traversal skips the
 
236
     *    number of elements the user specified in the mapping string. The traversal ends
 
237
     *    when the next mapping domain == the first mapping domain. Note that if the
 
238
     *    mapping string defines a domain that is larger than the system size, we exit
 
239
     *    with an error.
 
240
     *
 
241
     * 2. Define the number of possible binding domains within a mapping domain. This
 
242
     *    process is similar to step 1, in that we traverse the mapping domain finding
 
243
     *    all possible bind combinations, stopping when a duplicate of the first binding
 
244
     *    is reached. If a binding is larger (in # of PUs) than the mapping domain,
 
245
     *    the number of possible bindings for that domain is 1. In this stage, we also
 
246
     *    locate the first PU in each mapping domain for use later during binding.
 
247
     *
 
248
     * 3. Create the binding bitmaps. We allocate an array of bitmaps and fill them in
 
249
     *    with all possible bindings. The starting PU in each mapping domain is advanced
 
250
     *    if and when we wrap around to the beginning of the mapping domains. This ensures
 
251
     *    that we do not repeat.
 
252
     *
 
253
     */
 
254
 
 
255
    /* calculate the number of map domains */
 
256
    total_map_objs = hwloc_get_nbobjs_by_depth(topology, map_depth);
 
257
    num_pus_in_map_domain = (HYDT_topo_hwloc_info.total_num_pus / total_map_objs) * map_count;
 
258
    HYDU_ERR_CHKANDJUMP(status, num_pus_in_map_domain > HYDT_topo_hwloc_info.total_num_pus,
 
259
                        HYD_INTERNAL_ERROR, "mapping option \"%s\" larger than total system size\n",
 
260
                        mapping);
 
261
 
 
262
    /* The number of total_map_domains should be large enough to
 
263
     * contain all contiguous map object collections of length
 
264
     * map_count.  For example, if the map object is "socket" and the
 
265
     * map_count is 3, on a system with 4 sockets, the following map
 
266
     * domains should be included: (0,1,2), (3,0,1), (2,3,0), (1,2,3).
 
267
     * We do this by finding how many times we need to replicate the
 
268
     * list of the map objects so that an integral number of map
 
269
     * domains can map to them.  In the above case, the list of map
 
270
     * objects is replicated 3 times. */
 
271
    for (i = 1; (i * total_map_objs) % map_count; i++);
 
272
    total_map_domains = (i * total_map_objs) / map_count;
 
273
 
 
274
    /* initialize the map domains */
 
275
    HYDU_MALLOC(map_domains, hwloc_bitmap_t *, total_map_domains * sizeof(hwloc_bitmap_t), status);
 
276
    HYDU_MALLOC(start_pu, hwloc_obj_t *, total_map_domains * sizeof(hwloc_obj_t), status);
 
277
 
 
278
    /* For each map domain, find the next map object (first map object
 
279
     * for the first map domain) and add the following "map_count"
 
280
     * number of contiguous map objects, wrapping to the first one if
 
281
     * needed, to the map domain.  Store the first PU in the first map
 
282
     * object of the map domain as "start_pu".  This is needed later
 
283
     * for the actual binding. */
 
284
    map_obj = NULL;
 
285
    for (i = 0; i < total_map_domains; i++) {
 
286
        map_domains[i] = hwloc_bitmap_alloc();
 
287
        hwloc_bitmap_zero(map_domains[i]);
 
288
 
 
289
        for (j = 0; j < map_count; j++) {
 
290
            map_obj = hwloc_get_next_obj_by_depth(topology, map_depth, map_obj);
 
291
            /* map_obj will be NULL if it reaches the end. call again to wrap around */
 
292
            if (!map_obj)
 
293
                map_obj = hwloc_get_next_obj_by_depth(topology, map_depth, map_obj);
 
294
 
 
295
            if (j == 0)
 
296
                start_pu[i] =
 
297
                    hwloc_get_obj_inside_cpuset_by_type(topology, map_obj->cpuset, HWLOC_OBJ_PU, 0);
 
298
 
 
299
            hwloc_bitmap_or(map_domains[i], map_domains[i], map_obj->cpuset);
 
300
        }
 
301
    }
 
302
 
 
303
 
 
304
    /* Find the possible binding domains is similar to that of map
 
305
     * domains.  But if a binding domain is larger (in # of PUs) than
 
306
     * the mapping domain, the number of possible bindings for that
 
307
     * domain is 1. */
 
308
 
 
309
    /* calculate the number of possible bindings and allocate bitmaps for them */
 
310
    total_bind_objs = hwloc_get_nbobjs_by_depth(topology, bind_depth);
 
311
    num_pus_in_bind_domain = (HYDT_topo_hwloc_info.total_num_pus / total_bind_objs) * bind_count;
 
312
 
 
313
    if (num_pus_in_bind_domain < num_pus_in_map_domain) {
 
314
        for (i = 1; (i * num_pus_in_map_domain) % num_pus_in_bind_domain; i++);
 
315
        HYDT_topo_hwloc_info.num_bitmaps =
 
316
            (i * num_pus_in_map_domain * total_map_domains) / num_pus_in_bind_domain;
 
317
    }
 
318
    else {
 
319
        HYDT_topo_hwloc_info.num_bitmaps = total_map_domains;
 
320
    }
 
321
 
 
322
    /* initialize bitmaps */
 
323
    HYDU_MALLOC(HYDT_topo_hwloc_info.bitmap, hwloc_bitmap_t *,
 
324
                HYDT_topo_hwloc_info.num_bitmaps * sizeof(hwloc_bitmap_t), status);
 
325
 
 
326
    for (i = 0; i < HYDT_topo_hwloc_info.num_bitmaps; i++) {
 
327
        HYDT_topo_hwloc_info.bitmap[i] = hwloc_bitmap_alloc();
 
328
        hwloc_bitmap_zero(HYDT_topo_hwloc_info.bitmap[i]);
 
329
    }
 
330
 
 
331
    /* do bindings */
456
332
    i = 0;
457
333
    while (i < HYDT_topo_hwloc_info.num_bitmaps) {
458
 
        for (j = 0; j < bind_count; j++) {
459
 
            for (idx = 0, k = 0; k < MAP_LENGTH; k++)
460
 
                idx += (obj_idx[k] * nbpu_per_obj[k]);
461
 
 
462
 
            obj = find_obj_containing_pu(bind_obj_type, idx++, cache_depth);
463
 
            if (obj == NULL)
464
 
                break;
465
 
 
466
 
            hwloc_bitmap_or(HYDT_topo_hwloc_info.bitmap[i], HYDT_topo_hwloc_info.bitmap[i],
467
 
                            obj->cpuset);
468
 
 
469
 
            obj_idx[0] += map_count;
470
 
            balance_obj_idx(obj_idx, nbobjs_per_parent);
 
334
        for (j = 0; j < total_map_domains; j++) {
 
335
            bind_obj = hwloc_get_ancestor_obj_by_depth(topology, bind_depth, start_pu[j]);
 
336
 
 
337
            for (k = 0; k < bind_count; k++) {
 
338
                hwloc_bitmap_or(HYDT_topo_hwloc_info.bitmap[i], HYDT_topo_hwloc_info.bitmap[i],
 
339
                                bind_obj->cpuset);
 
340
 
 
341
                /* if the binding is smaller than the mapping domain, wrap around inside that domain */
 
342
                if (num_pus_in_bind_domain < num_pus_in_map_domain) {
 
343
                    bind_obj =
 
344
                        hwloc_get_next_obj_inside_cpuset_by_depth(topology, map_domains[j],
 
345
                                                                  bind_depth, bind_obj);
 
346
                    if (!bind_obj)
 
347
                        bind_obj =
 
348
                            hwloc_get_next_obj_inside_cpuset_by_depth(topology, map_domains[j],
 
349
                                                                      bind_depth, bind_obj);
 
350
                }
 
351
                else {
 
352
                    bind_obj = hwloc_get_next_obj_by_depth(topology, bind_depth, bind_obj);
 
353
                    if (!bind_obj)
 
354
                        bind_obj = hwloc_get_next_obj_by_depth(topology, bind_depth, bind_obj);
 
355
                }
 
356
 
 
357
            }
 
358
            i++;
 
359
 
 
360
            /* advance the starting position for this map domain, if needed */
 
361
            if (num_pus_in_bind_domain < num_pus_in_map_domain) {
 
362
                for (k = 0; k < num_pus_in_bind_domain; k++) {
 
363
                    start_pu[j] = hwloc_get_next_obj_inside_cpuset_by_type(topology, map_domains[j],
 
364
                                                                           HWLOC_OBJ_PU,
 
365
                                                                           start_pu[j]);
 
366
                    if (!start_pu[j])
 
367
                        start_pu[j] =
 
368
                            hwloc_get_next_obj_inside_cpuset_by_type(topology, map_domains[j],
 
369
                                                                     HWLOC_OBJ_PU, start_pu[j]);
 
370
                }
 
371
            }
471
372
        }
472
 
        i++;
473
373
    }
474
374
 
475
 
    /* reset the number of bitmaps available to what we actually set */
476
 
    HYDT_topo_hwloc_info.num_bitmaps = i;
 
375
    /* free temporary memory */
 
376
    HYDU_FREE(map_domains);
 
377
    HYDU_FREE(start_pu);
477
378
 
478
379
  fn_exit:
479
380
    HYDU_FUNC_EXIT();
485
386
 
486
387
HYD_status HYDT_topo_hwloc_init(const char *binding, const char *mapping, const char *membind)
487
388
{
488
 
    int i;
489
389
    HYD_status status = HYD_SUCCESS;
490
390
 
491
391
    HYDU_FUNC_ENTER();
495
395
    hwloc_topology_init(&topology);
496
396
    hwloc_topology_load(topology);
497
397
 
 
398
    HYDT_topo_hwloc_info.total_num_pus = hwloc_get_nbobjs_by_type(topology, HWLOC_OBJ_PU);
 
399
 
498
400
    hwloc_initialized = 1;
499
401
 
500
 
 
501
 
    /* initialize bitmaps */
502
 
    status = get_nbobjs_by_type(HWLOC_OBJ_PU, &HYDT_topo_hwloc_info.num_bitmaps, NULL);
503
 
    HYDU_ERR_POP(status, "unable to get number of PUs\n");
504
 
 
505
 
    HYDU_MALLOC(HYDT_topo_hwloc_info.bitmap, hwloc_bitmap_t *,
506
 
                HYDT_topo_hwloc_info.num_bitmaps * sizeof(hwloc_bitmap_t), status);
507
 
 
508
 
    for (i = 0; i < HYDT_topo_hwloc_info.num_bitmaps; i++) {
509
 
        HYDT_topo_hwloc_info.bitmap[i] = hwloc_bitmap_alloc();
510
 
        hwloc_bitmap_zero(HYDT_topo_hwloc_info.bitmap[i]);
511
 
    }
512
 
 
513
 
 
514
402
    /* bindings that don't require mapping */
515
403
    if (!strncmp(binding, "user:", strlen("user:"))) {
516
404
        status = handle_user_binding(binding + strlen("user:"));
558
446
 
559
447
HYD_status HYDT_topo_hwloc_bind(int idx)
560
448
{
561
 
    int id = idx % HYDT_topo_hwloc_info.num_bitmaps;
 
449
    int id;
562
450
    HYD_status status = HYD_SUCCESS;
563
451
 
564
452
    HYDU_FUNC_ENTER();
565
453
 
566
 
    hwloc_set_cpubind(topology, HYDT_topo_hwloc_info.bitmap[id], 0);
567
 
    hwloc_set_membind(topology, HYDT_topo_hwloc_info.bitmap[id],
568
 
                      HYDT_topo_hwloc_info.membind, 0);
 
454
    /* For processes where the user did not specify a binding unit, no binding is needed. */
 
455
    if (!HYDT_topo_hwloc_info.user_binding || (idx < HYDT_topo_hwloc_info.num_bitmaps)) {
 
456
        id = idx % HYDT_topo_hwloc_info.num_bitmaps;
 
457
 
 
458
        /* For debugging, print the binding bitmaps but don't actually bind. */
 
459
        if (HYDT_topo_info.debug) {
 
460
            int i, cur;
 
461
            char binding[DBG_STR_LENGTH];
 
462
 
 
463
            cur = HYDU_snprintf(binding, DBG_STR_LENGTH, "process %d binding: ", idx);
 
464
 
 
465
            for (i = 0; i < HYDT_topo_hwloc_info.total_num_pus; i++)
 
466
                cur += HYDU_snprintf(&binding[cur], DBG_STR_LENGTH - cur + 1, "%d ",
 
467
                                     hwloc_bitmap_isset(HYDT_topo_hwloc_info.bitmap[id], i));
 
468
 
 
469
            HYDU_snprintf(&binding[cur], DBG_STR_LENGTH - cur + 1, "\n");
 
470
            HYDU_dump_noprefix(stdout, "%s", binding);
 
471
        }
 
472
        else {
 
473
            hwloc_set_cpubind(topology, HYDT_topo_hwloc_info.bitmap[id], 0);
 
474
            hwloc_set_membind(topology, HYDT_topo_hwloc_info.bitmap[id],
 
475
                              HYDT_topo_hwloc_info.membind, 0);
 
476
        }
 
477
    }
569
478
 
570
479
    HYDU_FUNC_EXIT();
571
480
    return status;