cudaAlgorithms.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 __cudaAlgorithms_hpp__
23 #define __cudaAlgorithms_hpp__
24 
25 
26 #ifdef __CUDACC__
27 
28 #include <thrust/fill.h>
29 #include <thrust/count.h>
30 #include <thrust/scan.h>
31 #include <thrust/sort.h>
32 #include <thrust/extrema.h>
33 #include <thrust/execution_policy.h>
34 
35 
36 #include "pFlowMacros.hpp"
37 #include "algorithmFunctions.hpp"
38 #include "types.hpp"
39 
40 namespace pFlow::algorithms::CUDA
41 {
42 
43 template<typename Type>
45 int32 count(const Type* first, int32 numElems, const Type& val)
46 {
47  return thrust::count_if(
48  thrust::device,
49  first, first+numElems,
50  LAMBDA_HD(const Type& check){ return equal(check,val);} );
51 }
52 
53 
54 template<typename Type>
56 void fill(Type* first, int32 numElems, const Type& val)
57 {
58  thrust::fill(thrust::device, first, first+numElems, val);
59 }
60 
61 template<typename Type>
63 void fillSequence(Type* first, int32 numElems, const Type& firstVal)
64 {
65 
66  thrust::for_each_n(
67  thrust::device,
68  first,
69  numElems,
70  LAMBDA_HD(Type& ref){ ref = firstVal+(&ref - first);});
71 }
72 
73 template<typename Type, typename indexType>
75 void fillSelected(Type* first, const indexType* indices, const int32 numElems, const Type val)
76 {
77 
78  thrust::for_each_n(
79  thrust::device,
80  indices,
81  numElems,
82  LAMBDA_HD(indexType i){
83  first[i] = val;
84  });
85 
86 }
87 
88 template<typename Type>
90 Type max(const Type* first, int32 numElems)
91 {
92  Type resH;
93  auto* resD = thrust::max_element(
94  thrust::device,
95  first,
96  first+numElems,
97  less<Type>());
98 
99  cudaMemcpy(&resH, resD, sizeof(Type),cudaMemcpyDeviceToHost);
100  return resH;
101 }
102 
103 template<typename Type>
105 Type min(const Type* first, int32 numElems)
106 {
107  Type resH;
108  auto* resD = thrust::min_element(
109  thrust::device,
110  first,
111  first+numElems,
112  less<Type>());
113  cudaMemcpy(&resH, resD, sizeof(Type),cudaMemcpyDeviceToHost);
114  return resH;
115 }
116 
117 template<typename Type>
119 void sort(Type* first, int32 numElems)
120 {
121 
122  thrust::sort(
123  thrust::device,
124  first,
125  first+numElems,
126  less<Type>());
127 
128 }
129 
130 template<typename Type, typename CompareFunc>
132 void sort(Type* first, int32 numElems, CompareFunc compare)
133 {
134 
135  thrust::sort(
136  thrust::device,
137  first,
138  first+numElems,
139  compare);
140 
141 }
142 
143 template<typename Type, typename PermuteType>
145 void permuteSort(const Type* first, PermuteType* pFirst, int32 numElems)
146 {
147 
148  fillSequence(pFirst, numElems, static_cast<PermuteType>(0));
149  thrust::sort(
150  thrust::device,
151  pFirst,
152  pFirst+numElems,
153  LAMBDA_HD(const PermuteType& lhs, const PermuteType& rhs){
154  return first[lhs]<first[rhs]; });
155 }
156 
157 
158 template<typename Type, typename DestType>
159 void exclusiveScan(Type* first, DestType* dFirst, int32 numElems)
160 {
161  thrust::exclusive_scan(
162  thrust::device,
163  first, first+numElems,
164  dFirst, 0);
165 }
166 
167 template<typename Type, typename DestType>
168 void inclusiveScan(Type* first, DestType* dFirst, int32 numElems)
169 {
170  thrust::inclusive_scan(
171  thrust::device,
172  first, first+numElems,
173  dFirst);
174 }
175 
176 } // end of namespace
177 
178 
179 #endif //__CUDA__
180 
181 
182 #endif //__cudaAlgorithms_hpp__
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::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
types.hpp
algorithmFunctions.hpp
count_if
auto count_if(const Vector< T, Allocator > &vec, UnaryPredicate p)
pFlowMacros.hpp
pFlow::int32
int int32
Definition: builtinTypes.hpp:53
fill
void fill(Vector< T, Allocator > &vec, const T &val)
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
sort
void sort(Vector< T, Allocator > &vec)
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::sort
void sort(Vector< T, Allocator > &vec)
Definition: VectorAlgorithm.hpp:62
LAMBDA_HD
#define LAMBDA_HD
Definition: pFlowMacros.hpp:54
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
pFlow::equal
INLINE_FUNCTION_HD bool equal(const real &s1, const real &s2)
Definition: bTypesFunctions.hpp:188
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