mirror of
https://github.com/opencv/opencv.git
synced 2026-09-25 04:09:57 +03:00
G-API: Implement concurrent executor #24845 ## Overview This PR introduces the new G-API executor called `GThreadedExecutor` which can be selected when the `GComputation` is compiled in `serial` mode (a.k.a `GComputation::compile(...)`) ### ThreadPool `cv::gapi::own::ThreadPool` has been introduced in order to abstract usage of threads in `GThreadedExecutor`. `ThreadPool` is implemented by using `own::concurrent_bounded_queue` `ThreadPool` has only as single method `schedule` that will push task into the queue for the further execution. The **important** notice is that if `Task` executed in `ThreadPool` throws exception - this is `UB`. ### GThreadedExecutor The `GThreadedExecutor` is mostly copy-paste of `GExecutor`, should we extend `GExecutor` instead? #### Implementation details 1. Build the dependency graph for `Island` nodes. 2. Store the tasks that don't have dependencies into separate `vector` in order to run them first. 3. at the `GThreadedExecutor::run()` schedule the tasks that don't have dependencies that will schedule their dependents and wait for the completion. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [ ] I agree to contribute to the project under Apache 2 License. - [ ] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [ ] The PR is proposed to the proper branch - [ ] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake
68 lines
1.7 KiB
C++
68 lines
1.7 KiB
C++
// This file is part of OpenCV project.
|
|
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
|
// of this distribution and at http://opencv.org/license.html.
|
|
//
|
|
// Copyright (C) 2024 Intel Corporation
|
|
|
|
|
|
#include "thread_pool.hpp"
|
|
|
|
#include <opencv2/gapi/util/throw.hpp>
|
|
|
|
cv::gapi::own::Latch::Latch(const uint64_t expected)
|
|
: m_expected(expected) {
|
|
}
|
|
|
|
void cv::gapi::own::Latch::count_down() {
|
|
std::lock_guard<std::mutex> lk{m_mutex};
|
|
--m_expected;
|
|
if (m_expected == 0) {
|
|
m_all_done.notify_all();
|
|
}
|
|
}
|
|
|
|
void cv::gapi::own::Latch::wait() {
|
|
std::unique_lock<std::mutex> lk{m_mutex};
|
|
while (m_expected != 0u) {
|
|
m_all_done.wait(lk);
|
|
}
|
|
}
|
|
|
|
cv::gapi::own::ThreadPool::ThreadPool(const uint32_t num_workers) {
|
|
m_workers.reserve(num_workers);
|
|
for (uint32_t i = 0; i < num_workers; ++i) {
|
|
m_workers.emplace_back(
|
|
cv::gapi::own::ThreadPool::worker, std::ref(m_queue));
|
|
}
|
|
}
|
|
|
|
void cv::gapi::own::ThreadPool::worker(QueueClass<Task>& queue) {
|
|
while (true) {
|
|
cv::gapi::own::ThreadPool::Task task;
|
|
queue.pop(task);
|
|
if (!task) {
|
|
break;
|
|
}
|
|
task();
|
|
}
|
|
}
|
|
|
|
void cv::gapi::own::ThreadPool::schedule(cv::gapi::own::ThreadPool::Task&& task) {
|
|
m_queue.push(std::move(task));
|
|
};
|
|
|
|
void cv::gapi::own::ThreadPool::shutdown() {
|
|
for (size_t i = 0; i < m_workers.size(); ++i) {
|
|
// NB: Empty task - is an indicator for workers to stop their loops
|
|
m_queue.push({});
|
|
}
|
|
for (auto& worker : m_workers) {
|
|
worker.join();
|
|
}
|
|
m_workers.clear();
|
|
}
|
|
|
|
cv::gapi::own::ThreadPool::~ThreadPool() {
|
|
shutdown();
|
|
}
|