BLI_tasks: simplify/generalize heuristic computing default chunk size.

That code is simpler and more general (not limited to some specific
values of thread numbers). It still gives similar default chunk size as
what we had before, but handles smoother increase steps, and higher
number of threads, by keeping increasing the chunk size.

No functional change expected from that commit.
This commit is contained in:
Bastien Montagne 2019-09-18 17:35:00 +02:00
parent 84bd9e284e
commit 2409a9f0af
1 changed files with 21 additions and 15 deletions

View File

@ -1064,22 +1064,28 @@ BLI_INLINE void task_parallel_range_calc_chunk_size(const TaskParallelSettings *
chunk_size = settings->min_iter_per_thread;
}
else {
/* Basic heuristic to avoid threading on low amount of items. We could make that limit
* configurable in settings too... */
if (tot_items > 0 && tot_items < 256) {
chunk_size = tot_items;
}
/* NOTE: The idea here is to compensate for rather measurable threading
/* Multiplier used in heuristics below to define "optimal" chunk size.
* The idea here is to increase the chunk size to compensate for a rather measurable threading
* overhead caused by fetching tasks. With too many CPU threads we are starting
* to spend too much time in those overheads. */
else if (num_tasks > 32) {
chunk_size = 128;
}
else if (num_tasks > 16) {
chunk_size = 64;
}
else {
chunk_size = 32;
* to spend too much time in those overheads.
* First values are: 1 if num_tasks < 16;
* else 2 if num_tasks < 32;
* else 3 if num_tasks < 48;
* else 4 if num_tasks < 64;
* etc.
* Note: If we wanted to keep the 'power of two' multiplier, we'd need something like:
* 1 << max_ii(0, (int)(sizeof(int) * 8) - 1 - bitscan_reverse_i(num_tasks) - 3)
*/
const int num_tasks_factor = max_ii(1, num_tasks >> 3);
/* We could make that 'base' 32 number configurable in TaskParallelSettings too, or maybe just
* always use that heuristic using TaskParallelSettings.min_iter_per_thread as basis? */
chunk_size = 32 * num_tasks_factor;
/* Basic heuristic to avoid threading on low amount of items.
* We could make that limit configurable in settings too. */
if (tot_items > 0 && tot_items < max_ii(256, chunk_size * 2)) {
chunk_size = tot_items;
}
}