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 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. if (handle == nullptr) {
  29. MS_LOG(ERROR) << "The pointer[handle] is null.";
  30. return nullptr;
  31. }
  32. if (name == nullptr) {
  33. MS_LOG(ERROR) << "The pointer[name] is null.";
  34. return nullptr;
  35. }
  36. void *func = dlsym(handle, name);
  37. return func;
  38. }
  39. void ReleaseLibrary(void *handle) {
  40. if (handle != nullptr) {
  41. (void)dlclose(handle);
  42. }
  43. }
  44. void *GetNumaAdapterHandle() {
  45. void *handle = LoadLibrary("libnuma.so");
  46. return handle;
  47. }
  48. Status NumaBind(void *handle, const int32_t &rank_id) {
  49. if (handle == nullptr) {
  50. RETURN_STATUS_UNEXPECTED("Numa package not found.");
  51. }
  52. auto numa_max_node_func = GetNumaAdapterFunc(handle, "numa_max_node");
  53. if (numa_max_node_func == nullptr) {
  54. RETURN_STATUS_UNEXPECTED("Numa api: numa_max_node not found.");
  55. }
  56. auto numa_allocate_nodemask_func = GetNumaAdapterFunc(handle, "numa_allocate_nodemask");
  57. if (numa_allocate_nodemask_func == nullptr) {
  58. RETURN_STATUS_UNEXPECTED("Numa api: numa_allocate_nodemask not found.");
  59. }
  60. auto numa_bitmask_clearall_func = GetNumaAdapterFunc(handle, "numa_bitmask_clearall");
  61. if (numa_bitmask_clearall_func == nullptr) {
  62. RETURN_STATUS_UNEXPECTED("Numa api: numa_bitmask_clearall not found.");
  63. }
  64. auto numa_bitmask_setbit_func = GetNumaAdapterFunc(handle, "numa_bitmask_setbit");
  65. if (numa_bitmask_setbit_func == nullptr) {
  66. RETURN_STATUS_UNEXPECTED("Numa api: numa_bitmask_setbit not found.");
  67. }
  68. auto numa_bind_func = GetNumaAdapterFunc(handle, "numa_bind");
  69. if (numa_bind_func == nullptr) {
  70. RETURN_STATUS_UNEXPECTED("Numa api: numa_bind not found.");
  71. }
  72. auto numa_bitmask_free_func = GetNumaAdapterFunc(handle, "numa_bitmask_free");
  73. if (numa_bitmask_free_func == nullptr) {
  74. RETURN_STATUS_UNEXPECTED("Numa api: numa_bitmask_free not found.");
  75. }
  76. auto numa_max_node = (int (*)(void))(numa_max_node_func);
  77. auto numa_allocate_nodemask = (struct bitmask * (*)(void))(numa_allocate_nodemask_func);
  78. auto numa_bitmask_clearall = (struct bitmask * (*)(struct bitmask *))(numa_bitmask_clearall_func);
  79. auto numa_bitmask_setbit = (struct bitmask * (*)(struct bitmask *, unsigned int))(numa_bitmask_setbit_func);
  80. auto numa_bind = (void (*)(struct bitmask *))(numa_bind_func);
  81. auto numa_bitmask_free = (void (*)(struct bitmask *))(numa_bitmask_free_func);
  82. int numa_node_max_id = numa_max_node();
  83. if (numa_node_max_id < 0) {
  84. RETURN_STATUS_UNEXPECTED("Get numa max node failed.");
  85. }
  86. if (rank_id >= 0) {
  87. uint32_t numa_bind_id = static_cast<uint32_t>(rank_id % (numa_node_max_id + 1));
  88. auto bm = numa_allocate_nodemask();
  89. numa_bitmask_clearall(bm);
  90. numa_bitmask_setbit(bm, numa_bind_id);
  91. numa_bind(bm);
  92. numa_bitmask_free(bm);
  93. } else {
  94. RETURN_STATUS_UNEXPECTED("Value error, rank_id is a negative value.");
  95. }
  96. return Status::OK();
  97. }
  98. } // namespace dataset
  99. } // namespace mindspore