-
Notifications
You must be signed in to change notification settings - Fork 2
/
knn_punisher_v1.cu
412 lines (350 loc) · 11.8 KB
/
knn_punisher_v1.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<float.h> //DBL_MAX
#include <cuda_runtime_api.h>
#define restrict __restrict__
#define PADDINGCLASS -2
#define OUTPUT_FILE "ocuda"
#define INPUT_FILE "data"
#define KMAX 20
#define SPACEDIMMAX 150
#define CLASSESMAX 100
void check_error(cudaError_t err, const char *msg);
void printStats(cudaEvent_t before, cudaEvent_t after, const char *msg);
void readInput(FILE* file, float* coords, int* classes, int spacedim, int numels, int totalElements);
void writeOutput(float* coords, int* classes, int spacedim, int numels);
__global__ void knn(float* coords, float2* kOutput, int totalElements, int numels, int spacedim, int k, int* classes, int classes_num);
__global__ void knnPunisher(float2* kOutput, int* d_classes, int numels, int newels, int k, int classes_num);
__device__ float distance(float* coords, float* coords2, int spacedim);
__device__ int insert(float2* kPoints, float2 newDist, int* size, int k, int gid);
__device__ void swapfloat2(float2* d1, float2* d2);
__device__ int deviceFindMode(int* kclasses, int classes_num, int k);
__device__ float distanceShm(float* coords, int left, int spacedim);
__global__ void findIntInArray(int* input, int dim, int* result)
{
int gid = threadIdx.x + blockIdx.x*blockDim.x;
if (gid >= dim) return;
if (input[gid] == -1)
*result = 1;
}
//Declaration of shared-memory. It's going to contains partial minimum of distances
extern __shared__ int mPartial[];
int main(int argc, char *argv[])
{
int newels; //number of points we want classify
int k; //number of nearest points we use to classify
int numels; //total element already classified
int spacedim;
char filePath[255]; //path + filname of input file
int classes_num; //number of classes
float* h_coords; //coords of existing points with a class
int* h_classes; //array contains the class for each points
//*** Device-variables-declaration ***
float* d_coords;
int2* d_determinate;
int* d_classes;
int* d_again;
float2* d_kOutput;
//*** end-device-declaration
//***cudaEvent-declaration***
cudaEvent_t before_allocation, before_input, before_upload, before_knn, before_download;
cudaEvent_t after_allocation, after_input, after_upload, after_knn, after_download;
//***end-cudaEvent-declaration***
//Requisiti: numels e newels devono essere maggiori di K
if (argc > 2)
{
strcpy(filePath, argv[1]);
k = atoi(argv[2]);
}
else
{
printf("how-to-use: knn <inputfile> <k> \n");
exit(1);
}
//***cuda-init-event***
check_error(cudaEventCreate(&before_allocation), "create before_allocation cudaEvent");
check_error(cudaEventCreate(&before_input), "create before_input cudaEvent");
check_error(cudaEventCreate(&before_upload), "create before_upload cudaEvent");
check_error(cudaEventCreate(&before_knn), "create before_knn cudaEvent");
check_error(cudaEventCreate(&before_download), "create before_download cudaEvent");
check_error(cudaEventCreate(&after_allocation), "create after_allocation cudaEvent");
check_error(cudaEventCreate(&after_input), "create after_input cudaEvent");
check_error(cudaEventCreate(&after_upload), "create after_upload cudaEvent");
check_error(cudaEventCreate(&after_knn), "create after_knn cudaEvent");
check_error(cudaEventCreate(&after_download), "create after_download cudaEvent");
//***end-cuda-init-event***
FILE *fp;
if((fp = fopen(filePath, "r")) == NULL)
{
printf("No such file\n");
exit(1);
}
fseek(fp, 0L, SEEK_END);
float fileSize = ftell(fp);
rewind(fp);
int count = fscanf(fp, "%d,%d,%d,%d\n", &numels, &newels, &classes_num, &spacedim);
int totalElements = numels + newels;
//*** allocation ***
cudaEventRecord(before_allocation);
h_coords = (float*) malloc(sizeof(float)*totalElements*spacedim);
h_classes = (int*) malloc(sizeof(int)*totalElements);
//*** device-allocation ***
check_error(cudaMalloc(&d_coords, totalElements*spacedim*sizeof(float)), "alloc d_coords_x");
check_error(cudaMalloc(&d_classes, totalElements*sizeof(int)), "alloc d_classes");
check_error(cudaMalloc(&d_determinate, newels*2*sizeof(int)), "alloc d_determinate");
check_error(cudaMalloc(&d_kOutput, newels*KMAX*2*sizeof(float)), "alloc d_kOutput");
check_error(cudaMalloc(&d_again, sizeof(int)), "alloc d_again");
//*** end-device-allocation ***
cudaEventRecord(after_allocation);
///***input-from-file***
cudaEventRecord(before_input);
readInput(fp, h_coords, h_classes, spacedim, numels, totalElements);
cudaEventRecord(after_input);
fclose(fp);
///***end-input-from-file***
//***copy-arrays-on-device***
cudaEventRecord(before_upload);
check_error(cudaMemcpy(d_coords, h_coords, totalElements*spacedim*sizeof(float), cudaMemcpyHostToDevice), "copy d_coords");
check_error(cudaMemcpy(d_classes, h_classes, totalElements*sizeof(int), cudaMemcpyHostToDevice), "copy d_classes");
cudaEventRecord(after_upload);
//***end-copy-arrays-on-device***
int blockSize = 512;
int numBlocks = (newels + blockSize - 1)/blockSize;
cudaEventRecord(before_knn);
knn<<<numBlocks, blockSize>>>(d_coords, d_kOutput, totalElements, numels, spacedim, k, d_classes, classes_num);
blockSize = 32;
numBlocks = (newels + blockSize - 1)/blockSize;
int again = 1;
while (again != 0)
{
knnPunisher<<<numBlocks, blockSize, newels*sizeof(int)>>>(d_kOutput, d_classes, numels, newels, k, classes_num);
check_error(cudaMemset(d_again, 0, sizeof(int)), "reset d_again");
findIntInArray<<<numBlocks, blockSize>>>(d_classes+numels, newels, d_again);
check_error(cudaMemcpy(&again, d_again, sizeof(int), cudaMemcpyDeviceToHost), "download d_again");
}
cudaEventRecord(after_knn);
check_error(cudaMemcpy(h_classes+numels, d_classes+numels, newels*sizeof(int), cudaMemcpyDeviceToHost), "download classes");
check_error(cudaEventSynchronize(after_knn), "sync cudaEvents");
printStats(before_knn, after_knn, "knn");
writeOutput(h_coords, h_classes, spacedim, totalElements);
return 0;
}
void check_error(cudaError_t err, const char *msg)
{
if (err != cudaSuccess)
{
fprintf(stderr, "%s : error %d (%s)\n", msg, err, cudaGetErrorString(err));
exit(err);
}
}
float runtime;
void printStats(cudaEvent_t before, cudaEvent_t after, const char *msg)
{
check_error(cudaEventElapsedTime(&runtime, before, after), msg);
printf("%s %gms\n", msg, runtime);
}
__global__ void knn(float* coords, float2* kOutput, int totalElements, int numels, int spacedim, int k, int* classes, int classes_num)
{
int gid = numels + threadIdx.x + blockIdx.x*blockDim.x; //id del punto da determinare
if (gid >= totalElements) return;
float* newPointCoords = coords+spacedim*gid;
float* pointCoords;
float2 kPoints[KMAX];
int i = 0, size = 0, count = 0;
float2 dist;
for (i = 0; i < numels; i++)
{
pointCoords = coords+spacedim*i;
dist = make_float2(distance(newPointCoords, pointCoords, spacedim), i);
insert(kPoints, dist, &size, k, gid);
}
for (count=0; i < gid; i++)
{
pointCoords = coords+spacedim*i;
dist = make_float2(distance(newPointCoords, pointCoords, spacedim), i);
count += insert(kPoints, dist, &size, k, gid);
}
if (count > 0)
{
classes[gid] = -1;
}
else
{
int kclasses[KMAX];
for (int j = 0; j < k; j++)
kclasses[j] = classes[(int)(kPoints[j].y)];
classes[gid] = deviceFindMode(kclasses, classes_num, k);
}
//copia kPoints in kOutput
int newelId = gid-numels;
for (i = 0; i < k; i++)
kOutput[newelId*KMAX + i] = kPoints[i];
}
__global__ void knnPunisher(float2* kOutput, int* classes, int numels, int newels, int k, int classes_num)
{
int gid = threadIdx.x + blockIdx.x*blockDim.x;
int lid = threadIdx.x;
int i = lid;
while (i < gid)
{
mPartial[i] = classes[i+numels];
i += blockDim.x;
}
if (gid < newels)
mPartial[gid] = classes[gid+numels];
if (gid >= newels || mPartial[gid] != -1) return;
//Se sono qui la classe per il kPoint è da determinare
int kPoints[KMAX];
for (int i = 0; i < k; i++)
kPoints[i] = kOutput[gid*KMAX+i].y; //gid
//Le sue dipendenze, se già determinate stanno nella shared-memory
int count = 0;
for (int i = k-1; i >= 0; i--)
{
int id = kPoints[i];
int lid = id - numels;
if (id > numels && mPartial[lid] < 0)
{
//segno quelli indeterminati
count++;
break;
}
}
if (count == 0)
{
//posso determinare il punto
//le sue dipendenze si trovano in shared memory
int kclasses[KMAX];
for (int j = 0; j < k; j++)
kclasses[j] = classes[kPoints[j]];
int newClass = deviceFindMode(kclasses, classes_num, k);
classes[gid+numels] = newClass;
}
}
__device__ int deviceFindMode(int* kclasses, int classes_num, int k)
{
int classesCount[CLASSESMAX];
int i;
int temp=0;
for (i = 0; i < CLASSESMAX; i++)
classesCount[i] = 0;
for (i = 0; i < k; i++){
temp=kclasses[i];
classesCount[temp]+=1;
}
int max = 0;
int maxValue = classesCount[0];
for (i = 1; i < classes_num; i++)
{
int value = classesCount[i];
if (value > maxValue)
{
max = i;
maxValue = value;
}
else if (value != 0 && maxValue == value)
{
int j = 0;
for (j = 0; j < k; j++)
{
if (kclasses[j] == i)
{
max = i;
break;
}
else if (kclasses[j] == max)
break;
}
}
}
return max;
}
//inserimento smart in kPoints
__device__ int insert(float2* kPoints, float2 newDist, int* size, int k, int gid)
{
int inserted = 0;
if (*size == 0)
{
//Caso base: inserimento su array vuoto
kPoints[0] = newDist;
*size = *size + 1;
return 1;
}
int i = 1;
float2* value = &newDist; //nuovo elemento
float2* tail = &(kPoints[*size-i]);
if (*size < k)
{
kPoints[*size] = newDist;
value = &(kPoints[*size]);
inserted = 1;
}
//partire della fine, swap se trovo elemento più grande - mi fermo se trovo elemento più piccolo
while (i <= *size && (*tail).x > (*value).x)
{
swapfloat2(tail, value);
value = tail;
i++;
tail = &(kPoints[*size-i]);
inserted = 1;
}
if (inserted && *size < k) *size = *size + 1;
return inserted;
}
__device__ void swapfloat2(float2* d1, float2* d2)
{
//da provate tmp = d1, d1 = d2, d2 = tmp
float2 tmp;
tmp.x = (*d1).x;
tmp.y = (*d1).y;
(*d1).x = (*d2).x;
(*d1).y = (*d2).y;
(*d2).x = tmp.x;
(*d2).y = tmp.y;
}
// read input from file
void readInput(FILE* file, float* coords, int* classes, int spacedim, int numels, int totalElements)
{
int i, j;
int count;
for(i=0; i<numels; i++)
{
for (j = 0; j < spacedim; j++)
count = fscanf(file, "%f,", &(coords[i*spacedim +j]));
count = fscanf(file, "%d\n", &(classes[i]));
}
for(; i < totalElements; i++)
{
for (j = 0; j < spacedim; j++)
count = fscanf(file, "%f,", &(coords[i*spacedim+j]));
count = fscanf(file, "-1\n");
}
count++;
}
//Write Output on file
void writeOutput(float* coords, int* classes, int spacedim, int numels)
{
FILE *fp;
fp = fopen(OUTPUT_FILE, "w");
int i, j;
for( i = 0; i < numels; i++)
{
for (j = 0; j < spacedim; j++)
fprintf(fp, "%f,", coords[i*spacedim+j]);
fprintf(fp, "%d\n", classes[i]);
}
fclose(fp);
}
//multidimensional euclidian distance (without sqrt)
__device__ float distance(float* coords, float* coords2, int spacedim)
{
float sum = 0;
int i;
for (i = 0; i < spacedim; i++)
{
float diff = coords[i] - coords2[i];
sum += diff*diff;
}
return sum;
}