mirror of
https://github.com/hrydgard/ppsspp.git
synced 2026-07-11 01:25:07 +02:00
Add new minimal-overhead RunParallel function
This commit is contained in:
@@ -46,3 +46,50 @@ void ParallelRangeLoop(ThreadManager *threadMan, const std::function<void(int, i
|
||||
// NOTE: These support a max of 2GB.
|
||||
void ParallelMemcpy(ThreadManager *threadMan, void *dst, const void *src, size_t bytes, TaskPriority priority = TaskPriority::NORMAL);
|
||||
void ParallelMemset(ThreadManager *threadMan, void *dst, uint8_t value, size_t bytes, TaskPriority priority = TaskPriority::NORMAL);
|
||||
|
||||
|
||||
template<class T>
|
||||
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<class T>
|
||||
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<T>(counter, func, i, count, priority));
|
||||
}
|
||||
|
||||
return counter;
|
||||
}
|
||||
|
||||
// To wait for all tasks to finish: if (counter) counter->WaitAndRelease();
|
||||
|
||||
Reference in New Issue
Block a user