1 /* This example program plays with:
2  * - finding GPU OS devices
3  * - getting CUDA and OpenCL attributes
4  * - displaying the locality of the GPU
5  *
6  * Copyright © 2009-2019 Inria.  All rights reserved.
7  * Copyright © 2009-2011,2017 Université Bordeaux
8  * Copyright © 2009-2010 Cisco Systems, Inc.  All rights reserved.
9  * See COPYING in top-level directory.
10  */
11 
12 #include "hwloc.h"
13 
14 #include <errno.h>
15 #include <stdio.h>
16 #include <string.h>
17 
main(void)18 int main(void)
19 {
20     hwloc_topology_t topology;
21     hwloc_obj_t obj;
22     unsigned n, i;
23     int devid, platformid;
24     const char *dev;
25 
26     /* Allocate, initialize and load topology object. */
27     hwloc_topology_init(&topology);
28     hwloc_topology_set_io_types_filter(topology, HWLOC_TYPE_FILTER_KEEP_IMPORTANT);
29     hwloc_topology_load(topology);
30 
31     /* Find CUDA devices through the corresponding OS devices */
32     n = hwloc_get_nbobjs_by_type(topology, HWLOC_OBJ_OS_DEVICE);
33 
34     for (i = 0; i < n ; i++) {
35       const char *s;
36       obj = hwloc_get_obj_by_type(topology, HWLOC_OBJ_OS_DEVICE, i);
37       printf("%s:\n", obj->name);
38 
39       /* obj->attr->osdev.type is HWLOC_OBJ_OSDEV_COPROC */
40 
41       s = hwloc_obj_get_info_by_name(obj, "Backend");
42       /* obj->subtype also contains CUDA or OpenCL since v2.0 */
43 
44       if (s && !strcmp(s, "CUDA")) {
45         /* This is a CUDA device */
46         assert(!strncmp(obj->name, "cuda", 4));
47         devid = atoi(obj->name + 4);
48         printf("CUDA device %d\n", devid);
49 
50         s = hwloc_obj_get_info_by_name(obj, "GPUModel");
51         if (s)
52           printf("Model: %s\n", s);
53 
54         s = hwloc_obj_get_info_by_name(obj, "CUDAGlobalMemorySize");
55         if (s)
56           printf("Memory: %s\n", s);
57 
58         s = hwloc_obj_get_info_by_name(obj, "CUDAMultiProcessors");
59         if (s)
60         {
61           int mp = atoi(s);
62           s = hwloc_obj_get_info_by_name(obj, "CUDACoresPerMP");
63           if (s) {
64             int mp_cores = atoi(s);
65             printf("Cores: %d\n", mp * mp_cores);
66           }
67         }
68       }
69 
70       if (s && !strcmp(s, "OpenCL")) {
71         /* This is an OpenCL device */
72         assert(!strncmp(obj->name, "opencl", 6));
73         platformid = atoi(obj->name + 6);
74         printf("OpenCL platform %d\n", platformid);
75         dev = strchr(obj->name + 6, 'd');
76         devid = atoi(dev + 1);
77         printf("OpenCL device %d\n", devid);
78 
79         s = hwloc_obj_get_info_by_name(obj, "GPUModel");
80         if (s)
81           printf("Model: %s\n", s);
82 
83         s = hwloc_obj_get_info_by_name(obj, "OpenCLGlobalMemorySize");
84         if (s)
85           printf("Memory: %s\n", s);
86       }
87 
88       /* One can also use helpers from hwloc/cuda.h, hwloc/cudart.h,
89        * hwloc/opencl.h */
90 
91 
92       /* Find out cpuset this is connected to */
93       while (obj && (!obj->cpuset || hwloc_bitmap_iszero(obj->cpuset)))
94         obj = obj->parent;
95 
96       if (obj) {
97         char *cpuset_string;
98         char name[16];
99         hwloc_obj_type_snprintf(name, sizeof(name), obj, 0);
100         hwloc_bitmap_asprintf(&cpuset_string, obj->cpuset);
101         printf("Location: %s P#%u\n", name, obj->os_index);
102         printf("Cpuset: %s\n", cpuset_string);
103       }
104       printf("\n");
105     }
106 
107     /* Destroy topology object. */
108     hwloc_topology_destroy(topology);
109 
110     return 0;
111 }
112