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_runtime_api.h>
10 
11 #include "hwloc.h"
12 #include "hwloc/cudart.h"
13 
14 /* check the CUDA Runtime API helpers */
15 
main(void)16 int main(void)
17 {
18   hwloc_topology_t topology;
19   cudaError_t cerr;
20   int count, i;
21   int err;
22 
23   cerr = cudaGetDeviceCount(&count);
24   if (cerr) {
25     printf("cudaGetDeviceCount failed %d\n", cerr);
26     return 0;
27   }
28   printf("cudaGetDeviceCount found %d devices\n", count);
29 
30   hwloc_topology_init(&topology);
31   hwloc_topology_set_io_types_filter(topology, HWLOC_TYPE_FILTER_KEEP_IMPORTANT);
32   hwloc_topology_load(topology);
33 
34   for(i=0; i<count; i++) {
35     hwloc_bitmap_t set;
36     hwloc_obj_t osdev, ancestor;
37     const char *value;
38 
39     osdev = hwloc_cudart_get_device_osdev_by_index(topology, i);
40     assert(osdev);
41 
42     ancestor = hwloc_get_non_io_ancestor_obj(topology, osdev);
43 
44     printf("found OSDev %s\n", osdev->name);
45     err = strncmp(osdev->name, "cuda", 4);
46     assert(!err);
47     assert(atoi(osdev->name+4) == (int) i);
48 
49     value = hwloc_obj_get_info_by_name(osdev, "Backend");
50     err = strcmp(value, "CUDA");
51     assert(!err);
52 
53     assert(osdev->attr->osdev.type == HWLOC_OBJ_OSDEV_COPROC);
54 
55     value = osdev->subtype;
56     assert(value);
57     err = strcmp(value, "CUDA");
58     assert(!err);
59 
60     value = hwloc_obj_get_info_by_name(osdev, "GPUModel");
61     printf("found OSDev model %s\n", value);
62 
63     set = hwloc_bitmap_alloc();
64     err = hwloc_cudart_get_device_cpuset(topology, i, set);
65     if (err < 0) {
66       printf("failed to get cpuset for device %d\n", i);
67     } else {
68       char *cpuset_string = NULL;
69       hwloc_bitmap_asprintf(&cpuset_string, set);
70       printf("got cpuset %s for device %d\n", cpuset_string, i);
71       if (hwloc_bitmap_isequal(hwloc_topology_get_complete_cpuset(topology), hwloc_topology_get_topology_cpuset(topology)))
72 	/* only compare if the topology is complete, otherwise things can be significantly different */
73 	assert(hwloc_bitmap_isequal(set, ancestor->cpuset));
74       free(cpuset_string);
75     }
76     hwloc_bitmap_free(set);
77   }
78 
79   hwloc_topology_destroy(topology);
80 
81   return 0;
82 }
83