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