forked from XimingCheng/webgpu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage_Convolution.cu
More file actions
172 lines (137 loc) · 5.57 KB
/
Image_Convolution.cu
File metadata and controls
172 lines (137 loc) · 5.57 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
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
#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 12
#define BLOCK_WIDTH (O_TILE_WIDTH + 2*Mask_radius)
//@@ INSERT CODE HERE
__global__ void imageConvolution(float* deviceInputImageData, float* deviceOutputImageData,
int imageWidth, int imageHeight, int channels, int maskRows, int maskColumns,
const float* __restrict__ deviceMaskData) {
int tx = threadIdx.x;
int ty = threadIdx.y;
int row_o = blockIdx.y*O_TILE_WIDTH + ty;
int col_o = blockIdx.x*O_TILE_WIDTH + tx;
int row_i = row_o - Mask_radius;
int col_i = col_o - Mask_radius;
__shared__ float NsR[BLOCK_WIDTH][BLOCK_WIDTH];
__shared__ float NsG[BLOCK_WIDTH][BLOCK_WIDTH];
__shared__ float NsB[BLOCK_WIDTH][BLOCK_WIDTH];
if (row_i >= 0 && row_i < imageHeight && col_i >= 0 && col_i < imageWidth)
{
int baseIdx = (row_i*imageWidth + col_i)*channels;
NsR[ty][tx] = deviceInputImageData[baseIdx];
NsG[ty][tx] = deviceInputImageData[baseIdx + 1];
NsB[ty][tx] = deviceInputImageData[baseIdx + 2];
}
else
{
NsR[ty][tx] = 0.0f;
NsG[ty][tx] = 0.0f;
NsB[ty][tx] = 0.0f;
}
__syncthreads();
float outputR = 0.0f;
float outputG = 0.0f;
float outputB = 0.0f;
if (ty < O_TILE_WIDTH && tx < O_TILE_WIDTH)
{
for (int i = 0; i < maskColumns; i++)
{
for (int j = 0; j < maskRows; j++)
{
float mask = deviceMaskData[i*maskColumns + j];
outputR += mask*NsR[i + ty][j + tx];
outputG += mask*NsG[i + ty][j + tx];
outputB += mask*NsB[i + ty][j + tx];
}
}
if (row_o < imageHeight && col_o < imageWidth)
{
int baseIdx = (row_o*imageWidth + col_o)*channels;
deviceOutputImageData[baseIdx] = outputR;
deviceOutputImageData[baseIdx + 1] = outputG;
deviceOutputImageData[baseIdx + 2] = outputB;
}
}
}
int main(int argc, char* argv[]) {
wbArg_t args;
// the mask size data
int maskRows;
int maskColumns;
// input image size and channel data
int imageChannels;
int imageWidth;
int imageHeight;
// input files
char * inputImageFile;
char * inputMaskFile;
// input and output image object
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);
imageConvolution <<< dimGrid, dimBlock >>> (deviceInputImageData, deviceOutputImageData, imageWidth, imageHeight,
imageChannels, maskRows, maskColumns, deviceMaskData);
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;
}