inlines.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright © 2009 CNRS
  3. * Copyright © 2009-2018 Inria. All rights reserved.
  4. * Copyright © 2009-2012 Université Bordeaux
  5. * Copyright © 2009-2010 Cisco Systems, Inc. All rights reserved.
  6. * See COPYING in top-level directory.
  7. */
  8. /**
  9. * This file contains the inline code of functions declared in hwloc.h
  10. */
  11. #ifndef HWLOC_INLINES_H
  12. #define HWLOC_INLINES_H
  13. #ifndef HWLOC_H
  14. #error Please include the main hwloc.h instead
  15. #endif
  16. #include <stdlib.h>
  17. #include <errno.h>
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. static __hwloc_inline int
  22. hwloc_get_type_or_below_depth (hwloc_topology_t topology, hwloc_obj_type_t type)
  23. {
  24. int depth = hwloc_get_type_depth(topology, type);
  25. if (depth != HWLOC_TYPE_DEPTH_UNKNOWN)
  26. return depth;
  27. /* find the highest existing level with type order >= */
  28. for(depth = hwloc_get_type_depth(topology, HWLOC_OBJ_PU); ; depth--)
  29. if (hwloc_compare_types(hwloc_get_depth_type(topology, depth), type) < 0)
  30. return depth+1;
  31. /* Shouldn't ever happen, as there is always a Machine level with lower order and known depth. */
  32. /* abort(); */
  33. }
  34. static __hwloc_inline int
  35. hwloc_get_type_or_above_depth (hwloc_topology_t topology, hwloc_obj_type_t type)
  36. {
  37. int depth = hwloc_get_type_depth(topology, type);
  38. if (depth != HWLOC_TYPE_DEPTH_UNKNOWN)
  39. return depth;
  40. /* find the lowest existing level with type order <= */
  41. for(depth = 0; ; depth++)
  42. if (hwloc_compare_types(hwloc_get_depth_type(topology, depth), type) > 0)
  43. return depth-1;
  44. /* Shouldn't ever happen, as there is always a PU level with higher order and known depth. */
  45. /* abort(); */
  46. }
  47. static __hwloc_inline int
  48. hwloc_get_nbobjs_by_type (hwloc_topology_t topology, hwloc_obj_type_t type)
  49. {
  50. int depth = hwloc_get_type_depth(topology, type);
  51. if (depth == HWLOC_TYPE_DEPTH_UNKNOWN)
  52. return 0;
  53. if (depth == HWLOC_TYPE_DEPTH_MULTIPLE)
  54. return -1; /* FIXME: agregate nbobjs from different levels? */
  55. return (int) hwloc_get_nbobjs_by_depth(topology, depth);
  56. }
  57. static __hwloc_inline hwloc_obj_t
  58. hwloc_get_obj_by_type (hwloc_topology_t topology, hwloc_obj_type_t type, unsigned idx)
  59. {
  60. int depth = hwloc_get_type_depth(topology, type);
  61. if (depth == HWLOC_TYPE_DEPTH_UNKNOWN)
  62. return NULL;
  63. if (depth == HWLOC_TYPE_DEPTH_MULTIPLE)
  64. return NULL;
  65. return hwloc_get_obj_by_depth(topology, depth, idx);
  66. }
  67. static __hwloc_inline hwloc_obj_t
  68. hwloc_get_next_obj_by_depth (hwloc_topology_t topology, int depth, hwloc_obj_t prev)
  69. {
  70. if (!prev)
  71. return hwloc_get_obj_by_depth (topology, depth, 0);
  72. if (prev->depth != depth)
  73. return NULL;
  74. return prev->next_cousin;
  75. }
  76. static __hwloc_inline hwloc_obj_t
  77. hwloc_get_next_obj_by_type (hwloc_topology_t topology, hwloc_obj_type_t type,
  78. hwloc_obj_t prev)
  79. {
  80. int depth = hwloc_get_type_depth(topology, type);
  81. if (depth == HWLOC_TYPE_DEPTH_UNKNOWN || depth == HWLOC_TYPE_DEPTH_MULTIPLE)
  82. return NULL;
  83. return hwloc_get_next_obj_by_depth (topology, depth, prev);
  84. }
  85. static __hwloc_inline hwloc_obj_t
  86. hwloc_get_root_obj (hwloc_topology_t topology)
  87. {
  88. return hwloc_get_obj_by_depth (topology, 0, 0);
  89. }
  90. static __hwloc_inline const char *
  91. hwloc_obj_get_info_by_name(hwloc_obj_t obj, const char *name)
  92. {
  93. unsigned i;
  94. for(i=0; i<obj->infos_count; i++) {
  95. struct hwloc_info_s *info = &obj->infos[i];
  96. if (!strcmp(info->name, name))
  97. return info->value;
  98. }
  99. return NULL;
  100. }
  101. static __hwloc_inline void *
  102. hwloc_alloc_membind_policy(hwloc_topology_t topology, size_t len, hwloc_const_cpuset_t set, hwloc_membind_policy_t policy, int flags)
  103. {
  104. void *p = hwloc_alloc_membind(topology, len, set, policy, flags);
  105. if (p)
  106. return p;
  107. if (hwloc_set_membind(topology, set, policy, flags) < 0)
  108. /* hwloc_set_membind() takes care of ignoring errors if non-STRICT */
  109. return NULL;
  110. p = hwloc_alloc(topology, len);
  111. if (p && policy != HWLOC_MEMBIND_FIRSTTOUCH)
  112. /* Enforce the binding by touching the data */
  113. memset(p, 0, len);
  114. return p;
  115. }
  116. #ifdef __cplusplus
  117. } /* extern "C" */
  118. #endif
  119. #endif /* HWLOC_INLINES_H */