IterationUtils-Impl.hpp
Go to the documentation of this file.
1 // This code is based on Jet framework.
2 // Copyright (c) 2018 Doyub Kim
3 // CubbyFlow is voxel-based fluid simulation engine for computer games.
4 // Copyright (c) 2020 CubbyFlow Team
5 // Core Part: Chris Ohk, Junwoo Hwang, Jihong Sin, Seungwoo Yoo
6 // AI Part: Dongheon Cho, Minseo Kim
7 // We are making my contributions/submissions to this project solely in our
8 // personal capacity and are not conveying any rights to any intellectual
9 // property of any third parties.
10 
11 #ifndef CUBBYFLOW_ITERATION_UTILS_IMPL_HPP
12 #define CUBBYFLOW_ITERATION_UTILS_IMPL_HPP
13 
14 namespace CubbyFlow
15 {
16 namespace Internal
17 {
18 template <typename IndexType, size_t N, size_t I>
20 {
21  template <typename Func, typename... RemainingIndices>
22  static void Call(const Vector<IndexType, N>& begin,
23  const Vector<IndexType, N>& end, const Func& func,
24  RemainingIndices... indices)
25  {
26  for (IndexType i = begin[I - 1]; i < end[I - 1]; ++i)
27  {
28  ForEachIndex<IndexType, N, I - 1>::Call(begin, end, func, i,
29  indices...);
30  }
31  }
32 };
33 
34 template <typename IndexType, size_t N>
35 struct ForEachIndex<IndexType, N, 1>
36 {
37  template <typename Func, typename... RemainingIndices>
38  static void Call(const Vector<IndexType, N>& begin,
39  const Vector<IndexType, N>& end, const Func& func,
40  RemainingIndices... indices)
41  {
42  for (IndexType i = begin[0]; i < end[0]; ++i)
43  {
44  func(i, indices...);
45  }
46  }
47 };
48 } // namespace Internal
49 
50 template <typename IndexType, size_t N, typename Func>
52  const Vector<IndexType, N>& end, const Func& func)
53 {
54  for (IndexType i = begin[N - 1]; i < end[N - 1]; ++i)
55  {
57  }
58 }
59 
60 template <typename IndexType, typename Func>
62  const Vector<IndexType, 1>& end, const Func& func)
63 {
64  for (IndexType i = begin[0]; i < end[0]; ++i)
65  {
66  func(i);
67  }
68 }
69 
70 template <typename IndexType, typename Func>
71 void ForEachIndex(IndexType begin, IndexType end, const Func& func)
72 {
73  for (IndexType i = begin; i < end; ++i)
74  {
75  func(i);
76  }
77 }
78 
79 template <typename IndexType, size_t N, typename Func>
80 void ForEachIndex(const Vector<IndexType, N>& size, const Func& func)
81 {
82  ForEachIndex(Vector<IndexType, N>{}, size, func);
83 }
84 
85 template <typename IndexType, typename Func>
86 void ForEachIndex(const Vector<IndexType, 1>& size, const Func& func)
87 {
88  ForEachIndex(Vector<IndexType, 1>{}, size, func);
89 }
90 
91 template <typename IndexType, typename Func>
92 void ForEachIndex(IndexType size, const Func& func)
93 {
94  ForEachIndex(IndexType{}, size, func);
95 }
96 
97 template <typename IndexType, size_t N, typename Func>
99  const Vector<IndexType, N>& end, const Func& func,
100  ExecutionPolicy policy)
101 {
102  ParallelFor(
103  begin[N - 1], end[N - 1],
104  [&](IndexType i) {
106  i);
107  },
108  policy);
109 }
110 
111 template <typename IndexType, typename Func>
113  const Vector<IndexType, 1>& end, const Func& func,
114  ExecutionPolicy policy)
115 {
116  ParallelFor(begin[0], end[0], func, policy);
117 }
118 
119 template <typename IndexType, typename Func>
120 void ParallelForEachIndex(IndexType begin, IndexType end, const Func& func,
121  ExecutionPolicy policy)
122 {
123  ParallelFor(begin, end, func, policy);
124 }
125 
126 template <typename IndexType, size_t N, typename Func>
127 void ParallelForEachIndex(const Vector<IndexType, N>& size, const Func& func,
128  ExecutionPolicy policy)
129 {
130  ParallelForEachIndex(Vector<IndexType, N>{}, size, func, policy);
131 }
132 
133 template <typename IndexType, typename Func>
134 void ParallelForEachIndex(const Vector<IndexType, 1>& size, const Func& func,
135  ExecutionPolicy policy)
136 {
137  ParallelForEachIndex(Vector<IndexType, 1>{}, size, func, policy);
138 }
139 
140 template <typename IndexType, typename Func>
141 void ParallelForEachIndex(IndexType size, const Func& func,
142  ExecutionPolicy policy)
143 {
144  ParallelForEachIndex(IndexType{}, size, func, policy);
145 }
146 } // namespace CubbyFlow
147 
148 #endif
Definition: IterationUtils-Impl.hpp:19
static void Call(const Vector< IndexType, N > &begin, const Vector< IndexType, N > &end, const Func &func, RemainingIndices... indices)
Definition: IterationUtils-Impl.hpp:22
Definition: Matrix.hpp:27
Definition: pybind11Utils.hpp:20
void ParallelFor(IndexType beginIndex, IndexType endIndex, const Function &function, ExecutionPolicy policy)
Makes a for-loop from beginIndex to endIndex in parallel.
Definition: Parallel-Impl.hpp:212
void ParallelForEachIndex(const Vector< IndexType, N > &begin, const Vector< IndexType, N > &end, const Func &func, ExecutionPolicy policy)
Definition: IterationUtils-Impl.hpp:98
ExecutionPolicy
Execution policy tag.
Definition: Parallel.hpp:17
void ForEachIndex(const Vector< IndexType, N > &begin, const Vector< IndexType, N > &end, const Func &func)
Definition: IterationUtils-Impl.hpp:51
static void Call(const Vector< IndexType, N > &begin, const Vector< IndexType, N > &end, const Func &func, RemainingIndices... indices)
Definition: IterationUtils-Impl.hpp:38