1 /** 2 * Copyright 2022 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 17 #ifndef MINDSPORE_LITE_SRC_EXTENDRT_NUMA_ADAPTER_H_ 18 #define MINDSPORE_LITE_SRC_EXTENDRT_NUMA_ADAPTER_H_ 19 #include <cstdint> 20 #include <cstddef> 21 #include <memory> 22 #include <unordered_map> 23 #include <vector> 24 25 namespace mindspore { 26 namespace numa { 27 struct bitmask { 28 uint64_t size; 29 uint64_t *maskp; 30 }; 31 32 struct NUMAInterface { 33 int (*numa_available)(void); 34 int (*numa_num_configured_nodes)(void); 35 int (*numa_num_task_cpus)(); 36 struct bitmask *(*numa_allocate_nodemask)(void); 37 struct bitmask *(*numa_bitmask_clearall)(struct bitmask *); 38 struct bitmask *(*numa_bitmask_setbit)(struct bitmask *, unsigned int); 39 void (*numa_bind)(struct bitmask *); 40 void (*numa_bitmask_free)(struct bitmask *); 41 void *(*numa_alloc_onnode)(size_t size, int node); 42 int64_t (*numa_node_size64)(int node, int64_t *freep); 43 void (*numa_free)(void *start, size_t size); 44 }; 45 46 struct MemoryInfo { 47 int64_t total = 0; 48 int64_t free = 0; 49 }; 50 51 class NUMAAdapter { 52 public: GetInstance()53 static std::shared_ptr<NUMAAdapter> GetInstance() { 54 static std::shared_ptr<NUMAAdapter> const instance = std::make_shared<NUMAAdapter>(); 55 return instance; 56 } 57 58 NUMAAdapter(); 59 ~NUMAAdapter(); Available()60 inline bool Available() const { return available_; } 61 void Bind(int node_id) const; 62 void *Malloc(int node_id, size_t size) const; 63 void Free(void *data, size_t size) const; 64 int NodesNum() const; 65 int CPUNum() const; 66 std::vector<int> GetCPUList(int node_id); 67 MemoryInfo GetNodeSize(int node_id) const; 68 69 private: 70 void *handle_; // numa.so handle 71 bool available_ = false; 72 NUMAInterface numa_interfaces_; 73 std::unordered_map<int, std::vector<int>> node_cpu_list_; 74 }; 75 } // namespace numa 76 } // namespace mindspore 77 #endif // MINDSPORE_LITE_SRC_EXTENDRT_NUMA_ADAPTER_H_ 78