forked from NVIDIA/CUDALibrarySamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock_io.hpp
More file actions
384 lines (350 loc) · 20.8 KB
/
block_io.hpp
File metadata and controls
384 lines (350 loc) · 20.8 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
#ifndef CUFFTDX_EXAMPLE_BLOCK_IO_HPP_
#define CUFFTDX_EXAMPLE_BLOCK_IO_HPP_
#include "fp16_common.hpp"
namespace example {
namespace __io {
template<bool InRRIILayout = false>
inline __device__ cufftdx::complex<__half2> convert_to_rrii(const cufftdx::complex<__half2>& value) {
return to_rrii(value);
}
template<>
inline __device__ cufftdx::complex<__half2> convert_to_rrii<true>(const cufftdx::complex<__half2>& value) {
return value;
}
template<bool InRIRILayout = false>
inline __device__ cufftdx::complex<__half2> convert_to_riri(const cufftdx::complex<__half2>& value) {
return to_riri(value);
}
template<>
inline __device__ cufftdx::complex<__half2> convert_to_riri<true>(const cufftdx::complex<__half2>& value) {
return value;
}
} // namespace __io
template<class FFT>
struct io {
using complex_type = typename FFT::value_type;
using scalar_type = typename complex_type::value_type;
static inline __device__ unsigned int stride_size() {
return FFT::stride;
}
static inline __device__ unsigned int batch_offset(unsigned int local_fft_id) {
unsigned int global_fft_id =
FFT::ffts_per_block == 1 ? blockIdx.x : (blockIdx.x * FFT::ffts_per_block + local_fft_id);
return cufftdx::size_of<FFT>::value * global_fft_id;
}
template<typename DataType>
static inline __device__ void copy(const DataType* source, DataType* target, unsigned int n) {
unsigned int stride = blockDim.x * blockDim.y;
unsigned int index = threadIdx.y * blockDim.x + threadIdx.x;
for (int step = 0; step < FFT::elements_per_thread; step++) {
if (index < n) {
target[index] = source[index];
}
index += stride;
}
}
template<class DataType>
static inline __device__ void load_to_smem(const DataType* global, unsigned char* shared) {
if (cufftdx::type_of<FFT>::value == cufftdx::fft_type::c2c) {
unsigned int input_length = blockDim.y * cufftdx::size_of<FFT>::value;
copy(reinterpret_cast<const complex_type*>(global),
reinterpret_cast<complex_type*>(shared),
input_length);
} else if (cufftdx::type_of<FFT>::value == cufftdx::fft_type::c2r) {
unsigned int input_length = blockDim.y * ((cufftdx::size_of<FFT>::value / 2) + 1);
copy(reinterpret_cast<const complex_type*>(global),
reinterpret_cast<complex_type*>(shared),
input_length);
} else if (cufftdx::type_of<FFT>::value == cufftdx::fft_type::r2c) {
unsigned int input_length = blockDim.y * cufftdx::size_of<FFT>::value;
copy(reinterpret_cast<const scalar_type*>(global),
reinterpret_cast<scalar_type*>(shared),
input_length);
}
__syncthreads();
}
template<class DataType>
static inline __device__ void store_from_smem(const unsigned char* shared, DataType* global) {
__syncthreads();
if (cufftdx::type_of<FFT>::value == cufftdx::fft_type::c2c) {
unsigned int output_length = blockDim.y * cufftdx::size_of<FFT>::value;
copy(reinterpret_cast<const complex_type*>(shared),
reinterpret_cast<complex_type*>(global),
output_length);
} else if (cufftdx::type_of<FFT>::value == cufftdx::fft_type::c2r) {
unsigned int output_length = blockDim.y * cufftdx::size_of<FFT>::value;
copy(reinterpret_cast<const scalar_type*>(shared),
reinterpret_cast<scalar_type*>(global),
output_length);
} else if (cufftdx::type_of<FFT>::value == cufftdx::fft_type::r2c) {
unsigned int output_length = blockDim.y * ((cufftdx::size_of<FFT>::value / 2) + 1);
copy(reinterpret_cast<const complex_type*>(shared),
reinterpret_cast<complex_type*>(global),
output_length);
}
}
template<cufftdx::fft_type FFTType = cufftdx::type_of<FFT>::value, class ComplexType = complex_type>
static inline __device__ auto load(const void* input,
ComplexType* thread_data,
const unsigned int local_fft_id) ->
typename std::enable_if<FFTType == cufftdx::fft_type::c2c>::type {
return load_c2c<ComplexType>((ComplexType*)input, thread_data, local_fft_id);
}
template<cufftdx::fft_type FFTType = cufftdx::type_of<FFT>::value, class ComplexType = complex_type>
static inline __device__ auto load(const void* input,
ComplexType* thread_data,
const unsigned int local_fft_id) ->
typename std::enable_if<FFTType == cufftdx::fft_type::c2r>::type {
return load_c2r<ComplexType>((ComplexType*)input, thread_data, local_fft_id);
}
template<cufftdx::fft_type FFTType = cufftdx::type_of<FFT>::value, class ComplexType = complex_type>
static inline __device__ auto load(const void* input,
ComplexType* thread_data,
const unsigned int local_fft_id) ->
typename std::enable_if<FFTType == cufftdx::fft_type::r2c>::type {
return load_r2c<ComplexType>((scalar_type*)input, thread_data, local_fft_id);
}
template<cufftdx::fft_type FFTType = cufftdx::type_of<FFT>::value, class ComplexType = complex_type>
static inline __device__ auto store(const ComplexType* thread_data,
void* output,
const unsigned int local_fft_id) ->
typename std::enable_if<FFTType == cufftdx::fft_type::c2c>::type {
return store_c2c<ComplexType>(thread_data, (ComplexType*)output, local_fft_id);
}
template<cufftdx::fft_type FFTType = cufftdx::type_of<FFT>::value, class ComplexType = complex_type>
static inline __device__ auto store(const ComplexType* thread_data,
void* output,
const unsigned int local_fft_id) ->
typename std::enable_if<FFTType == cufftdx::fft_type::c2r>::type {
return store_c2r<ComplexType>(thread_data, (scalar_type*)output, local_fft_id);
}
template<cufftdx::fft_type FFTType = cufftdx::type_of<FFT>::value, class ComplexType = complex_type>
static inline __device__ auto store(const ComplexType* thread_data,
void* output,
const unsigned int local_fft_id) ->
typename std::enable_if<FFTType == cufftdx::fft_type::r2c>::type {
return store_r2c<ComplexType>(thread_data, (ComplexType*)output, local_fft_id);
}
// input - global input with all FFTs
// thread_data - local thread array to load values from input to
// local_fft_id - ID of FFT batch in CUDA block
template<class ComplexType = complex_type>
static inline __device__ void load_c2c(const ComplexType* input,
ComplexType* thread_data,
unsigned int local_fft_id) {
// Calculate global offset of FFT batch
const unsigned int offset = batch_offset(local_fft_id);
// Get stride, this shows how elements from batch should be split between threads
const unsigned int stride = stride_size();
unsigned int index = offset + threadIdx.x;
for (unsigned int i = 0; i < FFT::elements_per_thread; i++) {
if ((i * stride + threadIdx.x) < cufftdx::size_of<FFT>::value) {
thread_data[i] = input[index];
index += stride;
}
}
}
// If InputInRRIILayout is false, then function assumes that values in input are in RIRI
// layout, and before loading them to thread_data they are converted to RRII layout.
// Otherwise, if InputInRRIILayout is true, then function assumes values in input are in RRII
// layout, and don't need to be converted before loading to thread_data.
template<bool InputInRRIILayout = false, class ComplexType = complex_type>
static inline __device__ void load(const cufftdx::complex<__half2>* input,
cufftdx::complex<__half2>* thread_data,
unsigned int local_fft_id) {
static_assert(std::is_same<ComplexType, cufftdx::complex<__half2>>::value,
"This can be only used with half precision FFTs");
// Calculate global offset of FFT batch
const unsigned int offset = batch_offset(local_fft_id);
// Get stride, this shows how elements from batch should be split between threads
const unsigned int stride = stride_size();
unsigned int index = offset + threadIdx.x;
for (unsigned int i = 0; i < FFT::elements_per_thread; i++) {
if ((i * stride + threadIdx.x) < cufftdx::size_of<FFT>::value) {
thread_data[i] = __io::convert_to_rrii<InputInRRIILayout>(input[index]);
index += stride;
}
}
}
template<class ComplexType = complex_type>
static inline __device__ void store_c2c(const ComplexType* thread_data,
ComplexType* output,
unsigned int local_fft_id) {
const unsigned int offset = batch_offset(local_fft_id);
const unsigned int stride = stride_size();
unsigned int index = offset + threadIdx.x;
for (unsigned int i = 0; i < FFT::elements_per_thread; i++) {
if ((i * stride + threadIdx.x) < cufftdx::size_of<FFT>::value) {
output[index] = thread_data[i];
index += stride;
}
}
}
// Function assumes that values in thread_data are in RRII layout.
// If OutputInRRIILayout is false, values are saved into output in RIRI layout; otherwise - in RRII.
template<bool OutputInRRIILayout = false, class ComplexType = complex_type>
static inline __device__ void store(const cufftdx::complex<__half2>* thread_data,
cufftdx::complex<__half2>* output,
unsigned int local_fft_id) {
static_assert(std::is_same<ComplexType, cufftdx::complex<__half2>>::value,
"This can be only used with half precision FFTs");
const unsigned int offset = batch_offset(local_fft_id);
const unsigned int stride = stride_size();
unsigned int index = offset + threadIdx.x;
for (unsigned int i = 0; i < FFT::elements_per_thread; i++) {
if ((i * stride + threadIdx.x) < cufftdx::size_of<FFT>::value) {
output[index] = __io::convert_to_riri<OutputInRRIILayout>(thread_data[i]);
index += stride;
}
}
}
static inline __device__ unsigned int batch_offset_r2c(unsigned int local_fft_id) {
unsigned int global_fft_id =
FFT::ffts_per_block == 1 ? blockIdx.x : (blockIdx.x * FFT::ffts_per_block + local_fft_id);
return ((cufftdx::size_of<FFT>::value / 2) + 1) * global_fft_id;
}
template<class ComplexType = complex_type>
static inline __device__ void load_r2c(const scalar_type* input,
ComplexType* thread_data,
unsigned int local_fft_id) {
// Calculate global offset of FFT batch
const unsigned int offset = batch_offset(local_fft_id);
// Get stride, this shows how elements from batch should be split between threads
const unsigned int stride = stride_size();
unsigned int index = offset + threadIdx.x;
for (unsigned int i = 0; i < FFT::elements_per_thread; i++) {
if ((i * stride + threadIdx.x) < cufftdx::size_of<FFT>::value) {
reinterpret_cast<scalar_type*>(thread_data)[i] = input[index];
index += stride;
}
}
}
template<class ComplexType = complex_type>
static inline __device__ void store_r2c(const ComplexType* thread_data,
ComplexType* output,
unsigned int local_fft_id) {
const unsigned int offset = batch_offset_r2c(local_fft_id);
const unsigned int stride = stride_size();
unsigned int index = offset + threadIdx.x;
for (unsigned int i = 0; i < FFT::elements_per_thread / 2 + 1; i++) {
if ((i * stride + threadIdx.x) < ((cufftdx::size_of<FFT>::value / 2) + 1)) {
output[index] = thread_data[i];
index += stride;
}
}
}
// Function assumes that values in thread_data are in RRII layout.
// If OutputInRRIILayout is false, values are saved into output in RIRI layout; otherwise - in RRII.
template<bool OutputInRRIILayout = false, class ComplexType = complex_type>
static inline __device__ void store_r2c(const cufftdx::complex<__half2>* thread_data,
cufftdx::complex<__half2>* output,
unsigned int local_fft_id) {
const unsigned int offset = batch_offset_r2c(local_fft_id);
const unsigned int stride = stride_size();
unsigned int index = offset + threadIdx.x;
for (unsigned int i = 0; i < FFT::elements_per_thread / 2 + 1; i++) {
if ((i * stride + threadIdx.x) < ((cufftdx::size_of<FFT>::value / 2) + 1)) {
output[index] = __io::convert_to_riri<OutputInRRIILayout>(thread_data[i]);
index += stride;
}
}
}
static inline __device__ unsigned int batch_offset_c2r(unsigned int local_fft_id) {
unsigned int global_fft_id =
FFT::ffts_per_block == 1 ? blockIdx.x : (blockIdx.x * FFT::ffts_per_block + local_fft_id);
return ((cufftdx::size_of<FFT>::value / 2) + 1) * global_fft_id;
}
template<class ComplexType = complex_type>
static inline __device__ void load_c2r(const ComplexType* input,
ComplexType* thread_data,
unsigned int local_fft_id) {
// Calculate global offset of FFT batch
const unsigned int offset = batch_offset_c2r(local_fft_id);
// Get stride, this shows how elements from batch should be split between threads
const unsigned int stride = stride_size();
unsigned int index = offset + threadIdx.x;
for (unsigned int i = 0; i < FFT::elements_per_thread / 2 + 1; i++) {
if ((i * stride + threadIdx.x) < (cufftdx::size_of<FFT>::value / 2 + 1)) {
thread_data[i] = input[index];
index += stride;
}
}
}
// If InputInRRIILayout is false, then function assumes that values in input are in RIRI
// layout, and before loading them to thread_data they are converted to RRII layout.
// Otherwise, if InputInRRIILayout is true, then function assumes values in input are in RRII
// layout, and don't need to be converted before loading to thread_data.
template<bool InputInRRIILayout = false, class ComplexType = complex_type>
static inline __device__ void load_c2r(const cufftdx::complex<__half2>* input,
cufftdx::complex<__half2>* thread_data,
unsigned int local_fft_id) {
// Calculate global offset of FFT batch
const unsigned int offset = batch_offset_c2r(local_fft_id);
// Get stride, this shows how elements from batch should be split between threads
const unsigned int stride = stride_size();
unsigned int index = offset + threadIdx.x;
for (unsigned int i = 0; i < FFT::elements_per_thread / 2 + 1; i++) {
if ((i * stride + threadIdx.x) < (cufftdx::size_of<FFT>::value / 2 + 1)) {
thread_data[i] = __io::convert_to_rrii<InputInRRIILayout>(input[index]);
index += stride;
}
}
}
template<class ComplexType = complex_type>
static inline __device__ void store_c2r(const ComplexType* thread_data,
scalar_type* output,
unsigned int local_fft_id) {
const unsigned int offset = batch_offset(local_fft_id);
const unsigned int stride = stride_size();
unsigned int index = offset + threadIdx.x;
for (unsigned int i = 0; i < FFT::elements_per_thread; i++) {
if ((i * stride + threadIdx.x) < cufftdx::size_of<FFT>::value) {
output[index] = reinterpret_cast<const scalar_type*>(thread_data)[i];
index += stride;
}
}
}
};
template<class FFT>
struct io_fp16 {
using complex_type = typename FFT::value_type;
using scalar_type = typename complex_type::value_type;
static_assert(std::is_same<scalar_type, __half2>::value, "This IO class is only for half precision FFTs");
static_assert((FFT::ffts_per_block % 2 == 0), "This IO class works only for even FFT::ffts_per_block");
static inline __device__ unsigned int stride_size() {
return cufftdx::size_of<FFT>::value / FFT::elements_per_thread;
}
static inline __device__ unsigned int batch_offset(unsigned int local_fft_id) {
unsigned int global_fft_id =
FFT::ffts_per_block == 1 ? blockIdx.x : (blockIdx.x * FFT::ffts_per_block + local_fft_id);
return cufftdx::size_of<FFT>::value * global_fft_id;
}
static inline __device__ void load(const __half2* input, complex_type* thread_data, unsigned int local_fft_id) {
// Calculate global offset of FFT batch
const unsigned int offset = batch_offset(local_fft_id);
// Get stride, this shows how elements from batch should be split between threads
const unsigned int stride = stride_size();
unsigned int index = offset + threadIdx.x;
// We bundle __half2 data of X-th and X+(FFT::ffts_per_block/2) batches together.
const unsigned int batch_stride = FFT::ffts_per_block/2 * cufftdx::size_of<FFT>::value;
for (unsigned int i = 0; i < FFT::elements_per_thread; i++) {
thread_data[i] = to_rrii(input[index], input[index + batch_stride]);
index += stride;
}
}
static inline __device__ void store(const complex_type* thread_data,
__half2* output,
unsigned int local_fft_id) {
const unsigned int offset = batch_offset(local_fft_id);
const unsigned int stride = stride_size();
unsigned int index = offset + threadIdx.x;
const unsigned int batch_stride = FFT::ffts_per_block/2 * cufftdx::size_of<FFT>::value;
for (unsigned int i = 0; i < FFT::elements_per_thread; i++) {
output[index] = to_ri1(thread_data[i]);
output[index + batch_stride] = to_ri2(thread_data[i]);
index += stride;
}
}
};
} // namespace example
#endif // CUFFTDX_EXAMPLE_BLOCK_IO_HPP_