IterationUtils.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_HPP
12 #define CUBBYFLOW_ITERATION_UTILS_HPP
13 
14 #include <Core/Matrix/Matrix.hpp>
15 #include <Core/Utils/Parallel.hpp>
16 
17 namespace CubbyFlow
18 {
19 template <typename IndexType, size_t N, typename Func>
20 void ForEachIndex(const Vector<IndexType, N>& begin,
21  const Vector<IndexType, N>& end, const Func& func);
22 
23 template <typename IndexType, typename Func>
24 void ForEachIndex(const Vector<IndexType, 1>& begin,
25  const Vector<IndexType, 1>& end, const Func& func);
26 
27 template <typename IndexType, typename Func>
28 void ForEachIndex(IndexType begin, IndexType end, const Func& func);
29 
30 template <typename IndexType, size_t N, typename Func>
31 void ForEachIndex(const Vector<IndexType, N>& size, const Func& func);
32 
33 template <typename IndexType, typename Func>
34 void ForEachIndex(const Vector<IndexType, 1>& size, const Func& func);
35 
36 template <typename IndexType, typename Func>
37 void ForEachIndex(IndexType size, const Func& func);
38 
39 template <typename IndexType, size_t N, typename Func>
40 void ParallelForEachIndex(const Vector<IndexType, N>& begin,
41  const Vector<IndexType, N>& end, const Func& func,
43 
44 template <typename IndexType, typename Func>
45 void ParallelForEachIndex(const Vector<IndexType, 1>& begin,
46  const Vector<IndexType, 1>& end, const Func& func,
47  ExecutionPolicy policy);
48 
49 template <typename IndexType, typename Func>
50 void ParallelForEachIndex(IndexType begin, IndexType end, const Func& func,
51  ExecutionPolicy policy);
52 
53 template <typename IndexType, size_t N, typename Func>
54 void ParallelForEachIndex(const Vector<IndexType, N>& size, const Func& func,
56 
57 template <typename IndexType, typename Func>
58 void ParallelForEachIndex(const Vector<IndexType, 1>& size, const Func& func,
60 
61 template <typename IndexType, typename Func>
62 void ParallelForEachIndex(IndexType size, const Func& func,
64 
66 template <typename ReturnType>
67 std::function<ReturnType(size_t)> Unroll1(
68  const std::function<ReturnType(const Vector1UZ&)>& func)
69 {
70  return [func](size_t i) { return func(Vector1UZ{ i }); };
71 }
72 
74 template <typename ReturnType>
75 std::function<ReturnType(size_t, size_t)> Unroll2(
76  const std::function<ReturnType(const Vector2UZ&)>& func)
77 {
78  return [func](size_t i, size_t j) { return func(Vector2UZ{ i, j }); };
79 }
80 
82 template <typename ReturnType>
83 std::function<ReturnType(size_t, size_t, size_t)> Unroll3(
84  const std::function<ReturnType(const Vector3UZ&)>& func)
85 {
86  return [func](size_t i, size_t j, size_t k) {
87  return func(Vector3UZ{ i, j, k });
88  };
89 }
90 
91 template <typename ReturnType, size_t N>
92 struct GetUnroll
93 {
94  // Do nothing
95 };
96 
97 template <typename ReturnType>
98 struct GetUnroll<ReturnType, 1>
99 {
100  static std::function<ReturnType(size_t)> Unroll(
101  const std::function<ReturnType(const Vector1UZ&)>& func)
102  {
103  return [func](size_t i) { return func(Vector1UZ{ i }); };
104  }
105 };
106 
107 template <>
108 struct GetUnroll<void, 1>
109 {
110  static std::function<void(size_t)> Unroll(
111  const std::function<void(const Vector1UZ&)>& func)
112  {
113  return [func](size_t i) { func(Vector1UZ{ i }); };
114  }
115 };
116 
117 template <typename ReturnType>
118 struct GetUnroll<ReturnType, 2>
119 {
120  static std::function<ReturnType(size_t, size_t)> Unroll(
121  const std::function<ReturnType(const Vector2UZ&)>& func)
122  {
123  return [func](size_t i, size_t j) { return func(Vector2UZ{ i, j }); };
124  }
125 };
126 
127 template <>
128 struct GetUnroll<void, 2>
129 {
130  static std::function<void(size_t, size_t)> Unroll(
131  const std::function<void(const Vector2UZ&)>& func)
132  {
133  return [func](size_t i, size_t j) { func(Vector2UZ{ i, j }); };
134  }
135 };
136 
137 template <typename ReturnType>
138 struct GetUnroll<ReturnType, 3>
139 {
140  static std::function<ReturnType(size_t, size_t, size_t)> Unroll(
141  const std::function<ReturnType(const Vector3UZ&)>& func)
142  {
143  return [func](size_t i, size_t j, size_t k) {
144  return func(Vector3UZ{ i, j, k });
145  };
146  }
147 };
148 
149 template <>
150 struct GetUnroll<void, 3>
151 {
152  static std::function<void(size_t, size_t, size_t)> Unroll(
153  const std::function<void(const Vector3UZ&)>& func)
154  {
155  return [func](size_t i, size_t j, size_t k) {
156  return func(Vector3UZ{ i, j, k });
157  };
158  }
159 };
160 } // namespace CubbyFlow
161 
163 
164 #endif
static std::function< ReturnType(size_t, size_t, size_t)> Unroll(const std::function< ReturnType(const Vector3UZ &)> &func)
Definition: IterationUtils.hpp:140
static std::function< ReturnType(size_t, size_t)> Unroll(const std::function< ReturnType(const Vector2UZ &)> &func)
Definition: IterationUtils.hpp:120
static std::function< void(size_t, size_t, size_t)> Unroll(const std::function< void(const Vector3UZ &)> &func)
Definition: IterationUtils.hpp:152
std::function< ReturnType(size_t, size_t, size_t)> Unroll3(const std::function< ReturnType(const Vector3UZ &)> &func)
Unrolls vector-based DataPositionFunc indexing to size_t-based function.
Definition: IterationUtils.hpp:83
Definition: IterationUtils.hpp:92
Definition: Matrix.hpp:27
Definition: pybind11Utils.hpp:20
std::function< ReturnType(size_t)> Unroll1(const std::function< ReturnType(const Vector1UZ &)> &func)
Unrolls vector-based indexing to size_t-based function.
Definition: IterationUtils.hpp:67
void ParallelForEachIndex(const Vector< IndexType, N > &begin, const Vector< IndexType, N > &end, const Func &func, ExecutionPolicy policy)
Definition: IterationUtils-Impl.hpp:98
std::function< ReturnType(size_t, size_t)> Unroll2(const std::function< ReturnType(const Vector2UZ &)> &func)
Unrolls vector-based indexing to size_t-based function.
Definition: IterationUtils.hpp:75
static std::function< ReturnType(size_t)> Unroll(const std::function< ReturnType(const Vector1UZ &)> &func)
Definition: IterationUtils.hpp:100
ExecutionPolicy
Execution policy tag.
Definition: Parallel.hpp:17
static std::function< void(size_t)> Unroll(const std::function< void(const Vector1UZ &)> &func)
Definition: IterationUtils.hpp:110
void ForEachIndex(const Vector< IndexType, N > &begin, const Vector< IndexType, N > &end, const Func &func)
Definition: IterationUtils-Impl.hpp:51
static std::function< void(size_t, size_t)> Unroll(const std::function< void(const Vector2UZ &)> &func)
Definition: IterationUtils.hpp:130