-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmeans_openmp.c
189 lines (161 loc) · 4.25 KB
/
kmeans_openmp.c
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <omp.h>
#define NUMBER_OF_POINTS 2000 // Number of points
#define D 4 // Dimensions of data
#define K 5 // Number of clusters
float *create_rand_data(int num_points)
{
float *data = (float *)malloc(num_points * D * sizeof(float));
FILE *fp;
char filename[100];
sprintf(filename, "./data/input/omp_data_%d_%d%s", num_points, D, ".csv");
fp = fopen(filename, "w");
for (int i = 0; i < num_points; i++)
{
for (int j = 0; j < D; j++)
{
data[i * D + j] = (float)rand() / (float)RAND_MAX;
fprintf(fp, "%f", data[i * D + j]);
if (j < D - 1)
{
fprintf(fp, ",");
}
}
fprintf(fp, "\n");
}
fclose(fp);
return data;
}
float distance(float *vec1, float *vec2, int dim)
{
float dist = 0.0;
for (int i = 0; i < dim; i++)
{
dist += ((vec1[i] - vec2[i]) * (vec1[i] - vec2[i]));
}
return dist;
}
int assign_point(float *point, float *centroids, int k, int dim)
{
int cluster = 0;
float dist = distance(point, centroids, dim);
float *centroid = centroids + dim;
#pragma omp parallel for
for (int c = 1; c < k; c++)
{
float temp_dist = distance(point, centroid, dim);
if (temp_dist < dist)
{
#pragma omp critical
{
dist = temp_dist;
cluster = c;
}
}
centroid += dim;
}
return cluster;
}
void add_point(float *point, float *sum, int dim)
{
#pragma omp parallel for
for (int i = 0; i < dim; i++)
{
#pragma omp atomic
sum[i] += point[i];
}
}
void print_centroids(float *centroids, int k, int dim)
{
float *p = centroids;
printf("--------------------------CENTROIDS--------------------------\n");
for (int i = 0; i < k; i++)
{
printf("Centroid %d: ", i);
for (int j = 0; j < dim; j++, p++)
{
printf("%f ", *p);
}
printf("\n");
}
printf("\n");
}
int main()
{
srand(12345);
float *points, *sums, *centroids;
int *counts, *labels;
points = (float *)malloc(NUMBER_OF_POINTS * D * sizeof(float));
sums = (float *)malloc(K * D * sizeof(float));
counts = (int *)malloc(K * sizeof(int));
labels = (int *)malloc(NUMBER_OF_POINTS * sizeof(int));
centroids = (float *)malloc(K * D * sizeof(float));
points = create_rand_data(NUMBER_OF_POINTS);
double starttime = omp_get_wtime();
#pragma omp parallel for
for (int i = 0; i < K * D; i++)
{
centroids[i] = points[i];
}
float norm = 1.0;
while (norm > 0.0001)
{
#pragma omp parallel for
for (int i = 0; i < K * D; i++)
{
sums[i] = 0.0;
}
#pragma omp parallel for
for (int i = 0; i < K; i++)
{
counts[i] = 0;
}
#pragma omp parallel for
for (int i = 0; i < NUMBER_OF_POINTS; i++)
{
int label = assign_point(points + i * D, centroids, K, D);
#pragma omp atomic write
labels[i] = label;
#pragma omp atomic
counts[label]++;
add_point(points + i * D, sums + label * D, D);
}
#pragma omp parallel for
for (int i = 0; i < K * D; i++)
{
sums[i] = sums[i] / counts[i / D];
}
norm = distance(sums, centroids, K * D);
// printf("Normalized distance: %f\n", norm);
#pragma omp parallel for
for (int i = 0; i < K * D; i++)
{
centroids[i] = sums[i];
}
// print_centroids(centroids, K, D);
}
double endtime = omp_get_wtime();
printf("Time taken: %f seconds\n", endtime - starttime);
// Save labels in labels csv
FILE *fp;
char filename[100];
sprintf(filename, "./data/output/omp_labels_%d_%d%s", NUMBER_OF_POINTS, D, ".csv");
fp = fopen(filename, "w");
for (int i = 0; i < NUMBER_OF_POINTS; i++)
{
for (int j = 0; j < D; j++)
{
fprintf(fp, "%f,", points[i * D + j]);
}
fprintf(fp, "%d\n", labels[i]);
}
fclose(fp);
free(points);
free(sums);
free(counts);
free(labels);
free(centroids);
return 0;
}