From 3743b5dea67c666db9c39e962830056af3a41136 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 29 Dec 2024 10:23:03 +0100 Subject: [PATCH] Add new minimal-overhead RunParallel function --- Common/Thread/ParallelLoop.h | 47 ++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/Common/Thread/ParallelLoop.h b/Common/Thread/ParallelLoop.h index 0e2d52b37b..3f599b6713 100644 --- a/Common/Thread/ParallelLoop.h +++ b/Common/Thread/ParallelLoop.h @@ -46,3 +46,50 @@ void ParallelRangeLoop(ThreadManager *threadMan, const std::function +class SimpleParallelTask : public Task { +public: + SimpleParallelTask(WaitableCounter *counter, T func, int index, int count, TaskPriority p) + : counter_(counter), func_(func), index_(index), count_(count), priority_(p) { + } + + TaskType Type() const override { + return TaskType::CPU_COMPUTE; + } + + TaskPriority Priority() const override { + return priority_; + } + + void Run() override { + func_(index_, count_); + counter_->Count(); + } + + T func_; + WaitableCounter *counter_; + + int index_; + int count_; + const TaskPriority priority_; +}; + +template +WaitableCounter *RunParallel(ThreadManager *threadMan, T func, int count, TaskPriority priority = TaskPriority::NORMAL) { + if (count == 1) { + func(0, 1); + return nullptr; + } + + WaitableCounter *counter = new WaitableCounter(count); + + for (int i = 0; i < count; i++) { + threadMan->EnqueueTaskOnThread(i, new SimpleParallelTask(counter, func, i, count, priority)); + } + + return counter; +} + +// To wait for all tasks to finish: if (counter) counter->WaitAndRelease();