-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathlaunch-device-policy-aliases.cpp
More file actions
188 lines (153 loc) · 5.31 KB
/
Copy pathlaunch-device-policy-aliases.cpp
File metadata and controls
188 lines (153 loc) · 5.31 KB
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
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// Copyright (c) Lawrence Livermore National Security, LLC and other
// RAJA Project Developers. See top-level LICENSE and COPYRIGHT
// files for dates and other details. No copyright assignment is required
// to contribute to RAJA.
//
// SPDX-License-Identifier: (BSD-3-Clause)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//
// RAJA::launch with device policy aliases
//
// This example shows how to write a single RAJA::launch kernel using the
// RAJA::device_* aliases so the same source can target CUDA, HIP, or SYCL
// without downstream #if/#ifdef in user code.
//
// It also demonstrates Teams/Threads ordering helpers:
// - RAJA::Teams(x,y,z) and RAJA::Threads(x,y,z) are RAJA's canonical
// ordering.
// - RAJA::Teams::sycl_order(dim0,dim1,dim2) and Threads::sycl_order(...)
// express SYCL's (dim0,dim1,dim2) ordering and map to RAJA (x,y,z).
//
#include <iostream>
#include <string>
#include "RAJA/RAJA.hpp"
#include "RAJA/util/resource.hpp"
/*
* Define host/device launch policies.
*
* When compiling for a GPU backend, the RAJA::device_* aliases resolve to the
* active backend (CUDA/HIP/SYCL). When compiling host-only, we fall back to
* pure host policies.
*/
using host_launch = RAJA::seq_launch_t;
#if defined(RAJA_GPU_ACTIVE)
using device_launch = RAJA::device_launch_t<false>;
#endif
using launch_policy = RAJA::LaunchPolicy<
host_launch
#if defined(RAJA_GPU_ACTIVE)
, device_launch
#endif
>;
/*
* Define team and thread loop policies.
*
* These use RAJA::device_* aliases on GPU and expand to seq_exec on host.
*/
using teams_x = RAJA::LoopPolicy<
RAJA::seq_exec
#if defined(RAJA_GPU_ACTIVE)
, RAJA::device_block_x_direct
#endif
>;
using teams_y = RAJA::LoopPolicy<
RAJA::seq_exec
#if defined(RAJA_GPU_ACTIVE)
, RAJA::device_block_y_direct
#endif
>;
using threads_x = RAJA::LoopPolicy<
RAJA::seq_exec
#if defined(RAJA_GPU_ACTIVE)
, RAJA::device_thread_x_direct
#endif
>;
using threads_y = RAJA::LoopPolicy<
RAJA::seq_exec
#if defined(RAJA_GPU_ACTIVE)
, RAJA::device_thread_y_direct
#endif
>;
int main(int argc, char** argv)
{
constexpr int nteams_x = 2;
constexpr int nteams_y = 3;
constexpr int nthreads_x = 4;
constexpr int nthreads_y = 5;
constexpr int len = nteams_y * nteams_x * nthreads_y * nthreads_x;
RAJA::ExecPlace place = RAJA::ExecPlace::HOST;
#if defined(RAJA_GPU_ACTIVE)
if (argc != 2) {
RAJA_ABORT_OR_THROW("Usage: launch-device-policy-aliases host|device");
}
std::string exec_space = argv[1];
if (!(exec_space.compare("host") == 0 || exec_space.compare("device") == 0)) {
RAJA_ABORT_OR_THROW("Usage: launch-device-policy-aliases host|device");
}
if (exec_space.compare("host") == 0) { place = RAJA::ExecPlace::HOST; }
if (exec_space.compare("device") == 0) { place = RAJA::ExecPlace::DEVICE; }
#else
(void)argc;
(void)argv;
#endif
// Default host resource for host allocations/checks.
RAJA::resources::Host host_res {};
int* out_host = host_res.allocate<int>(len);
for (int i = 0; i < len; ++i) {
out_host[i] = -1;
}
#if defined(RAJA_GPU_ACTIVE)
auto device_res = RAJA::resources::get_default_resource<device_launch>();
int* out_device = device_res.allocate<int>(len);
device_res.memcpy(out_device, out_host, sizeof(int) * len);
device_res.wait();
int* out_ptr = (place == RAJA::ExecPlace::DEVICE) ? out_device : out_host;
#else
int* out_ptr = out_host;
#endif
// RAJA launch grid configuration is specified in (x,y,z). For SYCL-minded
// users, sycl_order(dim0,dim1,dim2) provides an explicit mapping.
RAJA::LaunchParams params_raja(RAJA::Teams(nteams_x, nteams_y, 1),
RAJA::Threads(nthreads_x, nthreads_y, 1));
RAJA::LaunchParams params_sycl(
RAJA::Teams::sycl_order(/*dim0=*/1, /*dim1=*/nteams_y, /*dim2=*/nteams_x),
RAJA::Threads::sycl_order(/*dim0=*/1, /*dim1=*/nthreads_y,
/*dim2=*/nthreads_x));
// Use the RAJA-ordered params for execution (it is backend-independent).
// params_sycl is included to demonstrate equivalent spelling.
(void)params_sycl;
RAJA::launch<launch_policy>(place,
params_raja,
[=] RAJA_HOST_DEVICE(RAJA::LaunchContext ctx) {
RAJA::loop<teams_y>(ctx, RAJA::RangeSegment(0, nteams_y), [&] (int by) {
RAJA::loop<teams_x>(ctx, RAJA::RangeSegment(0, nteams_x), [&] (int bx) {
RAJA::loop<threads_y>(ctx, RAJA::RangeSegment(0, nthreads_y), [&] (int ty) {
RAJA::loop<threads_x>(ctx, RAJA::RangeSegment(0, nthreads_x), [&] (int tx) {
int i = (((by * nteams_x) + bx) * nthreads_y + ty) * nthreads_x + tx;
out_ptr[i] = i;
});
});
});
});
});
#if defined(RAJA_GPU_ACTIVE)
if (place == RAJA::ExecPlace::DEVICE) {
device_res.memcpy(out_host, out_device, sizeof(int) * len);
device_res.wait();
}
device_res.deallocate(out_device);
#endif
bool ok = true;
for (int i = 0; i < len; ++i) {
if (out_host[i] != i) {
ok = false;
std::cerr << "Mismatch at i=" << i << " got " << out_host[i]
<< " expected " << i << "\n";
break;
}
}
host_res.deallocate(out_host);
std::cout << (ok ? "PASS\n" : "FAIL\n");
return ok ? 0 : 1;
}