5
5
* See COPYING in top-level directory.
6
6
****************************************************************************/
7
7
8
+ #include <time.h>
9
+ #include <stdlib.h>
8
10
#include "private/autogen/config.h"
9
11
#include "hwloc.h"
10
12
11
13
struct hwloc_distrib_level {
12
14
hwloc_obj_type_t type ; // level type.
13
15
unsigned depth ; // level depth.
14
- size_t user_index ; // Index of this level as provided by user order.
15
- size_t arity ; // Number of children of this level below parent.
16
- size_t coord ; // The current level object index [0..arity[.
16
+ unsigned user_index ; // Index of this level as provided by user order.
17
+ unsigned arity ; // Number of children of this level below parent.
18
+ unsigned coord ; // The current level object index [0..arity[.
17
19
// Iteration order of this level objects. index[coord] give logical_index below parent.
18
- size_t * index ;
20
+ unsigned * index ;
19
21
};
20
22
21
23
struct hwloc_distrib_iterator {
22
24
hwloc_obj_t * roots ;
23
- size_t n_roots ;
24
- size_t root_coord ;
25
+ unsigned n_roots ;
26
+ unsigned root_coord ;
25
27
struct hwloc_distrib_level * * levels ; // n_roots * n_levels
26
- size_t n_levels ;
28
+ unsigned n_levels ;
27
29
};
28
30
29
- static size_t * range (const size_t n ){
30
- size_t * r = malloc (n * sizeof (* r ));
31
+ static unsigned * range (const unsigned n ){
32
+ unsigned i , * r = malloc (n * sizeof (* r ));
31
33
32
- if (r == NULL ) return NULL ;
33
- for (size_t i = 0 ; i < n ; i ++ ){ r [i ] = i ; }
34
+ if (r == NULL )
35
+ return NULL ;
36
+ for (i = 0 ; i < n ; i ++ )
37
+ r [i ] = i ;
34
38
return r ;
35
39
}
36
40
37
- static size_t * reversed_range (const size_t n ){
38
- size_t * r = malloc (n * sizeof (* r ));
41
+ static unsigned * reversed_range (const unsigned n ){
42
+ unsigned i , * r = malloc (n * sizeof (* r ));
39
43
40
- if (r == NULL ) return NULL ;
41
- for (size_t i = 0 ; i < n ; i ++ ){ r [i ] = n - i - 1 ; }
44
+ if (r == NULL )
45
+ return NULL ;
46
+ for (i = 0 ; i < n ; i ++ ){ r [i ] = n - i - 1 ; }
42
47
return r ;
43
48
}
44
49
45
- static size_t * shuffled_range (const size_t n ){
46
- size_t i , * index , * ret , val ;
50
+ static unsigned * shuffled_range (const unsigned n ){
51
+ unsigned i , * index , * ret , val ;
52
+
53
+ if ((index = range (n )) == NULL )
54
+ return NULL ;
47
55
48
- if ((index = range (n )) == NULL ) return NULL ;
49
- if ((ret = malloc (n * sizeof (* ret ))) == NULL ) { free (index ); return NULL ; }
56
+ ret = malloc (n * sizeof (* ret ));
57
+ if (ret == NULL ) {
58
+ free (index );
59
+ return NULL ;
60
+ }
50
61
51
62
srand (time (NULL ));
52
63
for (i = n ;i > 0 ;i -- ){
53
64
val = rand ()%(i );
54
65
ret [i - 1 ] = index [val ];
55
66
index [val ] = index [i - 1 ];
56
- }
57
- free (index );
58
- return ret ;
67
+ }
68
+ free (index );
69
+ return ret ;
59
70
}
60
71
61
72
static int hwloc_distrib_level_cmp_depth (const void * la , const void * lb ){
62
73
const struct hwloc_distrib_level * a = (struct hwloc_distrib_level * )la ;
63
74
const struct hwloc_distrib_level * b = (struct hwloc_distrib_level * )lb ;
64
- if (a -> depth > b -> depth ) { return 1 ; }
65
- if (a -> depth < b -> depth ) { return -1 ; }
75
+ if (a -> depth > b -> depth )
76
+ return 1 ;
77
+ if (a -> depth < b -> depth )
78
+ return -1 ;
66
79
return 0 ;
67
80
}
68
81
69
82
static int hwloc_distrib_level_cmp_user_index (const void * la , const void * lb ){
70
83
const struct hwloc_distrib_level * a = (struct hwloc_distrib_level * )la ;
71
84
const struct hwloc_distrib_level * b = (struct hwloc_distrib_level * )lb ;
72
- if (a -> user_index > b -> user_index ) { return 1 ; }
73
- if (a -> user_index < b -> user_index ) { return -1 ; }
85
+ if (a -> user_index > b -> user_index )
86
+ return 1 ;
87
+ if (a -> user_index < b -> user_index )
88
+ return -1 ;
74
89
return 0 ;
75
90
}
76
91
77
92
static struct hwloc_distrib_level *
78
93
hwloc_distrib_root_levels (hwloc_topology_t topology ,
79
94
const hwloc_obj_t root ,
80
95
const hwloc_obj_type_t * types ,
81
- const size_t n_types ,
96
+ const unsigned n_types ,
82
97
const unsigned long flags )
83
98
{
84
- struct hwloc_distrib_level * levels = malloc (n_types * sizeof (* levels ));
99
+ unsigned i ;
100
+ unsigned arity ;
101
+ hwloc_obj_t parent ;
102
+ struct hwloc_distrib_level * levels ;
103
+
104
+ levels = malloc (n_types * sizeof (* levels ));
85
105
if (levels == NULL )
86
106
return NULL ;
87
107
88
- for (size_t i = 0 ; i < n_types ; i ++ ){
108
+ for (i = 0 ; i < n_types ; i ++ ){
89
109
levels [i ].type = types [i ];
90
110
levels [i ].depth = hwloc_get_type_depth (topology , types [i ]);
91
111
if (levels [i ].depth < 0 ){
92
- fprintf (stderr , "Cannot build iterator with objects %s of negative depth.\n" ,
93
- hwloc_obj_type_string (types [i ]));
112
+ fprintf (stderr , "Cannot build iterator with objects %s of negative depth.\n" , hwloc_obj_type_string (types [i ]));
94
113
goto failure ;
95
114
}
96
115
levels [i ].index = NULL ;
@@ -100,40 +119,27 @@ hwloc_distrib_root_levels(hwloc_topology_t topology,
100
119
}
101
120
102
121
// Sort by depth to compute arities.
103
- qsort (levels ,
104
- n_types ,
105
- sizeof (* levels ),
106
- hwloc_distrib_level_cmp_depth );
122
+ qsort (levels , n_types , sizeof (* levels ), hwloc_distrib_level_cmp_depth );
107
123
108
124
// Walk from top to bottom and set arity to the maximum arity below root field.
109
- size_t arity ;
110
- hwloc_obj_t parent = root ;
111
- for (size_t i = 0 ; i < n_types ; i ++ ){
125
+ parent = root ;
126
+ for (i = 0 ; i < n_types ; i ++ ){
112
127
while (parent ) {
113
- arity = hwloc_get_nbobjs_inside_cpuset_by_depth (topology ,
114
- parent -> cpuset ,
115
- levels [i ].depth );
128
+ arity = hwloc_get_nbobjs_inside_cpuset_by_depth (topology , parent -> cpuset , levels [i ].depth );
116
129
levels [i ].arity = arity > levels [i ].arity ? arity : levels [i ].arity ;
117
- parent = hwloc_get_next_obj_inside_cpuset_by_depth (topology ,
118
- root -> cpuset ,
119
- parent -> depth , parent );
130
+ parent = hwloc_get_next_obj_inside_cpuset_by_depth (topology , root -> cpuset , parent -> depth , parent );
120
131
}
121
132
122
133
if (levels [i ].arity == 0 ) {
123
- fprintf (stderr , "No object of type %s below level %s.\n" ,
124
- hwloc_obj_type_string (levels [i ].type ),
125
- hwloc_obj_type_string (levels [i - 1 ].type ));
134
+ fprintf (stderr , "No object of type %s below level %s.\n" , hwloc_obj_type_string (levels [i ].type ), hwloc_obj_type_string (levels [i - 1 ].type ));
126
135
goto failure ;
127
136
}
128
137
129
- parent = hwloc_get_obj_inside_cpuset_by_depth (topology ,
130
- root -> cpuset ,
131
- levels [i ].depth ,
132
- 0 );
138
+ parent = hwloc_get_obj_inside_cpuset_by_depth (topology , root -> cpuset , levels [i ].depth , 0 );
133
139
}
134
140
135
141
// Allocate levels index.
136
- for (size_t i = 0 ; i < n_types ; i ++ ){
142
+ for (i = 0 ; i < n_types ; i ++ ){
137
143
if (flags & HWLOC_DISTRIB_FLAG_SHUFFLE ) {
138
144
levels [i ].index = shuffled_range (levels [i ].arity );
139
145
}
@@ -165,25 +171,26 @@ static void hwloc_distrib_destroy_level(struct hwloc_distrib_level *levels){
165
171
struct hwloc_distrib_iterator *
166
172
hwloc_distrib_build_iterator (hwloc_topology_t topology ,
167
173
hwloc_obj_t * roots ,
168
- const size_t n_roots ,
174
+ const unsigned n_roots ,
169
175
const hwloc_obj_type_t * levels ,
170
- const size_t n_levels ,
176
+ const unsigned n_levels ,
171
177
const unsigned long flags ){
172
-
173
- struct hwloc_distrib_iterator * it = malloc (sizeof (* it ) +
174
- sizeof ( * it -> levels ) * n_roots );
175
- if ( it == NULL ) return NULL ;
178
+ unsigned i ;
179
+ struct hwloc_distrib_iterator * it = malloc (sizeof (* it ) + sizeof ( * it -> levels ) * n_roots );
180
+ if ( it == NULL )
181
+ return NULL ;
176
182
177
183
it -> roots = roots ;
178
184
it -> n_roots = n_roots ;
179
185
it -> root_coord = 0 ;
180
186
it -> n_levels = n_levels ;
181
187
it -> levels = (struct hwloc_distrib_level * * )((char * )it + sizeof (* it ));
182
188
183
- for (size_t i = 0 ; i < n_roots ; i ++ ){
189
+ for (i = 0 ; i < n_roots ; i ++ ){
184
190
it -> levels [i ] = hwloc_distrib_root_levels (topology , roots [i ], levels , n_levels , flags );
185
191
if (it -> levels [i ] == NULL ){
186
- while (i -- ){ hwloc_distrib_destroy_level (it -> levels [i ]); }
192
+ while (i -- )
193
+ hwloc_distrib_destroy_level (it -> levels [i ]);
187
194
goto failure ;
188
195
}
189
196
}
@@ -200,19 +207,18 @@ hwloc_distrib_iterator_round_robin(hwloc_topology_t topology,
200
207
const hwloc_obj_type_t type ,
201
208
const unsigned long flags ){
202
209
hwloc_obj_t root = hwloc_get_obj_by_depth (topology , 0 , 0 );
203
- struct hwloc_distrib_iterator * it = malloc (sizeof (* it ) +
204
- sizeof (hwloc_obj_t ) +
205
- sizeof (struct hwloc_distrib_level * ));
206
- if (it == NULL ) return NULL ;
210
+ struct hwloc_distrib_iterator * it ;
211
+
212
+ it = malloc (sizeof (* it ) + sizeof (hwloc_obj_t ) + sizeof (struct hwloc_distrib_level * ));
213
+ if (it == NULL )
214
+ return NULL ;
207
215
208
216
it -> roots = (hwloc_obj_t * ) ((char * )it + sizeof (* it ));
209
217
* it -> roots = root ;
210
218
it -> n_roots = 1 ;
211
219
it -> root_coord = 0 ;
212
220
it -> n_levels = 1 ;
213
- it -> levels = (struct hwloc_distrib_level * * )((char * )it +
214
- sizeof (* it ) +
215
- sizeof (hwloc_obj_t ));
221
+ it -> levels = (struct hwloc_distrib_level * * )((char * )it + sizeof (* it ) + sizeof (hwloc_obj_t ));
216
222
* it -> levels = hwloc_distrib_root_levels (topology , root , & type , 1 , flags );
217
223
218
224
if (* it -> levels == NULL ){ free (it ); return NULL ; }
@@ -224,26 +230,26 @@ hwloc_distrib_iterator_scatter(hwloc_topology_t topology,
224
230
const hwloc_obj_type_t type ,
225
231
const unsigned long flags ){
226
232
227
- size_t i = 0 , n = 0 ;
233
+ unsigned i = 0 , n = 0 ;
228
234
hwloc_obj_t obj , root = hwloc_get_obj_by_depth (topology , 0 , 0 );
229
- obj = root ;
235
+ hwloc_obj_type_t * levels ;
236
+ struct hwloc_distrib_iterator * it ;
230
237
231
238
// Count depths with a non empty cpuset.
239
+ obj = root ;
232
240
while (obj ){
233
- if ((obj -> cpuset != NULL && !hwloc_bitmap_iszero (obj -> cpuset )) &&
234
- hwloc_get_type_depth (topology , obj -> type ) >= 0 )
241
+ if ((obj -> cpuset != NULL && !hwloc_bitmap_iszero (obj -> cpuset )) && hwloc_get_type_depth (topology , obj -> type ) >= 0 )
235
242
n ++ ;
236
243
if (obj -> type == type )
237
244
break ;
238
245
obj = obj -> first_child ;
239
246
}
240
-
241
- obj = root ;
242
- hwloc_obj_type_t levels [n ];
247
+
243
248
// fill levels array.
249
+ levels = malloc (sizeof (* levels ) * n );
250
+ obj = root ;
244
251
while (obj ){
245
- if ( obj -> cpuset != NULL && !hwloc_bitmap_iszero (obj -> cpuset ) &&
246
- hwloc_get_type_depth (topology , obj -> type ) >= 0 ){
252
+ if ( obj -> cpuset != NULL && !hwloc_bitmap_iszero (obj -> cpuset ) && hwloc_get_type_depth (topology , obj -> type ) >= 0 ){
247
253
levels [n - 1 - i ] = obj -> type ;
248
254
i ++ ;
249
255
}
@@ -252,28 +258,37 @@ hwloc_distrib_iterator_scatter(hwloc_topology_t topology,
252
258
obj = obj -> first_child ;
253
259
}
254
260
255
- struct hwloc_distrib_iterator * it = malloc (sizeof (* it ) +
256
- sizeof ( hwloc_obj_t ) +
257
- sizeof ( struct hwloc_distrib_level * ));
258
- if ( it == NULL ) return NULL ;
261
+ it = malloc (sizeof (* it ) + sizeof ( hwloc_obj_t ) + sizeof ( struct hwloc_distrib_level * ));
262
+
263
+ if ( it == NULL )
264
+ goto failure ;
259
265
260
266
it -> roots = (hwloc_obj_t * ) ((char * )it + sizeof (* it ));
261
267
* it -> roots = root ;
262
268
it -> n_roots = 1 ;
263
269
it -> root_coord = 0 ;
264
270
it -> n_levels = n ;
265
- it -> levels = (struct hwloc_distrib_level * * )((char * )it +
266
- sizeof (* it ) +
267
- sizeof (hwloc_obj_t ));
271
+ it -> levels = (struct hwloc_distrib_level * * )((char * )it + sizeof (* it ) + sizeof (hwloc_obj_t ));
268
272
269
273
* it -> levels = hwloc_distrib_root_levels (topology , root , levels , n , flags );
270
274
271
- if (* it -> levels == NULL ){ free (it ); return NULL ; }
275
+ if (* it -> levels == NULL )
276
+ goto failure_with_it ;
277
+
278
+ free (levels );
272
279
return it ;
280
+
281
+ failure_with_it :
282
+ free (it );
283
+ failure :
284
+ free (levels );
285
+ return NULL ;
273
286
}
274
287
275
288
void hwloc_distrib_destroy_iterator (struct hwloc_distrib_iterator * it ){
276
- for (size_t i = 0 ; i < it -> n_roots ; i ++ )
289
+ unsigned i ;
290
+
291
+ for (i = 0 ; i < it -> n_roots ; i ++ )
277
292
hwloc_distrib_destroy_level (it -> levels [i ]);
278
293
free (it );
279
294
}
@@ -288,10 +303,7 @@ hwloc_distrib_iterator_inc(struct hwloc_distrib_iterator *it){
288
303
do_root :
289
304
// Sort by user_index to increment coordinates.
290
305
levels = it -> levels [it -> root_coord ];
291
- qsort (levels ,
292
- it -> n_levels ,
293
- sizeof (* levels ),
294
- hwloc_distrib_level_cmp_user_index );
306
+ qsort (levels , it -> n_levels , sizeof (* levels ), hwloc_distrib_level_cmp_user_index );
295
307
296
308
for (i = it -> n_levels - 1 ; i >=0 ; i -- ){
297
309
if (++ levels [i ].coord >= levels [i ].arity )
@@ -314,22 +326,17 @@ int
314
326
hwloc_distrib_iterator_next (hwloc_topology_t topology ,
315
327
struct hwloc_distrib_iterator * it ,
316
328
hwloc_obj_t * next ){
329
+ unsigned i ;
317
330
struct hwloc_distrib_level * levels = it -> levels [it -> root_coord ];
318
331
hwloc_obj_t obj = it -> roots [it -> root_coord ];
319
- size_t coord ;
332
+ unsigned coord ;
320
333
321
334
// Sort by depth to walk objects at set coordinates.
322
- qsort (levels ,
323
- it -> n_levels ,
324
- sizeof (* levels ),
325
- hwloc_distrib_level_cmp_depth );
335
+ qsort (levels , it -> n_levels , sizeof (* levels ), hwloc_distrib_level_cmp_depth );
326
336
327
- for (size_t i = 0 ; i < it -> n_levels ; i ++ ){
337
+ for (i = 0 ; i < it -> n_levels ; i ++ ){
328
338
coord = levels [i ].index [levels [i ].coord ];
329
- obj = hwloc_get_obj_inside_cpuset_by_depth (topology ,
330
- obj -> cpuset ,
331
- levels [i ].depth ,
332
- coord );
339
+ obj = hwloc_get_obj_inside_cpuset_by_depth (topology , obj -> cpuset , levels [i ].depth , coord );
333
340
if ( obj == NULL )
334
341
return hwloc_distrib_iterator_inc (it ) && hwloc_distrib_iterator_next (topology , it , next );
335
342
0 commit comments