Loading...
Searching...
No Matches
opencv_parallel.hpp
Go to the documentation of this file.
209 * @param[in] nThreads Requested thread count (`< 0` resets to default, `0`/`1` disables worker threads).
241 /** @brief Leave a `ScopedBackend` scope and join idle workers when the outermost scope exits. */
405 std::condition_variable done_cv_; /**< Signals batch completion (`pending_workers_ == 0` / `!busy_`). */
406 std::vector<std::unique_ptr<Worker>> workers_; /**< Live persistent workers (`0..configured_threads_-1`). */
408 std::atomic<int> atomic_threads_{1}; /**< Mirror of `configured_threads_` for lock-free `getNumThreads()`. */
414 size_t pending_workers_ = 0; /**< Participating workers not yet checked in for `generation_`. */
419 std::atomic<int64_t> next_task_{0}; /**< Next unclaimed stripe index in `[0, current_tasks_)`. */
420 std::atomic<bool> cancel_batch_{false}; /**< Set if a callback throws outside OpenCV's wrapper. */
438 * @brief RAII guard installing the backend at startup and joining workers before `main()` exits.
std::shared_ptr< ThreadPoolBackend > backend_
Definition opencv_parallel.hpp:457
~ScopedBackend()
Exit the scope and join idle worker threads when the outermost guard ends.
Definition opencv_parallel.hpp:449
ScopedBackend(const ScopedBackend &)=delete
Noncopyable: each guard must pair one scope entry with one exit.
ScopedBackend & operator=(const ScopedBackend &)=delete
Nonassignable: a guard cannot transfer an active scope.
ScopedBackend()
Ensure the OpenCV parallel backend is registered and enter a lifetime scope.
Definition opencv_parallel.hpp:446
static int & tls_thread_num() noexcept
Access the thread-local OpenCV worker index for the current thread.
Definition opencv_parallel.hpp:268
void run_chunks(FN_parallel_for_body_cb_t body, void *data, int task_count, int divisor) noexcept
Claim and execute guided [start, end) stripe chunks until exhausted.
Definition opencv_parallel.hpp:340
int getThreadNum() const override
Return the zero-based index of the calling thread within the pool.
Definition opencv_parallel.hpp:197
std::atomic< int64_t > next_task_
Definition opencv_parallel.hpp:419
std::atomic< int > atomic_threads_
Definition opencv_parallel.hpp:408
FN_parallel_for_body_cb_t current_body_
Definition opencv_parallel.hpp:415
int setNumThreads(int nThreads) override
Set the thread team size and immediately reclaim excess idle workers.
Definition opencv_parallel.hpp:212
void leave_scope() noexcept
Leave a ScopedBackend scope and join idle workers when the outermost scope exits.
Definition opencv_parallel.hpp:242
std::vector< std::unique_ptr< Worker > > workers_
Definition opencv_parallel.hpp:406
size_t active_limit_
Definition opencv_parallel.hpp:413
void * current_data_
Definition opencv_parallel.hpp:416
std::condition_variable done_cv_
Definition opencv_parallel.hpp:405
std::condition_variable wake_cv_
Definition opencv_parallel.hpp:404
void join_extracted_workers(std::vector< std::unique_ptr< Worker > > &excess) noexcept
Wake and join workers detached by extract_excess_workers_locked.
Definition opencv_parallel.hpp:320
std::vector< std::unique_ptr< Worker > > extract_excess_workers_locked(size_t keep)
Mark workers at indices [keep, workers_.size()) for termination and detach them.
Definition opencv_parallel.hpp:302
std::exception_ptr batch_exception_
Definition opencv_parallel.hpp:421
~ThreadPoolBackend() override
Wait for any active region to finish, then stop and join all workers.
Definition opencv_parallel.hpp:74
ThreadPoolBackend()
Construct an idle backend sized to the host's default CPU budget.
Definition opencv_parallel.hpp:69
ThreadPoolBackend(const ThreadPoolBackend &)=delete
Noncopyable: workers and synchronization state belong to one backend.
ThreadPoolBackend & operator=(const ThreadPoolBackend &)=delete
Nonassignable: replacing worker state would break active region ownership.
const char * getName() const override
Return the backend identifier reported by cv::currentParallelFramework().
Definition opencv_parallel.hpp:233
int getNumThreads() const override
Return the configured thread count for OpenCV parallel regions.
Definition opencv_parallel.hpp:203
static int default_threads() noexcept
Determine the default thread count from OpenCV's CPU discovery.
Definition opencv_parallel.hpp:277
std::atomic< bool > cancel_batch_
Definition opencv_parallel.hpp:420
void parallel_for(int tasks, FN_parallel_for_body_cb_t body_callback, void *callback_data) override
Execute [0, tasks) across the configured thread team.
Definition opencv_parallel.hpp:96
int current_tasks_
Definition opencv_parallel.hpp:417
static constexpr int kMaxThreads
Upper bound on total OpenCV team size (calling thread plus workers).
Definition opencv_parallel.hpp:66
size_t scope_depth_
Definition opencv_parallel.hpp:409
static int normalize_threads(int requested) noexcept
Normalize an OpenCV thread count argument into [1, kMaxThreads].
Definition opencv_parallel.hpp:287
int configured_threads_
Definition opencv_parallel.hpp:407
void worker_loop(Worker *self) noexcept
Persistent worker main loop awaiting new batch generations or retirement.
Definition opencv_parallel.hpp:375
uint64_t generation_
Definition opencv_parallel.hpp:412
bool shutting_down_
Definition opencv_parallel.hpp:411
size_t pending_workers_
Definition opencv_parallel.hpp:414
int current_divisor_
Definition opencv_parallel.hpp:418
void enter_scope() noexcept
Record entry into a ScopedBackend lifetime scope.
Definition opencv_parallel.hpp:236
static bool start(uint32_t session)
Admit a nonzero session and discard correlation from the previous one.
Definition dipla_camera_backend.c:170
Deterministic, race-free std::thread backend for OpenCV parallel regions.
Definition opencv_parallel.hpp:53
const std::shared_ptr< ThreadPoolBackend > & ensure_installed()
Install the process-wide Sensoraia OpenCV parallel backend once.
Definition opencv_parallel.hpp:428
Per-worker state owned until after its std::thread has joined.
Definition opencv_parallel.hpp:256
unsigned slot_id
Definition opencv_parallel.hpp:259
std::thread thread
Definition opencv_parallel.hpp:257
size_t slot_index
Definition opencv_parallel.hpp:258
uint64_t seen_generation
Definition opencv_parallel.hpp:260