Hardware Locality (hwloc)  PR-737-20250925.0822.gite8f69c77f
gl.h
1 /*
2  * SPDX-License-Identifier: BSD-3-Clause
3  * Copyright © 2012 Blue Brain Project, EPFL. All rights reserved.
4  * Copyright © 2012-2024 Inria. All rights reserved.
5  * See COPYING in top-level directory.
6  */
7 
15 #ifndef HWLOC_GL_H
16 #define HWLOC_GL_H
17 
18 #include "hwloc.h"
19 
20 #include <stdio.h>
21 #include <string.h>
22 
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 
54 static __hwloc_inline hwloc_obj_t
56  unsigned port, unsigned device)
57 {
58  unsigned x = (unsigned) -1, y = (unsigned) -1;
59  hwloc_obj_t osdev = NULL;
60  while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
61  if ((osdev->attr->osdev.types & HWLOC_OBJ_OSDEV_GPU) /* assume future GL devices will be at least GPU */
62  && osdev->name
63  && sscanf(osdev->name, ":%u.%u", &x, &y) == 2
64  && port == x && device == y)
65  return osdev;
66  }
67  errno = EINVAL;
68  return NULL;
69 }
70 
85 static __hwloc_inline hwloc_obj_t
87  const char *name)
88 {
89  hwloc_obj_t osdev = NULL;
90  while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
91  if ((osdev->attr->osdev.types & HWLOC_OBJ_OSDEV_GPU) /* assume future GL devices will be at least GPU */
92  && osdev->name
93  && !strcmp(name, osdev->name))
94  return osdev;
95  }
96  errno = EINVAL;
97  return NULL;
98 }
99 
113 static __hwloc_inline int
114 hwloc_gl_get_display_by_osdev(hwloc_topology_t topology __hwloc_attribute_unused,
115  hwloc_obj_t osdev,
116  unsigned *port, unsigned *device)
117 {
118  unsigned x = -1, y = -1;
119  if ((osdev->attr->osdev.types & HWLOC_OBJ_OSDEV_GPU) /* assume future GL devices will be at least GPU */
120  && sscanf(osdev->name, ":%u.%u", &x, &y) == 2) {
121  *port = x;
122  *device = y;
123  return 0;
124  }
125  errno = EINVAL;
126  return -1;
127 }
128 
132 #ifdef __cplusplus
133 } /* extern "C" */
134 #endif
135 
136 
137 #endif /* HWLOC_GL_H */
138 
hwloc_obj
Structure of a topology object.
Definition: hwloc.h:488
HWLOC_OBJ_OSDEV_GPU
@ HWLOC_OBJ_OSDEV_GPU
Operating system GPU device. For instance ":0.0" for a GL display, "card0" for a Linux DRM device,...
Definition: hwloc.h:389
hwloc_topology_t
struct hwloc_topology * hwloc_topology_t
Topology context.
Definition: hwloc.h:778
hwloc_obj_attr_u::osdev
struct hwloc_obj_attr_u::hwloc_osdev_attr_s osdev
hwloc_obj::attr
union hwloc_obj_attr_u * attr
Object type-specific Attributes, may be NULL if no attribute value was found.
Definition: hwloc.h:507
hwloc_gl_get_display_osdev_by_name
hwloc_obj_t hwloc_gl_get_display_osdev_by_name(hwloc_topology_t topology, const char *name)
Get the hwloc OS device object corresponding to the OpenGL display given by name.
Definition: gl.h:86
hwloc_obj_attr_u::hwloc_osdev_attr_s::types
hwloc_obj_osdev_types_t types
OR'ed set of at least one hwloc_obj_osdev_type_e.
Definition: hwloc.h:761
hwloc_obj::name
char * name
Object-specific name if any. Mostly used for identifying OS devices and Misc objects where a name str...
Definition: hwloc.h:500
hwloc_gl_get_display_by_osdev
int hwloc_gl_get_display_by_osdev(hwloc_topology_t topology, hwloc_obj_t osdev, unsigned *port, unsigned *device)
Get the OpenGL display port and device corresponding to the given hwloc OS object.
Definition: gl.h:114
hwloc_get_next_osdev
hwloc_obj_t hwloc_get_next_osdev(hwloc_topology_t topology, hwloc_obj_t prev)
Get the next OS device in the system.
Definition: helper.h:1291
hwloc_gl_get_display_osdev_by_port_device
hwloc_obj_t hwloc_gl_get_display_osdev_by_port_device(hwloc_topology_t topology, unsigned port, unsigned device)
Get the hwloc OS device object corresponding to the OpenGL display given by port and device index.
Definition: gl.h:55