Blender  V2.93
BLI_task.hh
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #pragma once
18 
23 #ifdef WITH_TBB
24 /* Quiet top level deprecation message, unrelated to API usage here. */
25 # if defined(WIN32) && !defined(NOMINMAX)
26 /* TBB includes Windows.h which will define min/max macros causing issues
27  * when we try to use std::min and std::max later on. */
28 # define NOMINMAX
29 # define TBB_MIN_MAX_CLEANUP
30 # endif
31 # include <tbb/blocked_range.h>
32 # include <tbb/parallel_for.h>
33 # include <tbb/parallel_for_each.h>
34 # ifdef WIN32
35 /* We cannot keep this defined, since other parts of the code deal with this on their own, leading
36  * to multiple define warnings unless we un-define this, however we can only undefine this if we
37  * were the ones that made the definition earlier. */
38 # ifdef TBB_MIN_MAX_CLEANUP
39 # undef NOMINMAX
40 # endif
41 # endif
42 #endif
43 
44 #include "BLI_index_range.hh"
45 #include "BLI_utildefines.h"
46 
47 namespace blender {
48 
49 template<typename Range, typename Function>
50 void parallel_for_each(Range &range, const Function &function)
51 {
52 #ifdef WITH_TBB
53  tbb::parallel_for_each(range, function);
54 #else
55  for (auto &value : range) {
56  function(value);
57  }
58 #endif
59 }
60 
61 template<typename Function>
62 void parallel_for(IndexRange range, int64_t grain_size, const Function &function)
63 {
64  if (range.size() == 0) {
65  return;
66  }
67 #ifdef WITH_TBB
68  tbb::parallel_for(tbb::blocked_range<int64_t>(range.first(), range.one_after_last(), grain_size),
69  [&](const tbb::blocked_range<int64_t> &subrange) {
70  function(IndexRange(subrange.begin(), subrange.size()));
71  });
72 #else
73  UNUSED_VARS(grain_size);
74  function(range);
75 #endif
76 }
77 
78 } // namespace blender
#define UNUSED_VARS(...)
constexpr int64_t first() const
constexpr int64_t one_after_last() const
constexpr int64_t size() const
void parallel_for(IndexRange range, int64_t grain_size, const Function &function)
Definition: BLI_task.hh:62
void parallel_for_each(Range &range, const Function &function)
Definition: BLI_task.hh:50
__int64 int64_t
Definition: stdint.h:92