ViewAlgorithms.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 __ViewAlgorithms_hpp__
22 #define __ViewAlgorithms_hpp__
23 
24 
25 #include "numericConstants.hpp"
26 #include "KokkosUtilities.hpp"
27 #include "kokkosAlgorithms.hpp"
28 #include "stdAlgorithms.hpp"
29 #include "cudaAlgorithms.hpp"
30 
31 
32 namespace pFlow
33 {
34 
35 inline const size_t maxSizeToSerial__ = 64;
36 
37 template<typename T, typename... properties>
40  const ViewType1D<T, properties...>& view,
41  int32 start,
42  int32 end,
43  const T& val)
44 {
45  using ExecutionSpace = typename ViewType1D<T, properties...>::execution_space;
46 
47  int32 numElems = end-start;
48 
49  if constexpr( isHostAccessible<ExecutionSpace>())
50  {
51  if(numElems<maxSizeToSerial__)
52  {
53  return pFlow::algorithms::STD::count<T,false>(
54  view.data()+start,
55  numElems,
56  val);
57  }
58  }
59 
60  return pFlow::algorithms::KOKKOS::count<T, ExecutionSpace>(
61  view.data()+start,
62  numElems,
63  val);
64 }
65 
66 template<typename T, typename... properties>
68 void fill
69 (
71  range span,
72  T val
73 )
74 {
75  using ExecutionSpace = typename ViewType1D<T, properties...>::execution_space;
76 
77 
78  if constexpr( isHostAccessible<ExecutionSpace>())
79  {
80  int32 numElems = span.second-span.first;
81  if( numElems<maxSizeToSerial__)
82  {
83  pFlow::algorithms::STD::fill<T,false>(
84  view.data()+span.first,
85  numElems,
86  val);
87  return;
88  }
89  }
90 
91  auto subV = Kokkos::subview(view, span);
92  Kokkos::deep_copy(subV, val);
93 }
94 
95 template<typename T, typename... properties>
96 void fill
97 (
99  int32 start,
100  int32 end,
101  T val
102 )
103 {
104  fill(view, range(start,end),val);
105 }
106 
107 template<
108  typename Type,
109  typename... properties>
112  int32 start,
113  int32 end,
114  const Type startVal
115  )
116 {
117 
118  using ExecutionSpace = typename ViewType1D<Type, properties...>::execution_space;
119  int32 numElems = end-start;
120 
121  if constexpr( isHostAccessible<ExecutionSpace>())
122  {
123  if(numElems<maxSizeToSerial__)
124  {
125  pFlow::algorithms::STD::fillSequence<Type,false>(
126  view.data()+start,
127  numElems,
128  startVal);
129  return ;
130  }
131  }
132 
133  pFlow::algorithms::KOKKOS::fillSequence<Type, ExecutionSpace>(
134  view.data()+start,
135  numElems,
136  startVal);
137 
138  return ;
139 }
140 
141 
142 template<
143  typename Type,
144  typename... properties,
145  typename indexType,
146  typename... indexProperties>
150  const int32 numElems,
151  const Type val,
152  typename std::enable_if_t<
156  bool> = true )
157 {
158 
159  using ExecutionSpace = typename ViewType1D<Type, properties...>::execution_space;
160 
161  if constexpr( isHostAccessible<ExecutionSpace>())
162  {
163  if(numElems<maxSizeToSerial__)
164  {
165  pFlow::algorithms::STD::fillSelected<Type,indexType,false>(
166  view.data(),
167  indices.data(),
168  numElems,
169  val);
170  return true;
171  }
172  }
173 
174  pFlow::algorithms::KOKKOS::fillSelected<Type, indexType, ExecutionSpace>(
175  view.data(),
176  indices.data(),
177  numElems,
178  val);
179 
180  return true;
181 }
182 
183 template<
184  typename Type,
185  typename... properties,
186  typename indexType,
187  typename... indexProperties>
188  //typename valType> //,
189  //typename... valProperties>
194  const int32 numElems ,
195  typename std::enable_if_t<
199  bool> = true )
200 {
201 
202  using ExecutionSpace = typename ViewType1D<Type, properties...>::execution_space;
203 
204 
205 
206  if constexpr( isHostAccessible<ExecutionSpace>())
207  {
208  if(numElems<maxSizeToSerial__)
209  {
210  pFlow::algorithms::STD::fillSelected<Type,indexType,false>(
211  view.data(),
212  indices.data(),
213  vals.data(),
214  numElems
215  );
216  return true;
217  }
218  }
219 
220 
221  pFlow::algorithms::KOKKOS::fillSelected<Type, indexType, ExecutionSpace>(
222  view.data(),
223  indices.data(),
224  vals.data(),
225  numElems
226  );
227 
228  return true;
229 }
230 
231 
232 template<typename T, typename... properties>
234 T min(
235  const ViewType1D<T, properties...>& view,
236  int32 start,
237  int32 end)
238 {
239 
240  using ExecutionSpace = typename ViewType1D<T, properties...>::execution_space;
241 
242  int32 numElems = end-start;
243 
244  if constexpr( isHostAccessible<ExecutionSpace>())
245  {
246  if(numElems<maxSizeToSerial__)
247  {
248  return
249  pFlow::algorithms::STD::min<T,false>(
250  view.data()+start,
251  numElems);
252  }
253  }
254 
255  return
256  pFlow::algorithms::KOKKOS::min<T, ExecutionSpace>(
257  view.data()+start,
258  numElems);
259 }
260 
261 template<typename T, typename... properties>
263 T max(
264  const ViewType1D<T, properties...>& view,
265  int32 start,
266  int32 end)
267 {
268 
269  using ExecutionSpace = typename ViewType1D<T, properties...>::execution_space;
270 
271  int32 numElems = end-start;
272 
273  if constexpr( isHostAccessible<ExecutionSpace>())
274  {
275  if(numElems<maxSizeToSerial__)
276  {
277  return
278  pFlow::algorithms::STD::max<T,false>(
279  view.data()+start,
280  numElems);
281  }
282  }
283 
284  return
285  pFlow::algorithms::KOKKOS::max<T, ExecutionSpace>(
286  view.data()+start,
287  numElems);
288 }
289 
290 template <
291  typename dType,
292  typename... dProperties,
293  typename sType,
294  typename... sProperties>
296 void copy(
299  )
300 {
301  Kokkos::deep_copy(dst,src);
302 }
303 
304 template <
305  typename dType,
306  typename... dProperties,
307  typename sType,
308  typename... sProperties>
310 void copy(
312  int32 dStart,
314  int32 sStart,
315  int32 sEnd
316  )
317 {
318 
319  range sSpan(sStart,sEnd);
320  range dSpan(dStart,dStart+(sEnd-sStart));
321 
322  auto srcSub = Kokkos::subview(src, sSpan);
323  auto dstSub = Kokkos::subview(dst, dSpan);
324 
325  Kokkos::deep_copy(dstSub,srcSub);
326 }
327 
328 template <
329  typename dType,
330  typename sType,
331  typename... sProperties>
333 void getNth(
334  dType& dst,
336  const int32 n
337  )
338 {
339  range span(n,n+1);
340  auto subV = Kokkos::subview(src, span);
341  hostViewType1D<dType> dstView("getNth",1);
342  Kokkos::deep_copy(dstView,subV);
343  dst = *dstView.data();
344 }
345 
346 
347 template<typename T, typename... properties>
349 void sort(
351  int32 start,
352  int32 end)
353 {
354 
355  using ExecutionSpace = typename ViewType1D<T, properties...>::execution_space;
356 
357  int32 numElems = end-start;
358 
359  if constexpr( isHostAccessible<ExecutionSpace>())
360  {
361  if(numElems<maxSizeToSerial__)
362  {
363  pFlow::algorithms::STD::sort<T,false>(
364  view.data()+start,
365  numElems);
366  return;
367  }
368  else
369  {
370  pFlow::algorithms::STD::sort<T,true>(
371  view.data()+start,
372  numElems);
373  return;
374  }
375  }
376 
377 #ifdef __CUDACC__
378 
379  pFlow::algorithms::CUDA::sort<T>(
380  view.data()+start,
381  numElems);
382 #else
383  static_assert("sort on device is not defined!");
384 
385 #endif
386 
387  return;
388 }
389 
390 template<typename T, typename... properties, typename CompareFunc>
392 void sort(
394  int32 start,
395  int32 end,
396  CompareFunc compare)
397 {
398 
399  using ExecutionSpace = typename ViewType1D<T, properties...>::execution_space;
400 
401  int32 numElems = end-start;
402 
403  if constexpr( isHostAccessible<ExecutionSpace>())
404  {
405  if(numElems<maxSizeToSerial__)
406  {
407  pFlow::algorithms::STD::sort<T,CompareFunc,false>(
408  view.data()+start,
409  numElems,
410  compare);
411  return;
412  }
413  else
414  {
415  pFlow::algorithms::STD::sort<T,CompareFunc,true>(
416  view.data()+start,
417  numElems,
418  compare);
419  return;
420  }
421  }
422 
423 #ifdef __CUDACC__
424 
425  pFlow::algorithms::CUDA::sort<T, CompareFunc>(
426  view.data()+start,
427  numElems,
428  compare);
429 #else
430  static_assert("sort on device is not defined!");
431 
432 #endif
433 
434  return;
435 }
436 
437 template<
438  typename Type,
439  typename... properties,
440  typename permType,
441  typename... permProperties>
444  int32 start,
445  int32 end,
447  int32 permStart,
448  typename std::enable_if_t<
452  bool> = true )
453 {
454  using ExecutionSpace = typename ViewType1D<Type, properties...>::execution_space;
455 
456  int32 numElems = end-start;
457 
458  if constexpr( isHostAccessible<ExecutionSpace>())
459  {
460  if(numElems<maxSizeToSerial__)
461  {
462  pFlow::algorithms::STD::permuteSort<Type,permType,false>(
463  view.data()+start,
464  permuteView.data()+permStart,
465  numElems );
466  return;
467  }
468  else
469  {
470  pFlow::algorithms::STD::permuteSort<Type,permType,true>(
471  view.data()+start,
472  permuteView.data()+permStart,
473  numElems);
474  return;
475  }
476  }
477 
478 #ifdef __CUDACC__
479 
481  view.data()+start,
482  permuteView.data()+permStart,
483  numElems);
484 #else
485  static_assert("sort on device is not defined!");
486 
487 #endif
488 
489 }
490 
491 template<
492  typename Type,
493  typename... properties>
497  int32 start,
498  int32 end,
499  const Type& val)
500 {
501 
502  if(end<=start)return -1;
503 
504  if(auto res =
505  pFlow::algorithms::binarySearch(view.data()+start,end-start,val); res>=0) {
506  return res+start;
507  }
508  else{
509  return res;
510  }
511 }
512 
513 template<
514  typename Type,
515  typename... properties,
516  typename dType,
517  typename... dProperties>
520  int32 start,
521  int32 end,
523  int32 dStart,
524  typename std::enable_if_t<
528  bool> = true )
529 {
530  using ExecutionSpace = typename ViewType1D<Type, properties...>::execution_space;
531 
532  int32 numElems = end-start;
533  if constexpr( isHostAccessible<ExecutionSpace>())
534  {
535  if(numElems<maxSizeToSerial__)
536  {
537  pFlow::algorithms::STD::exclusiveScan<Type,dType,false>(
538  view.data()+start,
539  dView.data()+dStart,
540  numElems);
541  return;
542  }
543  }
544 
545  pFlow::algorithms::KOKKOS::exclusiveScan<Type,dType,ExecutionSpace>(
546  view.data()+start,
547  dView.data()+dStart,
548  numElems);
549 }
550 
551 
552 template<
553  typename Type,
554  typename... properties,
555  typename dType,
556  typename... dProperties>
559  int32 start,
560  int32 end,
562  int32 dStart,
563  typename std::enable_if_t<
567  bool> = true )
568 {
569  using ExecutionSpace = typename ViewType1D<Type, properties...>::execution_space;
570 
571  int32 numElems = end-start;
572  if constexpr( isHostAccessible<ExecutionSpace>())
573  {
574  if(numElems<maxSizeToSerial__)
575  {
576  pFlow::algorithms::STD::inclusiveScan<Type,dType,false>(
577  view.data()+start,
578  dView.data()+dStart,
579  numElems);
580  return;
581  }
582  }
583 
584  pFlow::algorithms::KOKKOS::inclusiveScan<Type,dType,ExecutionSpace>(
585  view.data()+start,
586  dView.data()+dStart,
587  numElems);
588 }
589 
590 } // pFlow
591 
592 
593 #endif // Viewalgorithms
pFlow::inclusiveScan
void inclusiveScan(const ViewType1D< Type, properties... > &view, int32 start, int32 end, ViewType1D< dType, dProperties... > &dView, int32 dStart, typename std::enable_if_t< areAccessible< typename ViewType1D< Type, properties... >::execution_space, typename ViewType1D< dType, dProperties... >::memory_space >(), bool >=true)
Definition: ViewAlgorithms.hpp:557
pFlow::span
Definition: span.hpp:31
pFlow::fillSequence
void fillSequence(Vector< T, Allocator > &vec, int32 start, int32 end, const T &startVal)
Definition: VectorAlgorithm.hpp:50
pFlow::fill
void fill(Vector< T, Allocator > &vec, const T &val)
Definition: VectorAlgorithm.hpp:44
KokkosUtilities.hpp
pFlow::copy
INLINE_FUNCTION_H void copy(const ViewType1D< dType, dProperties... > &dst, const ViewType1D< sType, sProperties... > &src)
Definition: ViewAlgorithms.hpp:296
pFlow::algorithms::binarySearch
INLINE_FUNCTION_HD int binarySearch(const T *array, int length, const T &val)
Definition: algorithmFunctions.hpp:66
cudaAlgorithms.hpp
pFlow::maxSizeToSerial__
const size_t maxSizeToSerial__
Definition: ViewAlgorithms.hpp:35
pFlow
Definition: demComponent.hpp:28
n
int32 n
Definition: NBSCrossLoop.hpp:24
pFlow::int32
int int32
Definition: builtinTypes.hpp:53
INLINE_FUNCTION_H
#define INLINE_FUNCTION_H
Definition: pFlowMacros.hpp:53
pFlow::count
auto count(const Vector< T, Allocator > &vec, const T &val)
Definition: VectorAlgorithm.hpp:26
pFlow::binarySearch
INLINE_FUNCTION_HD int32 binarySearch(const ViewType1D< Type, properties... > &view, int32 start, int32 end, const Type &val)
Definition: ViewAlgorithms.hpp:495
pFlow::max
T max(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:164
pFlow::permuteSort
void permuteSort(const ViewType1D< Type, properties... > &view, int32 start, int32 end, ViewType1D< permType, permProperties... > &permuteView, int32 permStart, typename std::enable_if_t< areAccessible< typename ViewType1D< Type, properties... >::execution_space, typename ViewType1D< permType, permProperties... >::memory_space >(), bool >=true)
Definition: ViewAlgorithms.hpp:442
pFlow::hostViewType1D
Kokkos::View< T *, Kokkos::HostSpace > hostViewType1D
Definition: KokkosTypes.hpp:104
pFlow::getNth
INLINE_FUNCTION_H void getNth(dType &dst, const ViewType1D< sType, sProperties... > &src, const int32 n)
Definition: ViewAlgorithms.hpp:333
pFlow::sort
void sort(Vector< T, Allocator > &vec)
Definition: VectorAlgorithm.hpp:62
pFlow::areAccessible
INLINE_FUNCTION_H constexpr bool areAccessible()
Definition: KokkosUtilities.hpp:42
pFlow::ViewType1D
Kokkos::View< T *, properties... > ViewType1D
Definition: KokkosTypes.hpp:62
numericConstants.hpp
INLINE_FUNCTION_HD
#define INLINE_FUNCTION_HD
Definition: pFlowMacros.hpp:51
pFlow::exclusiveScan
void exclusiveScan(const ViewType1D< Type, properties... > &view, int32 start, int32 end, ViewType1D< dType, dProperties... > &dView, int32 dStart, typename std::enable_if_t< areAccessible< typename ViewType1D< Type, properties... >::execution_space, typename ViewType1D< dType, dProperties... >::memory_space >(), bool >=true)
Definition: ViewAlgorithms.hpp:518
kokkosAlgorithms.hpp
stdAlgorithms.hpp
pFlow::range
kPair< int, int > range
Definition: KokkosTypes.hpp:54
pFlow::min
T min(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:138
pFlow::fillSelected
bool fillSelected(ViewType1D< Type, properties... > view, const ViewType1D< indexType, indexProperties... > indices, const int32 numElems, const Type val, typename std::enable_if_t< areAccessible< typename ViewType1D< Type, properties... >::execution_space, typename ViewType1D< indexType, indexProperties... >::memory_space >(), bool >=true)
Definition: ViewAlgorithms.hpp:147