-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMSD_GPU_kernels_2d.cu
202 lines (166 loc) · 8.1 KB
/
MSD_GPU_kernels_2d.cu
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
#include <cuda.h>
#include <cuda_runtime.h>
#include <cuda_runtime_api.h>
#include "MSD_GPU_kernels_shared.cu"
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
//------------------- Kernels
//-----------------------------------------------------------------------
//---------------> Computes partials for mean and standard deviation
template<typename input_type>
__global__ void MSD_GPU_calculate_partials_2d(input_type const* __restrict__ d_input, float *d_output_partial_MSD, int *d_output_partial_nElements, size_t dim_x, size_t dim_y, int offset) {
__shared__ float s_par_MSD[2*MSD_NTHREADS];
__shared__ int s_par_nElements[MSD_NTHREADS];
float M, S, ftemp;
int j;
size_t x_pos = blockIdx.x*MSD_NTHREADS + threadIdx.x;
size_t global_pos = blockIdx.z*dim_x*dim_y + blockIdx.y*MSD_Y_STEPS*dim_x + x_pos;
M=0; S=0; j=0;
if( x_pos<(dim_x-offset) ){
ftemp = (float) d_input[global_pos];
Initiate( &M, &S, &j, ftemp);
global_pos = global_pos + dim_x;
for (int y = 1; y < MSD_Y_STEPS; y++) {
if( (blockIdx.y*MSD_Y_STEPS + y)<dim_y ){
ftemp = (float) d_input[global_pos];
Add_one( &M, &S, &j, ftemp);
global_pos = global_pos + dim_x;
}
}
}
s_par_MSD[threadIdx.x] = M;
s_par_MSD[blockDim.x + threadIdx.x] = S;
s_par_nElements[threadIdx.x] = j;
__syncthreads();
Reduce_SM( &M, &S, &j, s_par_MSD, s_par_nElements );
Reduce_WARP( &M, &S, &j);
//----------------------------------------------
//---- Writing data
if (threadIdx.x == 0) {
global_pos = blockIdx.z*gridDim.y*gridDim.x + blockIdx.y*gridDim.x + blockIdx.x;
d_output_partial_MSD[MSD_PARTIAL_SIZE*global_pos] = M;
d_output_partial_MSD[MSD_PARTIAL_SIZE*global_pos + 1] = S;
d_output_partial_nElements[global_pos] = j;
//if(blockIdx.x<5 && blockIdx.y<5) printf("M=%f; S=%f; j=%e\n", M, S, (double) j);
}
}
template<typename input_type>
__global__ void MSD_GPU_calculate_partials_2d_and_minmax(input_type const* __restrict__ d_input, float *d_output_partial_MSD, int *d_output_partial_nElements, size_t dim_x, size_t dim_y, int offset) {
__shared__ float s_par_MSD[MSD_PARTIAL_SIZE*MSD_NTHREADS];
__shared__ int s_par_nElements[MSD_NTHREADS];
float M, S, max, min, ftemp;
int j;
size_t x_pos = blockIdx.x*MSD_NTHREADS + threadIdx.x;
size_t global_pos = blockIdx.z*dim_x*dim_y + blockIdx.y*MSD_Y_STEPS*dim_x + x_pos;
M=0; S=0; j=0;
if( x_pos<(dim_x-offset) ){
ftemp = (float) d_input[global_pos];
Initiate( &M, &S, &j, ftemp);
max = ftemp;
min = ftemp;
global_pos = global_pos + dim_x;
for (int y = 1; y < MSD_Y_STEPS; y++) {
if( (blockIdx.y*MSD_Y_STEPS + y)<dim_y ){
ftemp = (float) d_input[global_pos];
max = (fmaxf(max,ftemp));
min = (fminf(min,ftemp));
Add_one( &M, &S, &j, ftemp);
global_pos = global_pos + dim_x;
}
}
}
s_par_MSD[threadIdx.x] = M;
s_par_MSD[blockDim.x + threadIdx.x] = S;
s_par_MSD[2*blockDim.x + threadIdx.x] = max;
s_par_MSD[3*blockDim.x + threadIdx.x] = min;
s_par_nElements[threadIdx.x] = j;
__syncthreads();
Reduce_SM_max( &M, &S, &max, &min, &j, s_par_MSD, s_par_nElements );
Reduce_WARP_max( &M, &S, &max, &min, &j);
//----------------------------------------------
//---- Writing data
if (threadIdx.x == 0) {
global_pos = blockIdx.z*gridDim.y*gridDim.x + blockIdx.y*gridDim.x + blockIdx.x;
d_output_partial_MSD[MSD_PARTIAL_SIZE*global_pos] = M;
d_output_partial_MSD[MSD_PARTIAL_SIZE*global_pos + 1] = S;
d_output_partial_MSD[MSD_PARTIAL_SIZE*global_pos + 2] = max;
d_output_partial_MSD[MSD_PARTIAL_SIZE*global_pos + 3] = min;
d_output_partial_nElements[global_pos] = j;
//if(blockIdx.x<5 && blockIdx.y<5) printf("M=%f; S=%f; max=%f; min=%f; j=%e\n", M, S, max, min, (double) j);
}
}
template<typename input_type>
__global__ void MSD_BLN_calculate_partials_2d_and_minmax_with_outlier_rejection(input_type const* __restrict__ d_input, float *d_output_partial_MSD, int *d_output_partial_nElements, float *d_MSD, size_t dim_x, size_t dim_y, int offset, float bln_sigma_constant) {
__shared__ float s_par_MSD[MSD_PARTIAL_SIZE*MSD_NTHREADS];
__shared__ int s_par_nElements[MSD_NTHREADS];
float M, S, ftemp, max, min;
int j;
float limit_down = d_MSD[0] - bln_sigma_constant*d_MSD[1];
float limit_up = d_MSD[0] + bln_sigma_constant*d_MSD[1];
size_t temp_gpos = blockIdx.z*gridDim.y*gridDim.x + blockIdx.y*gridDim.x + blockIdx.x;
max = d_output_partial_MSD[MSD_PARTIAL_SIZE*temp_gpos + 2];
min = d_output_partial_MSD[MSD_PARTIAL_SIZE*temp_gpos + 3];
if( (min>limit_down) && (max < limit_up) ) return;
size_t x_pos = blockIdx.x*MSD_NTHREADS + threadIdx.x;
size_t global_pos = blockIdx.z*dim_x*dim_y + blockIdx.y*MSD_Y_STEPS*dim_x + x_pos;
M=0; S=0; j=0; max=0; min=0;
if( x_pos<(dim_x-offset) ){
for (int y = 0; y < MSD_Y_STEPS; y++) {
if( (blockIdx.y*MSD_Y_STEPS + y)<dim_y ){
ftemp = (float) d_input[global_pos];
if( (ftemp>limit_down) && (ftemp < limit_up) ){
if(j==0){
Initiate( &M, &S, &j, ftemp);
max = ftemp;
min = ftemp;
}
else{
Add_one( &M, &S, &j, ftemp);
max = fmaxf(max, ftemp);
min = fminf(min, ftemp);
}
}
global_pos = global_pos + dim_x;
}
}
}
s_par_MSD[threadIdx.x] = M;
s_par_MSD[blockDim.x + threadIdx.x] = S;
s_par_MSD[2*blockDim.x + threadIdx.x] = max;
s_par_MSD[3*blockDim.x + threadIdx.x] = min;
s_par_nElements[threadIdx.x] = j;
__syncthreads();
Reduce_SM_max( &M, &S, &max, &min, &j, s_par_MSD, s_par_nElements );
Reduce_WARP_max( &M, &S, &max, &min, &j);
//----------------------------------------------
//---- Writing data
if (threadIdx.x == 0) {
global_pos = blockIdx.z*gridDim.y*gridDim.x + blockIdx.y*gridDim.x + blockIdx.x;
d_output_partial_MSD[MSD_PARTIAL_SIZE*global_pos] = M;
d_output_partial_MSD[MSD_PARTIAL_SIZE*global_pos + 1] = S;
d_output_partial_MSD[MSD_PARTIAL_SIZE*global_pos + 2] = max;
d_output_partial_MSD[MSD_PARTIAL_SIZE*global_pos + 3] = min;
d_output_partial_nElements[global_pos] = j;
//if(blockIdx.x<5 && blockIdx.y<5) printf("M=%f; S=%f; max=%f; min=%f; j=%e\n", M, S, max, min, (double) j);
}
}
//------------------- Kernels with functions
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
template<typename input_type>
void call_MSD_GPU_calculate_partials_2d(const dim3 &grid_size, const dim3 &block_size, int shared_memory_bytes, cudaStream_t streams, input_type *d_input, float *d_output_partial_MSD, int *d_output_partial_nElements, size_t dim_x, size_t dim_y, int offset){
MSD_GPU_calculate_partials_2d<<< grid_size, block_size, shared_memory_bytes, streams>>>(d_input, d_output_partial_MSD, d_output_partial_nElements, dim_x, dim_y, offset);
}
template<typename input_type>
void call_MSD_GPU_calculate_partials_2d_and_minmax(const dim3 &grid_size, const dim3 &block_size, int shared_memory_bytes, cudaStream_t streams,
input_type *d_input, float *d_output_partial_MSD, int *d_output_partial_nElements, size_t dim_x, size_t dim_y, int offset) {
MSD_GPU_calculate_partials_2d_and_minmax<<< grid_size, block_size, shared_memory_bytes, streams>>>(d_input, d_output_partial_MSD, d_output_partial_nElements, dim_x, dim_y, offset);
}
template<typename input_type>
void call_MSD_BLN_calculate_partials_2d_and_minmax_with_outlier_rejection(const dim3 &grid_size, const dim3 &block_size, int shared_memory_bytes, cudaStream_t streams,
input_type *d_input, float *d_output_partial_MSD, int *d_output_partial_nElements, float *d_MSD, size_t dim_x, size_t dim_y, int offset, float bln_sigma_constant){
MSD_BLN_calculate_partials_2d_and_minmax_with_outlier_rejection<<< grid_size, block_size, shared_memory_bytes, streams>>>(d_input, d_output_partial_MSD, d_output_partial_nElements, d_MSD, dim_x, dim_y, offset, bln_sigma_constant);
}
//---------------------------------------------------------------------------<