1 /*
2  * Copyright © 2010-2018 Inria.  All rights reserved.
3  * Copyright © 2011 Cisco Systems, Inc.  All rights reserved.
4  * See COPYING in top-level directory.
5  */
6 
7 #include <stdio.h>
8 #include <assert.h>
9 #include <cuda.h>
10 
11 #include "hwloc.h"
12 #include "hwloc/cuda.h"
13 
14 /* check the CUDA Driver API helpers */
15 
main(void)16 int main(void)
17 {
18   hwloc_topology_t topology;
19   CUresult cres;
20   CUdevice device;
21   int count, i;
22   int err;
23 
24   cres = cuInit(0);
25   if (cres != CUDA_SUCCESS) {
26     printf("cuInit failed %d\n", cres);
27     return 0;
28   }
29 
30   cres = cuDeviceGetCount(&count);
31   if (cres != CUDA_SUCCESS) {
32     printf("cuDeviceGetCount failed %d\n", cres);
33     return 0;
34   }
35   printf("cuDeviceGetCount found %d devices\n", count);
36 
37   hwloc_topology_init(&topology);
38   hwloc_topology_set_io_types_filter(topology, HWLOC_TYPE_FILTER_KEEP_IMPORTANT);
39   hwloc_topology_load(topology);
40 
41   for(i=0; i<count; i++) {
42     hwloc_bitmap_t set;
43     hwloc_obj_t osdev, osdev2, ancestor;
44     const char *value;
45 
46     cres = cuDeviceGet(&device, i);
47     if (cres != CUDA_SUCCESS) {
48       printf("failed to get device %d\n", i);
49       continue;
50     }
51 
52     osdev = hwloc_cuda_get_device_osdev(topology, device);
53     assert(osdev);
54     osdev2 = hwloc_cuda_get_device_osdev_by_index(topology, i);
55     assert(osdev == osdev2);
56 
57     ancestor = hwloc_get_non_io_ancestor_obj(topology, osdev);
58 
59     printf("found OSDev %s\n", osdev->name);
60     err = strncmp(osdev->name, "cuda", 4);
61     assert(!err);
62     assert(atoi(osdev->name+4) == (int) i);
63 
64     value = hwloc_obj_get_info_by_name(osdev, "Backend");
65     err = strcmp(value, "CUDA");
66     assert(!err);
67 
68     assert(osdev->attr->osdev.type == HWLOC_OBJ_OSDEV_COPROC);
69 
70     value = osdev->subtype;
71     assert(value);
72     err = strcmp(value, "CUDA");
73     assert(!err);
74 
75     value = hwloc_obj_get_info_by_name(osdev, "GPUModel");
76     printf("found OSDev model %s\n", value);
77 
78     set = hwloc_bitmap_alloc();
79     err = hwloc_cuda_get_device_cpuset(topology, device, set);
80     if (err < 0) {
81       printf("failed to get cpuset for device %d\n", i);
82     } else {
83       char *cpuset_string = NULL;
84       hwloc_bitmap_asprintf(&cpuset_string, set);
85       printf("got cpuset %s for device %d\n", cpuset_string, i);
86       if (hwloc_bitmap_isequal(hwloc_topology_get_complete_cpuset(topology), hwloc_topology_get_topology_cpuset(topology)))
87 	/* only compare if the topology is complete, otherwise things can be significantly different */
88 	assert(hwloc_bitmap_isincluded(ancestor->cpuset, set));
89       free(cpuset_string);
90     }
91     hwloc_bitmap_free(set);
92   }
93 
94   hwloc_topology_destroy(topology);
95 
96   return 0;
97 }
98