forked from NVIDIA/CUDALibrarySamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock_io_strided.hpp
More file actions
130 lines (120 loc) · 6.52 KB
/
block_io_strided.hpp
File metadata and controls
130 lines (120 loc) · 6.52 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
#ifndef CUFFTDX_EXAMPLE_BLOCK_IO_STRIDED_HPP
#define CUFFTDX_EXAMPLE_BLOCK_IO_STRIDED_HPP
#include "block_io.hpp"
namespace example {
template<class FFT>
struct io_strided: public io<FFT> {
using base_type = io<FFT>;
using complex_type = typename FFT::value_type;
using scalar_type = typename complex_type::value_type;
static inline __device__ unsigned int batch_id(unsigned int local_fft_id) {
unsigned int global_fft_id = blockIdx.x * FFT::ffts_per_block + local_fft_id;
return global_fft_id;
}
static inline __device__ unsigned int batch_offset_strided(unsigned int local_fft_id) {
return batch_id(local_fft_id);
}
template<unsigned int Stride, unsigned int Batches = Stride>
static inline __device__ void load_strided(const complex_type* input,
complex_type* thread_data,
unsigned int local_fft_id) {
// Calculate global offset of FFT batch
const unsigned int offset = batch_offset_strided(local_fft_id);
const unsigned int bid = batch_id(local_fft_id);
// Get stride, this shows how elements from batch should be split between threads
const unsigned int stride = Stride * base_type::stride_size();
unsigned int index = offset + (threadIdx.x * Stride);
for (unsigned int i = 0; i < FFT::elements_per_thread; i++) {
if ((i * base_type::stride_size() + threadIdx.x) < cufftdx::size_of<FFT>::value) {
if(bid < Batches) {
thread_data[i] = input[index];
}
index += stride;
}
}
}
template<unsigned int Stride, unsigned int Batches = Stride>
static inline __device__ void store_strided(const complex_type* thread_data,
complex_type* output,
unsigned int local_fft_id) {
const unsigned int offset = batch_offset_strided(local_fft_id);
const unsigned int bid = batch_id(local_fft_id);
const unsigned int stride = Stride * base_type::stride_size();
unsigned int index = offset + (threadIdx.x * Stride);
for (unsigned int i = 0; i < FFT::elements_per_thread; i++) {
if ((i * base_type::stride_size() + threadIdx.x) < cufftdx::size_of<FFT>::value) {
if(bid < Batches) {
output[index] = thread_data[i];
}
index += stride;
}
}
}
template<unsigned int Stride, unsigned int Batches = Stride>
static inline __device__ void load_strided(const complex_type* input,
complex_type* thread_data,
complex_type* shared_memory,
unsigned int local_fft_id) {
const unsigned int tid = threadIdx.x + blockDim.x * threadIdx.y;
const unsigned int tidx = tid / blockDim.y;
const unsigned int tidy = tid % blockDim.y;
// Calculate global offset of FFT batch
const unsigned int offset = batch_offset_strided(tidy);
const unsigned int bid = batch_id(tidy);
// Get stride, this shows how elements from batch should be split between threads
const unsigned int stride = Stride * base_type::stride_size();
unsigned int index = offset + (tidx * Stride);
unsigned int smem_index = tidx + tidy * blockDim.x;
for (unsigned int i = 0; i < FFT::elements_per_thread; i++) {
if ((i * base_type::stride_size() + tidx) < cufftdx::size_of<FFT>::value) {
if(bid < Batches) {
shared_memory[smem_index] = input[index];
}
index += stride;
smem_index += (blockDim.x * blockDim.y);
}
}
__syncthreads();
smem_index = threadIdx.x + threadIdx.y * blockDim.x;
for (unsigned int i = 0; i < FFT::elements_per_thread; i++) {
if ((i * base_type::stride_size() + threadIdx.x) < cufftdx::size_of<FFT>::value) {
thread_data[i] = shared_memory[smem_index];
smem_index += (blockDim.x * blockDim.y);
}
}
}
template<unsigned int Stride, unsigned int Batches = Stride>
static inline __device__ void store_strided(const complex_type* thread_data,
complex_type* shared_memory,
complex_type* output,
unsigned int local_fft_id) {
__syncthreads();
unsigned int smem_index = threadIdx.x + threadIdx.y * blockDim.x;
for (unsigned int i = 0; i < FFT::elements_per_thread; i++) {
if ((i * base_type::stride_size() + threadIdx.x) < cufftdx::size_of<FFT>::value) {
shared_memory[smem_index] = thread_data[i];
smem_index += (blockDim.x * blockDim.y);
}
}
__syncthreads();
const unsigned int tid = threadIdx.x + blockDim.x * threadIdx.y;
const unsigned int tidx = tid / blockDim.y;
const unsigned int tidy = tid % blockDim.y;
const unsigned int offset = batch_offset_strided(tidy);
const unsigned int bid = batch_id(tidy);
const unsigned int stride = Stride * base_type::stride_size();
unsigned int index = offset + (tidx * Stride);
smem_index = tidx + tidy * blockDim.x;
for (unsigned int i = 0; i < FFT::elements_per_thread; i++) {
if ((i * base_type::stride_size() + tidx) < cufftdx::size_of<FFT>::value) {
if(bid < Batches) {
output[index] = shared_memory[smem_index];
}
index += stride;
smem_index += (blockDim.x * blockDim.y);
}
}
}
};
} // namespace example
#endif // CUFFTDX_EXAMPLE_BLOCK_IO_STRIDED_HPP