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 2.2 kB

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 <atomic>
  18. #include <functional>
  19. #include <queue>
  20. #include <stdexcept>
  21. #include <utility>
  22. #include <vector>
  23. #include "register/register_types.h"
  24. namespace ge {
  25. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ThreadPool::ThreadPool(uint32_t size) : is_stoped_(false) {
  26. idle_thrd_num_ = size < 1 ? 1 : size;
  27. for (uint32_t i = 0; i < idle_thrd_num_; ++i) {
  28. pool_.emplace_back(ThreadFunc, this);
  29. }
  30. }
  31. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ThreadPool::~ThreadPool() {
  32. is_stoped_.store(true);
  33. {
  34. std::unique_lock<std::mutex> lock{m_lock_};
  35. cond_var_.notify_all();
  36. }
  37. for (std::thread &thd : pool_) {
  38. if (thd.joinable()) {
  39. try {
  40. thd.join();
  41. } catch (const std::system_error &) {
  42. GELOGW("system_error");
  43. } catch (...) {
  44. GELOGW("exception");
  45. }
  46. }
  47. }
  48. }
  49. void ThreadPool::ThreadFunc(ThreadPool *thread_pool) {
  50. if (thread_pool == nullptr) {
  51. return;
  52. }
  53. while (!thread_pool->is_stoped_) {
  54. std::function<void()> task;
  55. {
  56. std::unique_lock<std::mutex> lock{thread_pool->m_lock_};
  57. thread_pool->cond_var_.wait(
  58. lock, [thread_pool] { return thread_pool->is_stoped_.load() || !thread_pool->tasks_.empty(); });
  59. if (thread_pool->is_stoped_ && thread_pool->tasks_.empty()) {
  60. return;
  61. }
  62. task = std::move(thread_pool->tasks_.front());
  63. thread_pool->tasks_.pop();
  64. }
  65. --thread_pool->idle_thrd_num_;
  66. task();
  67. ++thread_pool->idle_thrd_num_;
  68. }
  69. }
  70. } // namespace ge

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示