|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef OPENCV_GAPI_TBB_EXECUTOR_HPP
|
|
|
#define OPENCV_GAPI_TBB_EXECUTOR_HPP
|
|
|
|
|
|
#if !defined(GAPI_STANDALONE)
|
|
|
#include <opencv2/cvconfig.h>
|
|
|
#endif
|
|
|
|
|
|
#ifdef HAVE_TBB
|
|
|
#ifndef TBB_SUPPRESS_DEPRECATED_MESSAGES
|
|
|
#define TBB_SUPPRESS_DEPRECATED_MESSAGES 1
|
|
|
#endif
|
|
|
#include <tbb/tbb.h>
|
|
|
#include <tbb/task.h>
|
|
|
#if TBB_INTERFACE_VERSION < 12000
|
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
#include <vector>
|
|
|
#include <functional>
|
|
|
#include <iosfwd>
|
|
|
|
|
|
#include <tbb/concurrent_priority_queue.h>
|
|
|
#include <tbb/task_arena.h>
|
|
|
|
|
|
#include <opencv2/gapi/util/variant.hpp>
|
|
|
|
|
|
namespace cv { namespace gimpl { namespace parallel {
|
|
|
|
|
|
|
|
|
template<typename count_t>
|
|
|
struct atomic_copyable_wrapper {
|
|
|
std::atomic<count_t> value;
|
|
|
|
|
|
atomic_copyable_wrapper(count_t val) : value(val) {}
|
|
|
atomic_copyable_wrapper(atomic_copyable_wrapper const& lhs) : value (lhs.value.load(std::memory_order_relaxed)) {}
|
|
|
|
|
|
atomic_copyable_wrapper& operator=(count_t val) {
|
|
|
value.store(val, std::memory_order_relaxed);
|
|
|
return *this;
|
|
|
}
|
|
|
|
|
|
count_t fetch_sub(count_t val) {
|
|
|
return value.fetch_sub(val);
|
|
|
}
|
|
|
|
|
|
count_t fetch_add(count_t val) {
|
|
|
return value.fetch_add(val);
|
|
|
}
|
|
|
};
|
|
|
|
|
|
struct async_tag {};
|
|
|
constexpr async_tag async;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct tile_node {
|
|
|
|
|
|
|
|
|
size_t total_order_index = 0;
|
|
|
|
|
|
|
|
|
struct sync_task_body {
|
|
|
std::function<void()> body;
|
|
|
};
|
|
|
struct async_task_body {
|
|
|
std::function<void(std::function<void()>&& callback, size_t total_order_index)> body;
|
|
|
};
|
|
|
|
|
|
util::variant<sync_task_body, async_task_body> task_body;
|
|
|
|
|
|
|
|
|
size_t dependencies = 0;
|
|
|
|
|
|
|
|
|
|
|
|
atomic_copyable_wrapper<size_t> dependency_count = 0;
|
|
|
|
|
|
std::vector<tile_node*> dependants;
|
|
|
|
|
|
tile_node(decltype(sync_task_body::body)&& f) : task_body(sync_task_body{std::move(f)}) {}
|
|
|
tile_node(async_tag, decltype(async_task_body::body)&& f) : task_body(async_task_body{std::move(f)}) {}
|
|
|
};
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& o, tile_node const& n);
|
|
|
|
|
|
struct tile_node_indirect_priority_comparator {
|
|
|
bool operator()(tile_node const * lhs, tile_node const * rhs) const {
|
|
|
return lhs->total_order_index > rhs->total_order_index;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
using prio_items_queue_t = tbb::concurrent_priority_queue<tile_node*, tile_node_indirect_priority_comparator>;
|
|
|
|
|
|
void execute(prio_items_queue_t& q);
|
|
|
void execute(prio_items_queue_t& q, tbb::task_arena& arena);
|
|
|
|
|
|
}}}
|
|
|
|
|
|
#endif
|
|
|
#endif
|
|
|
|
|
|
#endif
|
|
|
|