55 * See COPYING in top-level directory.
66****************************************************************************/
77
8+ #include <time.h>
9+ #include <stdlib.h>
810#include "private/autogen/config.h"
911#include "hwloc.h"
1012
1113struct hwloc_distrib_level {
1214 hwloc_obj_type_t type ; // level type.
1315 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[.
1719 // Iteration order of this level objects. index[coord] give logical_index below parent.
18- size_t * index ;
20+ unsigned * index ;
1921};
2022
2123struct hwloc_distrib_iterator {
2224 hwloc_obj_t * roots ;
23- size_t n_roots ;
24- size_t root_coord ;
25+ unsigned n_roots ;
26+ unsigned root_coord ;
2527 struct hwloc_distrib_level * * levels ; // n_roots * n_levels
26- size_t n_levels ;
28+ unsigned n_levels ;
2729};
2830
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 ));
3133
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 ;
3438 return r ;
3539}
3640
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 ));
3943
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 ; }
4247 return r ;
4348}
4449
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 ;
4755
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+ }
5061
5162 srand (time (NULL ));
5263 for (i = n ;i > 0 ;i -- ){
5364 val = rand ()%(i );
5465 ret [i - 1 ] = index [val ];
5566 index [val ] = index [i - 1 ];
56- }
57- free (index );
58- return ret ;
67+ }
68+ free (index );
69+ return ret ;
5970}
6071
6172static int hwloc_distrib_level_cmp_depth (const void * la , const void * lb ){
6273 const struct hwloc_distrib_level * a = (struct hwloc_distrib_level * )la ;
6374 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 ;
6679 return 0 ;
6780}
6881
6982static int hwloc_distrib_level_cmp_user_index (const void * la , const void * lb ){
7083 const struct hwloc_distrib_level * a = (struct hwloc_distrib_level * )la ;
7184 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 ;
7489 return 0 ;
7590}
7691
7792static struct hwloc_distrib_level *
7893hwloc_distrib_root_levels (hwloc_topology_t topology ,
7994 const hwloc_obj_t root ,
8095 const hwloc_obj_type_t * types ,
81- const size_t n_types ,
96+ const unsigned n_types ,
8297 const unsigned long flags )
8398{
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 ));
85105 if (levels == NULL )
86106 return NULL ;
87107
88- for (size_t i = 0 ; i < n_types ; i ++ ){
108+ for (i = 0 ; i < n_types ; i ++ ){
89109 levels [i ].type = types [i ];
90110 levels [i ].depth = hwloc_get_type_depth (topology , types [i ]);
91111 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 ]));
94113 goto failure ;
95114 }
96115 levels [i ].index = NULL ;
@@ -100,40 +119,27 @@ hwloc_distrib_root_levels(hwloc_topology_t topology,
100119 }
101120
102121 // 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 );
107123
108124 // 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 ++ ){
112127 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 );
116129 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 );
120131 }
121132
122133 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 ));
126135 goto failure ;
127136 }
128137
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 );
133139 }
134140
135141 // Allocate levels index.
136- for (size_t i = 0 ; i < n_types ; i ++ ){
142+ for (i = 0 ; i < n_types ; i ++ ){
137143 if (flags & HWLOC_DISTRIB_FLAG_SHUFFLE ) {
138144 levels [i ].index = shuffled_range (levels [i ].arity );
139145 }
@@ -165,25 +171,26 @@ static void hwloc_distrib_destroy_level(struct hwloc_distrib_level *levels){
165171struct hwloc_distrib_iterator *
166172hwloc_distrib_build_iterator (hwloc_topology_t topology ,
167173 hwloc_obj_t * roots ,
168- const size_t n_roots ,
174+ const unsigned n_roots ,
169175 const hwloc_obj_type_t * levels ,
170- const size_t n_levels ,
176+ const unsigned n_levels ,
171177 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 ;
176182
177183 it -> roots = roots ;
178184 it -> n_roots = n_roots ;
179185 it -> root_coord = 0 ;
180186 it -> n_levels = n_levels ;
181187 it -> levels = (struct hwloc_distrib_level * * )((char * )it + sizeof (* it ));
182188
183- for (size_t i = 0 ; i < n_roots ; i ++ ){
189+ for (i = 0 ; i < n_roots ; i ++ ){
184190 it -> levels [i ] = hwloc_distrib_root_levels (topology , roots [i ], levels , n_levels , flags );
185191 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 ]);
187194 goto failure ;
188195 }
189196 }
@@ -200,19 +207,18 @@ hwloc_distrib_iterator_round_robin(hwloc_topology_t topology,
200207 const hwloc_obj_type_t type ,
201208 const unsigned long flags ){
202209 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 ;
207215
208216 it -> roots = (hwloc_obj_t * ) ((char * )it + sizeof (* it ));
209217 * it -> roots = root ;
210218 it -> n_roots = 1 ;
211219 it -> root_coord = 0 ;
212220 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 ));
216222 * it -> levels = hwloc_distrib_root_levels (topology , root , & type , 1 , flags );
217223
218224 if (* it -> levels == NULL ){ free (it ); return NULL ; }
@@ -224,26 +230,26 @@ hwloc_distrib_iterator_scatter(hwloc_topology_t topology,
224230 const hwloc_obj_type_t type ,
225231 const unsigned long flags ){
226232
227- size_t i = 0 , n = 0 ;
233+ unsigned i = 0 , n = 0 ;
228234 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 ;
230237
231238 // Count depths with a non empty cpuset.
239+ obj = root ;
232240 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 )
235242 n ++ ;
236243 if (obj -> type == type )
237244 break ;
238245 obj = obj -> first_child ;
239246 }
240-
241- obj = root ;
242- hwloc_obj_type_t levels [n ];
247+
243248 // fill levels array.
249+ levels = malloc (sizeof (* levels ) * n );
250+ obj = root ;
244251 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 ){
247253 levels [n - 1 - i ] = obj -> type ;
248254 i ++ ;
249255 }
@@ -252,28 +258,37 @@ hwloc_distrib_iterator_scatter(hwloc_topology_t topology,
252258 obj = obj -> first_child ;
253259 }
254260
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 ;
259265
260266 it -> roots = (hwloc_obj_t * ) ((char * )it + sizeof (* it ));
261267 * it -> roots = root ;
262268 it -> n_roots = 1 ;
263269 it -> root_coord = 0 ;
264270 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 ));
268272
269273 * it -> levels = hwloc_distrib_root_levels (topology , root , levels , n , flags );
270274
271- if (* it -> levels == NULL ){ free (it ); return NULL ; }
275+ if (* it -> levels == NULL )
276+ goto failure_with_it ;
277+
278+ free (levels );
272279 return it ;
280+
281+ failure_with_it :
282+ free (it );
283+ failure :
284+ free (levels );
285+ return NULL ;
273286}
274287
275288void 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 ++ )
277292 hwloc_distrib_destroy_level (it -> levels [i ]);
278293 free (it );
279294}
@@ -288,10 +303,7 @@ hwloc_distrib_iterator_inc(struct hwloc_distrib_iterator *it){
288303 do_root :
289304 // Sort by user_index to increment coordinates.
290305 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 );
295307
296308 for (i = it -> n_levels - 1 ; i >=0 ; i -- ){
297309 if (++ levels [i ].coord >= levels [i ].arity )
@@ -314,22 +326,17 @@ int
314326hwloc_distrib_iterator_next (hwloc_topology_t topology ,
315327 struct hwloc_distrib_iterator * it ,
316328 hwloc_obj_t * next ){
329+ unsigned i ;
317330 struct hwloc_distrib_level * levels = it -> levels [it -> root_coord ];
318331 hwloc_obj_t obj = it -> roots [it -> root_coord ];
319- size_t coord ;
332+ unsigned coord ;
320333
321334 // 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 );
326336
327- for (size_t i = 0 ; i < it -> n_levels ; i ++ ){
337+ for (i = 0 ; i < it -> n_levels ; i ++ ){
328338 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 );
333340 if ( obj == NULL )
334341 return hwloc_distrib_iterator_inc (it ) && hwloc_distrib_iterator_next (topology , it , next );
335342
0 commit comments