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.

thread_pool.h 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * Copyright 2019 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. #ifndef PREDICT_SRC_RUNTIME_THREAD_POOL_H_
  17. #define PREDICT_SRC_RUNTIME_THREAD_POOL_H_
  18. #include <mutex>
  19. #include <condition_variable>
  20. #include <thread>
  21. #include <vector>
  22. #include <string>
  23. #include <atomic>
  24. #include <memory>
  25. #include <utility>
  26. #include <functional>
  27. #include "src/runtime/runtime_api.h"
  28. namespace mindspore {
  29. namespace predict {
  30. constexpr int kSingleThreadMaxTask = 4;
  31. using TvmEnv = TVMParallelGroupEnv;
  32. using WorkFun = FTVMParallelLambda;
  33. using TaskParam = struct Param {
  34. void *cdata;
  35. int32_t taskId;
  36. TvmEnv *tvmParam;
  37. };
  38. using ThreadPoolTask = std::pair<WorkFun, TaskParam>;
  39. class LiteQueue {
  40. public:
  41. LiteQueue() = default;
  42. ~LiteQueue() = default;
  43. bool Enqueue(const ThreadPoolTask &task);
  44. bool Dequeue(ThreadPoolTask *out);
  45. std::atomic<int> taskSize{0};
  46. private:
  47. std::atomic<int> head{0};
  48. std::atomic<int> tail{0};
  49. ThreadPoolTask buffer[kSingleThreadMaxTask]{};
  50. };
  51. class LiteThreadBind {
  52. public:
  53. LiteThreadBind() = default;
  54. ~LiteThreadBind() = default;
  55. bool Bind(int numThreads, int mode);
  56. std::vector<std::pair<pthread_t, bool>> threadIdList;
  57. private:
  58. enum AffinityMode : int { BIG_CORE = 1, MID_CORE = -1, NO_BIND = 0 };
  59. void InitSortedCpuId();
  60. bool BindAllThread(bool bindFlag);
  61. bool BindMasterThread(bool bindFlag, int mode = MID_CORE);
  62. bool BindThreads(bool bindFlag);
  63. bool SetCPUBind(pthread_t threadId, const cpu_set_t &cpuSet);
  64. int bigCore{0};
  65. int midCore{0};
  66. int threadNums{0};
  67. std::vector<int> sortedCpuIds{};
  68. AffinityMode bindModel{MID_CORE};
  69. };
  70. class LiteThreadPool {
  71. public:
  72. LiteThreadPool() = default;
  73. explicit LiteThreadPool(int numThreads);
  74. ~LiteThreadPool();
  75. void AddNewThread(int newNums);
  76. bool DistributeTask(ThreadPoolTask task, int numTask);
  77. std::vector<std::thread> threadList{};
  78. private:
  79. using errCode = std::pair<bool, int>;
  80. bool AddRunReference();
  81. bool SubRunReference();
  82. bool CheckResult();
  83. int curThreadNums{0};
  84. std::vector<std::unique_ptr<LiteQueue>> queueList;
  85. std::atomic_int running{0};
  86. std::mutex tMutex;
  87. std::condition_variable queueReady;
  88. std::atomic<bool> destroy = {false};
  89. std::vector<std::pair<int, errCode>> errorInfo{};
  90. };
  91. class ThreadPool {
  92. public:
  93. static ThreadPool *GetInstance();
  94. void ConfigThreadPool(int mode, int numThreads);
  95. bool LaunchThreadPoolTask();
  96. bool AddTask(const WorkFun &worker, void *cdata, int numTask);
  97. ThreadPool(const ThreadPool &) = delete;
  98. ThreadPool &operator=(const ThreadPool &) = delete;
  99. private:
  100. ThreadPool() = default;
  101. ~ThreadPool() = default;
  102. int GetThreadNum(int numThreads);
  103. void GetThreadIdList();
  104. bool SetThreadPool(int numThreads = 1);
  105. bool SetThreadCpulBind(int mode);
  106. std::unique_ptr<LiteThreadPool> gThreadPool{nullptr};
  107. std::unique_ptr<LiteThreadBind> gThreadBind{nullptr};
  108. std::mutex gPoolMutex;
  109. int totalThreadNum{1};
  110. int bindMode{-1};
  111. };
  112. } // namespace predict
  113. } // namespace mindspore
  114. #endif // PREDICT_SRC_RUNTIME_THREAD_POOL_H_