iMSTK
Interactive Medical Simulation Toolkit
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
imstkParallelFor.h
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #pragma once
8 
9 #include "imstkMacros.h"
10 
11 DISABLE_WARNING_PUSH
12  DISABLE_WARNING_PADDING
13 #include <tbb/tbb.h>
14 DISABLE_WARNING_POP
15 
16 namespace imstk
17 {
18 namespace ParallelUtils
19 {
27 template<class IndexType, class Function>
28 void
29 parallelFor(const IndexType beginIdx, const IndexType endIdx, Function&& function, const bool doParallel = true)
30 {
31  if (doParallel)
32  {
33  tbb::parallel_for(tbb::blocked_range<IndexType>(beginIdx, endIdx),
34  [&](const tbb::blocked_range<IndexType>& r) {
35  for (IndexType i = r.begin(), iEnd = r.end(); i < iEnd; ++i)
36  {
37  function(i);
38  }
39  });
40  }
41  else
42  {
43  for (IndexType i = beginIdx; i < endIdx; i++)
44  {
45  function(i);
46  }
47  }
48 }
49 
53 template<class IndexType, class Function>
54 void
55 parallelFor(const IndexType endIdx, Function&& function, const bool doParallel = true)
56 {
57  parallelFor(IndexType(0), endIdx, std::forward<Function>(function), doParallel);
58 }
59 
64 template<class IndexType, class Function>
65 void
66 parallelFor2Dx(const IndexType beginX, const IndexType endX,
67  const IndexType beginY, const IndexType endY,
68  Function&& function)
69 {
70  parallelFor(beginX, endX,
71  [&](IndexType i) {
72  for (IndexType j = beginY; j < endY; ++j)
73  {
74  function(i, j);
75  }
76  });
77 }
78 
83 template<class IndexType, class Function>
84 void
85 parallelFor2Dy(const IndexType beginX, const IndexType endX,
86  const IndexType beginY, const IndexType endY,
87  Function&& function)
88 {
89  parallelFor(beginY, endY,
90  [&](IndexType j) {
91  for (IndexType i = beginX; i < endX; ++i)
92  {
93  function(i, j);
94  }
95  });
96 }
97 
102 template<class IndexType, class Function>
103 void
104 parallelFor3Dx(const IndexType beginX, const IndexType endX,
105  const IndexType beginY, const IndexType endY,
106  const IndexType beginZ, const IndexType endZ,
107  Function&& function)
108 {
109  parallelFor(beginX, endX,
110  [&](IndexType i) {
111  for (IndexType j = beginY; j < endY; ++j)
112  {
113  for (IndexType k = beginZ; k < endZ; ++k)
114  {
115  function(i, j, k);
116  }
117  }
118  });
119 }
120 
125 template<class IndexType, class Function>
126 void
127 parallelFor3Dy(const IndexType beginX, const IndexType endX,
128  const IndexType beginY, const IndexType endY,
129  const IndexType beginZ, const IndexType endZ,
130  Function&& function)
131 {
132  parallelFor(beginY, endY,
133  [&](IndexType j) {
134  for (IndexType i = beginX; i < endX; ++i)
135  {
136  for (IndexType k = beginZ; k < endZ; ++k)
137  {
138  function(i, j, k);
139  }
140  }
141  });
142 }
143 
148 template<class IndexType, class Function>
149 void
150 parallelFor3Dz(const IndexType beginX, const IndexType endX,
151  const IndexType beginY, const IndexType endY,
152  const IndexType beginZ, const IndexType endZ,
153  Function&& function)
154 {
155  parallelFor(beginX, endX,
156  [&](IndexType i) {
157  for (IndexType j = beginY; j < endY; ++j)
158  {
159  for (IndexType k = beginZ; k < endZ; ++k)
160  {
161  function(i, j, k);
162  }
163  }
164  });
165 }
166 } // end namespace ParallelUtils
167 } // end namespace imstk
Compound Geometry.