forked from LLNL/RAJA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreductions_solution.cpp
258 lines (195 loc) · 7.67 KB
/
reductions_solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// Copyright (c) 2016-24, Lawrence Livermore National Security, LLC
// and RAJA project contributors. See the RAJA/LICENSE file for details.
//
// SPDX-License-Identifier: (BSD-3-Clause)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
#include <cstdlib>
#include <iostream>
#include <limits>
#include "memoryManager.hpp"
#include "RAJA/RAJA.hpp"
/*
* Reduction Example
*
* This example illustrates use of the RAJA reduction types: min, max,
* sum, min-loc, and max-loc.
*
* RAJA features shown:
* - `forall` loop iteration template method
* - Index range segment
* - Execution policies
* - Reduction types
*
* If CUDA is enabled, CUDA unified memory is used.
*/
/*
Specify the number of threads in a GPU thread block
*/
#if defined(RAJA_ENABLE_CUDA)
constexpr int CUDA_BLOCK_SIZE = 256;
#endif
#if defined(RAJA_ENABLE_HIP)
constexpr int HIP_BLOCK_SIZE = 256;
#endif
int main(int RAJA_UNUSED_ARG(argc), char** RAJA_UNUSED_ARG(argv[]))
{
std::cout << "\n\nRAJA reductions example...\n";
// _reductions_array_init_start
//
// Define array length
//
constexpr int N = 1000000;
//
// Allocate array data and initialize data to alternating sequence of 1, -1.
//
int* a = memoryManager::allocate<int>(N);
for (int i = 0; i < N; ++i) {
if ( i % 2 == 0 ) {
a[i] = 1;
} else {
a[i] = -1;
}
}
//
// Set min and max loc values
//
constexpr int minloc_ref = N / 2;
a[minloc_ref] = -100;
constexpr int maxloc_ref = N / 2 + 1;
a[maxloc_ref] = 100;
// _reductions_array_init_end
//
// Note: with this data initialization scheme, the following results will
// be observed for all reduction kernels below:
//
// - the sum will be zero
// - the min will be -100
// - the max will be 100
// - the min loc will be N/2
// - the max loc will be N/2 + 1
//
//
//
// Define index range for iterating over a elements in all examples
//
// _reductions_range_start
RAJA::TypedRangeSegment<int> arange(0, N);
// _reductions_range_end
//----------------------------------------------------------------------------//
std::cout << "\n Running RAJA sequential reductions...\n";
// _reductions_raja_seq_start
using EXEC_POL1 = RAJA::seq_exec;
using REDUCE_POL1 = RAJA::seq_reduce;
RAJA::ReduceSum<REDUCE_POL1, int> seq_sum(0);
RAJA::ReduceMin<REDUCE_POL1, int> seq_min(std::numeric_limits<int>::max());
RAJA::ReduceMax<REDUCE_POL1, int> seq_max(std::numeric_limits<int>::min());
RAJA::ReduceMinLoc<REDUCE_POL1, int> seq_minloc(std::numeric_limits<int>::max(), -1);
RAJA::ReduceMaxLoc<REDUCE_POL1, int> seq_maxloc(std::numeric_limits<int>::min(), -1);
RAJA::forall<EXEC_POL1>(arange, [=](int i) {
seq_sum += a[i];
seq_min.min(a[i]);
seq_max.max(a[i]);
seq_minloc.minloc(a[i], i);
seq_maxloc.maxloc(a[i], i);
});
std::cout << "\tsum = " << seq_sum.get() << std::endl;
std::cout << "\tmin = " << seq_min.get() << std::endl;
std::cout << "\tmax = " << seq_max.get() << std::endl;
std::cout << "\tmin, loc = " << seq_minloc.get() << " , "
<< seq_minloc.getLoc() << std::endl;
std::cout << "\tmax, loc = " << seq_maxloc.get() << " , "
<< seq_maxloc.getLoc() << std::endl;
// _reductions_raja_seq_end
//----------------------------------------------------------------------------//
#if defined(RAJA_ENABLE_OPENMP)
std::cout << "\n Running RAJA OpenMP reductions...\n";
// _reductions_raja_omppolicy_start
using EXEC_POL2 = RAJA::omp_parallel_for_exec;
using REDUCE_POL2 = RAJA::omp_reduce;
// _reductions_raja_omppolicy_end
RAJA::ReduceSum<REDUCE_POL2, int> omp_sum(0);
RAJA::ReduceMin<REDUCE_POL2, int> omp_min(std::numeric_limits<int>::max());
RAJA::ReduceMax<REDUCE_POL2, int> omp_max(std::numeric_limits<int>::min());
RAJA::ReduceMinLoc<REDUCE_POL2, int> omp_minloc(std::numeric_limits<int>::max(), -1);
RAJA::ReduceMaxLoc<REDUCE_POL2, int> omp_maxloc(std::numeric_limits<int>::min(), -1);
RAJA::forall<EXEC_POL2>(arange, [=](int i) {
omp_sum += a[i];
omp_min.min(a[i]);
omp_max.max(a[i]);
omp_minloc.minloc(a[i], i);
omp_maxloc.maxloc(a[i], i);
});
std::cout << "\tsum = " << omp_sum.get() << std::endl;
std::cout << "\tmin = " << omp_min.get() << std::endl;
std::cout << "\tmax = " << omp_max.get() << std::endl;
std::cout << "\tmin, loc = " << omp_minloc.get() << " , "
<< omp_minloc.getLoc() << std::endl;
std::cout << "\tmax, loc = " << omp_maxloc.get() << " , "
<< omp_maxloc.getLoc() << std::endl;
#endif
//----------------------------------------------------------------------------//
#if defined(RAJA_ENABLE_CUDA)
std::cout << "\n Running RAJA CUDA reductions...\n";
// _reductions_raja_cudapolicy_start
using EXEC_POL3 = RAJA::cuda_exec<CUDA_BLOCK_SIZE>;
using REDUCE_POL3 = RAJA::cuda_reduce;
// _reductions_raja_cudapolicy_end
RAJA::ReduceSum<REDUCE_POL3, int> cuda_sum(0);
RAJA::ReduceMin<REDUCE_POL3, int> cuda_min(std::numeric_limits<int>::max());
RAJA::ReduceMax<REDUCE_POL3, int> cuda_max(std::numeric_limits<int>::min());
RAJA::ReduceMinLoc<REDUCE_POL3, int> cuda_minloc(std::numeric_limits<int>::max(), -1);
RAJA::ReduceMaxLoc<REDUCE_POL3, int> cuda_maxloc(std::numeric_limits<int>::min(), -1);
RAJA::forall<EXEC_POL3>(arange, [=] RAJA_DEVICE (int i) {
cuda_sum += a[i];
cuda_min.min(a[i]);
cuda_max.max(a[i]);
cuda_minloc.minloc(a[i], i);
cuda_maxloc.maxloc(a[i], i);
});
std::cout << "\tsum = " << cuda_sum.get() << std::endl;
std::cout << "\tmin = " << cuda_min.get() << std::endl;
std::cout << "\tmax = " << cuda_max.get() << std::endl;
std::cout << "\tmin, loc = " << cuda_minloc.get() << " , "
<< cuda_minloc.getLoc() << std::endl;
std::cout << "\tmax, loc = " << cuda_maxloc.get() << " , "
<< cuda_maxloc.getLoc() << std::endl;
#endif
//----------------------------------------------------------------------------//
#if defined(RAJA_ENABLE_HIP)
std::cout << "\n Running RAJA HIP reductions...\n";
int* d_a = memoryManager::allocate_gpu<int>(N);
hipErrchk(hipMemcpy( d_a, a, N * sizeof(int), hipMemcpyHostToDevice ));
// _reductions_raja_hippolicy_start
using EXEC_POL3 = RAJA::hip_exec<HIP_BLOCK_SIZE>;
using REDUCE_POL3 = RAJA::hip_reduce;
// _reductions_raja_hippolicy_end
RAJA::ReduceSum<REDUCE_POL3, int> hip_sum(0);
RAJA::ReduceMin<REDUCE_POL3, int> hip_min(std::numeric_limits<int>::max());
RAJA::ReduceMax<REDUCE_POL3, int> hip_max(std::numeric_limits<int>::min());
RAJA::ReduceMinLoc<REDUCE_POL3, int> hip_minloc(std::numeric_limits<int>::max(), -1);
RAJA::ReduceMaxLoc<REDUCE_POL3, int> hip_maxloc(std::numeric_limits<int>::min(), -1);
RAJA::forall<EXEC_POL3>(arange, [=] RAJA_DEVICE (int i) {
hip_sum += d_a[i];
hip_min.min(d_a[i]);
hip_max.max(d_a[i]);
hip_minloc.minloc(d_a[i], i);
hip_maxloc.maxloc(d_a[i], i);
});
std::cout << "\tsum = " << hip_sum.get() << std::endl;
std::cout << "\tmin = " << hip_min.get() << std::endl;
std::cout << "\tmax = " << hip_max.get() << std::endl;
std::cout << "\tmin, loc = " << hip_minloc.get() << " , "
<< hip_minloc.getLoc() << std::endl;
std::cout << "\tmax, loc = " << hip_maxloc.get() << " , "
<< hip_maxloc.getLoc() << std::endl;
memoryManager::deallocate_gpu(d_a);
#endif
//----------------------------------------------------------------------------//
//
// Clean up.
//
memoryManager::deallocate(a);
std::cout << "\n DONE!...\n";
return 0;
}