-
Notifications
You must be signed in to change notification settings - Fork 0
/
photomosaic.cu
193 lines (153 loc) · 5.47 KB
/
photomosaic.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
#include <math.h>
#include <stdio.h>
#include <vector>
#include <limits.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <driver_functions.h>
#include "photomosaic.h"
struct GlobalConstants {
int numImages;
int numSlices;
int cutSize;
int* imageAverages;
int* allAverages;
int* imageIndex;
};
__constant__ GlobalConstants cuConstMosaicParams;
__device__ __inline__ int square(int x) {
return x * x;
}
__device__ __inline__ int RGBdistance(int3 rgb1, int3 rgb2) {
int red = square(rgb1.x - rgb2.x);
int green = square(rgb1.y - rgb2.y);
int blue = square(rgb1.z - rgb2.z);
return (int)sqrt((float)(red + green + blue));
}
__global__ void kernelMatchImages() {
int numImages = cuConstMosaicParams.numImages;
int numSlices = cuConstMosaicParams.numSlices;
int cutSize = cuConstMosaicParams.cutSize;
int width = numSlices * cutSize;
int index = blockIdx.x * blockDim.x + (blockDim.y * blockIdx.y * width);
index += (threadIdx.x + (blockDim.x * threadIdx.y));
int imageAverageStart = index * square(cutSize) * 3;
int dist;
int minIndex = 0;
int minVal = INT_MAX;
for (int i = 0; i < (numImages * square(cutSize) * 3); i += (square(cutSize) * 3)) {
dist = 0;
for (int j = 0; j < square(cutSize); j += 3) {
int3 rgb1 = *(int3*)(&cuConstMosaicParams.imageAverages[imageAverageStart + j]);
int3 rgb2 = *(int3*)(&cuConstMosaicParams.allAverages[i + j]);
dist += RGBdistance(rgb1, rgb2);
}
if (dist < minVal) {
minVal = dist;
minIndex = (i / square(cutSize) * 3);
}
}
__syncthreads();
cuConstMosaicParams.imageIndex[index] = minIndex;
__syncthreads();
}
CudaMosaic::CudaMosaic() {
printf("Constructor\n");
cudaDeviceImageAverages = NULL;
cudaDeviceAllAverages = NULL;
cudaDeviceImageIndex = NULL;
imageAverages = NULL;
allAverages = NULL;
imageIndex = NULL;
}
CudaMosaic::~CudaMosaic() {
if (imageAverages) {
delete [] imageAverages;
delete [] allAverages;
delete [] imageIndex;
}
if (cudaDeviceImageAverages) {
cudaFree(cudaDeviceImageAverages);
cudaFree(cudaDeviceAllAverages);
cudaFree(cudaDeviceImageIndex);
}
delete [] imageAverages;
delete [] allAverages;
delete [] imageIndex;
}
// void CudaMosaic::setAllAverages(int *averages) {
// for (int i = 0; i < (numImages * cutSize * cutSize * 3); i++) {
// allAverages[i] = averages[i];
// }
// }
// void CudaMosaic::setImageAverages(int *averages) {
// printf("Trying to set image averages\n");
// for (int i = 0; i < (numSlices * numSlices * cutSize * cutSize * 3); i++) {
// imageAverages[i] = averages[i];
// }
// }
const int* CudaMosaic::getIndices() {
printf("Copying index data from device\n");
cudaMemcpy(imageIndex,
cudaDeviceImageIndex,
sizeof(int) * numSlices * numSlices,
cudaMemcpyDeviceToHost);
for (int i = 0; i < numSlices * numSlices; i++) {
printf("Index %d found match at %d\n", i, imageIndex[i]);
}
return imageIndex;
}
void CudaMosaic::setup(int ni, int ns, int cs, int* imgavg, int* allavg) {
printf("Calling setup with numImages %d, numSlices %d, cutSize %d\n", ni, ns, cs);
numImages = ni;
numSlices = ns;
cutSize = cs;
imageAverages = new int[numSlices * numSlices * cutSize * cutSize * 3];
allAverages = new int[numImages * cutSize * cutSize * 3];
imageIndex = new int[numSlices * numSlices];
int i;
for (i = 0; i < (numSlices * numSlices * cutSize * cutSize * 3); i++) {
imageAverages[i] = imgavg[i];
// if (i % 100 == 0) {
// printf("Setting image average %d for index %d\n", imageAverages[i], i);
// }
}
for (i = 0; i < (numImages * cutSize * cutSize * 3); i++) {
allAverages[i] = allavg[i];
// if (i % 100 == 0) {
// printf("Setting all average %d for index %d\n", allAverages[i], i);
// }
}
int deviceCount = 0;
cudaError_t err = cudaGetDeviceCount(&deviceCount);
printf("Initializing CUDA for CudaMosaic\n");
printf("Found %d CUDA devices\n", deviceCount);
for (int i=0; i<deviceCount; i++) {
cudaDeviceProp deviceProps;
cudaGetDeviceProperties(&deviceProps, i);
printf("Device %d: %s\n", i, deviceProps.name);
printf(" SMs: %d\n", deviceProps.multiProcessorCount);
printf(" Global mem: %.0f MB\n", static_cast<float>(deviceProps.totalGlobalMem) / (1024 * 1024));
printf(" CUDA Cap: %d.%d\n", deviceProps.major, deviceProps.minor);
}
cudaMalloc(&cudaDeviceImageAverages, sizeof(int) * cutSize * 3 * (numSlices * numSlices));
cudaMalloc(&cudaDeviceAllAverages, sizeof(int) * cutSize * 3 * numImages);
cudaMalloc(&cudaDeviceImageIndex, sizeof(int) * numImages);
cudaMemcpy(cudaDeviceImageAverages, imageAverages, sizeof(int) * cutSize * 3 * (numSlices * numSlices), cudaMemcpyHostToDevice);
cudaMemcpy(cudaDeviceAllAverages, allAverages, sizeof(int) * cutSize * 3 * numImages, cudaMemcpyHostToDevice);
cudaMemcpy(cudaDeviceImageIndex, imageIndex, sizeof(int) * numImages, cudaMemcpyHostToDevice);
GlobalConstants params;
params.numImages = numImages;
params.numSlices = numSlices;
params.cutSize = cutSize;
params.imageAverages = cudaDeviceImageAverages;
params.allAverages = cudaDeviceAllAverages;
params.imageIndex = cudaDeviceImageIndex;
cudaMemcpyToSymbol(cuConstMosaicParams, ¶ms, sizeof(GlobalConstants));
}
void CudaMosaic::imageMatch() {
dim3 threadsPerBock(cutSize, cutSize, 1);
dim3 numBlocks(numSlices, numSlices, 1);
kernelMatchImages<<<numBlocks, threadsPerBock>>>();
cudaThreadSynchronize();
}