Blender V5.3
BLI_task.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
10
11#ifdef WITH_TBB
12/* Quiet top level deprecation message, unrelated to API usage here. */
13# include <tbb/blocked_range.h>
14# include <tbb/parallel_for.h>
15# include <tbb/parallel_for_each.h>
16# include <tbb/parallel_invoke.h>
17# include <tbb/parallel_reduce.h>
18# include <tbb/task_arena.h>
19#endif
20
21#include "BLI_function_ref.hh"
22#include "BLI_index_range.hh"
23#include "BLI_lazy_threading.hh"
25
26namespace blender {
27
28namespace threading {
29
30template<typename Range, typename Function>
31inline void parallel_for_each(Range &&range, const Function &function)
32{
33#ifdef WITH_TBB
34 tbb::parallel_for_each(range, function);
35#else
36 for (auto &&value : range) {
37 function(value);
38 }
39#endif
40}
41
42namespace detail {
44 int64_t grain_size,
45 FunctionRef<void(IndexRange)> function,
46 const TaskSizeHints &size_hints);
48void max_threads_task_impl(int max_threads, FunctionRef<void()> function);
49} // namespace detail
50
68template<typename Function>
69inline void parallel_for(const IndexRange range,
70 const int64_t grain_size,
71 const Function &function,
72 const TaskSizeHints &size_hints = detail::TaskSizeHints_Static(1))
73{
74 if (range.is_empty()) {
75 return;
76 }
77 /* Invoking tbb for small workloads has a large overhead. */
78 if (use_single_thread(size_hints, range, grain_size)) {
79 function(range);
80 return;
81 }
82 detail::parallel_for_impl(range, grain_size, function, size_hints);
83}
84
89inline IndexRange align_sub_range(const IndexRange unaligned_range,
90 const int64_t alignment,
91 const IndexRange global_range)
92{
93 const int64_t global_begin = global_range.start();
94 const int64_t global_end = global_range.one_after_last();
95 const int64_t alignment_mask = ~(alignment - 1);
96
97 const int64_t unaligned_begin = unaligned_range.start();
98 const int64_t unaligned_end = unaligned_range.one_after_last();
99 const int64_t aligned_begin = std::max(global_begin, unaligned_begin & alignment_mask);
100 const int64_t aligned_end = unaligned_end == global_end ?
101 unaligned_end :
102 std::max(global_begin, unaligned_end & alignment_mask);
103 const IndexRange aligned_range = IndexRange::from_begin_end(aligned_begin, aligned_end);
104 return aligned_range;
105}
106
114template<typename Function>
115inline void parallel_for_aligned(const IndexRange range,
116 const int64_t grain_size,
117 const int64_t alignment,
118 const Function &function)
119{
120 parallel_for(range, grain_size, [&](const IndexRange unaligned_range) {
121 const IndexRange aligned_range = align_sub_range(unaligned_range, alignment, range);
122 function(aligned_range);
123 });
124}
125
126template<typename Value, typename Function, typename Reduction>
127inline Value parallel_reduce(IndexRange range,
128 int64_t grain_size,
129 const Value &identity,
130 const Function &function,
131 const Reduction &reduction)
132{
133#ifdef WITH_TBB
134 if (range.size() >= grain_size) {
136 return tbb::parallel_reduce(
137 tbb::blocked_range<int64_t>(range.first(), range.one_after_last(), grain_size),
138 identity,
139 [&](const tbb::blocked_range<int64_t> &subrange, const Value &ident) {
140 return function(IndexRange(subrange.begin(), subrange.size()), ident);
141 },
142 reduction);
143 }
144#else
145 UNUSED_VARS(grain_size, reduction);
146#endif
147 return function(range, identity);
148}
149
150template<typename Value, typename Function, typename Reduction>
151inline Value parallel_reduce_aligned(const IndexRange range,
152 const int64_t grain_size,
153 const int64_t alignment,
154 const Value &identity,
155 const Function &function,
156 const Reduction &reduction)
157{
159 range,
160 grain_size,
161 identity,
162 [&](const IndexRange unaligned_range, const Value &ident) {
163 const IndexRange aligned_range = align_sub_range(unaligned_range, alignment, range);
164 function(aligned_range, ident);
165 },
166 reduction);
167}
168
169template<typename Value, typename Function, typename Reduction>
171 int64_t grain_size,
172 const Value &identity,
173 const Function &function,
174 const Reduction &reduction)
175{
176#ifdef WITH_TBB
177 if (range.size() >= grain_size) {
179 return tbb::parallel_deterministic_reduce(
180 tbb::blocked_range<int64_t>(range.first(), range.one_after_last(), grain_size),
181 identity,
182 [&](const tbb::blocked_range<int64_t> &subrange, const Value &ident) {
183 return function(IndexRange(subrange.begin(), subrange.size()), ident);
184 },
185 reduction);
186 }
187#else
188 UNUSED_VARS(grain_size, reduction);
189#endif
190 return function(range, identity);
191}
192
197template<typename... Functions> inline void parallel_invoke(Functions &&...functions)
198{
199#ifdef WITH_TBB
200 tbb::parallel_invoke(std::forward<Functions>(functions)...);
201#else
202 (functions(), ...);
203#endif
204}
205
211template<typename... Functions>
212inline void parallel_invoke(const bool use_threading, Functions &&...functions)
213{
214 if (use_threading) {
216 parallel_invoke(std::forward<Functions>(functions)...);
217 }
218 else {
219 (functions(), ...);
220 }
221}
222
224template<typename Function> inline auto isolate_task(const Function &function)
225{
226#ifdef WITH_TBB
228 return tbb::this_task_arena::isolate(function);
229#else
230 return function();
231#endif
232}
233
238template<typename Function>
239inline void max_threads_task(const int max_threads, const Function &function)
240{
241 detail::max_threads_task_impl(max_threads, function);
242}
243
250template<typename Function>
251inline void memory_bandwidth_bound_task(const int64_t approximate_bytes_touched,
252 const Function &function)
253{
254 /* Don't limit threading when all touched memory can stay in the CPU cache, because there a much
255 * higher memory bandwidth is available compared to accessing RAM. This value is supposed to be
256 * on the order of the L3 cache size. Accessing that value is not quite straight forward and even
257 * if it was, it's not clear if using the exact cache size would be beneficial because there is
258 * often more stuff going on the CPU at the same time. */
259 if (approximate_bytes_touched <= 8 * 1024 * 1024) {
260 function();
261 return;
262 }
264}
265
266} // namespace threading
267} // namespace blender
#define UNUSED_VARS(...)
constexpr int64_t first() const
constexpr int64_t one_after_last() const
constexpr int64_t size() const
constexpr bool is_empty() const
static constexpr IndexRange from_begin_end(const int64_t begin, const int64_t end)
constexpr int64_t start() const
void parallel_for_impl(IndexRange range, int64_t grain_size, FunctionRef< void(IndexRange)> function, const TaskSizeHints &size_hints)
void max_threads_task_impl(int max_threads, FunctionRef< void()> function)
void memory_bandwidth_bound_task_impl(FunctionRef< void()> function)
void parallel_invoke(Functions &&...functions)
Definition BLI_task.hh:197
void parallel_for_each(Range &&range, const Function &function)
Definition BLI_task.hh:31
void max_threads_task(const int max_threads, const Function &function)
Definition BLI_task.hh:239
Value parallel_deterministic_reduce(IndexRange range, int64_t grain_size, const Value &identity, const Function &function, const Reduction &reduction)
Definition BLI_task.hh:170
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
bool use_single_thread(const TaskSizeHints &size_hints, const IndexRange range, const int64_t threshold)
void memory_bandwidth_bound_task(const int64_t approximate_bytes_touched, const Function &function)
Definition BLI_task.hh:251
void parallel_for_aligned(const IndexRange range, const int64_t grain_size, const int64_t alignment, const Function &function)
Definition BLI_task.hh:115
auto isolate_task(const Function &function)
Definition BLI_task.hh:224
Value parallel_reduce(IndexRange range, int64_t grain_size, const Value &identity, const Function &function, const Reduction &reduction)
Definition BLI_task.hh:127
Value parallel_reduce_aligned(const IndexRange range, const int64_t grain_size, const int64_t alignment, const Value &identity, const Function &function, const Reduction &reduction)
Definition BLI_task.hh:151
IndexRange align_sub_range(const IndexRange unaligned_range, const int64_t alignment, const IndexRange global_range)
Definition BLI_task.hh:89
BMesh Iterators.
__int64 int64_t
Definition BKE_text.h:90