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