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 3.5 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 "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. const size_t kDeviceNum = 8;
  26. #endif
  27. const size_t kMaxThreadNum = 23;
  28. ThreadPool::ThreadPool() {
  29. size_t process_core_num = std::thread::hardware_concurrency() - 1;
  30. if (process_core_num < 1) {
  31. process_core_num = 1;
  32. }
  33. #if ENABLE_D || ENABLE_GPU
  34. max_thread_num_ = process_core_num / kDeviceNum;
  35. #else
  36. max_thread_num_ = process_core_num;
  37. #endif
  38. if (max_thread_num_ < 1) {
  39. max_thread_num_ = 1;
  40. }
  41. if (max_thread_num_ > kMaxThreadNum) {
  42. max_thread_num_ = kMaxThreadNum;
  43. }
  44. }
  45. void ThreadPool::SyncRunLoop() {
  46. while (true) {
  47. Task task;
  48. {
  49. std::unique_lock<std::mutex> lock(task_mutex_);
  50. task_cond_var_.wait(lock, [this] { return !task_queue_.empty() || exit_run_; });
  51. if (exit_run_) {
  52. return;
  53. }
  54. task = task_queue_.front();
  55. task_queue_.pop();
  56. }
  57. try {
  58. task();
  59. } catch (std::exception &e) {
  60. MsException::Instance().SetException();
  61. }
  62. {
  63. std::unique_lock<std::mutex> task_lock(task_mutex_);
  64. task_finished_count_ = task_finished_count_ + 1;
  65. }
  66. finished_cond_var_.notify_one();
  67. }
  68. }
  69. bool ThreadPool::SyncRun(const std::vector<Task> &tasks) {
  70. if (tasks.size() == 1) {
  71. auto ret = tasks[0]();
  72. return ret == SUCCESS;
  73. }
  74. std::unique_lock<std::mutex> lock(pool_mtx_);
  75. exit_run_ = false;
  76. size_t task_num = tasks.size();
  77. size_t thread_num = sync_run_threads_.size();
  78. if (thread_num < max_thread_num_ && thread_num < task_num) {
  79. auto new_thread_num = max_thread_num_;
  80. if (task_num < max_thread_num_) {
  81. new_thread_num = task_num;
  82. }
  83. for (size_t i = thread_num; i < new_thread_num; ++i) {
  84. sync_run_threads_.emplace_back(std::thread(&ThreadPool::SyncRunLoop, this));
  85. }
  86. }
  87. for (auto &task : tasks) {
  88. std::lock_guard<std::mutex> task_lock(task_mutex_);
  89. task_queue_.push(task);
  90. task_cond_var_.notify_one();
  91. }
  92. {
  93. std::unique_lock<std::mutex> task_lock(task_mutex_);
  94. finished_cond_var_.wait(task_lock, [this, task_num] { return task_num == task_finished_count_; });
  95. task_finished_count_ = 0;
  96. }
  97. return true;
  98. }
  99. ThreadPool &ThreadPool::GetInstance() {
  100. static ThreadPool instance{};
  101. return instance;
  102. }
  103. void ThreadPool::ClearThreadPool() {
  104. std::lock_guard<std::mutex> sync_run_lock(pool_mtx_);
  105. if (exit_run_) {
  106. return;
  107. }
  108. exit_run_ = true;
  109. task_cond_var_.notify_all();
  110. for (auto &it : sync_run_threads_) {
  111. if (it.joinable()) {
  112. it.join();
  113. }
  114. }
  115. sync_run_threads_.clear();
  116. }
  117. ThreadPool::~ThreadPool() {
  118. try {
  119. ClearThreadPool();
  120. } catch (...) {
  121. }
  122. }
  123. } // namespace common
  124. } // namespace mindspore