-
Notifications
You must be signed in to change notification settings - Fork 7
/
Kmeans-OpenMP.cpp
188 lines (165 loc) · 5.17 KB
/
Kmeans-OpenMP.cpp
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
#include "lab1_sequential.h"
#include <bits/stdc++.h>
#include <malloc.h>
#include <cmath>
#include <stdlib.h>
#include <stdio.h>
#include <omp.h>
using namespace std;
void kmeans_sequential(int N, int K, int* data_points, int** data_point_cluster, int** centroids, int* num_iterations);
vector<int> vect;
int num_threads;
struct Point{
double x; //x coordinate
double y; //y coordinate
double z; //z coordinate
int cluster; //cluster to which the point belongs
};
//helper function prototypes
void mean_recompute(int K, int N, Point points[],Point centr[]);
Point addtwo(Point a, Point b);
double euclid(Point a, Point b);
Point mean(Point arr[], int N);
void assignclusters(Point points[], Point centr[], int K, int N);
void putback(Point centr[],int K);
//Point points[N];
//driver function
void kmeans_omp(int num_threads,int N, int K, int* data_points, int** data_point_cluster, float** centroids, int* num_iterations){
Point points[N];
::num_threads=num_threads;
//---------------------------
int j=0;
for (int i=0; i<(3*N); i+=3){
points[j].x = data_points[i];
points[j].y = data_points[i+1];
points[j].z = data_points[i+2];
j++;
}
//---------------------------
//random centroid initialization. centroids are random points
//center is the array of centroids containing their locations and cluster number as their index in this array
srand(10);
Point centr[K];
for (int i = 0; i< K; i++){
int random = rand()%N;//some random value
centr[i] = points[random];
}
//---------------------------
//array to keep a track of distances of a point from all centroids, to take the minimum out of them
double distances[K];
//computing distance of a point and assigning all
for (int i=0; i<N; i++){
for(int j=0; j<K ; j++){
distances[j] = euclid(points[i], centr[j]);
}
int index = 0;
for(int i = 1; i < K; i++){
if(distances[i] < distances[index])
index = i;
}
points[i].cluster = index;
}
//---------------------------
mean_recompute(K, N, points,centr);
putback(centr, K);
//---------------------------
int iterations = 1;
int count;
do {
mean_recompute(K, N, points,centr);
putback(centr, K);
//storing old values for convergence check
int old[N];
for (int i=0; i<N; i++){
old[i] = points[i].cluster;
}
assignclusters(points, centr, K, N);
iterations++;
count = 0;
for (int i=0; i<N; i++){
if (old[i] == points[i].cluster)
count++;
}
} while(count!=N);
//---------------------------
*data_point_cluster= (int*) calloc(4*N, sizeof(int));
*centroids = (float*) calloc(vect.size(), sizeof(float));
int q = 0;
for (int i = 0; i< 4*N; i+=4){
data_point_cluster[0][i] = points[q].x;
data_point_cluster[0][i+1] = points[q].y;
data_point_cluster[0][i+2] = points[q].z;
data_point_cluster[0][i+3] = points[q].cluster;
q++;
}
for (int i = 0; i<vect.size(); i++){
centroids[0][i] = vect[i];
}
* num_iterations = vect.size()/K -1 ;
}
//funtion to recompute the new centroids for each cluster
//N is the total number of data points and K is the total number of clusters
void mean_recompute(int K, int N, Point points[], Point centr[]){
int count[K];
Point sum[K];
for(int i=0; i< N ; i++){
count[points[i].cluster]++;
sum[points[i].cluster] = addtwo(points[i],sum[points[i].cluster] );
}
for(int i=0; i< K ; i++){
centr[i].x = sum[i].x/count[i];
centr[i].y = sum[i].y/count[i];
centr[i].z = sum[i].z/count[i];
}
}
//assuming they are the same cluster
Point addtwo(Point a, Point b){
Point ans;
ans.x = a.x + b.x;
ans.y = a.y + b.y;
ans.z = a.z + b.z;
ans.cluster = a.cluster;
return ans;
}
int checkClosestCluster(Point points[], Point centr[],int K, int N, int i){
double distances[K];
for(int j=0; j<K ; j++){
distances[j] = euclid(points[i], centr[j]);
}
int index = 0;
for(int i = 1; i < K; i++)
{
if(distances[i] < distances[index])
index = i;
}
return index;
}
void assignclusters(Point points[], Point centr[],int K, int N){
double distances[K];
//computing distance of a point and assigning all the data points, a centroid/cluster value
#pragma omp parallel num_threads(num_threads)
{
#pragma omp for
for (int i=0; i<N; i++)
{
//assigning the minimum distance cluster, which is an index
points[i].cluster = checkClosestCluster(points, centr,K, N, i);
// points[i].cluster = index;
}
}
}
//function to calculate euclidea distance between two points
double euclid(Point a, Point b){
double x = a.x- b.x;
double y = a.y- b.y;
double z = a.z- b.z;
double dist = sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2));
return dist;
}
void putback(Point centr[],int K){
for (int i =0; i<K; i++) {
vect.push_back(centr[i].x);
vect.push_back(centr[i].y);
vect.push_back(centr[i].z);
}
}