www.cemf.ir
baseAlgorithms_.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 __baseAlgorithms_hpp__
22 #define __baseAlgorithms_hpp__
23 
24 #include "KokkosUtilities.hpp"
25 #include "numericConstants.hpp"
26 
27 inline const size_t sizeToSerial__ = 64;
28 
29 namespace pFlow
30 {
31 
32 // counts the number of elements that matches val
33 // the execution space is selected based on the View::execution_spcae
34 /*template<typename T, typename... properties>
35 INLINE_FUNCTION_H
36 size_t count(
37  const ViewType1D<T, properties...>& view,
38  size_t start,
39  size_t end,
40  const T& val
41  )
42 {
43 
44  auto RP = Kokkos::RangePolicy<
45  Kokkos::IndexType<size_t>,
46  typename ViewType1D<T, properties...>::execution_space >(start, end);
47 
48  size_t totalNum=0;
49  Kokkos::parallel_reduce(
50  "baseAlgorithms-count",
51  RP,
52  LAMBDA_HD(label i, size_t & valueToUpdate){
53  if( equal(view[i], val) ) valueToUpdate += 1;
54  }, totalNum );
55 
56  return totalNum;
57 }*/
58 
59 template<typename T, typename... properties>
61 min(const ViewType1D<T, properties...>& view, size_t start, size_t end)
62 {
63  T minValue = largestPositive<T>();
64 
65  auto RP = Kokkos::RangePolicy<
66  Kokkos::IndexType<size_t>,
67  typename ViewType1D<T, properties...>::execution_space>(start, end);
68 
69  Kokkos::parallel_reduce(
70  "baseAlgorithms-min",
71  RP,
72  LAMBDA_HD(label i, T & valueToUpdate) {
73  valueToUpdate = min(view[i], valueToUpdate);
74  },
75  Kokkos ::Min<T>(minValue)
76  );
77  return minValue;
78 }
79 
80 template<typename T, typename... properties>
82 max(const ViewType1D<T, properties...>& view, size_t start, size_t end)
83 {
84  T maxValue = largestNegative<T>();
85 
86  auto RP = Kokkos::RangePolicy<
87  Kokkos::IndexType<size_t>,
88  typename ViewType1D<T, properties...>::execution_space>(start, end);
89 
90  Kokkos::parallel_reduce(
91  "baseAlgorithms-max",
92  RP,
93  LAMBDA_HD(label i, T & valueToUpdate) {
94  valueToUpdate = max(view[i], valueToUpdate);
95  },
96  Kokkos::Max<T>(maxValue)
97  );
98  return maxValue;
99 }
100 
101 template<typename T, typename... properties>
103 min_serial(const ViewType1D<T, properties...>& view, size_t start, size_t end)
104 {
105  T minValue = largestPositive<T>();
106  for (label i = start; i < end; ++i)
107  {
108  minValue = min(minValue, view[i]);
109  }
110  return minValue;
111 }
112 
113 template<typename T, typename... properties>
115 max_serial(const ViewType1D<T, properties...>& view, size_t start, size_t end)
116 {
117  T maxValue = largestNegative<T>();
118  for (label i = start; i < end; ++i)
119  {
120  maxValue = max(maxValue, view[i]);
121  }
122  return maxValue;
123 }
124 
125 template<typename UnaryFunction, typename T, typename... properties>
126 void
128  const ViewType1D<T, properties...>& view,
129  size_t start,
130  size_t end,
131  UnaryFunction func
132 )
133 {
134  auto RP = Kokkos::RangePolicy<
135  Kokkos::IndexType<size_t>,
136  typename ViewType1D<T, properties...>::execution_space>(start, end);
137 
138  Kokkos::parallel_for(
139  "baseAlgorithms-for_each", RP, LAMBDA_HD(label i) { view[i] = func(i); }
140  );
141 }
142 
143 template<typename T, typename... properties>
144 void
147  hostViewType1D<label>& selected,
148  T val
149 )
150 {
151  for (auto i = 0; i < selected.size(); ++i)
152  {
153  view[selected[i]] = val;
154  }
155 }
156 
157 template<typename T, typename... properties>
158 void
161  hostViewType1D<label>& selected,
162  hostViewType1D<T>& vals
163 )
164 {
165  for (auto i = 0; i < selected.size(); ++i)
166  {
167  view[selected[i]] = static_cast<const T&>(vals[i]);
168  }
169 }
170 
171 template<typename T, typename... properties>
172 void
175  deviceViewType1D<label>& selected,
176  T val
177 )
178 {
179  auto RP = Kokkos::RangePolicy<
180  Kokkos::IndexType<size_t>,
181  typename ViewType1D<T, properties...>::execution_space>(
182  0, selected.size()
183  );
184 
185  Kokkos::parallel_for(
186  "baseAlgorithms-insertSetElementD",
187  RP,
188  LAMBDA_D(size_t i) { view[selected[i]] = val; }
189  );
190 }
191 
192 template<typename T, typename... properties>
193 void
196  deviceViewType1D<label>& selected,
197  deviceViewType1D<T>& vals
198 )
199 {
200  auto RP = Kokkos::RangePolicy<
201  Kokkos::IndexType<size_t>,
202  typename ViewType1D<T, properties...>::execution_space>(
203  0, selected.size()
204  );
205 
206  Kokkos::parallel_for(
207  "baseAlgorithms-insertSetElementD",
208  RP,
209  LAMBDA_D(size_t i) { view[selected[i]] = vals[i]; }
210  );
211 }
212 
213 template<typename T, typename... properties>
214 void
217  range range1,
218  range range2,
219  range range3,
220  T val
221 )
222 {
223  auto subV = Kokkos::subview(view, range1, range2, range3);
224  Kokkos::deep_copy(subV, val);
225 }
226 
227 }
228 
229 #endif // __VectorSingleMath_hpp__
pFlow::fill
void fill(Vector< T, Allocator > &vec, const T &val)
Definition: VectorAlgorithm.hpp:44
KokkosUtilities.hpp
sizeToSerial__
const size_t sizeToSerial__
Definition: baseAlgorithms_.hpp:27
LAMBDA_D
#define LAMBDA_D
Definition: pFlowMacros.hpp:59
pFlow::max
T max(const internalField< T, MemorySpace > &iField)
Definition: internalFieldAlgorithms.hpp:79
pFlow::deviceViewType1D
Kokkos::View< T * > deviceViewType1D
1D array (vector) with default device (memory space and execution space)
Definition: KokkosTypes.hpp:121
pFlow
Definition: demGeometry.hpp:27
pFlow::apply_to_each
void apply_to_each(const ViewType1D< T, properties... > &view, size_t start, size_t end, UnaryFunction func)
Definition: baseAlgorithms_.hpp:127
pFlow::min_serial
INLINE_FUNCTION_H T min_serial(const ViewType1D< T, properties... > &view, size_t start, size_t end)
Definition: baseAlgorithms_.hpp:103
pFlow::insertSetElementH
void insertSetElementH(ViewType1D< T, properties... > &view, hostViewType1D< label > &selected, T val)
Definition: baseAlgorithms_.hpp:145
pFlow::insertSetElementD
void insertSetElementD(ViewType1D< T, properties... > &view, deviceViewType1D< label > &selected, T val)
Definition: baseAlgorithms_.hpp:173
INLINE_FUNCTION_H
#define INLINE_FUNCTION_H
Definition: pFlowMacros.hpp:57
pFlow::min
T min(const internalField< T, MemorySpace > &iField)
Definition: internalFieldAlgorithms.hpp:28
pFlow::ViewType1D
Kokkos::View< T *, properties... > ViewType1D
1D veiw as a vector
Definition: KokkosTypes.hpp:93
pFlow::ViewType3D
Kokkos::View< T ***, properties... > ViewType3D
3D view as an array
Definition: KokkosTypes.hpp:101
pFlow::hostViewType1D
Kokkos::View< T *, Kokkos::HostSpace > hostViewType1D
1D array (vector with host memeory space)
Definition: KokkosTypes.hpp:136
pFlow::max_serial
INLINE_FUNCTION_H T max_serial(const ViewType1D< T, properties... > &view, size_t start, size_t end)
Definition: baseAlgorithms_.hpp:115
LAMBDA_HD
#define LAMBDA_HD
Definition: pFlowMacros.hpp:58
numericConstants.hpp