pointFieldAlgorithms.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 
22 namespace pFlow
23 {
24 
25 
26 const inline auto ActivePoint = pointStructure::ACTIVE;
27 
28 template<class T, typename... properties>
30  const Kokkos::View<T*, properties...>& view,
31  const Kokkos::View<int8*, Kokkos::LayoutLeft, Kokkos::HostSpace>& flag,
32  size_t start,
33  size_t end
34  )
35 {
36  T maxValue = largestNegative<T>();
37  for(label i=start; i<end; ++i)
38  {
39  if(flag[i] == ActivePoint )maxValue = max(maxValue, view[i]);
40  }
41  return maxValue;
42 }
43 
44 // to be executed on host
45 template<typename T, typename... properties>
48  const Kokkos::View<T*, properties...>& view,
49  const Kokkos::View<int8*, Kokkos::LayoutLeft, Kokkos::HostSpace>& flag,
50  size_t start,
51  size_t end
52  )
53 {
54 
55  T maxValue = largestNegative<T>();
56 
57  auto RP = Kokkos::RangePolicy<
58  Kokkos::IndexType<size_t>,
59  typename Kokkos::View<T, properties...>::execution_space >(start, end);
60 
61  Kokkos::parallel_reduce("pointFieldAlgorithms-maxActive",
62  RP,
63  LAMBDA_HD(label i, T& valueToUpdate){
64  if(flag[i] == ActivePoint) valueToUpdate = max(view[i],valueToUpdate);
65  },
66  Kokkos::Max<T>( maxValue )
67  );
68  return maxValue;
69 }
70 
71 
72 // to be executed on device
73 template<typename T, typename... properties>
76  const Kokkos::View<T*, properties...>& view,
77  const Kokkos::View<int8*, Kokkos::LayoutLeft>& flag,
78  size_t start,
79  size_t end
80  )
81 {
82 
83  T maxValue = largestNegative<T>();
84 
85  auto RP = Kokkos::RangePolicy<
86  Kokkos::IndexType<size_t>,
87  typename Kokkos::View<T, properties...>::execution_space >(start, end);
88 
89  Kokkos::parallel_reduce("pointFieldAlgorithms-maxActive",
90  RP,
91  LAMBDA_HD(label i, T& valueToUpdate){
92  if(flag[i] == ActivePoint) valueToUpdate = max(view[i],valueToUpdate);
93  },
94  Kokkos::Max<T>( maxValue )
95  );
96  return maxValue;
97 }
98 
99 template<class T, class MemorySpace>
101 {
102 
103  // if all points are active, perfrom a simple max
104  if( pField.allActive() )
105  return max(pField.VectorField());
106 
107  const auto len = pField.size();
108  if constexpr ( pField.isHostAccessible())
109  {
110  if(len < sizeToSerial__ ) // serial execution instead of lunching parallel execution
111  return maxActive_serial(
112  pField.deviceVectorAll(),
113  pField.pointFlag().hostVectorAll(),
114  static_cast<size_t>(0),
115  len
116  );
117  else
118  return maxActiveH(
119  pField.deviceVectorAll(),
120  pField.pointFlag().hostVectorAll(),
121  static_cast<size_t>(0),
122  len
123  );
124  }
125  else
126  {
127  return maxActiveD(
128  pField.deviceVectorAll(),
129  pField.pointFlag().deviceVectorAll(),
130  static_cast<size_t>(0),
131  len
132  );
133  }
134 
135  // to remove the warning of CUDAC++ compiler when dealing with constexpr
136  return 0;
137 }
138 
139 template<class side, class T, class MemorySpace=void>
141 {
142  if( pField.allActive() )
143  return max(pField.VectorField());
144 
145  auto len = pField.size();
146 
147  if constexpr (std::is_same<side,HostSide>::value)
148  {
149  if( len < sizeToSerial__)
150  return maxActive_serial(
151  pField.hostVectorAll(),
152  pField.pointFlag().hostVectorAll(),
153  static_cast<size_t>(0),
154  len
155  );
156  else
157  return maxActiveH(
158  pField.hostVectorAll(),
159  pField.pointFlag().hostVectorAll(),
160  static_cast<size_t>(0),
161  len
162  );
163  }else
164  {
165  return maxActiveD(
166  pField.deviceVectorAll(),
167  pField.pointFlag().deviceVectorAll(),
168  static_cast<size_t>(0),
169  len
170  );
171  }
172 
173  return 0;
174 }
175 
176 /*template<typename T>
177  void inline fillActive(pointField<T>& field, const T& val)
178 {
179  if(field.pStruct().allActive)
180  {
181  fill_n(field, field.size(), val);
182  return;
183  }
184  ForAll(i, field)
185  {
186  if(field.pStruct().isActive(i)) field[i] = val;
187  }
188 }
189 
190 template<typename T>
191  void inline fillMarkedDelete( pointField<T>& field, const T& val)
192 {
193  if(field.pStruct().allActive())return;
194  ForAll(i,field)
195  {
196  if(!field.pStruct().isActive(i)) field[i] = val;
197  }
198 }
199 
200 template<typename T>
201 inline auto countActive(const pointField<T>& field, const T& val)
202 {
203  if(field.pStruct().allActive())
204  {
205  return count(field, val);
206  }
207  else
208  {
209  return count(field, val, [&](label i){return field.pointField().isActive(i);});
210  }
211 }
212 
213 template<typename T, typename UnaryPredicate>
214 inline auto for_eachActive(pointField<T>& field, UnaryPredicate func)
215 {
216  if(field.pStruct().allActive())
217  {
218  ForAll(i,field)
219  {
220  func(i);
221  }
222  }
223  else
224  {
225  ForAll(i, field)
226  {
227  if(field.pStruct().isActive(i)) func(i);
228  }
229  }
230  return func;
231 }
232 
233 template<typename T, typename UnaryPredicate>
234 inline bool for_eachActiveBreak(pointField<T>& field, UnaryPredicate func)
235 {
236  if(field.pStruct().allActive())
237  {
238  ForAll(i,field)
239  {
240  if(!func(i))return false;
241  }
242  }
243  else
244  {
245  ForAll(i, field)
246  {
247  if(field.pStruct().isActive(i))
248  {
249  if(!func(i)) return false;
250  }
251  }
252  }
253  return true;
254 }
255 
256 template<typename T, typename UnaryPredicate>
257 inline auto for_eachMarkedDelete(pointField<T>& field, UnaryPredicate func)
258 {
259  if(field.pStruct().allActive()) return func;
260 
261  ForAll(i, field)
262  {
263  if(!field.pStruct().isActive(i)) func(i);
264  }
265  return func;
266 }*/
267 
268 
269 }
pFlow::pointStructure::ACTIVE
@ ACTIVE
Definition: pointStructure.hpp:53
pFlow::ActivePoint
const auto ActivePoint
Definition: pointFieldAlgorithms.hpp:26
sizeToSerial__
const size_t sizeToSerial__
Definition: baseAlgorithms.hpp:28
pFlow
Definition: demComponent.hpp:28
pFlow::pointField
Definition: pointField.hpp:35
pFlow::maxActiveD
INLINE_FUNCTION_H T maxActiveD(const Kokkos::View< T *, properties... > &view, const Kokkos::View< int8 *, Kokkos::LayoutLeft > &flag, size_t start, size_t end)
Definition: pointFieldAlgorithms.hpp:75
INLINE_FUNCTION_H
#define INLINE_FUNCTION_H
Definition: pFlowMacros.hpp:53
pFlow::pointField::allActive
INLINE_FUNCTION_H bool allActive() const
Definition: pointField.hpp:123
pFlow::max
T max(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:164
pFlow::maxActiveH
INLINE_FUNCTION_H T maxActiveH(const Kokkos::View< T *, properties... > &view, const Kokkos::View< int8 *, Kokkos::LayoutLeft, Kokkos::HostSpace > &flag, size_t start, size_t end)
Definition: pointFieldAlgorithms.hpp:47
LAMBDA_HD
#define LAMBDA_HD
Definition: pFlowMacros.hpp:54
pFlow::label
std::size_t label
Definition: builtinTypes.hpp:61
pFlow::maxActive
T maxActive(const pointField< VectorSingle, T, MemorySpace > &pField)
Definition: pointFieldAlgorithms.hpp:100
pFlow::maxActive_serial
T maxActive_serial(const Kokkos::View< T *, properties... > &view, const Kokkos::View< int8 *, Kokkos::LayoutLeft, Kokkos::HostSpace > &flag, size_t start, size_t end)
Definition: pointFieldAlgorithms.hpp:29
pFlow::pointField::pointFlag
const auto & pointFlag() const
Definition: pointField.hpp:133