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.cc 4.4 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * Copyright 2020 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 "include/common/thread_pool.h"
  17. #include <algorithm>
  18. #include <exception>
  19. #include "utils/log_adapter.h"
  20. #include "utils/convert_utils_base.h"
  21. #include "utils/ms_exception.h"
  22. namespace mindspore {
  23. namespace common {
  24. #if ENABLE_D || ENABLE_GPU
  25. constexpr size_t kDeviceNum = 8;
  26. #endif
  27. constexpr size_t kMaxThreadNum = 23;
  28. constexpr size_t kYieldThreshold = 1000;
  29. ThreadPool::ThreadPool() {
  30. size_t process_core_num = std::thread::hardware_concurrency() - 1;
  31. if (process_core_num < 1) {
  32. process_core_num = 1;
  33. }
  34. #if ENABLE_D || ENABLE_GPU
  35. max_thread_num_ = process_core_num / kDeviceNum;
  36. #else
  37. max_thread_num_ = process_core_num;
  38. #endif
  39. if (max_thread_num_ < 1) {
  40. max_thread_num_ = 1;
  41. }
  42. if (max_thread_num_ > kMaxThreadNum) {
  43. max_thread_num_ = kMaxThreadNum;
  44. }
  45. }
  46. void ThreadPool::SyncRunLoop(const std::shared_ptr<ThreadContext> &context) {
  47. if (context == nullptr) {
  48. return;
  49. }
  50. size_t yield_count = 0;
  51. while (true) {
  52. if (exit_run_) {
  53. return;
  54. }
  55. if (!context->task) {
  56. ++yield_count;
  57. if (yield_count > kYieldThreshold) {
  58. yield_count = 0;
  59. std::unique_lock<std::mutex> lock(context->mutex);
  60. context->cond_var.wait(lock, [&context, this] { return context->task != nullptr || exit_run_; });
  61. } else {
  62. std::this_thread::yield();
  63. continue;
  64. }
  65. }
  66. if (exit_run_) {
  67. return;
  68. }
  69. try {
  70. auto &task = *(context->task);
  71. task();
  72. } catch (std::exception &e) {
  73. MsException::Instance().SetException();
  74. }
  75. yield_count = 0;
  76. context->task = nullptr;
  77. }
  78. }
  79. bool ThreadPool::SyncRun(const std::vector<Task> &tasks) {
  80. if (tasks.empty()) {
  81. return true;
  82. }
  83. if (tasks.size() == 1) {
  84. auto ret = tasks[0]();
  85. return ret == SUCCESS;
  86. }
  87. std::unique_lock<std::mutex> lock(pool_mtx_);
  88. exit_run_ = false;
  89. size_t task_num = tasks.size();
  90. size_t thread_num = sync_run_threads_.size();
  91. if (thread_num < max_thread_num_ && thread_num < task_num) {
  92. auto new_thread_num = max_thread_num_;
  93. if (task_num < max_thread_num_) {
  94. new_thread_num = task_num;
  95. }
  96. contexts_.resize(new_thread_num);
  97. for (size_t i = thread_num; i < new_thread_num; ++i) {
  98. contexts_[i] = std::make_shared<ThreadContext>();
  99. sync_run_threads_.emplace_back(std::thread(&ThreadPool::SyncRunLoop, this, contexts_[i]));
  100. }
  101. }
  102. if (contexts_.empty()) {
  103. return true;
  104. }
  105. size_t used_thread_num = contexts_.size();
  106. if (task_num < used_thread_num) {
  107. used_thread_num = task_num;
  108. }
  109. bool running = true;
  110. size_t task_index = 0;
  111. while (running) {
  112. running = false;
  113. for (size_t i = 0; i < used_thread_num; ++i) {
  114. MS_EXCEPTION_IF_NULL(contexts_[i]);
  115. auto &task_run = contexts_[i]->task;
  116. if (task_run) {
  117. running = true;
  118. } else if (task_index < task_num) {
  119. std::lock_guard<std::mutex> task_lock(contexts_[i]->mutex);
  120. contexts_[i]->task = &(tasks[task_index]);
  121. contexts_[i]->cond_var.notify_one();
  122. running = true;
  123. ++task_index;
  124. }
  125. }
  126. if (running) {
  127. std::this_thread::yield();
  128. }
  129. }
  130. return true;
  131. }
  132. ThreadPool &ThreadPool::GetInstance() {
  133. static ThreadPool instance{};
  134. return instance;
  135. }
  136. void ThreadPool::ClearThreadPool() {
  137. std::lock_guard<std::mutex> sync_run_lock(pool_mtx_);
  138. if (exit_run_) {
  139. return;
  140. }
  141. exit_run_ = true;
  142. for (auto &context : contexts_) {
  143. MS_EXCEPTION_IF_NULL(context);
  144. context->cond_var.notify_one();
  145. }
  146. for (auto &it : sync_run_threads_) {
  147. if (it.joinable()) {
  148. it.join();
  149. }
  150. }
  151. sync_run_threads_.clear();
  152. }
  153. ThreadPool::~ThreadPool() {
  154. try {
  155. ClearThreadPool();
  156. } catch (...) {
  157. // exit
  158. }
  159. }
  160. } // namespace common
  161. } // namespace mindspore