File: hwloc.c

package info (click to toggle)
mpich 4.3.0%2Breally4.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 419,120 kB
  • sloc: ansic: 1,215,557; cpp: 74,755; javascript: 40,763; f90: 20,649; sh: 18,463; xml: 14,418; python: 14,397; perl: 13,772; makefile: 9,279; fortran: 8,063; java: 4,553; asm: 324; ruby: 176; lisp: 19; php: 8; sed: 4
file content (288 lines) | stat: -rw-r--r-- 9,108 bytes parent folder | download | duplicates (11)
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
279
280
281
282
283
284
285
286
287
288
/*
 * Copyright © 2016-2018 Inria.  All rights reserved.
 *
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 * See COPYING in top-level directory.
 *
 * $HEADER$
 */

#define _GNU_SOURCE         /* See feature_test_macros(7) */

#include <sys/types.h>
#include <dirent.h>
#include <libgen.h>

#include <private/netloc.h>
#include <netloc.h>
#include <hwloc.h>

static UT_icd topos_icd = {sizeof(hwloc_topology_t), NULL, NULL, NULL};

int netloc_topology_read_hwloc(netloc_topology_t *topology, int num_nodes,
        netloc_node_t **node_list)
{
    int ret = 0;
    int all = 0;

    char *hwloc_path;

    if (!topology->hwlocpath) {
        printf("No hwloc directory recorded in the topology\n");
        return NETLOC_ERROR;
    }

    if (topology->hwlocpath[0] != '/') {
        char *path_tmp = strdup(topology->topopath);
        asprintf(&hwloc_path, "%s/%s", dirname(path_tmp), topology->hwlocpath);
        free(path_tmp);
    } else {
        hwloc_path = strdup(topology->hwlocpath);
    }

    DIR* dir = opendir(hwloc_path);
    /* Directory does not exist */
    if (!dir) {
        printf("Directory (%s) to hwloc does not exist\n", hwloc_path);
        free(hwloc_path);
        return NETLOC_ERROR;
    }
    else {
        closedir(dir);
    }

    UT_array *hwloc_topo_names = topology->topos;
    UT_array *hwloc_topos;
    utarray_new(hwloc_topos, &topos_icd);

    int num_diffs = 0;

    if (!num_nodes) {
        netloc_node_t *node, *node_tmp;
        num_nodes = HASH_COUNT(topology->nodes);
        node_list = (netloc_node_t **)malloc(sizeof(netloc_node_t *[num_nodes]));
        int n = 0;
        netloc_topology_iter_nodes(topology, node, node_tmp) {
            node_list[n++] = node;
        }
        all = 1;
    }

    for (int n  = 0; n < num_nodes; n++) {
        netloc_node_t *node = node_list[n];
        char *hwloc_file;
        char *refname;

        if (netloc_node_is_switch(node))
            continue;

        /* We try to find a diff file */
        asprintf(&hwloc_file, "%s/%s.diff.xml", hwloc_path, node->hostname);
        hwloc_topology_diff_t diff;
        int err;
        if ((err = hwloc_topology_diff_load_xml(hwloc_file, &diff, &refname)) >= 0) {
            refname[strlen(refname)-4] = '\0';
            hwloc_topology_diff_destroy(diff);
            num_diffs++;
        }
        else {
            free(hwloc_file);
            /* We try to find a regular file */
            asprintf(&hwloc_file, "%s/%s.xml", hwloc_path, node->hostname);
            FILE *fxml;
            if (!(fxml = fopen(hwloc_file, "r"))) {
                printf("Missing hwloc file: %s\n", hwloc_file);
            }
            else
                fclose(fxml);
            asprintf(&refname, "%s", node->hostname);
        }

        /* Add the hwloc topology */
        unsigned int t = 0;
        while (t < utarray_len(hwloc_topo_names) &&
                strcmp(*(char **)utarray_eltptr(hwloc_topo_names, t), refname)) {
            t++;
        }
        /* Topology not found */
        if (t == utarray_len(hwloc_topo_names)) {
            utarray_push_back(hwloc_topo_names, &refname);

            /* Read the hwloc topology */
            hwloc_topology_t topology;
            hwloc_topology_init(&topology);
            hwloc_topology_set_flags(topology, HWLOC_TOPOLOGY_FLAG_INCLUDE_DISALLOWED);

            char *hwloc_ref_path;
            asprintf(&hwloc_ref_path, "%s/%s.xml", hwloc_path, refname);
            ret = hwloc_topology_set_xml(topology, hwloc_ref_path);
            free(hwloc_ref_path);
            if (ret == -1) {
                void *null = NULL;
                utarray_push_back(hwloc_topos, &null);
                fprintf(stdout, "Warning: no topology for %s\n", refname);
                hwloc_topology_destroy(topology);
                free(refname); free(hwloc_file);
                continue;
            }

            ret = hwloc_topology_set_all_types_filter(topology, HWLOC_TYPE_FILTER_KEEP_STRUCTURE);
            if (ret == -1) {
                fprintf(stderr, "hwloc_topology_set_all_types_filter failed\n");
                free(refname); free(hwloc_file);
                goto ERROR;
            }

            ret = hwloc_topology_set_io_types_filter(topology, HWLOC_TYPE_FILTER_KEEP_NONE);
            if (ret == -1) {
                fprintf(stderr, "hwloc_topology_set_all_types_filter failed\n");
                free(refname); free(hwloc_file);
                goto ERROR;
            }

            ret = hwloc_topology_load(topology);
            if (ret == -1) {
                fprintf(stderr, "hwloc_topology_load failed\n");
                free(refname); free(hwloc_file);
                goto ERROR;
            }
            utarray_push_back(hwloc_topos, &topology);
        }
        free(refname);
        free(hwloc_file);
        node->hwlocTopo = *(hwloc_topology_t *)utarray_eltptr(hwloc_topos, t);
        node->hwlocTopoIdx = t;
    }

    if (!num_diffs) {
        printf("Warning: no hwloc diff file found!\n");
    }

    topology->topos = hwloc_topo_names;
    topology->hwloc_topos = (hwloc_topology_t *)hwloc_topos->d;

    printf("%d hwloc topologies found:\n", utarray_len(topology->topos));
    for (unsigned int p = 0; p < utarray_len(topology->topos); p++) {
        printf("\t'%s'\n", *(char **)utarray_eltptr(topology->topos, p));
    }

    ret = NETLOC_SUCCESS;

ERROR:
    if (all) {
        free(node_list);
    }
    free(hwloc_path);
    if (ret == NETLOC_SUCCESS)
        free(hwloc_topos);
    else
        utarray_free(hwloc_topos);
    return ret;
}

