Blender  V3.3
BLI_task.hh
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #pragma once
4 
9 #ifdef WITH_TBB
10 /* Quiet top level deprecation message, unrelated to API usage here. */
11 # if defined(WIN32) && !defined(NOMINMAX)
12 /* TBB includes Windows.h which will define min/max macros causing issues
13  * when we try to use std::min and std::max later on. */
14 # define NOMINMAX
15 # define TBB_MIN_MAX_CLEANUP
16 # endif
17 # include <tbb/blocked_range.h>
18 # include <tbb/parallel_for.h>
19 # include <tbb/parallel_for_each.h>
20 # include <tbb/parallel_invoke.h>
21 # include <tbb/parallel_reduce.h>
22 # include <tbb/task_arena.h>
23 # ifdef WIN32
24 /* We cannot keep this defined, since other parts of the code deal with this on their own, leading
25  * to multiple define warnings unless we un-define this, however we can only undefine this if we
26  * were the ones that made the definition earlier. */
27 # ifdef TBB_MIN_MAX_CLEANUP
28 # undef NOMINMAX
29 # endif
30 # endif
31 #endif
32 
33 #include "BLI_index_range.hh"
34 #include "BLI_utildefines.h"
35 
36 namespace blender::threading {
37 
38 template<typename Range, typename Function>
39 void parallel_for_each(Range &range, const Function &function)
40 {
41 #ifdef WITH_TBB
42  tbb::parallel_for_each(range, function);
43 #else
44  for (auto &value : range) {
45  function(value);
46  }
47 #endif
48 }
49 
50 template<typename Function>
51 void parallel_for(IndexRange range, int64_t grain_size, const Function &function)
52 {
53  if (range.size() == 0) {
54  return;
55  }
56 #ifdef WITH_TBB
57  /* Invoking tbb for small workloads has a large overhead. */
58  if (range.size() >= grain_size) {
60  tbb::blocked_range<int64_t>(range.first(), range.one_after_last(), grain_size),
61  [&](const tbb::blocked_range<int64_t> &subrange) {
62  function(IndexRange(subrange.begin(), subrange.size()));
63  });
64  return;
65  }
66 #else
67  UNUSED_VARS(grain_size);
68 #endif
69  function(range);
70 }
71 
72 template<typename Value, typename Function, typename Reduction>
74  int64_t grain_size,
75  const Value &identity,
76  const Function &function,
77  const Reduction &reduction)
78 {
79 #ifdef WITH_TBB
80  if (range.size() >= grain_size) {
81  return tbb::parallel_reduce(
82  tbb::blocked_range<int64_t>(range.first(), range.one_after_last(), grain_size),
83  identity,
84  [&](const tbb::blocked_range<int64_t> &subrange, const Value &ident) {
85  return function(IndexRange(subrange.begin(), subrange.size()), ident);
86  },
87  reduction);
88  }
89 #else
90  UNUSED_VARS(grain_size, reduction);
91 #endif
92  return function(range, identity);
93 }
94 
99 template<typename... Functions> void parallel_invoke(Functions &&...functions)
100 {
101 #ifdef WITH_TBB
102  tbb::parallel_invoke(std::forward<Functions>(functions)...);
103 #else
104  (functions(), ...);
105 #endif
106 }
107 
113 template<typename... Functions>
114 void parallel_invoke(const bool use_threading, Functions &&...functions)
115 {
116  if (use_threading) {
117  parallel_invoke(std::forward<Functions>(functions)...);
118  }
119  else {
120  (functions(), ...);
121  }
122 }
123 
125 template<typename Function> void isolate_task(const Function &function)
126 {
127 #ifdef WITH_TBB
128  tbb::this_task_arena::isolate(function);
129 #else
130  function();
131 #endif
132 }
133 
134 } // namespace blender::threading
#define UNUSED_VARS(...)
long functions
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Bright Control the brightness and contrast of the input color Vector Map an input vectors to used to fine tune the interpolation of the input Camera Retrieve information about the camera and how it relates to the current shading point s position Clamp a value between a minimum and a maximum Vector Perform vector math operation Invert a producing a negative Combine Generate a color from its and blue Hue Saturation Value
constexpr int64_t first() const
constexpr int64_t one_after_last() const
constexpr int64_t size() const
void isolate_task(const Function &function)
Definition: BLI_task.hh:125
void parallel_for(IndexRange range, int64_t grain_size, const Function &function)
Definition: BLI_task.hh:51
void parallel_invoke(Functions &&...functions)
Definition: BLI_task.hh:99
void parallel_invoke(const bool use_threading, Functions &&...functions)
Definition: BLI_task.hh:114
Value parallel_reduce(IndexRange range, int64_t grain_size, const Value &identity, const Function &function, const Reduction &reduction)
Definition: BLI_task.hh:73
void parallel_for_each(Range &range, const Function &function)
Definition: BLI_task.hh:39
__int64 int64_t
Definition: stdint.h:89