1 /* 2 * Copyright © 2012 Blue Brain Project, EPFL. All rights reserved. 3 * Copyright © 2012-2013 Inria. All rights reserved. 4 * See COPYING in top-level directory. 5 */ 6 7 /** \file 8 * \brief Macros to help interaction between hwloc and OpenGL displays. 9 * 10 * Applications that use both hwloc and OpenGL may want to include 11 * this file so as to get topology information for OpenGL displays. 12 */ 13 14 #ifndef HWLOC_GL_H 15 #define HWLOC_GL_H 16 17 #include <hwloc.h> 18 19 #include <stdio.h> 20 #include <string.h> 21 22 23 #ifdef __cplusplus 24 extern "C" { 25 #endif 26 27 28 /** \defgroup hwlocality_gl Interoperability with OpenGL displays 29 * 30 * This interface offers ways to retrieve topology information about 31 * OpenGL displays. 32 * 33 * Only the NVIDIA display locality information is currently available, 34 * using the NV-CONTROL X11 extension and the NVCtrl library. 35 * 36 * @{ 37 */ 38 39 /** \brief Get the hwloc OS device object corresponding to the 40 * OpenGL display given by port and device index. 41 * 42 * Return the OS device object describing the OpenGL display 43 * whose port (server) is \p port and device (screen) is \p device. 44 * Return NULL if there is none. 45 * 46 * The topology \p topology does not necessarily have to match the current 47 * machine. For instance the topology may be an XML import of a remote host. 48 * I/O devices detection and the GL component must be enabled in the topology. 49 * 50 * \note The corresponding PCI device object can be obtained by looking 51 * at the OS device parent object (unless PCI devices are filtered out). 52 */ 53 static __hwloc_inline hwloc_obj_t 54 hwloc_gl_get_display_osdev_by_port_device(hwloc_topology_t topology, 55 unsigned port, unsigned device) 56 { 57 unsigned x = (unsigned) -1, y = (unsigned) -1; 58 hwloc_obj_t osdev = NULL; 59 while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) { 60 if (HWLOC_OBJ_OSDEV_GPU == osdev->attr->osdev.type 61 && osdev->name 62 && sscanf(osdev->name, ":%u.%u", &x, &y) == 2 63 && port == x && device == y) 64 return osdev; 65 } 66 errno = EINVAL; 67 return NULL; 68 } 69 70 /** \brief Get the hwloc OS device object corresponding to the 71 * OpenGL display given by name. 72 * 73 * Return the OS device object describing the OpenGL display 74 * whose name is \p name, built as ":port.device" such as ":0.0" . 75 * Return NULL if there is none. 76 * 77 * The topology \p topology does not necessarily have to match the current 78 * machine. For instance the topology may be an XML import of a remote host. 79 * I/O devices detection and the GL component must be enabled in the topology. 80 * 81 * \note The corresponding PCI device object can be obtained by looking 82 * at the OS device parent object (unless PCI devices are filtered out). 83 */ 84 static __hwloc_inline hwloc_obj_t 85 hwloc_gl_get_display_osdev_by_name(hwloc_topology_t topology, 86 const char *name) 87 { 88 hwloc_obj_t osdev = NULL; 89 while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) { 90 if (HWLOC_OBJ_OSDEV_GPU == osdev->attr->osdev.type 91 && osdev->name 92 && !strcmp(name, osdev->name)) 93 return osdev; 94 } 95 errno = EINVAL; 96 return NULL; 97 } 98 99 /** \brief Get the OpenGL display port and device corresponding 100 * to the given hwloc OS object. 101 * 102 * Return the OpenGL display port (server) in \p port and device (screen) 103 * in \p screen that correspond to the given hwloc OS device object. 104 * Return \c -1 if there is none. 105 * 106 * The topology \p topology does not necessarily have to match the current 107 * machine. For instance the topology may be an XML import of a remote host. 108 * I/O devices detection and the GL component must be enabled in the topology. 109 */ 110 static __hwloc_inline int 111 hwloc_gl_get_display_by_osdev(hwloc_topology_t topology __hwloc_attribute_unused, 112 hwloc_obj_t osdev, 113 unsigned *port, unsigned *device) 114 { 115 unsigned x = -1, y = -1; 116 if (HWLOC_OBJ_OSDEV_GPU == osdev->attr->osdev.type 117 && sscanf(osdev->name, ":%u.%u", &x, &y) == 2) { 118 *port = x; 119 *device = y; 120 return 0; 121 } 122 errno = EINVAL; 123 return -1; 124 } 125 126 /** @} */ 127 128 129 #ifdef __cplusplus 130 } /* extern "C" */ 131 #endif 132 133 134 #endif /* HWLOC_GL_H */ 135