/* Set the info from hwloc of the node in the correspondig arch */
int netloc_arch_node_get_hwloc_info(netloc_arch_node_t *arch_node)
{
    hwloc_topology_t topology = arch_node->node->hwlocTopo;

    hwloc_obj_t root = hwloc_get_root_obj(topology);

    int depth = hwloc_topology_get_depth(topology);
    hwloc_obj_t first_object = root->first_child;

    UT_array **down_degrees_by_level;
    NETLOC_int *max_down_degrees_by_level;

    down_degrees_by_level = (UT_array **)malloc(depth*sizeof(UT_array *));
    for (int l = 0; l < depth; l++) {
        utarray_new(down_degrees_by_level[l], &ut_int_icd);
    }
    max_down_degrees_by_level = (NETLOC_int *)
        calloc(depth-1, sizeof(NETLOC_int));

    int level = depth-1;
    hwloc_obj_t current_object = first_object;
    while (level >= 1) {
        int degree = 1;
        /* we go through the siblings */
        while (current_object->next_sibling) {
            current_object = current_object->next_sibling;
            degree++;
        }
        /* Add the degree to the list of degrees */
        utarray_push_back(down_degrees_by_level[depth-1-level], &degree);
        max_down_degrees_by_level[depth-1-level] =
            max_down_degrees_by_level[depth-1-level] > degree ?
            max_down_degrees_by_level[depth-1-level] : degree;

        current_object = current_object->next_cousin;

        if (!current_object) {
            level--;
            if (!first_object->first_child)
                break;
            first_object = first_object->first_child;
            current_object = first_object;
        }
    }

    /* List of PUs */
    unsigned int max_os_index = 0;
    UT_array *ordered_host_array;
    int *ordered_hosts;
    utarray_new(ordered_host_array, &ut_int_icd);
    current_object = first_object;
    while (current_object) {
        max_os_index = (max_os_index >= current_object->os_index)?
            max_os_index: current_object->os_index;
        utarray_push_back(ordered_host_array, &current_object->os_index);
        current_object = current_object->next_cousin;
    }
    ordered_hosts = (int *)ordered_host_array->d;;

    /* Weight for the edges in the tree */
    NETLOC_int *cost = (NETLOC_int *)malloc((depth-1)*sizeof(NETLOC_int));
    int level_coeff = 3;
    cost[depth-2] = 1;
    for (int l = depth-3; l >= 0; l--) {
        cost[l] = cost[l+1]*level_coeff;
    }

    netloc_arch_tree_t *tree = (netloc_arch_tree_t *)
        malloc(sizeof(netloc_arch_tree_t));
    tree->num_levels = depth-1;
    tree->degrees = max_down_degrees_by_level;
    tree->cost = cost;

    int *arch_idx;
    int num_cores = utarray_len(ordered_host_array);
    netloc_arch_tree_complete(tree, down_degrees_by_level, num_cores, &arch_idx);

    int *slot_idx = (int *)malloc(sizeof(int[max_os_index+1]));
    for (int i = 0; i < num_cores; i++) {
        slot_idx[ordered_hosts[i]] = arch_idx[i];
    }

    int num_leaves = netloc_arch_tree_num_leaves(tree);
    int *slot_os_idx = (int *)malloc(sizeof(int[num_leaves]));
    for (int i = 0; i < num_cores; i++) {
        slot_os_idx[arch_idx[i]] = ordered_hosts[i];
    }
    free(arch_idx);

    arch_node->slot_tree = tree;
    arch_node->slot_idx = slot_idx;
    arch_node->slot_os_idx = slot_os_idx;
    arch_node->num_slots = max_os_index+1;

    for (int l = 0; l < depth; l++) {
        utarray_free(down_degrees_by_level[l]);
    }
    free(down_degrees_by_level);

    utarray_free(ordered_host_array);

    return NETLOC_SUCCESS;
}