Allow specifying a cap on the number of threads used in ParallelLoop

This commit is contained in:
Henrik Rydgård committed 2026-10-09 14:33:58 +02:00
1 parent 88f9379376
commit 831acb099e
11 files changed
+58 -28

No files matched your search

+9 -5
View File
@@ -41,12 +41,15 @@ public:
const TaskPriority priority_;
};
WaitableCounter *ParallelRangeLoopWaitable(ThreadManager *threadMan, const std::function<void(int, int)> &loop, int lower, int upper, int minSize, TaskPriority priority) {
WaitableCounter *ParallelRangeLoopWaitable(ThreadManager *threadMan, const std::function<void(int, int)> &loop, int lower, int upper, int minSize, int maxThreads, TaskPriority priority) {
if (minSize == -1) {
minSize = 1;
}
const int numLooperTasks = threadMan->GetNumLooperThreads();
int numLooperTasks = threadMan->GetNumLooperThreads();
if (maxThreads > 0) {
numLooperTasks = std::min(maxThreads, numLooperTasks);
}
int range = upper - lower;
if (range <= 0) {
// Nothing to do. A finished counter allocated to keep the API.
@@ -106,7 +109,7 @@ WaitableCounter *ParallelRangeLoopWaitable(ThreadManager *threadMan, const std::
}
}
void ParallelRangeLoop(ThreadManager *threadMan, const std::function<void(int, int)> &loop, int lower, int upper, int minSize, TaskPriority priority) {
void ParallelRangeLoop(ThreadManager *threadMan, const std::function<void(int, int)> &loop, int lower, int upper, int minSize, int maxThreads, TaskPriority priority) {
if (cpu_info.num_cores == 1 || (minSize >= (upper - lower) && upper > lower)) {
// "Optimization" for single-core devices, or minSize larger than the range.
// No point in adding threading overhead, let's just do it inline (since this is the blocking variant).
@@ -119,7 +122,7 @@ void ParallelRangeLoop(ThreadManager *threadMan, const std::function<void(int, i
minSize = 1;
}
WaitableCounter *counter = ParallelRangeLoopWaitable(threadMan, loop, lower, upper, minSize, priority);
WaitableCounter *counter = ParallelRangeLoopWaitable(threadMan, loop, lower, upper, minSize, maxThreads, priority);
// TODO: Optimize using minSize. We'll just compute whether there's a remainer, remove it from the call to ParallelRangeLoopWaitable,
// and process the remainder right here. If there's no remainer, we'll steal a whole chunk.
if (counter) {
@@ -139,10 +142,11 @@ void ParallelMemcpy(ThreadManager *threadMan, void *dst, const void *src, size_t
// unknown's testing showed that 128kB is an appropriate minimum size.
// That was a long time ago though. raised the limits.
// Capping the number of threads at 4, more than that is probably rarely useful.
char *d = (char *)dst;
const char *s = (const char *)src;
ParallelRangeLoop(threadMan, [d, s](int l, int h) {
memmove(d + l, s + l, h - l);
}, 0, (int)bytes, blocksize, priority);
}, 0, (int)bytes, blocksize, 4, priority);
}