1 /** 2 * Copyright 2021-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_CORE_MINDRT_RUNTIME_CORE_AFFINITY_H_ 18 #define MINDSPORE_CORE_MINDRT_RUNTIME_CORE_AFFINITY_H_ 19 20 #include <vector> 21 #include <thread> 22 23 #ifdef PARALLEL_INFERENCE 24 #define BIND_CORE 25 #endif 26 #if defined(__ANDROID__) || defined(MS_COMPILE_OHOS) 27 #define BIND_CORE 28 #include <sched.h> 29 #endif 30 #ifdef _WIN32 31 #define BIND_CORE 32 #endif 33 // Lite not support bind core. 34 #if !defined(BUILD_LITE) && defined(__linux__) 35 #define BIND_CORE 36 #endif 37 38 namespace mindspore { 39 enum BindMode { 40 Power_NoBind = 0, // free schedule 41 Power_Higher = 1, 42 Power_Middle = 2, 43 }; 44 #define PARSE_CPU_GAP 3 45 #define PARSE_CPU_DEC 10 46 #define PARSE_CPU_HEX 16 47 48 #ifdef _WIN32 49 void SetWindowsSelfAffinity(uint64_t core_id); 50 #endif 51 52 class Worker; 53 class CoreAffinity { 54 public: 55 CoreAffinity() = default; 56 ~CoreAffinity() = default; 57 58 int InitHardwareCoreInfo(); 59 60 int BindThreads(const std::vector<Worker *> &workers, const std::vector<int> &core_list); 61 int BindThreads(const std::vector<Worker *> &workers, BindMode bind_mode); 62 int BindProcess(BindMode bind_mode); 63 std::vector<int> GetCoreId(size_t thread_num, BindMode bind_mode) const; 64 void SetCoreId(const std::vector<int> &core_list); 65 static float GetServerFrequency(); 66 67 private: 68 #ifdef _WIN32 69 int SetAffinity(); 70 #elif defined(BIND_CORE) 71 int SetAffinity(const pthread_t &thread_id, cpu_set_t *cpu_set); 72 #endif 73 74 int InitBindCoreId(size_t thread_num, BindMode bind_mode); 75 76 int BindThreadsToCoreList(const std::vector<Worker *> &workers); 77 int FreeScheduleThreads(const std::vector<Worker *> &workers); 78 79 // bind_id contains the CPU cores to bind 80 // the size of bind_id is equal to the size of workers 81 std::vector<int> bind_id_; 82 // sorted_id contains the ordered CPU core id 83 // the size of sorted_id is equal to the size of hardware_concurrency 84 std::vector<int> sorted_id_; 85 // used to store the frequency of core 86 // the core id corresponds to the index 87 std::vector<int> core_freq_; 88 size_t core_num_{0}; 89 size_t higher_num_{0}; 90 }; 91 } // namespace mindspore 92 93 #endif // MINDSPORE_CORE_MINDRT_RUNTIME_CORE_AFFINITY_H_ 94