-
Notifications
You must be signed in to change notification settings - Fork 0
/
2dconvol.cu
263 lines (200 loc) · 5.13 KB
/
2dconvol.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// For the CUDA runtime routines (prefixed with "cuda_")
#include <cuda_runtime.h>
int M=0, N=0, M_final, N_final;
int J=0, K=0;
long pos;
double A[10000][10000], H[10][10];
/*--------------------------------
Reading from the input matrix file
=> A of size M_final x N_final
=> H of size J x K
---------------------------------*/
void file_read(FILE *file){
int i, j;
if (file == NULL) {
printf("Error: file is not provided.");
return;;
}
char c, c1;
int space, space1 ,flag=0 ;
for(i = 0; i < 1000000; i++){
space1 = space;
space = 0;
M++;
for(j = 0; j < 100000; j++){
c=fgetc(file);
//printf("%d\t", c);
if(c == 32){
space++;
}
else if (c == '\n'){
//printf("hello\n");
break;
}
}
if(space==0) break;
}
N = space1 + 1;
M = M-1;
printf("size of array A is %d x %d\n",M,N);
rewind(file);
M_final = M;
N_final = N;
for(i=0;i<M;i++){
for(j=0;j<N;j++){
fscanf(file,"%lf",&A[i][j]);
}
}
pos = ftell(file);
rewind(file);
M=0;
for(i = 0; i < 1000000; i++){
if(space==0) M=0;
space1 = space;
space = 0;
M++;
for(j = 0; j < 100000; j++){
c1=c;
c=fgetc(file);
//printf("%d\t", c);
if(c == 32){
space++;
}
else if(c == '\n'){
break;
}
else if(c == EOF){
flag = 1;
break;
}
}
//printf("\n");
//printf("line = %d , space = %d\n",M, space);
if(flag == 1){
break;
}
}
K = space1+1;
if(c1=='\n'){
J = M-1;
}
else{
J = M;
}
printf("size of array H is %d x %d\n",J,K);
fseek(file, pos, SEEK_SET);
for(i=0;i<J;i++){
for(j=0;j<K;j++){
fscanf(file,"%lf",&H[i][j]);
}
}
rewind(file);
}
__global__ void convolution(double *d_A, double *d_C, int size_c, int J, int K, int M_final, int N_final)
{
int tidx = blockIdx.x*blockDim.x+threadIdx.x;
int tidy = blockIdx.y*blockDim.y+threadIdx.y;
int j,k;
double sum;
__shared__ double shared_H[100];
if(tidy%32<J && tidx%32<K){
shared_H[(tidy%32)*K+(tidx%32)] = d_A[(M_final*N_final)+(tidy%32)*K+(tidx%32)];
}
__syncthreads();
if (tidy<(M_final+J-1) && tidx<(N_final+K-1)){
sum=0;
for(j=0;j<J;j++){
for(k=0;k<K;k++){
if(!((tidy-j)<0 || (tidx-k)<0 || (tidy-j)>=M_final || (tidx-k)>=N_final)){
sum += d_A[((tidy-j)*N_final)+(tidx-k)]*shared_H[j*K+k];
}
}
}
d_C[tidy*(N_final+K-1)+tidx]=sum;
}
}
int main(int argc, char** argv){
char *inputfile;
double *d_A = NULL, *d_C = NULL;
cudaError_t err = cudaSuccess;
inputfile = argv[1];
int m;
FILE *fp = fopen(inputfile, "r");
file_read(fp);
int size_c = (M_final+J-1)*(N_final+K-1);
size_t size_A = ((M_final*N_final)+(J*K)) * sizeof(double);
size_t size_C = (M_final+J-1) * (N_final+K-1) * sizeof(double);
double *h_A, *h_C;
h_A = (double*) malloc (((M_final*N_final)+(J*K))*sizeof(double));
h_C = (double*) malloc ((M_final+J-1)*(N_final+K-1)*sizeof(double));
printf("\nh_A: %dx%d\n",M_final,N_final);
for(int i=0;i<M_final;i++){
for(int j=0;j<N_final;j++){
h_A[i*N_final+j]=A[i][j];
}
}
printf("\nh_H: %dx%d\n",J,K);
for(int i=0;i<J;i++){
for(int j=0;j<K;j++){
h_A[(M_final*N_final)+i*K+j]=H[i][j];
}
}
printf("Allocating memory on the CUDA device\n");
err = cudaMalloc((void **)&d_A, size_A);
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to allocate device vector A (error code %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
err = cudaMalloc((void **)&d_C, size_C);
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to allocate device vector C (error code %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
printf("Size of C = %dx%d\n",M_final+J-1,N_final+K-1);
printf("Copy input data from the host memory to the CUDA device\n");
err = cudaMemcpy(d_A,h_A,size_A,cudaMemcpyHostToDevice);
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to Copy device vector A (error code %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
dim3 Grid_Size(((N_final+K-2)/32)+1,((M_final+J-2)/32)+1,1);
dim3 Block_Size(32,32,1);
printf("No. of Blocks Launched = %dx%d\n",((N_final+K-2)/32)+1,((M_final+J-2)/32)+1);
convolution<<<Grid_Size,Block_Size>>>(d_A,d_C,size_c,J,K,M_final,N_final);
err = cudaGetLastError();
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to launch vectorAdd kernel (error code: %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
err = cudaDeviceSynchronize();
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to synchronize the device (error code: %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
err = cudaMemcpy(h_C,d_C,size_C,cudaMemcpyDeviceToHost);
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to Copy the result back (error code: %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
printf("Copied!!\n");
for(m=0;m<size_c;m++){
printf("C[%d] = %.3lf\n",m,h_C[m]);
}
cudaFree(d_A);
cudaFree(d_C);
free(h_A);
free(h_C);
cudaDeviceReset();
printf("-----------------Done and Dusted----------------\n");
fclose(fp);
return 0;
}