-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathk_meanCUDA.cu
72 lines (58 loc) · 2.43 KB
/
k_meanCUDA.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
#include "k_meanCUDA.cuh"
#include "cuda.h"
#include "cuda_runtime.h"
#include "math.h"
#include "device_launch_parameters.h"
#include "k_mean.h"
#define tile 32
__device__ double calculatedistanceGPU(unit* point1, unit* point2) {
return (double)sqrt((double)pow(point1->dim1 - point2->dim1, 2) + (double)pow(point1->dim2 - point2->dim2, 2) + (double)pow(point1->dim3 - point2->dim3, 2) + (double)pow(point1->dim4 - point2->dim4, 2));
}
__global__ void closestcentroidGPU(unit* points, unit* centroids, int numofcentr, int numofpoints) {
int threadsPerBlock = blockDim.x * blockDim.y * blockDim.z;
int threadPosInBlock = threadIdx.x + blockDim.x * threadIdx.y + blockDim.x * blockDim.y * threadIdx.z;
int blockPosInGrid = blockIdx.x + gridDim.x * blockIdx.y + gridDim.x * gridDim.y * blockIdx.z;
int tid = blockPosInGrid * threadsPerBlock + threadPosInBlock;
if (tid < numofpoints) {
double dist = 0;
double firstdistance = calculatedistanceGPU(&points[tid], ¢roids[0]);
points[tid].cluster = 0;
for (int i = 1; i < numofcentr; i++) {
dist = calculatedistanceGPU(&points[tid], ¢roids[i]);
if (dist <= firstdistance) {
points[tid].cluster = i;
firstdistance = dist;
}
}
}
}
__global__ void closestcentroidSharedGPU(unit* points, unit* centroids, int numofcentr, int numofpoints) {
int threadsPerBlock = blockDim.x * blockDim.y * blockDim.z;
int threadPosInBlock = threadIdx.x + blockDim.x * threadIdx.y + blockDim.x * blockDim.y * threadIdx.z;
int blockPosInGrid = blockIdx.x + gridDim.x * blockIdx.y + gridDim.x * gridDim.y * blockIdx.z;
int tid = blockPosInGrid * threadsPerBlock + threadPosInBlock;
__shared__ unit sh_points[tile*tile];
__shared__ unit sh_centrs[4];
if (tid < numofpoints) {
sh_points[threadPosInBlock] = points[tid];
if (tid%threadsPerBlock ==0) {
for (int i = 0; i < numofcentr; i++) {
sh_centrs[i] = centroids[i];
}
}
__syncthreads();
double dist = 0;
double firstdistance = calculatedistanceGPU(&sh_points[threadPosInBlock], &sh_centrs[0]);
sh_points[threadPosInBlock].cluster = 0;
for (int i = 1; i < numofcentr; i++) {
dist = calculatedistanceGPU(&sh_points[threadPosInBlock], &sh_centrs[i]);
if (dist <= firstdistance) {
sh_points[threadPosInBlock].cluster = i;
firstdistance = dist;
}
}
__syncthreads();
points[tid] = sh_points[threadPosInBlock];
__syncthreads();
}
}