| 100 | } // anonymous namespace |
| 101 | |
| 102 | CpuDispatchChecker::TaskExecutor::TaskExecutor(TaskExecutorConfig* config) { |
| 103 | if (config != nullptr) { |
| 104 | #if MEGDNN_ENABLE_MULTI_THREADS |
| 105 | m_main_thread_affinity = false; |
| 106 | m_stop = false; |
| 107 | auto worker_threads_main_loop = [this](size_t i) { |
| 108 | if (m_cpu_ids.size() > i) |
| 109 | MEGDNN_MARK_USED_VAR(set_cpu_affinity({m_cpu_ids[i]})); |
| 110 | while (!m_stop) { |
| 111 | int index = -1; |
| 112 | if (m_workers_flag[i]->load(std::memory_order_acquire)) { |
| 113 | while ((index = m_current_task_iter.fetch_sub( |
| 114 | 1, std::memory_order_acq_rel)) && |
| 115 | index > 0) { |
| 116 | m_task(static_cast<size_t>(m_all_task_iter - index), i); |
| 117 | } |
| 118 | //! Flag worker is finished |
| 119 | m_workers_flag[i]->store(false, std::memory_order_release); |
| 120 | } |
| 121 | std::this_thread::yield(); |
| 122 | } |
| 123 | }; |
| 124 | m_nr_threads = config->nr_thread; |
| 125 | m_cpu_ids.insert( |
| 126 | m_cpu_ids.end(), config->affinity_core_set.begin(), |
| 127 | config->affinity_core_set.end()); |
| 128 | if (m_cpu_ids.empty()) { |
| 129 | megdnn_log_warn("Thread affinity was not set."); |
| 130 | } else { |
| 131 | megdnn_assert( |
| 132 | m_cpu_ids.size() <= get_cpu_count(), |
| 133 | "The input affinity_core_set size exceed the " |
| 134 | "number of CPU cores, got: %zu cpu_count: %zu.", |
| 135 | m_cpu_ids.size(), get_cpu_count()); |
| 136 | } |
| 137 | for (size_t i = 0; i < m_nr_threads - 1; i++) { |
| 138 | m_workers_flag.emplace_back(new std::atomic_bool{false}); |
| 139 | m_workers.emplace_back(std::bind(worker_threads_main_loop, i)); |
| 140 | } |
| 141 | #else |
| 142 | megdnn_throw( |
| 143 | "Try to use multithreading with " |
| 144 | "\'MEGDNN_ENABLE_MULTI_THREADS\' set to 0."); |
| 145 | #endif |
| 146 | } else { |
| 147 | m_nr_threads = 1; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void CpuDispatchChecker::TaskExecutor::add_task( |
| 152 | const MultiThreadingTask& task, size_t parallelism) { |
nothing calls this directly
no test coverage detected