gl.h 4.2 KB

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