www.cemf.ir
stdAlgorithms.hpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 
21 #ifndef __stdAlgorithms_hpp__
22 #define __stdAlgorithms_hpp__
23 
24 #include <algorithm>
25 #include <execution>
26 
27 #include "pFlowMacros.hpp"
28 #include "algorithmFunctions.hpp"
29 #include "types.hpp"
30 
32 {
33 
34 template<typename Type, bool useParallel = useStdParallel__>
36 int32 count(const Type* first, int32 numElems, const Type& val)
37 {
38  if constexpr (useParallel)
39  return std::count_if(
40  std::execution::par_unseq,
41  first, first+numElems,
42  [=](const Type& check){ return equal(check,val);} );
43  else
44  return std::count_if(
45  first, first+numElems,
46  [=](const Type& check){ return equal(check,val);} );
47  return 0; // for nvcc
48 }
49 
50 
51 template<typename Type, bool useParallel = useStdParallel__>
53 void fill(Type* first, int32 numElems, const Type& val)
54 {
55  if constexpr (useParallel)
56  std::fill(std::execution::par_unseq, first, first+numElems, val);
57  else
58  std::fill(first, first+numElems, val);
59 }
60 
61 template<typename Type, typename indexType, bool useParallel = useStdParallel__>
63 void fillSelected(Type* first, const indexType* indices, const int32 numElems, const Type val)
64 {
65  if constexpr(useParallel)
66  {
67  std::for_each_n(
68  std::execution::par_unseq,
69  indices,
70  numElems,
71  [=](indexType i){
72  first[i] = val;
73  });
74  }
75  else
76  {
77  std::for_each_n(
78  indices,
79  numElems,
80  [=](indexType i){
81  first[i] = val;
82  });
83  }
84 }
85 
86 template<typename Type, typename indexType, bool useParallel = useStdParallel__>
88 void fillSelected(Type* first, const indexType* indices, const Type* vals, const int32 numElems)
89 {
90  for(indexType i=0; i<numElems; i++)
91  {
92  first[indices[i]]=vals[i];
93  }
94 }
95 
96 template<typename Type, bool useParallel = useStdParallel__>
98 void fillSequence(Type* first, int32 numElems, const Type& firstVal)
99 {
100  if constexpr (useParallel)
101  std::for_each_n(
102  std::execution::par_unseq,
103  first,
104  numElems,
105  [=](Type& ref){ ref = firstVal+std::distance(first,&ref);});
106  else
107  std::iota(first, first+numElems, firstVal);
108 }
109 
110 template<typename Type, bool useParallel = useStdParallel__>
112 Type max(const Type* first, int32 numElems)
113 {
114  if constexpr(useParallel)
115  return *std::max_element(
116  std::execution::par_unseq,
117  first,
118  first+numElems,
119  less<Type>());
120  else
121  return *std::max_element(
122  first,
123  first+numElems,
124  less<Type>());
125 }
126 
127 template<typename Type, bool useParallel = useStdParallel__>
129 Type min(const Type* first, int32 numElems)
130 {
131  if constexpr(useParallel)
132  return *(std::min_element(
133  std::execution::par_unseq,
134  first,
135  first+numElems,
136  less<Type>()));
137  else
138  return *(std::min_element(
139  first,
140  first+numElems,
141  less<Type>()));
142 }
143 
144 template<typename Type, bool useParallel = useStdParallel__>
146 void sort(Type* first, int32 numElems)
147 {
148  if constexpr(useParallel)
149  {
150  std::sort(
151  std::execution::par,
152  first,
153  first+numElems,
154  less<Type>());
155  }
156  else
157  {
158  std::sort(
159  first,
160  first+numElems,
161  less<Type>());
162  }
163 }
164 
165 template<typename Type, typename CompareFunc, bool useParallel = useStdParallel__>
167 void sort(Type* first, int32 numElems, CompareFunc compare)
168 {
169  if constexpr(useParallel)
170  {
171  std::sort(
172  std::execution::par_unseq,
173  first,
174  first+numElems,
175  compare);
176  }
177  else
178  {
179  std::sort(
180  first,
181  first+numElems,
182  compare);
183  }
184 }
185 
186 template<typename Type, typename PermuteType, bool useParallel = useStdParallel__>
188 void permuteSort(const Type* first, PermuteType* pFirst, int32 numElems)
189 {
190  struct compOperator
191  {
192  const Type* first_;
193  bool operator()(const PermuteType& lhs, const PermuteType& rhs)const {
194  return first_[lhs]<first_[rhs]; }
195  };
196 
197  compOperator compare{first};
198  fillSequence<PermuteType, useParallel>(pFirst, numElems, static_cast<PermuteType>(0));
199  sort<PermuteType, compOperator, useParallel>(pFirst, numElems, compare);
200 }
201 
202 
203 template<typename Type, typename DestType, bool useParallel = useStdParallel__>
204 void exclusiveScan(Type* first, DestType* dFirst, int32 numElems)
205 {
206  if constexpr (useParallel)
207  {
209  std::exclusive_scan(
210  //std::execution::par_unseq,
211  first, first+numElems,
212  dFirst, 0);
213  }
214  else
215  {
216  std::exclusive_scan(
217  first, first+numElems,
218  dFirst,0);
219  }
220 }
221 
222 template<typename Type, typename DestType, bool useParallel = useStdParallel__>
223 void inclusiveScan(Type* first, DestType* dFirst, int32 numElems)
224 {
225  if constexpr (useParallel)
226  {
227  std::inclusive_scan(
228  std::execution::par_unseq,
229  first, first+numElems,
230  dFirst);
231  }
232  else
233  {
234  std::inclusive_scan(
235  first, first+numElems,
236  dFirst);
237  }
238 }
239 
240 }
241 
242 
243 #endif //__stdAlgorithms_hpp__
pFlow::algorithms::STD::count
INLINE_FUNCTION_H int32 count(const Type *first, int32 numElems, const Type &val)
Definition: stdAlgorithms.hpp:36
pFlow::algorithms::STD::max
INLINE_FUNCTION_H Type max(const Type *first, int32 numElems)
Definition: stdAlgorithms.hpp:112
types.hpp
pFlow::algorithms::STD::fillSelected
INLINE_FUNCTION_H void fillSelected(Type *first, const indexType *indices, const int32 numElems, const Type val)
Definition: stdAlgorithms.hpp:63
algorithmFunctions.hpp
pFlow::algorithms::STD
Definition: stdAlgorithms.hpp:31
pFlow::equal
INLINE_FUNCTION_HD bool equal(const box &b1, const box &b2, real tol=smallValue)
Definition: box.hpp:135
count_if
auto count_if(const Vector< T, Allocator > &vec, UnaryPredicate p)
pFlow::algorithms::STD::exclusiveScan
void exclusiveScan(Type *first, DestType *dFirst, int32 numElems)
Definition: stdAlgorithms.hpp:204
pFlowMacros.hpp
pFlow::int32
int int32
Definition: builtinTypes.hpp:50
pFlow::algorithms::STD::min
INLINE_FUNCTION_H Type min(const Type *first, int32 numElems)
Definition: stdAlgorithms.hpp:129
fill
void fill(Vector< T, Allocator > &vec, const T &val)
INLINE_FUNCTION_H
#define INLINE_FUNCTION_H
Definition: pFlowMacros.hpp:57
pFlow::algorithms::STD::fill
INLINE_FUNCTION_H void fill(Type *first, int32 numElems, const Type &val)
Definition: stdAlgorithms.hpp:53
pFlow::algorithms::STD::fillSequence
INLINE_FUNCTION_H void fillSequence(Type *first, int32 numElems, const Type &firstVal)
Definition: stdAlgorithms.hpp:98
sort
void sort(Vector< T, Allocator > &vec)
pFlow::algorithms::STD::inclusiveScan
void inclusiveScan(Type *first, DestType *dFirst, int32 numElems)
Definition: stdAlgorithms.hpp:223
pFlow::algorithms::STD::permuteSort
INLINE_FUNCTION_H void permuteSort(const Type *first, PermuteType *pFirst, int32 numElems)
Definition: stdAlgorithms.hpp:188
pFlow::algorithms::STD::sort
INLINE_FUNCTION_H void sort(Type *first, int32 numElems)
Definition: stdAlgorithms.hpp:146
pFlow::algorithms::less
Definition: algorithmFunctions.hpp:39