-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathnccl_ofi_topo.c
More file actions
1662 lines (1462 loc) · 43.6 KB
/
Copy pathnccl_ofi_topo.c
File metadata and controls
1662 lines (1462 loc) · 43.6 KB
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 (c) 2023-2024 Amazon.com, Inc. or its affiliates. All rights reserved.
*/
#include "config.h"
#include <stdbool.h>
#include <string.h>
#include <hwloc.h>
#include <rdma/fabric.h>
#include <errno.h>
#include <stdlib.h>
#include <assert.h>
#include "nccl_ofi_log.h"
#include "nccl_ofi_topo.h"
#include "nccl_ofi_math.h"
#include "nccl_ofi_ofiutils.h"
#include "nccl_ofi_platform.h"
static const uint8_t target_class_id = 0x03; /* Display controller class */
static const unsigned short target_vendor_id = 0x10de; /* NVIDIA */
/* Maximum length of the device property read from file by function
* get_device_property() */
#define MAX_DEV_PROPERTY_LENGTH 16
const char *speed_name = "max_link_speed";
const char *width_name = "max_link_width";
/* `pcie_gen[i]` defines the speed of a PCIe lane of PCIe generation `i+1` */
const char *pcie_gen[] = {"2.5", "5", "8", "16", "32", "64"};
/*
* @brief Create vector of nccl_ofi_topo_data_t structs
*
* Allocate vector and initalize elements.
*
* @param size
* Size of the vector
* @return allocated vector, if allocation succeeds
* NULL, on others
*/
static nccl_ofi_topo_data_vec_t *nccl_ofi_topo_data_vec_create(size_t size)
{
nccl_ofi_topo_data_vec_t *vec = NULL;
vec = (nccl_ofi_topo_data_vec_t*)calloc(1, sizeof(nccl_ofi_topo_data_vec_t));
if (!vec) {
NCCL_OFI_WARN("Failed to allocate struct nccl_ofi_topo_data_vec_t");
return NULL;
}
if (size > 0) {
vec->size = size;
vec->data = (nccl_ofi_topo_data_t*)calloc(size, sizeof(nccl_ofi_topo_data_t));
if (!vec->data) {
free(vec);
vec = NULL;
NCCL_OFI_WARN("Failed to allocate array of struct nccl_ofi_topo_data_vec_t");
}
}
return vec;
}
int nccl_ofi_topo_set_to_begin(nccl_ofi_topo_t *topo, nccl_ofi_topo_data_iterator_t *iter)
{
if (!topo) {
NCCL_OFI_WARN("Invalid NCCL OFI topology");
return -EINVAL;
}
if (!topo->data_vec) {
NCCL_OFI_WARN("Invalid NCCL OFI topology user data");
return -EINVAL;
}
iter->begin = topo->data_vec->data;
iter->end = topo->data_vec->data + topo->data_vec->size;
return 0;
}
/*
* @brief Return user data struct
*
* @return user data, if available
* NULL, if iterator has reached end of vector
*/
static nccl_ofi_topo_data_t *nccl_ofi_get_user_data(nccl_ofi_topo_data_iterator_t *iter)
{
if (!iter || iter->begin >= iter->end) return NULL;
return iter->begin;
}
/*
* @brief Increment user data iterator
*/
static void nccl_ofi_inc_user_data_iter(nccl_ofi_topo_data_iterator_t *iter)
{
if (!iter) return;
++(iter->begin);
}
/*
* @brief Return pointer to fi_pci_attr struct of libfabric NIC info
*
* @return Pointer to fi_pci_attr struct, if available
* NULL, on others
*/
static struct fi_pci_attr *ofi_info_get_pci_attr(struct fi_info *info) {
struct fi_pci_attr *pci_addr = NULL;
struct fi_bus_attr *bus = NULL;
struct fid_nic *nic = info->nic;
if (!nic) {
return NULL;
}
bus = nic->bus_attr;
if (!bus || bus->bus_type != FI_BUS_PCI) {
return NULL;
}
pci_addr = &(bus->attr.pci);
if (!pci_addr) {
return NULL;
}
return pci_addr;
}
/*
* @brief Test whether topology node represents an NVIDIA GPU PCI device
*/
static int is_accelerator_dev(hwloc_obj_t obj, bool *res)
{
uint8_t class_code;
bool class_match;
bool vendor_match;
if (obj->type != HWLOC_OBJ_PCI_DEVICE) {
*res = false;
return 0;
}
if (!obj->attr) {
NCCL_OFI_WARN("Invalid hwloc object attribute pointer. Expected pointer to pcidev, but got NULL");
return -EINVAL;
}
/* The HWLOC class id is a 16 bit integer of format class
code:subclass, where each field is 8 bits. We only want
the class code. */
class_code = obj->attr->pcidev.class_id >> 8;
class_match = target_class_id == class_code;
vendor_match = obj->attr->pcidev.vendor_id == target_vendor_id;
*res = class_match && vendor_match;
return 0;
}
void nccl_ofi_topo_free(nccl_ofi_topo_t *topo)
{
if (!topo) return;
if (topo->topo) hwloc_topology_destroy(topo->topo);
if (topo->data_vec) {
nccl_ofi_topo_data_iterator_t data_iter;
nccl_ofi_topo_set_to_begin(topo, &data_iter);
/* Free libfabric NIC info lists */
nccl_ofi_topo_data_t *data = nccl_ofi_get_user_data(&data_iter);
nccl_ofi_inc_user_data_iter(&data_iter);
while (data) {
nccl_ofi_ofiutils_free_info_list(data->info_list);
data = nccl_ofi_get_user_data(&data_iter);
nccl_ofi_inc_user_data_iter(&data_iter);
}
/* Free data array and vector */
free(topo->data_vec->data);
free(topo->data_vec);
}
free(topo);
}
/*
* brief Enable I/O discovery for hardware topology
*
* Hwloc I/O discovery is disabled by default. This function enables
* topology discovery of I/O devices.
*
* If I/O discovery is disabled, the hwloc creates a hardware topology
* tree that does not include I/O devices as nodes. I/O devices are
* hwloc OS devices, hwloc PCI devices, and hwloc bridges.
*
* Hwloc hostbridges are below normal hwloc objects (e.g., machine or
* NUMA node). Children of hostbridges may be other hwloc bridges and
* hwloc PCI devices. Hwloc OS devices may be children of PCI devices,
* representing software handles such as 'eth0', 'sda', or 'cuda0'.
*
* If I/O discovery is enabled, Libfabric NICs are represented by
* corresponding hwloc PCI devices. Hwloc may also add PCI devices to
* represent GPUs.
*
* The libfabric NIC grouping algorithm uses the locality of NICs and
* GPUs in the hardware topology to group NICs.
*
* This function is to be called after hardware topology is
* initialized but before hardware topology is loaded.
*/
static void enable_hwloc_io_types(hwloc_topology_t topo)
{
/* HWLOC API changes introduced in version 2.0.0 */
#if (HWLOC_API_VERSION >= 0x00020000)
/*
* HWLOC_TOPOLOGY_FLAG_WHOLE_IO has been removed in favor of
* hwloc_topology_set_io_types_filter() with HWLOC_TYPE_FILTER_KEEP_ALL
* or HWLOC_TYPE_FILTER_KEEP_IMPORTANT
*/
enum hwloc_type_filter_e filter = HWLOC_TYPE_FILTER_KEEP_ALL;
hwloc_topology_set_io_types_filter(topo, filter);
#else
unsigned long flags = hwloc_topology_get_flags(topo);
/*
* We want to detect I/O devices, DMA buses, and bridges in the PCIe
* topology in addition to the default system components such as CPU,
* memory, etc.
*/
flags |= HWLOC_TOPOLOGY_FLAG_WHOLE_IO;
hwloc_topology_set_flags(topo, flags);
#endif
}
/*
* brief Return PCI device topology node corresponding to the
* libfabric NIC info struct
*
* A PCI topology node corresponds to the libfabric NIC info struct if
* the topology node matches the bus ID reported by the libfabric NIC
* info struct and if the topology node is of PCI class 02 (display
* controller).
*
* @param topo
* The topology
* @param info
* Libfabric NIC info struct
* @return topology node, if a corresponding topology node is found.
* NULL, on others
* @return 0, if a corresponding topology node is found
* or if no topology node is found for the bus ID reported by `info`
* -EINVAL, on others
*/
static int get_hwloc_pcidev_by_fi_info(hwloc_topology_t topo,
struct fi_info *info,
hwloc_obj_t *obj) {
*obj = NULL;
hwloc_obj_t ret_obj = NULL;
struct fi_pci_attr *attr = ofi_info_get_pci_attr(info);
if (!attr) {
NCCL_OFI_WARN("Failed to retrieve PCI attributes from NIC");
return -EINVAL;;
}
ret_obj = hwloc_get_pcidev_by_busid(topo,
attr->domain_id,
attr->bus_id,
attr->device_id,
attr->function_id);
if (!ret_obj) {
/* Not finding a topology node corresponding to the
* info struct does not mean that something is going
* wrong. E.g., the NIC might just be removed manually from
* hwloc topology file. */
return 0;
}
if (!ret_obj->attr) {
NCCL_OFI_WARN("Invalid hwloc object attribute pointer. Expected pointer to pcidev, but got NULL");
return -EINVAL;
}
*obj = ret_obj;
return 0;
}
/*
* @brief Return libfabric NIC info struct from info list that corresponds to input topology node
*
* @param node
* The topology node
* @param info_list
* List of Libfabric NIC info structs
* @return Info struct that corresponds to topology node if found in info list
* NULL, otherwise
* @return 0, on success
* non-zero, on error
*/
static int get_info_for_node(hwloc_obj_t node, struct fi_info *info_list, struct fi_info **ret_info)
{
*ret_info = NULL;
union hwloc_obj_attr_u *node_attr;
struct fi_info *next;
if (!info_list) {
NCCL_OFI_WARN("No info list provided");
return -EINVAL;
}
node_attr = node->attr;
if (!node_attr) {
NCCL_OFI_WARN("Failed to retrieve attributes from hwloc topology node");
return -EINVAL;
}
if (node->type != HWLOC_OBJ_PCI_DEVICE) {
return 0;
}
/* Iterate through list, return info struct if found */
next = info_list;
do {
struct fi_info *info = next;
next = info->next;
struct fi_pci_attr *attr = ofi_info_get_pci_attr(info);
if (!attr) {
NCCL_OFI_WARN("Failed to retrieve PCI attributes from NIC");
return -EINVAL;
}
if (node_attr->pcidev.domain == attr->domain_id &&
node_attr->pcidev.bus == attr->bus_id &&
node_attr->pcidev.dev == attr->device_id &&
node_attr->pcidev.func == attr->function_id) {
*ret_info = info;
return 0;
}
/* Stop loop if end of list is reached or if info
* struct closes loop to list head */
} while (next && next != info_list);
return 0;
}
/*
* @brief Count number of topology nodes that have a NIC or Nvidia GPU in its subtree
*
*
* @param topo
* Hwloc topology. Userdata pointers of topology nodes are expected to be set to NULL
* @param info_list
* Libfabric NIC info list to identify the NICs
* @return Number of nodes
* @return 0, on success
* non-zero, on error
*/
static int count_nodes_with_accel_or_nic_in_subtree(hwloc_topology_t topo,
struct fi_info *info_list,
int *count)
{
int ret = 0;
hwloc_obj_t obj = NULL;
while ((obj = hwloc_get_next_pcidev(topo, obj))) {
bool is_accel = false;
struct fi_info *info;
ret = is_accelerator_dev(obj, &is_accel);
if (ret != 0) {
NCCL_OFI_WARN("Error while checking whether hwloc topology node is nvidia GPU");
return ret;
}
ret = get_info_for_node(obj, info_list, &info);
if (ret != 0) {
NCCL_OFI_WARN("Error while retrieving libfabric NIC info struct corresponding to hwloc topology node");
return ret;
}
if (is_accel || info) {
/* Walk towards root, set counter and increment counter each time counter is set */
hwloc_obj_t node = obj;
while (node) {
/* Skip node if this function is counting and
* if node it has already contributed to the
* counter (indicated by set user data
* pointer) */
if (count && node->userdata) break;
node->userdata = count;
if (count) ++(*count);
node = node->parent;
}
}
}
/* While counting, the function sets the user data pointer of
* the topology nodes to avoid counting nodes
* twice. Afterwards, invoke this function another time to
* clear the user data pointers. */
if (count != NULL) return count_nodes_with_accel_or_nic_in_subtree(topo, info_list, NULL);
else return 0;
}
/*
* @brief Walk to root from `node` and set user data
*
* Starting from input node `node`, walk upwards to the root and set
* userdata. End ascend if a node is reached that already stores user
* data. This likely means that another traversal from another node to
* the root has already set the user data of the remaining nodes
* towards the root. The user data is provided by `data_iter`.
*
* @param node
* Input topology node
* @param data_iter
* Data iterator from which the user data objects are extracted.
* @return 0, on error
* -EINVAL, if data iterator does not provide enough user data objects
*/
static int set_userdata_to_root(hwloc_obj_t node,
nccl_ofi_topo_data_iterator_t *data_iter)
{
nccl_ofi_topo_data_t * user_data;
/* Walk upwards to the root */
while (node) {
if (!node->userdata) {
/* Abort when a node is reached that already stores user data */
if (node->userdata) break;
user_data = nccl_ofi_get_user_data(data_iter);
if (!user_data) {
NCCL_OFI_WARN("Failed to access user data of data_iter");
return -EINVAL;
}
node->userdata = user_data;
user_data->node = node;
nccl_ofi_inc_user_data_iter(data_iter);
}
node = node->parent;
}
return 0;
}
/*
* @brief Add user data to topology nodes with a NIC or Nvidia GPU in its subtree.
* Also, add libfabric NIC info struct to NIC topology nodes.
*
* @param ofi_topo
* NCCL OFI topology
* @param info_list
* List of libfabric NIC info structs used to identify topology nodes corresponding to NICs
* @return
*/
static int set_user_data(nccl_ofi_topo_t *ofi_topo,
struct fi_info *info_list)
{
int ret = 0;
hwloc_obj_t obj = NULL;
nccl_ofi_topo_data_iterator_t data_iter;
/* Retrieve number of topology nodes that have a Nvidia GPU or a NIC in their subtree */
int num_nodes = 0;
ret = count_nodes_with_accel_or_nic_in_subtree(ofi_topo->topo, info_list, &num_nodes);
if (ret != 0) {
NCCL_OFI_WARN("Failed counting number of nodes that have a Nvidia GPU or NIC in their subtree.");
return ret;
}
/* Create vector that provides one user data struct for each
* topology node that has a Nvidia GPU or a NIC in its subtree */
ofi_topo->data_vec = nccl_ofi_topo_data_vec_create(num_nodes);
if (!ofi_topo->data_vec) {
NCCL_OFI_WARN("Could not create user data vector.");
return -ENOMEM;
}
nccl_ofi_topo_set_to_begin(ofi_topo, &data_iter);
/* Iterate over all PCI topology nodes and find nodes
* corresponding to NICs and Nvidia GPUs. From those nodes,
* walk up towards the root and set user data. */
while ((obj = hwloc_get_next_pcidev(ofi_topo->topo, obj))) {
bool is_accel = false;
struct fi_info *info;
ret = is_accelerator_dev(obj, &is_accel);
if (ret != 0) {
NCCL_OFI_WARN("Error while checking whether hwloc topology node is nvidia GPU");
return ret;
}
ret = get_info_for_node(obj, info_list, &info);
if (ret != 0) {
NCCL_OFI_WARN("Error while retrieving libfabric NIC info struct corresponding to hwloc topology node");
return ret;
}
if (is_accel || info) {
ret = set_userdata_to_root(obj, &data_iter);
if (ret != 0) {
NCCL_OFI_WARN("Error while setting user data on path to root");
return ret;
}
}
if (info) {
/* Copy libfabric NIC info struct and store info struct in
* user data of topology node */
nccl_ofi_topo_data_t *user_data = (nccl_ofi_topo_data_t *)obj->userdata;
user_data->info_list = fi_dupinfo(info);
user_data->info_list_len = 1;
if (!user_data->info_list) {
NCCL_OFI_WARN("Unable to duplicate libfabric NIC info");
return -EINVAL;
}
ofi_topo->max_group_size = 1;
}
}
return 0;
}
nccl_ofi_topo_t *nccl_ofi_topo_create(struct fi_info *info_list)
{
int ret = 0;
/* Allocate NCCL OFI topology */
nccl_ofi_topo_t *ofi_topo = (nccl_ofi_topo_t *)calloc(1, sizeof(nccl_ofi_topo_t));
if (!ofi_topo) {
NCCL_OFI_TRACE(NCCL_INIT | NCCL_NET,
"Unable to allocate nccl_ofi_topo");
goto error;
}
/*
* Load hardware topology
*/
if (hwloc_topology_init(&ofi_topo->topo) != 0) {
NCCL_OFI_WARN("Unable to initialize hardware topology.");
goto error;
}
/* Prepare hardware topology ready to load IO nodes as well */
enable_hwloc_io_types(ofi_topo->topo);
if (hwloc_topology_load(ofi_topo->topo) != 0) {
NCCL_OFI_WARN("Unable to load hardware topology.");
goto error;
}
/* Add user data to topology nodes that have a nic or NVIDIA
* GPU in their subtree. Also, add libfabric NIC info structs
* to user data to topology nodes corresponding to the NICs. */
ret = set_user_data(ofi_topo, info_list);
if (ret != 0) {
NCCL_OFI_WARN("Data decoration failed.");
goto error;
}
return ofi_topo;
error:
nccl_ofi_topo_free(ofi_topo);
return NULL;
}
/*
* @brief Mark all topology nodes that store a libfabric NIC info
* struct in their subtrees
*/
static int mark_topo_nodes_with_ofi_info_subtree(nccl_ofi_topo_t *topo)
{
int status;
nccl_ofi_topo_data_t *data = NULL;
/* Iterate over user data that stores libfabric NIC info structs */
nccl_ofi_topo_data_iterator_t data_iter;
if ((status = nccl_ofi_topo_set_to_begin(topo, &data_iter)) < 0) {
return status;
}
while ((data = nccl_ofi_get_user_data(&data_iter))) {
nccl_ofi_inc_user_data_iter(&data_iter);
if (!data->info_list) {
continue;
}
hwloc_obj_t obj = data->node;
if (!obj) {
NCCL_OFI_WARN("Expected initialized topology");
return -EINVAL;
}
/* Walk up topology tree up to root and mark topology nodes */
while (obj) {
nccl_ofi_topo_data_t *obj_data = (nccl_ofi_topo_data_t *)obj->userdata;
if (!obj_data) {
NCCL_OFI_WARN("Invalid user data pointer");
return -EINVAL;
}
obj_data->is_nic_subtree = true;
obj = obj->parent;
}
}
return 0;
}
/*
* @brief Walk up the tree until we find a subtree that has a
* libfabric NIC info struct (marked topology node) in its subtree
* and increase its group count by one
*
* @param gpu_node
* Topology node on which this operation is started
*/
static int propoagate_accel_count(hwloc_obj_t gpu_node)
{
hwloc_obj_t node = gpu_node;
nccl_ofi_topo_data_t *userdata = (nccl_ofi_topo_data_t *)node->userdata;
if (!userdata) {
NCCL_OFI_WARN("Invalid user data pointer");
return -EINVAL;
}
if (userdata->contributed_gpu) return 0;
userdata->contributed_gpu = true;
/* Walk towards root */
while (node) {
userdata = (nccl_ofi_topo_data_t *)node->userdata;
if (!userdata) {
NCCL_OFI_WARN("Invalid user data pointer");
return -EINVAL;
}
/* Node found. Increase group count. */
if (userdata->is_nic_subtree) {
userdata->num_groups++;
userdata->gpu_group_node = gpu_node;
break;
}
node = node->parent;
}
return 0;
}
/*
* @brief Propagate counts from accelerator topology nodes to marked topology nodes.
*/
static int propoagate_accel_group_counts(hwloc_topology_t topo)
{
int ret = 0;
hwloc_obj_t obj = NULL;
/* Iterate over all PCI topology nodes and find nodes
* corresponding to Nvidia GPUs. From those nodes, walk up
* towards the root and increase group count on closest
* ancestor that has NICs attached. */
while ((obj = hwloc_get_next_pcidev(topo, obj))) {
bool is_accel = false;
ret = is_accelerator_dev(obj, &is_accel);
if (ret != 0) {
NCCL_OFI_WARN("Error while checking whether hwloc topology node is an accelerator");
return ret;
}
if (is_accel) {
propoagate_accel_count(obj);
}
}
return 0;
}
/*
* @brief Lift libfabric NIC info objects, stored in the user data of
* topology nodes, up to nodes with group count of one or more
*/
static int lift_up_ofi_infos(nccl_ofi_topo_t *topo)
{
nccl_ofi_topo_data_t *source_data = NULL;
nccl_ofi_topo_data_t *target_data = NULL;
/* Iterate over user data. Since user data is added to all
* topology nodes that have a "NIC topology nodes" or a
* "accelerator topology nodes" it their subtree, all info
* structs are found. */
nccl_ofi_topo_data_iterator_t data_iter = {};
nccl_ofi_topo_set_to_begin(topo, &data_iter);
while ((source_data = nccl_ofi_get_user_data(&data_iter))) {
nccl_ofi_inc_user_data_iter(&data_iter);
if (!source_data->info_list) {
continue;
}
hwloc_obj_t target_obj = source_data->node;
if (!target_obj) {
NCCL_OFI_WARN("Expected initialized topology");
return -EINVAL;
}
target_data = (nccl_ofi_topo_data_t *)target_obj->userdata;
/* Info object is already stored at appropriate node */
if (target_data->num_groups > 0) continue;
/* Search for topology nodes towards the root with a
* group count of one or more and add libfabric NIC
* info list to that node. */
while (target_obj) {
target_data = (nccl_ofi_topo_data_t *)target_obj->userdata;
if (!target_data) {
NCCL_OFI_WARN("Invalid user data pointer");
return -EINVAL;
}
if (target_data->num_groups > 0) {
/* Find end of list */
struct fi_info *list_end = source_data->info_list;
while(list_end->next) {
list_end = list_end->next;
}
/* Concatenate lists */
list_end->next = target_data->info_list;
target_data->info_list = source_data->info_list;
target_data->info_list_len += source_data->info_list_len;
source_data->info_list = NULL;
source_data->info_list_len = 0;
break;
}
target_obj = target_obj->parent;
if (!target_obj) {
/* No accelerator found to which the
* info list can be assigned to, i.e.,
* neither the source node, not any
* ancestor has a group count larger
* than `0`. This can have two
* reasons; either the topology does
* not contain a known accelerator at
* all, or each accelerator has a NIC
* that is closer to the accelerator
* than NICs of the source node. We
* still want to expose those NICs,
* and thus, expose each NIC as one
* group. */
source_data->num_groups = source_data->info_list_len;
break;
}
}
}
return 0;
}
/*
* @brief Split NIC info list into 'num_groups' and add each group to the
* topology node corresponding to its leader (first NIC of the list).
*
* @param topo
* The topology
* @param info_list
* Libfabric NIC info list
* @param num_infos
* Length of `info_list`
* @param gpu_group_node
* One GPU topology node that is the closest to the NICs in `info_list`
* @param num_group
* Number of groups to create
*
* @return 0, on success
* -EINVAL, on others
*/
static int create_groups_from_info_list(nccl_ofi_topo_t *topo,
struct fi_info **info_list,
int num_infos,
hwloc_obj_t gpu_group_node,
int num_groups)
{
int ret = 0;
int group_idx = 0;
/* Adjust number of groups if input list does not provide enough members */
num_groups = NCCL_OFI_MIN(num_groups, num_infos);
/* Number of groups with one additional member. Handles the
* case where list size is not a multiple of number of
* groups */
const int num_large_groups = num_infos % num_groups;
int group_size = num_infos / num_groups + 1;
/* sort the provider list to match network rail ordering. See
* the documentation comment for platform_sort_rails() for
* more information. We do this here so that there is
* consistency
*/
if (platform_sort_rails != NULL) {
platform_sort_rails(info_list, (size_t)num_infos, (size_t)group_size);
}
for (; group_idx < num_groups; ++group_idx) {
hwloc_obj_t obj;
/* If the number of NIC infos is not a multiple of
* group size, latter candidates have one candidate
* less. */
if (group_idx == num_large_groups) --group_size;
if (group_size == 0) break;
/* Retrieve topology node of leader */
ret = get_hwloc_pcidev_by_fi_info(topo->topo, *info_list, &obj);
if (ret != 0) {
NCCL_OFI_WARN("Retrieval of topology node corresponding to libfabric NIC failed with error");
break;
}
if (!obj) {
NCCL_OFI_WARN("hwloc failed detecting PCI NIC info.");
ret = -EINVAL;
break;
}
nccl_ofi_topo_data_t *user_data = (nccl_ofi_topo_data_t *)obj->userdata;
if (!user_data) {
NCCL_OFI_WARN("Invalid user data pointer");
return -EINVAL;
}
if (user_data->info_list == *info_list) {
if (group_idx + 1 == num_groups) {
break;
} else {
NCCL_OFI_WARN("Invalid state of topology. "
"This state should not be reached.");
return -EINVAL;
}
}
/* Add list topology node */
user_data->info_list = *info_list;
user_data->info_list_len = group_size;
user_data->gpu_group_node = gpu_group_node;
/* Track maximum group size */
topo->max_group_size = NCCL_OFI_MAX(topo->max_group_size, group_size);
/* Cut list into two lists after group size list elements */
struct fi_info *end = user_data->info_list;
int i = 1;
for (; i < group_size; ++i) {
end = end->next;
}
/* Move list remainder to input list */
*info_list = end->next;
end->next = NULL;
}
return ret;
}
/*
* @brief Split libfabric NIC info lists of topology nodes with 'num_groups' > 0
* into 'num_groups' lists (groups) and add these lists
* to the corresponding topology nodes of their leaders (first
* NIC of the list).
*
* @return 0, on success
* -errno code, on others
*/
static int create_groups_from_info_lists(nccl_ofi_topo_t *topo)
{
nccl_ofi_topo_data_t *data = NULL;
/* Iterate over user data of topology nodes */
nccl_ofi_topo_data_iterator_t data_iter = {};
nccl_ofi_topo_set_to_begin(topo, &data_iter);
while ((data = nccl_ofi_get_user_data(&data_iter))) {
nccl_ofi_inc_user_data_iter(&data_iter);
if (!data->info_list) {
continue;
}
if (data->num_groups == 0) {
continue;
}
struct fi_info *info_list = data->info_list;
data->info_list = NULL;
int info_list_len = data->info_list_len;
data->info_list_len = 0;
int num_groups = data->num_groups;
data->num_groups = 0;
/* Create groups from list */
int ret = create_groups_from_info_list(topo,
&info_list,
info_list_len,
data->gpu_group_node,
num_groups);
if (ret != 0) {
data->info_list = info_list;
return ret;
}
}
return 0;
}
/*
* @brief Print libfabric NIC info lists stored in user data of topology nodes
*/
static void print_nic_groups(nccl_ofi_topo_t *topo) {
nccl_ofi_topo_data_t *data = NULL;
nccl_ofi_topo_data_iterator_t data_iter = {};
nccl_ofi_topo_set_to_begin(topo, &data_iter);
int group_idx = 0;
while ((data = nccl_ofi_get_user_data(&data_iter))) {
nccl_ofi_inc_user_data_iter(&data_iter);
if (!data->info_list) {
continue;
}
struct fi_info *info = data->info_list;
int info_idx = 0;
while (info) {
struct fi_pci_attr *attr = ofi_info_get_pci_attr(info);
if (attr) {
NCCL_OFI_INFO(NCCL_INIT,
"NIC group %i device #%i "
"%04x:%02x:%02x.%01x",
group_idx, info_idx,
attr->domain_id,
attr->bus_id,
attr->device_id,
attr->function_id);
}
++info_idx;
info = info->next;
}
++group_idx;
}
}
int nccl_ofi_topo_group(nccl_ofi_topo_t *topo)
{
int ret = 0;
ret = mark_topo_nodes_with_ofi_info_subtree(topo);
if (ret != 0) {
return ret;
}
ret = propoagate_accel_group_counts(topo->topo);
if (ret != 0) {
return ret;
}
ret = lift_up_ofi_infos(topo);
if (ret != 0) {
return ret;
}
ret = create_groups_from_info_lists(topo);
if (ret != 0) {
return ret;
}
print_nic_groups(topo);
return ret;
}
/*
* @brief Return NUMA node child from memory children list of `node`
*
* Since HWLOC 2.0, NUMA topology nodes are stored in a specific
* memory children list. Scan this list and return the first NUMA
* child node that is found that does not store user data.
*
* @param node
* Topology node whose memory children list is to be scanned
* @return NUMA node topology node, if found
* NULL, otherwise
*/
static hwloc_obj_t get_numa_mem_child(hwloc_obj_t node)