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