www.cemf.ir
kokkosAlgorithms.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 #ifndef __kokkosAlgorithms_hpp__
23 #define __kokkosAlgorithms_hpp__
24 
25 #include "KokkosTypes.hpp"
26 
27 
29 {
30 
31 template<typename Type, typename ExecutionSpace>
33 uint32 count(const Type* first, uint32 numElems, const Type& val)
34 {
35  using policy = Kokkos::RangePolicy<
36  ExecutionSpace,
37  Kokkos::IndexType<uint32> >;
38  uint32 num = 0;
39  Kokkos::parallel_reduce("count",
40  policy(0, numElems),
41  LAMBDA_HD(uint32 i, uint32& updateVal){
42  if(equal(first[i],val)) updateVal++;
43  },
44  num);
45  return num;
46 }
47 
48 // fill should be done using deep_copy by kokkos
49 //void fill(Type* first, int32 numElems, const Type& val);
50 
51 template<typename Type, typename ExecutionSpace>
53 void fillSequence(Type* first, uint32 numElems, const Type& firstVal)
54 {
55 
56  using policy = Kokkos::RangePolicy<
57  ExecutionSpace,
58  Kokkos::IndexType<uint32> >;
59 
60  Kokkos::parallel_for(
61  "fillSequence",
62  policy(0, numElems),
63  LAMBDA_HD(uint32 i){
64  first[i] = firstVal+i;
65  });
66  Kokkos::fence();
67 }
68 
69 template<typename Type, typename indexType, typename ExecutionSpace>
71 void fillSelected(Type* first, const indexType* indices, const uint32 numElems, const Type val)
72 {
73  using policy = Kokkos::RangePolicy<
74  ExecutionSpace,
75  Kokkos::IndexType<uint32> >;
76  Kokkos::parallel_for(
77  "fillSelected",
78  policy(0,numElems),
79  LAMBDA_HD(uint32 i){
80  first[indices[i]]= val;
81  });
82  Kokkos::fence();
83 }
84 
85 template<typename Type, typename indexType, typename ExecutionSpace>
87 void fillSelected(Type* first, const indexType* indices, const Type* vals, const uint32 numElems)
88 {
89  using policy = Kokkos::RangePolicy<
90  ExecutionSpace,
91  Kokkos::IndexType<uint32> >;
92 
93  Kokkos::parallel_for(
94  "fillSelected",
95  policy(0,numElems),
96  LAMBDA_HD(uint32 i){
97  first[indices[i]]= vals[i];
98  });
99  Kokkos::fence();
100 }
101 
102 template<typename Type, typename ExecutionSpace>
104 Type max(const Type* first, uint32 numElems)
105 {
106  using policy = Kokkos::RangePolicy<
107  ExecutionSpace,
108  Kokkos::IndexType<uint32> >;
109  Type maxElement=0;
110 
111  Kokkos::parallel_reduce(
112  "max",
113  policy(0, numElems),
114  LAMBDA_HD(uint32 i, Type& maxUpdate){
115  if(maxUpdate<first[i]) maxUpdate = first[i];
116  },
117  Kokkos::Max<Type>(maxElement));
118 
119  return maxElement;
120 }
121 
122 template<typename Type, typename ExecutionSpace>
124 Type min(const Type* first, int32 numElems)
125 {
126  using policy = Kokkos::RangePolicy<
127  ExecutionSpace,
128  Kokkos::IndexType<int32> >;
129  Type minElement;
130 
131  Kokkos::parallel_reduce(
132  "min",
133  policy(0, numElems),
134  LAMBDA_HD(int32 i, Type& minUpdate){
135  if(first[i] < minUpdate) minUpdate = first[i];
136  },
137  Kokkos::Min<Type>(minElement));
138 
139  return minElement;
140 }
141 
142 // we either use CUDA or STD srots
143 //void sort(Type* first, int32 numElems);
144 //void sort(Type* first, int32 numElems, CompareFunc compare);
145 //void permuteSort(const Type* first, PermuteType* pFirst, int32 numElems);
146 
147 template<typename Type, typename ExecutionSpace>
148 void exclusiveScan(Type* first, Type* dFirst, uint32 numElems)
149 {
150  using policy = Kokkos::RangePolicy<
151  ExecutionSpace,
152  Kokkos::IndexType<uint32> >;
153 
154  Kokkos::parallel_scan(
155  "exclusiveScan",
156  policy(0, numElems),
157  LAMBDA_HD(const uint32 i, Type& valToUpdate, const bool final)
158  {
159  const Type val = first[i];
160  if(final)
161  dFirst[i] = valToUpdate;
162  valToUpdate += val;
163  });
164 }
165 
166 template<typename Type, typename ExecutionSpace>
167 void inclusiveScan(Type* first, Type* dFirst, uint32 numElems)
168 {
169  using policy = Kokkos::RangePolicy<
170  ExecutionSpace,
171  Kokkos::IndexType<uint32> >;
172 
173  Kokkos::parallel_scan(
174  "inclusiveScan",
175  policy(0, numElems),
176  LAMBDA_HD(const uint32 i, Type& valToUpdate, const bool final)
177  {
178  const Type val = first[i];
179  valToUpdate += val;
180  if(final)
181  dFirst[i] = valToUpdate;
182  });
183 }
184 
185 } //pFlow::algorithms::KOKKOS
186 
187 
188 #endif //__kokkosAlgorithms_hpp__
pFlow::algorithms::KOKKOS::fillSequence
INLINE_FUNCTION_H void fillSequence(Type *first, uint32 numElems, const Type &firstVal)
Definition: kokkosAlgorithms.hpp:53
pFlow::algorithms::KOKKOS::max
INLINE_FUNCTION_H Type max(const Type *first, uint32 numElems)
Definition: kokkosAlgorithms.hpp:104
pFlow::equal
INLINE_FUNCTION_HD bool equal(const box &b1, const box &b2, real tol=smallValue)
Definition: box.hpp:135
pFlow::algorithms::KOKKOS::min
INLINE_FUNCTION_H Type min(const Type *first, int32 numElems)
Definition: kokkosAlgorithms.hpp:124
pFlow::algorithms::KOKKOS::count
INLINE_FUNCTION_H uint32 count(const Type *first, uint32 numElems, const Type &val)
Definition: kokkosAlgorithms.hpp:33
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:56
KokkosTypes.hpp
pFlow::algorithms::KOKKOS::fillSelected
INLINE_FUNCTION_H void fillSelected(Type *first, const indexType *indices, const uint32 numElems, const Type val)
Definition: kokkosAlgorithms.hpp:71
policy
Kokkos::RangePolicy< pFlow::DefaultExecutionSpace, Kokkos::Schedule< Kokkos::Static >, Kokkos::IndexType< pFlow::uint32 > > policy
Definition: grainParticlesKernels.cpp:25
pFlow::int32
int int32
Definition: builtinTypes.hpp:50
INLINE_FUNCTION_H
#define INLINE_FUNCTION_H
Definition: pFlowMacros.hpp:57
pFlow::algorithms::KOKKOS::exclusiveScan
void exclusiveScan(Type *first, Type *dFirst, uint32 numElems)
Definition: kokkosAlgorithms.hpp:148
pFlow::algorithms::KOKKOS::inclusiveScan
void inclusiveScan(Type *first, Type *dFirst, uint32 numElems)
Definition: kokkosAlgorithms.hpp:167
pFlow::algorithms::KOKKOS
Definition: kokkosAlgorithms.hpp:28
LAMBDA_HD
#define LAMBDA_HD
Definition: pFlowMacros.hpp:58