-
Notifications
You must be signed in to change notification settings - Fork 4
/
mp6.cu
155 lines (122 loc) · 5.64 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <wb.h>
#define wbCheck(stmt) do { \
cudaError_t err = stmt; \
if (err != cudaSuccess) { \
wbLog(ERROR, "Failed to run stmt ", #stmt); \
return -1; \
} \
} while(0)
#define MASK 5
#define TILE_WIDTH 16
#define RADIUS MASK/2
#define AREA (TILE_WIDTH + MASK - 1)
#define GRID_SIZE(x) (ceil((float)x/TILE_WIDTH))
__device__ inline void setIndexes(unsigned int d,
unsigned int &dX,
unsigned int &dY,
int &sX, int &sY){
dX = d % AREA;
dY = d / AREA;
sX = blockIdx.x * TILE_WIDTH + dX - RADIUS;
sY = blockIdx.y * TILE_WIDTH + dY - RADIUS;
}
__global__ void convolution(float* I, const float* __restrict__ M, float* P,
int channels, int width, int height) {
__shared__ float tmp[AREA][AREA];
float acc;
int sourceY, sourceX;
unsigned int source, destination, destinationY, destinationX;
unsigned int y = blockIdx.y * TILE_WIDTH + threadIdx.y;
unsigned int x = blockIdx.x * TILE_WIDTH + threadIdx.x;
for (unsigned int k = 0; k < channels; k++) {
destination = threadIdx.y * TILE_WIDTH + threadIdx.x;
setIndexes(destination,
destinationX,
destinationY,
sourceX, sourceY);
source = (sourceY * width + sourceX) * channels + k;
if (sourceY >= 0 && sourceY < height && sourceX >= 0 && sourceX < width)
tmp[destinationY][destinationX] = I[source];
else
tmp[destinationY][destinationX] = 0;
destination = threadIdx.y * TILE_WIDTH + threadIdx.x + TILE_WIDTH * TILE_WIDTH;
setIndexes(destination,
destinationX,
destinationY,
sourceX, sourceY);
source = (sourceY * width + sourceX) * channels + k;
if (destinationY < AREA)
if (sourceY >= 0 && sourceY < height && sourceX >= 0 && sourceX < width)
tmp[destinationY][destinationX] = I[source];
else
tmp[destinationY][destinationX] = 0;
__syncthreads();
acc = 0;
#pragma unroll
for (unsigned int i = 0; i < MASK; i++)
#pragma unroll
for (unsigned int j = 0; j < MASK; j++)
acc += tmp[threadIdx.y + i][threadIdx.x + j] * M[i * MASK + j];
if (y < height && x < width) P[(y * width + x) * channels + k] = min(max(acc, 0.0), 1.0);
__syncthreads();
}
}
int main(int argc, char* argv[]) {
wbImage_t inputImage, outputImage;
int maskRows, maskColumns, imageChannels,
imageWidth, imageHeight;
char *inputImageFile, *inputMaskFile;
float *hostInputImageData, * hostOutputImageData,
*hostMaskData, *deviceInputImageData,
*deviceOutputImageData, *deviceMaskData;
wbArg_t arg = wbArg_read(argc, argv); /* parse the input arguments */
inputImageFile = wbArg_getInputFile(arg, 0);
inputMaskFile = wbArg_getInputFile(arg, 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 */
size_t maskMemSize = maskRows * maskColumns * sizeof(float);
imageWidth = wbImage_getWidth(inputImage);
imageHeight = wbImage_getHeight(inputImage);
imageChannels = wbImage_getChannels(inputImage);
size_t imageMemSize = imageWidth * imageHeight * imageChannels * sizeof(float);
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");
wbCheck( cudaMalloc((void **) &deviceInputImageData, imageMemSize) );
wbCheck( cudaMalloc((void **) &deviceOutputImageData, imageMemSize) );
wbCheck( cudaMalloc((void **) &deviceMaskData, maskMemSize) );
wbTime_stop(GPU, "Doing GPU memory allocation");
wbTime_start(Copy, "Copying data to the GPU");
wbCheck( cudaMemcpy(deviceInputImageData, hostInputImageData, imageMemSize, cudaMemcpyHostToDevice) );
wbCheck( cudaMemcpy(deviceMaskData, hostMaskData, maskMemSize, cudaMemcpyHostToDevice) );
wbTime_stop(Copy, "Copying data to the GPU");
wbTime_start(Compute, "Doing the computation on the GPU");
unsigned int dimGridX = GRID_SIZE(imageWidth);
unsigned int dimGridY = GRID_SIZE(imageHeight);
dim3 dimGrid(dimGridX, dimGridY);
dim3 dimBlock(TILE_WIDTH, TILE_WIDTH);
convolution<<<dimGrid, dimBlock>>>(deviceInputImageData,
deviceMaskData,
deviceOutputImageData,
imageChannels,
imageWidth,
imageHeight);
wbCheck( cudaThreadSynchronize() );
wbTime_stop(Compute, "Doing the computation on the GPU");
wbTime_start(Copy, "Copying data from the GPU");
wbCheck( cudaMemcpy(hostOutputImageData, deviceOutputImageData, imageMemSize, cudaMemcpyDeviceToHost) );
wbTime_stop(Copy, "Copying data from the GPU");
wbTime_stop(GPU, "Doing GPU Computation (memory + compute)");
wbSolution(arg, outputImage);
wbCheck( cudaFree(deviceInputImageData) );
wbCheck( cudaFree(deviceOutputImageData) );
wbCheck( cudaFree(deviceMaskData) );
free(hostMaskData);
wbImage_delete(outputImage);
wbImage_delete(inputImage);
return 0;
}