-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathscan_local.cu
More file actions
393 lines (332 loc) · 13.3 KB
/
scan_local.cu
File metadata and controls
393 lines (332 loc) · 13.3 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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/* Copyright 2022 NVIDIA Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "cunumeric/scan/scan_local.h"
#include "cunumeric/scan/scan_local_template.inl"
#include "cunumeric/unary/isnan.h"
#include <thrust/scan.h>
#include <thrust/execution_policy.h>
#include <thrust/iterator/transform_iterator.h>
#include "cunumeric/cuda_help.h"
namespace cunumeric {
using namespace Legion;
using namespace legate;
template <typename RES>
static __global__ void __launch_bounds__(THREADS_PER_BLOCK, MIN_CTAS_PER_SM)
lazy_kernel(RES* out, RES* sum_val)
{
const size_t idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx >= 1) return;
sum_val[0] = out[0];
}
template <typename RES, int DIM>
static __global__ void __launch_bounds__(THREADS_PER_BLOCK, MIN_CTAS_PER_SM) partition_sum(
RES* out, Buffer<RES, DIM> sum_val, const Pitches<DIM - 1> pitches, uint64_t len, uint64_t stride)
{
unsigned int tid = threadIdx.x;
uint64_t blid = blockIdx.x * blockDim.x;
uint64_t index = (blid + tid) * stride;
if (index < len) {
auto sum_valp = pitches.unflatten(index, Point<DIM>::ZEROES());
sum_valp[DIM - 1] = 0;
sum_val[sum_valp] = out[index + stride - 1];
}
}
template <typename RES, typename OP>
static __global__ void __launch_bounds__(THREADS_PER_BLOCK, MIN_CTAS_PER_SM)
cuda_add(RES* B, uint64_t len, uint64_t stride, OP func, RES* block_sum)
{
unsigned int tid = threadIdx.x;
unsigned int blid = blockIdx.x * blockDim.x;
uint64_t pad_stride = stride;
bool must_copy = true;
if (stride & (stride - 1)) {
pad_stride = 1 << (32 - __clz(stride));
must_copy = (tid & (pad_stride - 1)) < stride;
}
uint64_t blocks_per_batch;
bool last_block;
bool first_block;
blocks_per_batch = (stride - 1) / THREADS_PER_BLOCK + 1;
pad_stride = blocks_per_batch * THREADS_PER_BLOCK;
last_block = (blockIdx.x + 1) % blocks_per_batch == 0;
first_block = (blockIdx.x) % blocks_per_batch == 0;
int remaining_batch = stride % THREADS_PER_BLOCK;
if (remaining_batch == 0) { remaining_batch = THREADS_PER_BLOCK; }
must_copy = !last_block || (tid < remaining_batch);
int pad_per_batch = pad_stride - stride;
uint64_t idx0 = tid + blid;
uint64_t batch_id = idx0 / pad_stride;
idx0 = idx0 - pad_per_batch * batch_id;
if (idx0 < len && must_copy && !first_block) {
B[idx0] = func(block_sum[blockIdx.x - 1], B[idx0]);
}
}
template <typename RES, typename OP>
static __global__ void __launch_bounds__(THREADS_PER_BLOCK, MIN_CTAS_PER_SM) batch_scan_cuda(
const RES* A, RES* B, uint64_t len, uint64_t stride, OP func, RES identity, RES* block_sum)
{
__shared__ RES temp[THREADS_PER_BLOCK];
unsigned int tid = threadIdx.x;
unsigned int blid = blockIdx.x * blockDim.x;
uint64_t pad_stride = stride;
bool must_copy = true;
if (stride & (stride - 1)) {
pad_stride = 1 << (32 - __clz(stride));
must_copy = (tid & (pad_stride - 1)) < stride;
}
bool last_block;
if (pad_stride > THREADS_PER_BLOCK) {
uint64_t blocks_per_batch = (stride - 1) / THREADS_PER_BLOCK + 1;
pad_stride = blocks_per_batch * THREADS_PER_BLOCK;
last_block = (blockIdx.x + 1) % blocks_per_batch == 0;
int remaining_batch = stride % THREADS_PER_BLOCK;
if (remaining_batch == 0) { remaining_batch = THREADS_PER_BLOCK; }
must_copy = !last_block || (tid < remaining_batch);
}
int pad_per_batch = pad_stride - stride;
int n_batches_block = THREADS_PER_BLOCK / pad_stride;
uint64_t idx0 = tid + blid;
uint64_t batch_id = idx0 / pad_stride;
idx0 = idx0 - pad_per_batch * batch_id;
if (idx0 < len) {
temp[tid] = (must_copy) ? A[idx0] : identity;
__syncthreads();
if (!n_batches_block) {
n_batches_block = 1;
pad_stride = THREADS_PER_BLOCK;
}
for (int j = 0; j < n_batches_block; j++) {
int offset = j * pad_stride;
for (int i = 1; i <= pad_stride; i <<= 1) {
int index = ((tid + 1) * 2 * i - 1);
int index_block = offset + index;
if (index < (pad_stride)) {
temp[index_block] = func(temp[index_block - i], temp[index_block]);
}
__syncthreads();
}
for (int i = pad_stride >> 1; i > 0; i >>= 1) {
int index = ((tid + 1) * 2 * i - 1);
int index_block = offset + index;
if ((index + i) < (pad_stride)) {
temp[index_block + i] = func(temp[index_block], temp[index_block + i]);
}
__syncthreads();
}
}
if (must_copy) { B[idx0] = temp[tid]; }
if (block_sum != nullptr && tid == THREADS_PER_BLOCK - 1 && !last_block) {
block_sum[blockIdx.x] = temp[tid];
}
}
}
template <typename RES, typename OP>
static __global__ void __launch_bounds__(THREADS_PER_BLOCK, MIN_CTAS_PER_SM) batch_scan_cuda_nan(
const RES* A, RES* B, uint64_t len, uint64_t stride, OP func, RES identity, RES* block_sum)
{
__shared__ RES temp[THREADS_PER_BLOCK];
unsigned int tid = threadIdx.x;
unsigned int blid = blockIdx.x * blockDim.x;
uint64_t pad_stride = stride;
bool must_copy = true;
if (stride & (stride - 1)) {
pad_stride = 1 << (32 - __clz(stride));
must_copy = (tid & (pad_stride - 1)) < stride;
}
bool last_block;
if (pad_stride > THREADS_PER_BLOCK) {
uint64_t blocks_per_batch = (stride - 1) / THREADS_PER_BLOCK + 1;
pad_stride = blocks_per_batch * THREADS_PER_BLOCK;
last_block = (blockIdx.x + 1) % blocks_per_batch == 0;
int remaining_batch = stride % THREADS_PER_BLOCK;
if (remaining_batch == 0) { remaining_batch = THREADS_PER_BLOCK; }
must_copy = !last_block || (tid < remaining_batch);
}
int pad_per_batch = pad_stride - stride;
int n_batches_block = THREADS_PER_BLOCK / pad_stride;
uint64_t idx0 = tid + blid;
uint64_t batch_id = idx0 / pad_stride;
idx0 = idx0 - pad_per_batch * batch_id;
if (idx0 < len) {
RES val = (must_copy) ? A[idx0] : identity;
temp[tid] = cunumeric::is_nan(val) ? identity : val;
__syncthreads();
if (!n_batches_block) {
n_batches_block = 1;
pad_stride = THREADS_PER_BLOCK;
}
for (int j = 0; j < n_batches_block; j++) {
int offset = j * pad_stride;
for (int i = 1; i <= pad_stride; i <<= 1) {
int index = ((tid + 1) * 2 * i - 1);
int index_block = offset + index;
if (index < (pad_stride)) {
temp[index_block] = func(temp[index_block - i], temp[index_block]);
}
__syncthreads();
}
for (int i = pad_stride >> 1; i > 0; i >>= 1) {
int index = ((tid + 1) * 2 * i - 1);
int index_block = offset + index;
if ((index + i) < (pad_stride)) {
temp[index_block + i] = func(temp[index_block], temp[index_block + i]);
}
__syncthreads();
}
}
if (must_copy) { B[idx0] = temp[tid]; }
if (block_sum != nullptr && tid == THREADS_PER_BLOCK - 1 && !last_block) {
block_sum[blockIdx.x] = temp[tid];
}
}
}
template <typename RES, typename OP>
void cuda_scan(
const RES* A, RES* B, uint64_t len, uint64_t stride, OP func, RES identity, cudaStream_t stream)
{
assert(stride != 0);
uint64_t pad_stride = 1 << (32 - __builtin_clz(stride));
if (pad_stride > THREADS_PER_BLOCK) {
uint64_t blocks_per_batch = (stride - 1) / THREADS_PER_BLOCK + 1;
pad_stride = blocks_per_batch * THREADS_PER_BLOCK;
}
uint64_t pad_len = (len / stride) * pad_stride;
uint64_t grid_dim = (pad_len - 1) / THREADS_PER_BLOCK + 1;
RES* blocked_sum = nullptr;
uint64_t blocked_len, blocked_stride;
if (stride > THREADS_PER_BLOCK) {
blocked_len = grid_dim;
blocked_stride = grid_dim / (len / stride);
CHECK_CUDA(cudaMalloc(&blocked_sum, blocked_len * sizeof(RES)));
}
batch_scan_cuda<RES, OP>
<<<grid_dim, THREADS_PER_BLOCK, 0, stream>>>(A, B, len, stride, func, identity, blocked_sum);
CHECK_CUDA_STREAM(stream);
if (stride > THREADS_PER_BLOCK) {
cuda_scan(blocked_sum, blocked_sum, blocked_len, blocked_stride, func, identity, stream);
cuda_add<<<grid_dim, THREADS_PER_BLOCK, 0, stream>>>(B, len, stride, func, blocked_sum);
CHECK_CUDA_STREAM(stream);
}
if (stride > THREADS_PER_BLOCK) { CHECK_CUDA(cudaFree(blocked_sum)); }
}
template <typename RES, typename OP>
void cuda_scan_nan(
const RES* A, RES* B, uint64_t len, uint64_t stride, OP func, RES identity, cudaStream_t stream)
{
assert(stride != 0);
uint64_t pad_stride = 1 << (32 - __builtin_clz(stride));
if (pad_stride > THREADS_PER_BLOCK) {
uint64_t blocks_per_batch = (stride - 1) / THREADS_PER_BLOCK + 1;
pad_stride = blocks_per_batch * THREADS_PER_BLOCK;
}
uint64_t pad_len = (len / stride) * pad_stride;
uint64_t grid_dim = (pad_len - 1) / THREADS_PER_BLOCK + 1;
RES* blocked_sum = nullptr;
uint64_t blocked_len, blocked_stride;
if (stride > THREADS_PER_BLOCK) {
blocked_len = grid_dim;
blocked_stride = grid_dim / (len / stride);
CHECK_CUDA(cudaMalloc(&blocked_sum, blocked_len * sizeof(RES)));
}
batch_scan_cuda_nan<RES, OP>
<<<grid_dim, THREADS_PER_BLOCK, 0, stream>>>(A, B, len, stride, func, identity, blocked_sum);
CHECK_CUDA_STREAM(stream);
if (stride > THREADS_PER_BLOCK) {
cuda_scan(blocked_sum, blocked_sum, blocked_len, blocked_stride, func, identity, stream);
cuda_add<<<grid_dim, THREADS_PER_BLOCK, 0, stream>>>(B, len, stride, func, blocked_sum);
CHECK_CUDA_STREAM(stream);
}
if (stride > THREADS_PER_BLOCK) { CHECK_CUDA(cudaFree(blocked_sum)); }
}
template <ScanCode OP_CODE, LegateTypeCode CODE, int DIM>
struct ScanLocalImplBody<VariantKind::GPU, OP_CODE, CODE, DIM> {
using OP = ScanOp<OP_CODE, CODE>;
using VAL = legate_type_of<CODE>;
void operator()(OP func,
AccessorWO<VAL, DIM>& out,
const AccessorRO<VAL, DIM>& in,
Array& sum_vals,
const Pitches<DIM - 1>& pitches,
const Rect<DIM>& rect) const
{
auto outptr = out.ptr(rect.lo);
auto inptr = in.ptr(rect.lo);
auto volume = rect.volume();
auto stride = rect.hi[DIM - 1] - rect.lo[DIM - 1] + 1;
auto stream = get_cached_stream();
Point<DIM> extents = rect.hi - rect.lo + Point<DIM>::ONES();
extents[DIM - 1] = 1; // one element along scan axis
auto sum_valsptr = sum_vals.create_output_buffer<VAL, DIM>(extents, true);
VAL identity = (VAL)ScanOp<OP_CODE, CODE>::nan_identity;
if (volume == stride) {
// Thrust is slightly faster for the 1D case
thrust::inclusive_scan(thrust::cuda::par.on(stream), inptr, inptr + stride, outptr, func);
} else {
cuda_scan<VAL, OP>(inptr, outptr, volume, stride, func, identity, stream);
}
uint64_t grid_dim = ((volume / stride) - 1) / THREADS_PER_BLOCK + 1;
partition_sum<<<grid_dim, THREADS_PER_BLOCK, 0, stream>>>(
outptr, sum_valsptr, pitches, volume, stride);
CHECK_CUDA_STREAM(stream);
}
};
template <ScanCode OP_CODE, LegateTypeCode CODE, int DIM>
struct ScanLocalNanImplBody<VariantKind::GPU, OP_CODE, CODE, DIM> {
using OP = ScanOp<OP_CODE, CODE>;
using VAL = legate_type_of<CODE>;
struct convert_nan_func {
__device__ VAL operator()(VAL x)
{
return cunumeric::is_nan(x) ? (VAL)ScanOp<OP_CODE, CODE>::nan_identity : x;
}
};
void operator()(OP func,
AccessorWO<VAL, DIM>& out,
const AccessorRO<VAL, DIM>& in,
Array& sum_vals,
const Pitches<DIM - 1>& pitches,
const Rect<DIM>& rect) const
{
auto outptr = out.ptr(rect.lo);
auto inptr = in.ptr(rect.lo);
auto volume = rect.volume();
auto stride = rect.hi[DIM - 1] - rect.lo[DIM - 1] + 1;
auto stream = get_cached_stream();
Point<DIM> extents = rect.hi - rect.lo + Point<DIM>::ONES();
extents[DIM - 1] = 1; // one element along scan axis
auto sum_valsptr = sum_vals.create_output_buffer<VAL, DIM>(extents, true);
VAL identity = (VAL)ScanOp<OP_CODE, CODE>::nan_identity;
if (volume == stride) {
// Thrust is slightly faster for the 1D case
thrust::inclusive_scan(thrust::cuda::par.on(stream),
thrust::make_transform_iterator(inptr, convert_nan_func()),
thrust::make_transform_iterator(inptr + stride, convert_nan_func()),
outptr,
func);
} else {
cuda_scan_nan<VAL, OP>(inptr, outptr, volume, stride, func, identity, stream);
}
uint64_t grid_dim = ((volume / stride) - 1) / THREADS_PER_BLOCK + 1;
partition_sum<<<grid_dim, THREADS_PER_BLOCK, 0, stream>>>(
outptr, sum_valsptr, pitches, volume, stride);
CHECK_CUDA_STREAM(stream);
}
};
/*static*/ void ScanLocalTask::gpu_variant(TaskContext& context)
{
scan_local_template<VariantKind::GPU>(context);
}
} // namespace cunumeric