forked from LLNL/RAJA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegment-indexset-basics_solution.cpp
286 lines (206 loc) · 7.79 KB
/
segment-indexset-basics_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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// 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 <cstring>
#include <iostream>
#include <vector>
#include <algorithm>
#include "RAJA/RAJA.hpp"
#include "camp/resource.hpp"
/*
* Segments and Index Sets exercise
*
* In this exercise, you will learn how to create RAJA segments and index sets
* and use them to execute kernels. There are no computations performed in the
* exercises and no parallel execution. The kernels contain only print
* statements to illustrate various iteration patterns. Thus, all kernels
* look the same. The only thing that changes in these versions is the object
* passed to the 'forall' method that defines the iteration space.
*
* RAJA features shown:
* - `forall` loop iteration template method
* - TypedRangeSegment iteration space
* - TypedRangeStrideSegment iteration space
* - TypedListSegment iteration space
* - TypedIndexSet segment container
* - Hierarchical execution policies
*/
//----------------------------------------------------------------------------//
// Define aliases for types used in the exercises
// (so example code is less verbose)
//----------------------------------------------------------------------------//
// _raja_segment_type_start
using IdxType = int;
using RangeSegType = RAJA::TypedRangeSegment<IdxType>;
using RangeStrideSegType = RAJA::TypedRangeStrideSegment<IdxType>;
using ListSegType = RAJA::TypedListSegment<IdxType>;
using IndexSetType = RAJA::TypedIndexSet< RangeSegType, ListSegType >;
// _raja_segment_type_end
int main(int RAJA_UNUSED_ARG(argc), char **RAJA_UNUSED_ARG(argv[]))
{
std::cout << "\n\nRAJA segments index sets and index sets...\n";
// Resource object used to construct list segment objects with indices
// living in host (CPU) memory.
camp::resources::Resource host_res{camp::resources::Host()};
//----------------------------------------------------------------------------//
// Stride-1 iteration spaces
//----------------------------------------------------------------------------//
std::cout << "\n Running C-version range kernel...\n";
// _cstyle_range1_start
for (IdxType i = 0; i < 20; i++) {
std::cout << i << " ";
}
// _cstyle_range1_end
std::cout << std::endl;
//----------------------------------//
std::cout << "\n Running RAJA range kernel...\n";
// _raja_range1_start
RAJA::forall<RAJA::seq_exec>(RangeSegType(0, 20), [=] (IdxType i) {
std::cout << i << " ";
});
// _raja_range1_end
std::cout << std::endl;
//----------------------------------//
std::cout << "\n Running RAJA stride-1 range kernel...\n";
// _raja_striderange1_start
RAJA::forall<RAJA::seq_exec>(RangeStrideSegType(0, 20, 1), [=] (IdxType i) {
std::cout << i << " ";
});
// _raja_striderange1_end
std::cout << std::endl;
//----------------------------------//
std::cout << "\n Running RAJA stride-1 list kernel...\n";
// _raja_list1_start
//
// Collect indices in a vector to create list segment
//
std::vector<IdxType> idx;
for (IdxType i = 0; i < 20; ++i) {
idx.push_back(i);
}
ListSegType idx_list1( idx, host_res );
RAJA::forall<RAJA::seq_exec>(idx_list1, [=] (IdxType i) {
std::cout << i << " ";
});
// _raja_list1_end
std::cout << std::endl;
//----------------------------------//
std::cout << "\n Running C-style stride-1 list kernel...\n";
// _cstyle_list1_start
IdxType iis = static_cast<IdxType>(idx.size()); // to avoid compiler warning
for (IdxType ii = 0; ii < iis; ++ii) {
std::cout << idx[ ii ] << " ";
}
// _cstyle_list1_end
std::cout << std::endl;
//----------------------------------------------------------------------------//
// Negative stride iteration spaces
//----------------------------------------------------------------------------//
std::cout << "\n Running C-version negative stride kernel...\n";
// _cstyle_negstriderange1_start
for (IdxType i = 19; i > -1; i--) {
std::cout << i << " ";
}
// _cstyle_negstriderange1_end
std::cout << std::endl;
//----------------------------------//
std::cout << "\n Running RAJA negative stride kernel...\n";
// _raja_negstriderange1_start
RAJA::forall<RAJA::seq_exec>(RangeStrideSegType(19, -1, -1), [=] (IdxType i) {
std::cout << i << " ";
});
// _raja_negstriderange1_end
std::cout << std::endl;
//----------------------------------//
// List variant
//----------------------------------//
std::cout << "\n Running RAJA negative stride list kernel...\n";
// _raja_negstridelist1_start
//
// Reverse the order of indices in the vector
//
std::reverse( idx.begin(), idx.end() );
ListSegType idx_list1_reverse( &idx[0], idx.size(), host_res );
RAJA::forall<RAJA::seq_exec>(idx_list1_reverse, [=] (IdxType i) {
std::cout << i << " ";
});
// _raja_negstridelist1_end
std::cout << std::endl;
//----------------------------------------------------------------------------//
// Non-unit uniform stride iteration spaces
//----------------------------------------------------------------------------//
std::cout << "\n Running C-version stride-2 range kernel...\n";
// _cstyle_range2_start
for (IdxType i = 0; i < 20; i += 2) {
std::cout << i << " ";
}
// _cstyle_range2_end
std::cout << std::endl;
//----------------------------------//
std::cout << "\n Running RAJA stride-2 range kernel...\n";
// _raja_range2_start
RAJA::forall<RAJA::seq_exec>(RangeStrideSegType(0, 20, 2), [=] (IdxType i) {
std::cout << i << " ";
});
// _raja_range2_end
std::cout << std::endl;
//----------------------------------//
std::cout << "\n Running RAJA stride-3 range kernel...\n";
// _raja_range3_start
RAJA::forall<RAJA::seq_exec>(RangeStrideSegType(0, 20, 3), [=] (IdxType i) {
std::cout << i << " ";
});
// _raja_range3_end
std::cout << std::endl;
//----------------------------------------------------------------------------//
// IndexSets: complex iteration spaces
//----------------------------------------------------------------------------//
//
// Sequential index set execution policy used in several of the following
// example implementations.
//
std::cout << "\n Running RAJA index set (2 RangeSegments) kernel...\n";
// _raja_indexset_2ranges_start
using SEQ_ISET_EXECPOL = RAJA::ExecPolicy<RAJA::seq_segit,
RAJA::seq_exec>;
IndexSetType is2;
is2.push_back( RangeSegType(0, 10) );
is2.push_back( RangeSegType(15, 20) );
RAJA::forall<SEQ_ISET_EXECPOL>(is2, [=] (IdxType i) {
std::cout << i << " ";
});
// _raja_indexset_2ranges_end
std::cout << std::endl;
//----------------------------------//
std::cout << "\n Running C-version of two segment kernel...\n";
// _cstyle_2ranges_start
for (IdxType i = 0; i < 10; ++i) {
std::cout << i << " ";
}
for (IdxType i = 15; i < 20; ++i) {
std::cout << i << " ";
}
// _cstyle_2ranges_end
std::cout << std::endl;
//----------------------------------//
std::cout << "\n Running RAJA index set (3 segments) kernel...\n";
// _raja_indexset_3segs_start
IndexSetType is3;
is3.push_back( RangeSegType(0, 8) );
IdxType indx[ ] = {10, 11, 14, 20, 22};
ListSegType list2( indx, 5, host_res );
is3.push_back( list2 );
is3.push_back( RangeSegType(24, 28) );
RAJA::forall<SEQ_ISET_EXECPOL>(is3, [=] (IdxType i) {
std::cout << i << " ";
});
// _raja_indexset_3segs_end
std::cout << std::endl;
//----------------------------------------------------------------------------//
std::cout << "\n DONE!...\n";
return 0;
}