Embedded Multicore Building Blocks V1.0.0
for_each.h
1 /*
2  * Copyright (c) 2014-2017, Siemens AG. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #ifndef EMBB_ALGORITHMS_FOR_EACH_H_
28 #define EMBB_ALGORITHMS_FOR_EACH_H_
29 
30 #include <embb/mtapi/job.h>
31 #include <embb/mtapi/execution_policy.h>
32 #include <embb/algorithms/internal/int_iterator.h>
33 
34 namespace embb {
35 namespace algorithms {
36 
44 #ifdef DOXYGEN
45 
71 template<typename RAI, typename Function>
72 void ForEach(
73  RAI first,
75  RAI last,
78  Function unary,
82  size_t block_size = 0
89  );
90 
115 template<typename Integer, typename Diff, typename Function>
116 void ForLoop(
117  Integer first,
119  Integer last,
121  Diff stride = 1,
123  Function unary,
127  size_t block_size = 0
134  );
135 
136 #else // DOXYGEN
137 
141 template<typename RAI>
142 void ForEach(
143  RAI first,
144  RAI last,
145  embb::mtapi::Job unary,
146  const embb::mtapi::ExecutionPolicy& policy,
147  size_t block_size
148 );
149 
153 template<typename RAI, typename Function>
154 void ForEach(
155  RAI first,
156  RAI last,
157  Function unary,
158  const embb::mtapi::ExecutionPolicy& policy,
159  size_t block_size
160  );
161 
165 template<typename RAI, typename Function>
166 void ForEach(
167  RAI first,
168  RAI last,
169  Function unary
170  ) {
171  ForEach(first, last, unary, embb::mtapi::ExecutionPolicy(), 0);
172 }
173 
177 template<typename RAI, typename Function>
178 void ForEach(
179  RAI first,
180  RAI last,
181  Function unary,
182  const embb::mtapi::ExecutionPolicy& policy
183  ) {
184  ForEach(first, last, unary, policy, 0);
185 }
186 
190 template<typename Integer, typename Diff, typename Function>
191 void ForLoop(
192  Integer first,
193  Integer last,
194  Diff stride,
195  Function unary,
196  const embb::mtapi::ExecutionPolicy& policy,
197  size_t block_size
198  ) {
199  ForEach(internal::IntIterator<Integer>(first, stride),
200  internal::IntIterator<Integer>(last, stride),
201  unary, policy, block_size);
202 }
203 
207 template<typename Integer, typename Function>
208 void ForLoop(
209  Integer first,
210  Integer last,
211  Function unary,
212  const embb::mtapi::ExecutionPolicy& policy,
213  size_t block_size
214  ) {
215  ForLoop(first, last, 1, unary, policy, block_size);
216 }
217 
221 template<typename Integer, typename Diff, typename Function>
222 void ForLoop(
223  Integer first,
224  Integer last,
225  Diff stride,
226  Function unary
227  ) {
228  ForLoop(first, last, stride, unary, embb::mtapi::ExecutionPolicy(), 0);
229 }
230 
234 template<typename Integer, typename Function>
235 void ForLoop(
236  Integer first,
237  Integer last,
238  Function unary
239  ) {
240  ForLoop(first, last, unary, embb::mtapi::ExecutionPolicy(), 0);
241 }
242 
246 template<typename RAI, typename Function>
247 void ForLoop(
248  RAI first,
249  RAI last,
250  Function unary,
251  const embb::mtapi::ExecutionPolicy& policy
252  ) {
253  ForLoop(first, last, unary, policy, 0);
254 }
255 
256 #endif // else DOXYGEN
257 
262 } // namespace algorithms
263 } // namespace embb
264 
265 #include <embb/algorithms/internal/for_each-inl.h>
266 
267 #endif // EMBB_ALGORITHMS_FOR_EACH_H_
void ForEach(RAI first, RAI last, Function unary, const embb::mtapi::ExecutionPolicy &policy=embb::mtapi::ExecutionPolicy(), size_t block_size=0)
Applies a unary function to the elements of a range in parallel.
Definition: lock_free_mpmc_queue.h:40
void ForLoop(Integer first, Integer last, Diff stride=1, Function unary, const embb::mtapi::ExecutionPolicy &policy=embb::mtapi::ExecutionPolicy(), size_t block_size=0)
Applies a unary function to the integers of a range in parallel.
Represents a collection of Actions.
Definition: job.h:41
Describes the execution policy of a parallel algorithm.
Definition: execution_policy.h:48