forked from anighose25/HP3Lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fusion_coarsening.cu
297 lines (232 loc) · 8.24 KB
/
fusion_coarsening.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
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
#include <stdio.h>
#include <cuda.h>
#include <cuda_runtime.h>
#define LOG_INPUT if(1)
#define LOG_SCAN if(0)
#define LOG_OUTPUT if(1)
void print_array(float *A, int N)
{
for(int i=0;i<N;i++)
printf("%.2f ",A[i]);
printf("\n");
}
#define TILE_SIZE 512
__global__ void Convolution1D(float *N, float *M, float *P, int Mask_Width, int Width){
int i = blockIdx.x*blockDim.x + threadIdx.x;
__shared__ float N_ds[TILE_SIZE];
N_ds[threadIdx.x] = N[i];
__syncthreads();
int This_tile_start_point = blockIdx.x * blockDim.x;
int Next_tile_start_point = (blockIdx.x + 1) * blockDim.x;
int N_start_point = i - (Mask_Width/2);
float Pvalue = 0;
for (int j = 0; j < Mask_Width; j ++) {
int N_index = N_start_point + j;
if (N_index >= 0 && N_index < Width) {
if ((N_index >= This_tile_start_point) && (N_index < Next_tile_start_point))
Pvalue += N_ds[threadIdx.x+j-(Mask_Width/2)]*M[j];
else
Pvalue += N[N_index] * M[j];
}
}
P[i] = Pvalue;
}
__global__ void ConvolutionPoolingFused(float *N, float *M, float *P, float *A, int Mask_Width, int Width){
__shared__ float N_ds[TILE_SIZE];
__shared__ float P_ds[TILE_SIZE];
unsigned int tid0 = ( threadIdx .x /16) *16*2 + threadIdx .x %16;
unsigned int tid1 = tid0 + 16;
unsigned int data_point0 = blockIdx .x *2* blockDim .x + tid0 ;
unsigned int data_point1 = blockIdx .x *2* blockDim .x + tid1 ;
N_ds[tid0] = N[data_point0] ;
N_ds[tid1] = N[data_point1];
__syncthreads ();
int cur_tile_start_point = blockIdx.x *2 * blockDim.x;
int next_tile_start_point = (blockIdx.x + 1) *2* blockDim.x;
int N_start_point0 = data_point0 - (Mask_Width/2);
int N_start_point1 = data_point1 - (Mask_Width/2);
float PValue0 = 0;
float PValue1 = 0;
for (int j = 0; j < Mask_Width; j ++) {
int N_index0 = N_start_point0 + j;
int N_index1 = N_start_point1 + j;
if (N_index0 >= 0 && N_index0 < Width) {
if ((N_index0 >= cur_tile_start_point) && (N_index0 < next_tile_start_point) )
PValue0 += N_ds[tid0 +j-(Mask_Width/2)]*M[j];
else
PValue0 += N[N_index0] * M[j];
}
if (N_index1 >= 0 && N_index1 < Width) {
if ((N_index1 >= cur_tile_start_point) && (N_index1 < next_tile_start_point) )
PValue1 += N_ds[tid1 +j-(Mask_Width/2)]*M[j];
else
PValue1 += N[N_index1] * M[j];
}
}
__syncthreads ();
P_ds[tid0] = PValue0;
P_ds[tid1] = PValue1;
__syncthreads();
tid0 = threadIdx.x;
int bound0 = blockDim.x;
tid1 = blockDim.x + threadIdx.x;
int bound1 = blockDim.x + blockDim.x;
int blockSize = blockDim.x;
for(int s=1;s<blockSize;s*=2)
{
if(tid0%2*s == 0 && tid0+s<bound0)
P_ds[tid0] = max(P_ds[tid0],P_ds[tid0+s]);
__syncthreads();
if(tid1%2*s == 0 && tid1+s<bound1)
P_ds[tid1] = max(P_ds[tid1],P_ds[tid1+s]);
__syncthreads();
}
if(threadIdx.x == 0)
{
A[2*blockIdx.x] = P_ds[tid0];
A[2*blockIdx.x+1] = P_ds[tid0 + blockDim.x];
}
}
__global__ void Pool1D(float* A, float* B, int N)
{
int tid = (blockIdx.x) * blockDim.x + threadIdx.x;
int bound = (blockIdx.x) * blockDim.x + blockDim.x;
int blockSize = blockDim.x;
for(int s=1;s<blockSize;s*=2)
{
if(tid%2*s == 0 && tid+s<bound)
A[tid] = max(A[tid],A[tid+s]);
__syncthreads();
}
if(threadIdx.x == 0)
B[blockIdx.x] = A[(blockIdx.x) * blockDim.x];
}
int main(void)
{
cudaError_t err = cudaSuccess;
int numElements;
int numMaskElements;
int pooling_size;
scanf("%d",&numElements);
scanf("%d",&numMaskElements);
scanf("%d",&pooling_size);
size_t size = numElements * sizeof(float);
size_t sizeMask = numMaskElements * sizeof(float);
float *h_input1 = (float *)malloc(size);
float *h_input2 = (float *)malloc(sizeMask);
float *h_output1 = (float *)malloc(size);
float *h_output2 = (float *)malloc(size);
if (h_input1 == NULL || h_input2 == NULL || h_output1 == NULL || h_output2 == NULL )
{
fprintf(stderr, "Failed to allocate host vectors!\n");
exit(EXIT_FAILURE);
}
LOG_INPUT
for (int i = 0; i < numElements; ++i){
h_input1[i] = 1.0;
}
for(int i=0;i<numMaskElements;++i){
h_input2[i] = 0.25;
}
LOG_SCAN
{
for (int i = 0; i < numElements; ++i)
{
scanf("%f",&h_input1[i]);
}
for (int i = 0; i < numMaskElements; ++i)
{
scanf("%f",&h_input2[i]);
}
}
LOG_INPUT print_array(h_input1,numElements);
LOG_INPUT print_array(h_input2,numMaskElements);
//H2D Transfers
float *d_input1 = NULL;
err = cudaMalloc((void **)&d_input1, size);
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to allocate device vector d_input1 (error code %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
float *d_input2 = NULL;
err = cudaMalloc((void **)&d_input2, sizeMask);
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to allocate device vector d_input2 (error code %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
float *d_output1 = NULL;
err = cudaMalloc((void **)&d_output1, size);
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to allocate device vector h_output1 (error code %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
float *d_output2 = NULL;
err = cudaMalloc((void **)&d_output2, size/pooling_size);
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to allocate device vector h_output1 (error code %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
err = cudaMemcpy(d_input1, h_input1, size, cudaMemcpyHostToDevice);
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to copy vector h_input1 from host to device (error code %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
err = cudaMemcpy(d_input2, h_input2, sizeMask, cudaMemcpyHostToDevice);
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to copy vector h_input2 from host to device (error code %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
int block_size=512;
int grid_size=numElements/block_size;
//Launch kernel
Convolution1D<<<grid_size, block_size>>>(d_input1, d_input2, d_output1, numMaskElements,numElements);
err = cudaGetLastError();
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to launch Convolution1D kernel (error code %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
// D2H transfer
err = cudaMemcpy(h_output1, d_output1, size, cudaMemcpyDeviceToHost);
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to copy vector d_output1 from device to host (error code %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
LOG_OUTPUT printf("Output of convolution\n");
LOG_OUTPUT print_array(h_output1,numElements);
grid_size=numElements/pooling_size;
block_size=pooling_size;
Pool1D<<<grid_size,block_size>>>(d_output1,d_output2,numElements);
err = cudaGetLastError();
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to launch Pooling1D kernel (error code %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
err = cudaMemcpy(h_output2, d_output2, size/pooling_size, cudaMemcpyDeviceToHost);
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to copy vector d_output2 from device to host (error code %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
LOG_OUTPUT printf("Output of pooling\n");
LOG_OUTPUT print_array(h_output2,numElements/pooling_size);
grid_size=numElements/512;
block_size=pooling_size;
ConvolutionPoolingFused<<<grid_size,block_size>>>(d_input1, d_input2, d_output1, d_output2, numMaskElements,numElements);
err = cudaMemcpy(h_output2, d_output2, size/pooling_size, cudaMemcpyDeviceToHost);
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to copy vector d_output2 from device to host (error code %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
LOG_OUTPUT printf("Output of pooling\n");
LOG_OUTPUT print_array(h_output2,numElements/pooling_size);
}