-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmp6.cu
133 lines (102 loc) · 5.03 KB
/
mp6.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
#include <wb.h>
#define wbCheck(stmt) do { \
cudaError_t err = stmt; \
if (err != cudaSuccess) { \
wbLog(ERROR, "Failed to run stmt ", #stmt); \
wbLog(ERROR, "Got CUDA error ... ", cudaGetErrorString(err)); \
return -1; \
} \
} while(0)
#define Mask_width 5
#define Mask_radius Mask_width/2
#define O_TILE_WIDTH 16
#define BLOCK_WIDTH (O_TILE_WIDTH + Mask_width - 1)
//@@ INSERT CODE HERE
__global__ void convolution(float *deviceInputImageData, const float * __restrict__ deviceMaskData, float *deviceOutputImageData, int imageWidth, int imageHeight, int imageChannels) {
int tx = threadIdx.x, ty = threadIdx.y;
int row_o = blockIdx.y * O_TILE_WIDTH + ty, col_o = blockIdx.x * O_TILE_WIDTH + tx;
int row_i = row_o - Mask_radius, col_i = col_o - Mask_radius;
for(int channel = 0; channel < imageChannels; channel++) {
__shared__ float s_input[BLOCK_WIDTH][BLOCK_WIDTH];
if(row_i >= 0 && row_i < imageHeight && col_i >= 0 && col_i < imageWidth) s_input[ty][tx] = deviceInputImageData[(row_i * imageWidth + col_i) * imageChannels + channel];
else s_input[ty][tx] = 0;
__syncthreads();
float output = 0;
if(ty < O_TILE_WIDTH && tx < O_TILE_WIDTH) {
for(int i = 0; i < Mask_width; i++)
for(int j = 0; j < Mask_width; j++)
output += (deviceMaskData[i * Mask_width + j] * s_input[i+ty][j+tx]);
if(row_o < imageHeight && col_o < imageWidth)
deviceOutputImageData[(row_o * imageWidth + col_o) * imageChannels + channel] = output;
}
__syncthreads();
}
}
int main(int argc, char* argv[]) {
wbArg_t args;
int maskRows;
int maskColumns;
int imageChannels;
int imageWidth;
int imageHeight;
char * inputImageFile;
char * inputMaskFile;
wbImage_t inputImage;
wbImage_t outputImage;
float * hostInputImageData;
float * hostOutputImageData;
float * hostMaskData;
float * deviceInputImageData;
float * deviceOutputImageData;
float * deviceMaskData;
args = wbArg_read(argc, argv); /* parse the input arguments */
inputImageFile = wbArg_getInputFile(args, 0);
inputMaskFile = wbArg_getInputFile(args, 1);
inputImage = wbImport(inputImageFile);
hostMaskData = (float *) wbImport(inputMaskFile, &maskRows, &maskColumns);
assert(maskRows == 5); /* mask height is fixed to 5 in this mp */
assert(maskColumns == 5); /* mask width is fixed to 5 in this mp */
imageWidth = wbImage_getWidth(inputImage);
imageHeight = wbImage_getHeight(inputImage);
imageChannels = wbImage_getChannels(inputImage);
outputImage = wbImage_new(imageWidth, imageHeight, imageChannels);
hostInputImageData = wbImage_getData(inputImage);
hostOutputImageData = wbImage_getData(outputImage);
wbTime_start(GPU, "Doing GPU Computation (memory + compute)");
wbTime_start(GPU, "Doing GPU memory allocation");
cudaMalloc((void **) &deviceInputImageData, imageWidth * imageHeight * imageChannels * sizeof(float));
cudaMalloc((void **) &deviceOutputImageData, imageWidth * imageHeight * imageChannels * sizeof(float));
cudaMalloc((void **) &deviceMaskData, maskRows * maskColumns * sizeof(float));
wbTime_stop(GPU, "Doing GPU memory allocation");
wbTime_start(Copy, "Copying data to the GPU");
cudaMemcpy(deviceInputImageData,
hostInputImageData,
imageWidth * imageHeight * imageChannels * sizeof(float),
cudaMemcpyHostToDevice);
cudaMemcpy(deviceMaskData,
hostMaskData,
maskRows * maskColumns * sizeof(float),
cudaMemcpyHostToDevice);
wbTime_stop(Copy, "Copying data to the GPU");
wbTime_start(Compute, "Doing the computation on the GPU");
//@@ INSERT CODE HERE
dim3 dimBlock(BLOCK_WIDTH, BLOCK_WIDTH, 1);
dim3 dimGrid((imageWidth-1)/O_TILE_WIDTH + 1, (imageHeight-1)/O_TILE_WIDTH + 1, 1);
convolution<<<dimGrid, dimBlock>>>(deviceInputImageData, deviceMaskData, deviceOutputImageData, imageWidth, imageHeight, imageChannels);
wbTime_stop(Compute, "Doing the computation on the GPU");
wbTime_start(Copy, "Copying data from the GPU");
cudaMemcpy(hostOutputImageData,
deviceOutputImageData,
imageWidth * imageHeight * imageChannels * sizeof(float),
cudaMemcpyDeviceToHost);
wbTime_stop(Copy, "Copying data from the GPU");
wbTime_stop(GPU, "Doing GPU Computation (memory + compute)");
wbSolution(args, outputImage);
cudaFree(deviceInputImageData);
cudaFree(deviceOutputImageData);
cudaFree(deviceMaskData);
free(hostMaskData);
wbImage_delete(outputImage);
wbImage_delete(inputImage);
return 0;
}