-
Notifications
You must be signed in to change notification settings - Fork 168
/
distances.c
1093 lines (947 loc) · 32.2 KB
/
distances.c
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright © 2010-2021 Inria. All rights reserved.
* Copyright © 2011-2012 Université Bordeaux
* Copyright © 2011 Cisco Systems, Inc. All rights reserved.
* See COPYING in top-level directory.
*/
#include "private/autogen/config.h"
#include "hwloc.h"
#include "private/private.h"
#include "private/debug.h"
#include "private/misc.h"
#include <float.h>
#include <math.h>
static struct hwloc_internal_distances_s *
hwloc__internal_distances_from_public(hwloc_topology_t topology, struct hwloc_distances_s *distances);
/******************************************************
* Global init, prepare, destroy, dup
*/
/* called during topology init() */
void hwloc_internal_distances_init(struct hwloc_topology *topology)
{
topology->first_dist = topology->last_dist = NULL;
topology->next_dist_id = 0;
}
/* called at the beginning of load() */
void hwloc_internal_distances_prepare(struct hwloc_topology *topology)
{
char *env;
hwloc_localeswitch_declare;
topology->grouping = 1;
if (topology->type_filter[HWLOC_OBJ_GROUP] == HWLOC_TYPE_FILTER_KEEP_NONE)
topology->grouping = 0;
env = getenv("HWLOC_GROUPING");
if (env && !atoi(env))
topology->grouping = 0;
if (topology->grouping) {
topology->grouping_next_subkind = 0;
HWLOC_BUILD_ASSERT(sizeof(topology->grouping_accuracies)/sizeof(*topology->grouping_accuracies) == 5);
topology->grouping_accuracies[0] = 0.0f;
topology->grouping_accuracies[1] = 0.01f;
topology->grouping_accuracies[2] = 0.02f;
topology->grouping_accuracies[3] = 0.05f;
topology->grouping_accuracies[4] = 0.1f;
topology->grouping_nbaccuracies = 5;
hwloc_localeswitch_init();
env = getenv("HWLOC_GROUPING_ACCURACY");
if (!env) {
/* only use 0.0 */
topology->grouping_nbaccuracies = 1;
} else if (strcmp(env, "try")) {
/* use the given value */
topology->grouping_nbaccuracies = 1;
topology->grouping_accuracies[0] = (float) atof(env);
} /* otherwise try all values */
hwloc_localeswitch_fini();
topology->grouping_verbose = 0;
env = getenv("HWLOC_GROUPING_VERBOSE");
if (env)
topology->grouping_verbose = atoi(env);
}
}
static void hwloc_internal_distances_free(struct hwloc_internal_distances_s *dist)
{
free(dist->name);
free(dist->different_types);
free(dist->indexes);
free(dist->objs);
free(dist->values);
free(dist);
}
/* called during topology destroy */
void hwloc_internal_distances_destroy(struct hwloc_topology * topology)
{
struct hwloc_internal_distances_s *dist, *next = topology->first_dist;
while ((dist = next) != NULL) {
next = dist->next;
hwloc_internal_distances_free(dist);
}
topology->first_dist = topology->last_dist = NULL;
}
static int hwloc_internal_distances_dup_one(struct hwloc_topology *new, struct hwloc_internal_distances_s *olddist)
{
struct hwloc_tma *tma = new->tma;
struct hwloc_internal_distances_s *newdist;
unsigned nbobjs = olddist->nbobjs;
newdist = hwloc_tma_malloc(tma, sizeof(*newdist));
if (!newdist)
return -1;
if (olddist->name) {
newdist->name = hwloc_tma_strdup(tma, olddist->name);
if (!newdist->name) {
assert(!tma || !tma->dontfree); /* this tma cannot fail to allocate */
hwloc_internal_distances_free(newdist);
return -1;
}
} else {
newdist->name = NULL;
}
if (olddist->different_types) {
newdist->different_types = hwloc_tma_malloc(tma, nbobjs * sizeof(*newdist->different_types));
if (!newdist->different_types) {
assert(!tma || !tma->dontfree); /* this tma cannot fail to allocate */
hwloc_internal_distances_free(newdist);
return -1;
}
memcpy(newdist->different_types, olddist->different_types, nbobjs * sizeof(*newdist->different_types));
} else
newdist->different_types = NULL;
newdist->unique_type = olddist->unique_type;
newdist->nbobjs = nbobjs;
newdist->kind = olddist->kind;
newdist->id = olddist->id;
newdist->indexes = hwloc_tma_malloc(tma, nbobjs * sizeof(*newdist->indexes));
newdist->objs = hwloc_tma_calloc(tma, nbobjs * sizeof(*newdist->objs));
newdist->iflags = olddist->iflags & ~HWLOC_INTERNAL_DIST_FLAG_OBJS_VALID; /* must be revalidated after dup() */
newdist->values = hwloc_tma_malloc(tma, nbobjs*nbobjs * sizeof(*newdist->values));
if (!newdist->indexes || !newdist->objs || !newdist->values) {
assert(!tma || !tma->dontfree); /* this tma cannot fail to allocate */
hwloc_internal_distances_free(newdist);
return -1;
}
memcpy(newdist->indexes, olddist->indexes, nbobjs * sizeof(*newdist->indexes));
memcpy(newdist->values, olddist->values, nbobjs*nbobjs * sizeof(*newdist->values));
newdist->next = NULL;
newdist->prev = new->last_dist;
if (new->last_dist)
new->last_dist->next = newdist;
else
new->first_dist = newdist;
new->last_dist = newdist;
return 0;
}
/* This function may be called with topology->tma set, it cannot free() or realloc() */
int hwloc_internal_distances_dup(struct hwloc_topology *new, struct hwloc_topology *old)
{
struct hwloc_internal_distances_s *olddist;
int err;
new->next_dist_id = old->next_dist_id;
for(olddist = old->first_dist; olddist; olddist = olddist->next) {
err = hwloc_internal_distances_dup_one(new, olddist);
if (err < 0)
return err;
}
return 0;
}
/******************************************************
* Remove distances from the topology
*/
int hwloc_distances_remove(hwloc_topology_t topology)
{
if (!topology->is_loaded) {
errno = EINVAL;
return -1;
}
if (topology->adopted_shmem_addr) {
errno = EPERM;
return -1;
}
hwloc_internal_distances_destroy(topology);
return 0;
}
int hwloc_distances_remove_by_depth(hwloc_topology_t topology, int depth)
{
struct hwloc_internal_distances_s *dist, *next;
hwloc_obj_type_t type;
if (!topology->is_loaded) {
errno = EINVAL;
return -1;
}
if (topology->adopted_shmem_addr) {
errno = EPERM;
return -1;
}
/* switch back to types since we don't support groups for now */
type = hwloc_get_depth_type(topology, depth);
if (type == (hwloc_obj_type_t)-1) {
errno = EINVAL;
return -1;
}
next = topology->first_dist;
while ((dist = next) != NULL) {
next = dist->next;
if (dist->unique_type == type) {
if (next)
next->prev = dist->prev;
else
topology->last_dist = dist->prev;
if (dist->prev)
dist->prev->next = dist->next;
else
topology->first_dist = dist->next;
hwloc_internal_distances_free(dist);
}
}
return 0;
}
int hwloc_distances_release_remove(hwloc_topology_t topology,
struct hwloc_distances_s *distances)
{
struct hwloc_internal_distances_s *dist = hwloc__internal_distances_from_public(topology, distances);
if (!dist) {
errno = EINVAL;
return -1;
}
if (dist->prev)
dist->prev->next = dist->next;
else
topology->first_dist = dist->next;
if (dist->next)
dist->next->prev = dist->prev;
else
topology->last_dist = dist->prev;
hwloc_internal_distances_free(dist);
hwloc_distances_release(topology, distances);
return 0;
}
/******************************************************
* Add distances to the topology
*/
static void
hwloc__groups_by_distances(struct hwloc_topology *topology, unsigned nbobjs, struct hwloc_obj **objs, uint64_t *values, unsigned long kind, unsigned nbaccuracies, float *accuracies, int needcheck);
/* insert a distance matrix in the topology.
* the caller gives us the distances and objs pointers, we'll free them later.
*/
static int
hwloc_internal_distances__add(hwloc_topology_t topology, const char *name,
hwloc_obj_type_t unique_type, hwloc_obj_type_t *different_types,
unsigned nbobjs, hwloc_obj_t *objs, uint64_t *indexes, uint64_t *values,
unsigned long kind, unsigned iflags)
{
struct hwloc_internal_distances_s *dist;
if (different_types) {
kind |= HWLOC_DISTANCES_KIND_HETEROGENEOUS_TYPES; /* the user isn't forced to give it */
} else if (kind & HWLOC_DISTANCES_KIND_HETEROGENEOUS_TYPES) {
errno = EINVAL;
goto err;
}
dist = calloc(1, sizeof(*dist));
if (!dist)
goto err;
if (name)
dist->name = strdup(name); /* ignore failure */
dist->unique_type = unique_type;
dist->different_types = different_types;
dist->nbobjs = nbobjs;
dist->kind = kind;
dist->iflags = iflags;
assert(!!(iflags & HWLOC_INTERNAL_DIST_FLAG_OBJS_VALID) == !!objs);
if (!objs) {
assert(indexes);
/* we only have indexes, we'll refresh objs from there */
dist->indexes = indexes;
dist->objs = calloc(nbobjs, sizeof(hwloc_obj_t));
if (!dist->objs)
goto err_with_dist;
} else {
unsigned i;
assert(!indexes);
/* we only have objs, generate the indexes arrays so that we can refresh objs later */
dist->objs = objs;
dist->indexes = malloc(nbobjs * sizeof(*dist->indexes));
if (!dist->indexes)
goto err_with_dist;
if (HWLOC_DIST_TYPE_USE_OS_INDEX(dist->unique_type)) {
for(i=0; i<nbobjs; i++)
dist->indexes[i] = objs[i]->os_index;
} else {
for(i=0; i<nbobjs; i++)
dist->indexes[i] = objs[i]->gp_index;
}
}
dist->values = values;
dist->id = topology->next_dist_id++;
if (topology->last_dist)
topology->last_dist->next = dist;
else
topology->first_dist = dist;
dist->prev = topology->last_dist;
dist->next = NULL;
topology->last_dist = dist;
return 0;
err_with_dist:
if (name)
free(dist->name);
free(dist);
err:
free(different_types);
free(objs);
free(indexes);
free(values);
return -1;
}
int hwloc_internal_distances_add_by_index(hwloc_topology_t topology, const char *name,
hwloc_obj_type_t unique_type, hwloc_obj_type_t *different_types, unsigned nbobjs, uint64_t *indexes, uint64_t *values,
unsigned long kind, unsigned long flags)
{
unsigned iflags = 0; /* objs not valid */
if (nbobjs < 2) {
errno = EINVAL;
goto err;
}
/* cannot group without objects,
* and we don't group from XML anyway since the hwloc that generated the XML should have grouped already.
*/
if (flags & HWLOC_DISTANCES_ADD_FLAG_GROUP) {
errno = EINVAL;
goto err;
}
return hwloc_internal_distances__add(topology, name, unique_type, different_types, nbobjs, NULL, indexes, values, kind, iflags);
err:
free(indexes);
free(values);
free(different_types);
return -1;
}
static void
hwloc_internal_distances_restrict(hwloc_obj_t *objs,
uint64_t *indexes,
hwloc_obj_type_t *different_types,
uint64_t *values,
unsigned nbobjs, unsigned disappeared);
int hwloc_internal_distances_add(hwloc_topology_t topology, const char *name,
unsigned nbobjs, hwloc_obj_t *objs, uint64_t *values,
unsigned long kind, unsigned long flags)
{
hwloc_obj_type_t unique_type, *different_types;
unsigned i, disappeared = 0;
unsigned iflags = HWLOC_INTERNAL_DIST_FLAG_OBJS_VALID;
if (nbobjs < 2) {
errno = EINVAL;
goto err;
}
/* is there any NULL object? (useful in case of problem during insert in backends) */
for(i=0; i<nbobjs; i++)
if (!objs[i])
disappeared++;
if (disappeared) {
/* some objects are NULL */
if (disappeared == nbobjs) {
/* nothing left, drop the matrix */
free(objs);
free(values);
return 0;
}
/* restrict the matrix */
hwloc_internal_distances_restrict(objs, NULL, NULL, values, nbobjs, disappeared);
nbobjs -= disappeared;
}
unique_type = objs[0]->type;
for(i=1; i<nbobjs; i++)
if (objs[i]->type != unique_type) {
unique_type = HWLOC_OBJ_TYPE_NONE;
break;
}
if (unique_type == HWLOC_OBJ_TYPE_NONE) {
/* heterogeneous types */
different_types = malloc(nbobjs * sizeof(*different_types));
if (!different_types)
goto err;
for(i=0; i<nbobjs; i++)
different_types[i] = objs[i]->type;
} else {
/* homogeneous types */
different_types = NULL;
}
if (topology->grouping && (flags & HWLOC_DISTANCES_ADD_FLAG_GROUP) && !different_types) {
float full_accuracy = 0.f;
float *accuracies;
unsigned nbaccuracies;
if (flags & HWLOC_DISTANCES_ADD_FLAG_GROUP_INACCURATE) {
accuracies = topology->grouping_accuracies;
nbaccuracies = topology->grouping_nbaccuracies;
} else {
accuracies = &full_accuracy;
nbaccuracies = 1;
}
if (topology->grouping_verbose) {
unsigned j;
int gp = !HWLOC_DIST_TYPE_USE_OS_INDEX(unique_type);
fprintf(stderr, "Trying to group objects using distance matrix:\n");
fprintf(stderr, "%s", gp ? "gp_index" : "os_index");
for(j=0; j<nbobjs; j++)
fprintf(stderr, " % 5d", (int)(gp ? objs[j]->gp_index : objs[j]->os_index));
fprintf(stderr, "\n");
for(i=0; i<nbobjs; i++) {
fprintf(stderr, " % 5d", (int)(gp ? objs[i]->gp_index : objs[i]->os_index));
for(j=0; j<nbobjs; j++)
fprintf(stderr, " % 5lld", (long long) values[i*nbobjs + j]);
fprintf(stderr, "\n");
}
}
hwloc__groups_by_distances(topology, nbobjs, objs, values,
kind, nbaccuracies, accuracies, 1 /* check the first matrice */);
}
return hwloc_internal_distances__add(topology, name, unique_type, different_types, nbobjs, objs, NULL, values, kind, iflags);
err:
free(objs);
free(values);
return -1;
}
#define HWLOC_DISTANCES_KIND_FROM_ALL (HWLOC_DISTANCES_KIND_FROM_OS|HWLOC_DISTANCES_KIND_FROM_USER)
#define HWLOC_DISTANCES_KIND_MEANS_ALL (HWLOC_DISTANCES_KIND_MEANS_LATENCY|HWLOC_DISTANCES_KIND_MEANS_BANDWIDTH)
#define HWLOC_DISTANCES_KIND_ALL (HWLOC_DISTANCES_KIND_FROM_ALL|HWLOC_DISTANCES_KIND_MEANS_ALL)
#define HWLOC_DISTANCES_ADD_FLAG_ALL (HWLOC_DISTANCES_ADD_FLAG_GROUP|HWLOC_DISTANCES_ADD_FLAG_GROUP_INACCURATE)
/* The actual function exported to the user
*/
int hwloc_distances_add(hwloc_topology_t topology,
unsigned nbobjs, hwloc_obj_t *objs, hwloc_uint64_t *values,
unsigned long kind, unsigned long flags)
{
unsigned i;
uint64_t *_values;
hwloc_obj_t *_objs;
int err;
if (nbobjs < 2 || !objs || !values || !topology->is_loaded) {
errno = EINVAL;
return -1;
}
if (topology->adopted_shmem_addr) {
errno = EPERM;
return -1;
}
if ((kind & ~HWLOC_DISTANCES_KIND_ALL)
|| hwloc_weight_long(kind & HWLOC_DISTANCES_KIND_FROM_ALL) != 1
|| hwloc_weight_long(kind & HWLOC_DISTANCES_KIND_MEANS_ALL) != 1
|| (flags & ~HWLOC_DISTANCES_ADD_FLAG_ALL)) {
errno = EINVAL;
return -1;
}
/* no strict need to check for duplicates, things shouldn't break */
for(i=1; i<nbobjs; i++)
if (!objs[i]) {
errno = EINVAL;
return -1;
}
/* copy the input arrays and give them to the topology */
_objs = malloc(nbobjs*sizeof(hwloc_obj_t));
_values = malloc(nbobjs*nbobjs*sizeof(*_values));
if (!_objs || !_values)
goto out_with_arrays;
memcpy(_objs, objs, nbobjs*sizeof(hwloc_obj_t));
memcpy(_values, values, nbobjs*nbobjs*sizeof(*_values));
err = hwloc_internal_distances_add(topology, NULL, nbobjs, _objs, _values, kind, flags);
if (err < 0)
goto out; /* _objs and _values freed in hwloc_internal_distances_add() */
/* in case we added some groups, see if we need to reconnect */
hwloc_topology_reconnect(topology, 0);
return 0;
out_with_arrays:
free(_values);
free(_objs);
out:
return -1;
}
/******************************************************
* Refresh objects in distances
*/
static void
hwloc_internal_distances_restrict(hwloc_obj_t *objs,
uint64_t *indexes,
hwloc_obj_type_t *different_types,
uint64_t *values,
unsigned nbobjs, unsigned disappeared)
{
unsigned i, newi;
unsigned j, newj;
for(i=0, newi=0; i<nbobjs; i++)
if (objs[i]) {
for(j=0, newj=0; j<nbobjs; j++)
if (objs[j]) {
values[newi*(nbobjs-disappeared)+newj] = values[i*nbobjs+j];
newj++;
}
newi++;
}
for(i=0, newi=0; i<nbobjs; i++)
if (objs[i]) {
objs[newi] = objs[i];
if (indexes)
indexes[newi] = indexes[i];
if (different_types)
different_types[newi] = different_types[i];
newi++;
}
}
static int
hwloc_internal_distances_refresh_one(hwloc_topology_t topology,
struct hwloc_internal_distances_s *dist)
{
hwloc_obj_type_t unique_type = dist->unique_type;
hwloc_obj_type_t *different_types = dist->different_types;
unsigned nbobjs = dist->nbobjs;
hwloc_obj_t *objs = dist->objs;
uint64_t *indexes = dist->indexes;
unsigned disappeared = 0;
unsigned i;
if (dist->iflags & HWLOC_INTERNAL_DIST_FLAG_OBJS_VALID)
return 0;
for(i=0; i<nbobjs; i++) {
hwloc_obj_t obj;
/* TODO use cpuset/nodeset to find pus/numas from the root?
* faster than traversing the entire level?
*/
if (HWLOC_DIST_TYPE_USE_OS_INDEX(unique_type)) {
if (unique_type == HWLOC_OBJ_PU)
obj = hwloc_get_pu_obj_by_os_index(topology, (unsigned) indexes[i]);
else if (unique_type == HWLOC_OBJ_NUMANODE)
obj = hwloc_get_numanode_obj_by_os_index(topology, (unsigned) indexes[i]);
else
abort();
} else {
obj = hwloc_get_obj_by_type_and_gp_index(topology, different_types ? different_types[i] : unique_type, indexes[i]);
}
objs[i] = obj;
if (!obj)
disappeared++;
}
if (nbobjs-disappeared < 2)
/* became useless, drop */
return -1;
if (disappeared) {
hwloc_internal_distances_restrict(objs, dist->indexes, dist->different_types, dist->values, nbobjs, disappeared);
dist->nbobjs -= disappeared;
}
dist->iflags |= HWLOC_INTERNAL_DIST_FLAG_OBJS_VALID;
return 0;
}
/* This function may be called with topology->tma set, it cannot free() or realloc() */
void
hwloc_internal_distances_refresh(hwloc_topology_t topology)
{
struct hwloc_internal_distances_s *dist, *next;
for(dist = topology->first_dist; dist; dist = next) {
next = dist->next;
if (hwloc_internal_distances_refresh_one(topology, dist) < 0) {
assert(!topology->tma || !topology->tma->dontfree); /* this tma cannot fail to allocate */
if (dist->prev)
dist->prev->next = next;
else
topology->first_dist = next;
if (next)
next->prev = dist->prev;
else
topology->last_dist = dist->prev;
hwloc_internal_distances_free(dist);
continue;
}
}
}
void
hwloc_internal_distances_invalidate_cached_objs(hwloc_topology_t topology)
{
struct hwloc_internal_distances_s *dist;
for(dist = topology->first_dist; dist; dist = dist->next)
dist->iflags &= ~HWLOC_INTERNAL_DIST_FLAG_OBJS_VALID;
}
/******************************************************
* User API for getting distances
*/
/* what we actually allocate for user queries, even if we only
* return the distances part of it.
*/
struct hwloc_distances_container_s {
unsigned id;
struct hwloc_distances_s distances;
};
#define HWLOC_DISTANCES_CONTAINER_OFFSET ((char*)&((struct hwloc_distances_container_s*)NULL)->distances - (char*)NULL)
#define HWLOC_DISTANCES_CONTAINER(_d) (struct hwloc_distances_container_s *) ( ((char*)_d) - HWLOC_DISTANCES_CONTAINER_OFFSET )
static struct hwloc_internal_distances_s *
hwloc__internal_distances_from_public(hwloc_topology_t topology, struct hwloc_distances_s *distances)
{
struct hwloc_distances_container_s *cont = HWLOC_DISTANCES_CONTAINER(distances);
struct hwloc_internal_distances_s *dist;
for(dist = topology->first_dist; dist; dist = dist->next)
if (dist->id == cont->id)
return dist;
return NULL;
}
void
hwloc_distances_release(hwloc_topology_t topology __hwloc_attribute_unused,
struct hwloc_distances_s *distances)
{
struct hwloc_distances_container_s *cont = HWLOC_DISTANCES_CONTAINER(distances);
free(distances->values);
free(distances->objs);
free(cont);
}
const char *
hwloc_distances_get_name(hwloc_topology_t topology, struct hwloc_distances_s *distances)
{
struct hwloc_internal_distances_s *dist = hwloc__internal_distances_from_public(topology, distances);
return dist ? dist->name : NULL;
}
static struct hwloc_distances_s *
hwloc_distances_get_one(hwloc_topology_t topology __hwloc_attribute_unused,
struct hwloc_internal_distances_s *dist)
{
struct hwloc_distances_container_s *cont;
struct hwloc_distances_s *distances;
unsigned nbobjs;
cont = malloc(sizeof(*cont));
if (!cont)
return NULL;
distances = &cont->distances;
nbobjs = distances->nbobjs = dist->nbobjs;
distances->objs = malloc(nbobjs * sizeof(hwloc_obj_t));
if (!distances->objs)
goto out;
memcpy(distances->objs, dist->objs, nbobjs * sizeof(hwloc_obj_t));
distances->values = malloc(nbobjs * nbobjs * sizeof(*distances->values));
if (!distances->values)
goto out_with_objs;
memcpy(distances->values, dist->values, nbobjs*nbobjs*sizeof(*distances->values));
distances->kind = dist->kind;
cont->id = dist->id;
return distances;
out_with_objs:
free(distances->objs);
out:
free(cont);
return NULL;
}
static int
hwloc__distances_get(hwloc_topology_t topology,
const char *name, hwloc_obj_type_t type,
unsigned *nrp, struct hwloc_distances_s **distancesp,
unsigned long kind, unsigned long flags __hwloc_attribute_unused)
{
struct hwloc_internal_distances_s *dist;
unsigned nr = 0, i;
/* We could return the internal arrays (as const),
* but it would require to prevent removing distances between get() and free().
* Not performance critical anyway.
*/
if (flags) {
errno = EINVAL;
return -1;
}
/* we could refresh only the distances that match, but we won't have many distances anyway,
* so performance is totally negligible.
*
* This is also useful in multithreaded apps that modify the topology.
* They can call any valid hwloc_distances_get() to force a refresh after
* changing the topology, so that future concurrent get() won't cause
* concurrent refresh().
*/
hwloc_internal_distances_refresh(topology);
for(dist = topology->first_dist; dist; dist = dist->next) {
unsigned long kind_from = kind & HWLOC_DISTANCES_KIND_FROM_ALL;
unsigned long kind_means = kind & HWLOC_DISTANCES_KIND_MEANS_ALL;
if (name && (!dist->name || strcmp(name, dist->name)))
continue;
if (type != HWLOC_OBJ_TYPE_NONE && type != dist->unique_type)
continue;
if (kind_from && !(kind_from & dist->kind))
continue;
if (kind_means && !(kind_means & dist->kind))
continue;
if (nr < *nrp) {
struct hwloc_distances_s *distances = hwloc_distances_get_one(topology, dist);
if (!distances)
goto error;
distancesp[nr] = distances;
}
nr++;
}
for(i=nr; i<*nrp; i++)
distancesp[i] = NULL;
*nrp = nr;
return 0;
error:
for(i=0; i<nr; i++)
hwloc_distances_release(topology, distancesp[i]);
return -1;
}
int
hwloc_distances_get(hwloc_topology_t topology,
unsigned *nrp, struct hwloc_distances_s **distancesp,
unsigned long kind, unsigned long flags)
{
if (flags || !topology->is_loaded) {
errno = EINVAL;
return -1;
}
return hwloc__distances_get(topology, NULL, HWLOC_OBJ_TYPE_NONE, nrp, distancesp, kind, flags);
}
int
hwloc_distances_get_by_depth(hwloc_topology_t topology, int depth,
unsigned *nrp, struct hwloc_distances_s **distancesp,
unsigned long kind, unsigned long flags)
{
hwloc_obj_type_t type;
if (flags || !topology->is_loaded) {
errno = EINVAL;
return -1;
}
/* FIXME: passing the depth of a group level may return group distances at a different depth */
type = hwloc_get_depth_type(topology, depth);
if (type == (hwloc_obj_type_t)-1) {
errno = EINVAL;
return -1;
}
return hwloc__distances_get(topology, NULL, type, nrp, distancesp, kind, flags);
}
int
hwloc_distances_get_by_name(hwloc_topology_t topology, const char *name,
unsigned *nrp, struct hwloc_distances_s **distancesp,
unsigned long flags)
{
if (flags || !topology->is_loaded) {
errno = EINVAL;
return -1;
}
return hwloc__distances_get(topology, name, HWLOC_OBJ_TYPE_NONE, nrp, distancesp, HWLOC_DISTANCES_KIND_ALL, flags);
}
int
hwloc_distances_get_by_type(hwloc_topology_t topology, hwloc_obj_type_t type,
unsigned *nrp, struct hwloc_distances_s **distancesp,
unsigned long kind, unsigned long flags)
{
if (flags || !topology->is_loaded) {
errno = EINVAL;
return -1;
}
return hwloc__distances_get(topology, NULL, type, nrp, distancesp, kind, flags);
}
/******************************************************
* Grouping objects according to distances
*/
static int hwloc_compare_values(uint64_t a, uint64_t b, float accuracy)
{
if (accuracy != 0.0f && fabsf((float)a-(float)b) < (float)a * accuracy)
return 0;
return a < b ? -1 : a == b ? 0 : 1;
}
/*
* Place objects in groups if they are in a transitive graph of minimal values.
* Return how many groups were created, or 0 if some incomplete distance graphs were found.
*/
static unsigned
hwloc__find_groups_by_min_distance(unsigned nbobjs,
uint64_t *_values,
float accuracy,
unsigned *groupids,
int verbose)
{
uint64_t min_distance = UINT64_MAX;
unsigned groupid = 1;
unsigned i,j,k;
unsigned skipped = 0;
#define VALUE(i, j) _values[(i) * nbobjs + (j)]
memset(groupids, 0, nbobjs*sizeof(*groupids));
/* find the minimal distance */
for(i=0; i<nbobjs; i++)
for(j=0; j<nbobjs; j++) /* check the entire matrix, it may not be perfectly symmetric depending on the accuracy */
if (i != j && VALUE(i, j) < min_distance) /* no accuracy here, we want the real minimal */
min_distance = VALUE(i, j);
hwloc_debug(" found minimal distance %llu between objects\n", (unsigned long long) min_distance);
if (min_distance == UINT64_MAX)
return 0;
/* build groups of objects connected with this distance */
for(i=0; i<nbobjs; i++) {
unsigned size;
unsigned firstfound;
/* if already grouped, skip */
if (groupids[i])
continue;
/* start a new group */
groupids[i] = groupid;
size = 1;
firstfound = i;
while (firstfound != (unsigned)-1) {
/* we added new objects to the group, the first one was firstfound.
* rescan all connections from these new objects (starting at first found) to any other objects,
* so as to find new objects minimally-connected by transivity.
*/
unsigned newfirstfound = (unsigned)-1;
for(j=firstfound; j<nbobjs; j++)
if (groupids[j] == groupid)
for(k=0; k<nbobjs; k++)
if (!groupids[k] && !hwloc_compare_values(VALUE(j, k), min_distance, accuracy)) {
groupids[k] = groupid;
size++;
if (newfirstfound == (unsigned)-1)
newfirstfound = k;
if (i == j)
hwloc_debug(" object %u is minimally connected to %u\n", k, i);
else
hwloc_debug(" object %u is minimally connected to %u through %u\n", k, i, j);
}
firstfound = newfirstfound;
}
if (size == 1) {
/* cancel this useless group, ignore this object and try from the next one */
groupids[i] = 0;
skipped++;
continue;
}
/* valid this group */
groupid++;
if (verbose)
fprintf(stderr, " Found transitive graph with %u objects with minimal distance %llu accuracy %f\n",
size, (unsigned long long) min_distance, accuracy);
}
if (groupid == 2 && !skipped)
/* we created a single group containing all objects, ignore it */
return 0;
/* return the last id, since it's also the number of used group ids */
return groupid-1;
}
/* check that the matrix is ok */
static int
hwloc__check_grouping_matrix(unsigned nbobjs, uint64_t *_values, float accuracy, int verbose)
{
unsigned i,j;
for(i=0; i<nbobjs; i++) {
for(j=i+1; j<nbobjs; j++) {
/* should be symmetric */
if (hwloc_compare_values(VALUE(i, j), VALUE(j, i), accuracy)) {
if (verbose)
fprintf(stderr, " Distance matrix asymmetric ([%u,%u]=%llu != [%u,%u]=%llu), aborting\n",
i, j, (unsigned long long) VALUE(i, j), j, i, (unsigned long long) VALUE(j, i));
return -1;
}
/* diagonal is smaller than everything else */
if (hwloc_compare_values(VALUE(i, j), VALUE(i, i), accuracy) <= 0) {
if (verbose)
fprintf(stderr, " Distance to self not strictly minimal ([%u,%u]=%llu <= [%u,%u]=%llu), aborting\n",
i, j, (unsigned long long) VALUE(i, j), i, i, (unsigned long long) VALUE(i, i));
return -1;
}
}
}
return 0;
}
/*
* Look at object physical distances to group them.
*/
static void
hwloc__groups_by_distances(struct hwloc_topology *topology,
unsigned nbobjs,
struct hwloc_obj **objs,
uint64_t *_values,
unsigned long kind,
unsigned nbaccuracies,
float *accuracies,
int needcheck)
{
unsigned *groupids;
unsigned nbgroups = 0;
unsigned i,j;
int verbose = topology->grouping_verbose;
hwloc_obj_t *groupobjs;
unsigned * groupsizes;
uint64_t *groupvalues;
unsigned failed = 0;
if (nbobjs <= 2)
return;
if (!(kind & HWLOC_DISTANCES_KIND_MEANS_LATENCY))
/* don't know use to use those for grouping */
/* TODO hwloc__find_groups_by_max_distance() for bandwidth */
return;