You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

numa_interface.cc 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * Copyright 2021 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "minddata/dataset/util/numa_interface.h"
  17. #include <dlfcn.h>
  18. namespace mindspore {
  19. namespace dataset {
  20. inline void *LoadLibrary(const char *name) {
  21. if (name == nullptr) {
  22. return nullptr;
  23. }
  24. auto handle = dlopen(name, RTLD_LAZY | RTLD_LOCAL);
  25. return handle;
  26. }
  27. inline void *GetNumaAdapterFunc(void *handle, const char *name) {
  28. void *func = dlsym(handle, name);
  29. return func;
  30. }
  31. void ReleaseLibrary(void *handle) {
  32. if (handle != nullptr) {
  33. (void)dlclose(handle);
  34. }
  35. }
  36. void *GetNumaAdapterHandle() {
  37. void *handle = LoadLibrary("libnuma.so");
  38. return handle;
  39. }
  40. typedef int (*GetNumaMaxNodeFunc)(void);
  41. typedef struct bitmask *(*NumaAllocateNodemaskFunc)(void);
  42. typedef struct bitmask *(*NumaBitmaskClearallFunc)(struct bitmask *);
  43. typedef struct bitmask *(*NumaBitmaskSetbitFunc)(struct bitmask *, unsigned int);
  44. typedef void (*NumaBindFunc)(struct bitmask *);
  45. typedef void (*NumaBitmaskFreeFunc)(struct bitmask *);
  46. Status NumaBind(void *handle, const int32_t &rank_id) {
  47. if (handle == nullptr) {
  48. RETURN_STATUS_UNEXPECTED("Numa package not found.");
  49. }
  50. auto numa_max_node_func_pointer = GetNumaAdapterFunc(handle, "numa_max_node");
  51. if (numa_max_node_func_pointer == nullptr) {
  52. RETURN_STATUS_UNEXPECTED("Numa api: numa_max_node not found.");
  53. }
  54. auto numa_allocate_nodemask_func_pointer = GetNumaAdapterFunc(handle, "numa_allocate_nodemask");
  55. if (numa_allocate_nodemask_func_pointer == nullptr) {
  56. RETURN_STATUS_UNEXPECTED("Numa api: numa_allocate_nodemask not found.");
  57. }
  58. auto numa_bitmask_clearall_func_pointer = GetNumaAdapterFunc(handle, "numa_bitmask_clearall");
  59. if (numa_bitmask_clearall_func_pointer == nullptr) {
  60. RETURN_STATUS_UNEXPECTED("Numa api: numa_bitmask_clearall not found.");
  61. }
  62. auto numa_bitmask_setbit_func_pointer = GetNumaAdapterFunc(handle, "numa_bitmask_setbit");
  63. if (numa_bitmask_setbit_func_pointer == nullptr) {
  64. RETURN_STATUS_UNEXPECTED("Numa api: numa_bitmask_setbit not found.");
  65. }
  66. auto numa_bind_func_pointer = GetNumaAdapterFunc(handle, "numa_bind");
  67. if (numa_bind_func_pointer == nullptr) {
  68. RETURN_STATUS_UNEXPECTED("Numa api: numa_bind not found.");
  69. }
  70. auto numa_bitmask_free_func_pointer = GetNumaAdapterFunc(handle, "numa_bitmask_free");
  71. if (numa_bitmask_free_func_pointer == nullptr) {
  72. RETURN_STATUS_UNEXPECTED("Numa api: numa_bitmask_free not found.");
  73. }
  74. auto numa_max_node_func = reinterpret_cast<GetNumaMaxNodeFunc>(numa_max_node_func_pointer);
  75. auto numa_allocate_nodemask_func = reinterpret_cast<NumaAllocateNodemaskFunc>(numa_allocate_nodemask_func_pointer);
  76. auto numa_bitmask_clearall_func = reinterpret_cast<NumaBitmaskClearallFunc>(numa_bitmask_clearall_func_pointer);
  77. auto numa_bitmask_setbit_func = reinterpret_cast<NumaBitmaskSetbitFunc>(numa_bitmask_setbit_func_pointer);
  78. auto numa_bind_func = reinterpret_cast<NumaBindFunc>(numa_bind_func_pointer);
  79. auto numa_bitmask_free_func = reinterpret_cast<NumaBitmaskFreeFunc>(numa_bitmask_free_func_pointer);
  80. int numa_node_max_id = numa_max_node_func();
  81. if (numa_node_max_id < 0) {
  82. RETURN_STATUS_UNEXPECTED("Get numa max node failed.");
  83. }
  84. if (rank_id >= 0) {
  85. uint32_t numa_bind_id = static_cast<uint32_t>(rank_id % (numa_node_max_id + 1));
  86. auto bm = numa_allocate_nodemask_func();
  87. numa_bitmask_clearall_func(bm);
  88. numa_bitmask_setbit_func(bm, numa_bind_id);
  89. numa_bind_func(bm);
  90. numa_bitmask_free_func(bm);
  91. } else {
  92. RETURN_STATUS_UNEXPECTED("Value error, rank_id is a negative value.");
  93. }
  94. return Status::OK();
  95. }
  96. } // namespace dataset
  97. } // namespace mindspore