-
Notifications
You must be signed in to change notification settings - Fork 1
/
KMeansMultiProcess.py
398 lines (341 loc) · 13.1 KB
/
KMeansMultiProcess.py
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
import math
import os
import random
from ctypes import CDLL, POINTER, c_double, c_int, cast
from multiprocessing import Process
from multiprocessing.sharedctypes import RawArray
import matplotlib.pyplot as plt
import numpy as np
def assign_points_to_cluster(
num_dimension,
num_clusters,
start_index,
end_index,
centroids_coordinates,
points,
squared_distances,
min_distance_index,
):
for i in range(start_index, end_index):
point_index = i * num_dimension
min_distance_index[i] = -1
squared_distances[i] = math.inf
for j in range(num_clusters):
cluster_index = j * num_dimension
dis = 0.0
for d in range(num_dimension):
delta = (
points[point_index + d] - centroids_coordinates[cluster_index + d]
)
dis += delta * delta
if dis < squared_distances[i]:
min_distance_index[i] = j
squared_distances[i] = dis
class KMeansMultiProcess:
def __init__(
self,
num_centroids,
input_points,
num_iterations,
num_processes,
external_kernel=False,
random_state=None,
):
if input_points.shape[0] < num_centroids:
raise ValueError(
"Number of clusters k={} is smaller than number of data points n={}".format(
self.num_clusters, self.num_points
)
)
if input_points.shape[1] < 2:
raise ValueError("data points must have at least two dimensions")
self.num_iterations = num_iterations
self.num_clusters = num_centroids
self.num_dimension = input_points.shape[1]
self.num_points = input_points.shape[0]
self.num_processes = num_processes
self.random_state = random_state
self.centroids = np.full(
self.num_clusters * self.num_dimension, np.inf, dtype=np.double
)
self.point_class = np.full(self.num_points, -1, dtype=np.int)
self.cluster_energy = np.zeros(self.num_clusters)
self.point_distances = np.zeros(self.num_points)
self.clusters_size = np.zeros(self.num_clusters, dtype=np.int)
self.clusters_sum = np.full(
self.num_clusters * self.num_dimension, 0.0, dtype=np.double
)
if external_kernel:
self.points = np.full(
self.num_points * self.num_dimension, np.inf, dtype=np.double
)
self.squared_distances = np.full(self.num_points, math.inf, dtype=np.double)
self.min_distance_index = np.full(self.num_points, -1, dtype=np.int)
else:
self.points = RawArray(
"d",
np.full(self.num_points * self.num_dimension, np.inf, dtype=np.double),
)
self.squared_distances = RawArray(
"d", np.full(self.num_points, math.inf, dtype=np.double)
)
self.min_distance_index = RawArray(
"i", np.full(self.num_points, -1, dtype=np.int)
)
for i, point in enumerate(input_points):
point_index = i * self.num_dimension
for d in range(self.num_dimension):
self.points[point_index + d] = point[d]
self._initialize_centroids_k_means_pp()
self.external_kernel = external_kernel
if external_kernel:
current_working_dir = os.getcwd()
dll_name = "DistanceCalculator.dll"
dll_path = os.path.join(current_working_dir, dll_name)
self.kernelDll = CDLL(dll_path)
print(self.kernelDll)
def _initialize_centroids_k_means_pp(self):
# k-means plus plus
if self.random_state:
random.seed(self.random_state)
first_index = random.randint(0, self.num_points)
# first centroid
point_index = first_index * self.num_dimension
for d in range(self.num_dimension):
self.centroids[d] = self.points[point_index + d]
for cluster_index in range(1, self.num_clusters):
assign_points_to_cluster(
self.num_dimension,
cluster_index,
0,
self.num_points,
self.centroids,
self.points,
self.squared_distances,
self.min_distance_index,
)
distances = np.sqrt(self.squared_distances)
sum_distances = np.sum(distances)
normalized_distances = distances / sum_distances
r = random.uniform(0, 1)
acc = 0
chosen_index = 0
for n in normalized_distances:
acc += n
if acc >= r:
break
chosen_index += 1
cluster_index = cluster_index * self.num_dimension
point_index = chosen_index * self.num_dimension
for d in range(self.num_dimension):
self.centroids[cluster_index + d] = self.points[point_index + d]
def _spawn_process(self):
self.processes = []
num_points_per_thread = math.ceil(self.num_points / self.num_processes)
start_index = 0
for num_process in range(self.num_processes):
end_index = start_index + num_points_per_thread
if end_index > self.num_points:
end_index = self.num_points
self.processes.append(
Process(
target=assign_points_to_cluster,
args=(
self.num_dimension,
self.num_clusters,
start_index,
end_index,
self.centroids,
self.points,
self.squared_distances,
self.min_distance_index,
),
)
)
start_index = end_index + 1
assert end_index == self.num_points
def _start_process(self):
for p in self.processes:
p.start()
def _join_process(self):
for p in self.processes:
p.join()
def _compute_distances(self):
if self.external_kernel:
centroids_pointer = cast(self.centroids.ctypes.data, POINTER(c_double))
points_pointer = cast(self.points.ctypes.data, POINTER(c_double))
min_distance_pointer = cast(
self.squared_distances.ctypes.data, POINTER(c_double)
)
min_distance_index_pointer = cast(
self.min_distance_index.ctypes.data, POINTER(c_int)
)
# invoke the external distance calculator function
self.kernelDll.distance_calculator(
c_int(self.num_clusters),
c_int(self.num_points),
c_int(self.num_dimension),
c_int(self.num_processes),
centroids_pointer,
points_pointer,
min_distance_pointer,
min_distance_index_pointer,
)
else:
# spawn workers
self._spawn_process()
self._start_process()
self._join_process()
def fit(self):
for iteration in range(self.num_iterations):
self._compute_distances()
for i in range(self.num_points):
# subtract the current point from previous cluster
if self.min_distance_index[i] != self.point_class[i]:
if self.point_class[i] > 0:
self.clusters_size[self.point_class[i]] -= 1
self.cluster_energy[
self.point_class[i]
] -= self.point_distances[i]
cluster_index = self.point_class[i] * self.num_dimension
point_index = i * self.num_dimension
for d in range(self.num_dimension):
self.clusters_sum[cluster_index + d] -= self.points[
point_index + d
]
# assign new cluster and distances
self.point_class[i] = self.min_distance_index[i]
self.clusters_size[self.min_distance_index[i]] += 1
cluster_index = self.point_class[i] * self.num_dimension
point_index = i * self.num_dimension
for d in range(self.num_dimension):
self.clusters_sum[cluster_index + d] += self.points[
point_index + d
]
self.point_distances[i] = self.squared_distances[i]
self.cluster_energy[
self.min_distance_index[i]
] += self.point_distances[i]
# update centroids
diff = 0.0
for i in range(self.num_clusters):
cluster_index = i * self.num_dimension
for d in range(self.num_dimension):
new_coordinate = (
self.clusters_sum[cluster_index + d] / self.clusters_size[i]
)
diff = max(
diff, abs(self.centroids[cluster_index + d] - new_coordinate)
)
self.centroids[cluster_index + d] = new_coordinate
# break iteration
if diff < 1e-12:
print("Centroids unchanged at iteration ", iteration, " terminating...")
break
def plot_strong_scaling(n_samples, num_clusters, max_num_processes):
global kMeansMultiProcess
from sklearn.datasets.samples_generator import make_blobs
input_points, y_values = make_blobs(
n_samples=n_samples, centers=num_clusters, cluster_std=0.4, random_state=42
)
times_multi_processes_python = np.empty((max_num_processes,))
for num_processes in range(1, max_num_processes + 1):
print("num_processes ", num_processes)
kMeansMultiProcess = KMeansMultiProcess(
num_clusters,
input_points,
num_iterations=100,
num_processes=num_processes,
random_state=42,
external_kernel=False,
)
from timeit import timeit
times_multi_processes_python[num_processes - 1] = timeit(
"kMeansMultiProcess.fit()", number=1, globals=globals()
)
print(times_multi_processes_python)
times_multi_threaded_external = np.empty((max_num_processes,))
for num_processes in range(1, max_num_processes + 1):
print(num_processes)
kMeansMultiProcess = KMeansMultiProcess(
num_clusters,
input_points,
num_iterations=100,
num_processes=num_processes,
random_state=42,
external_kernel=True,
)
from timeit import timeit
times_multi_threaded_external[num_processes - 1] = timeit(
"kMeansMultiProcess.fit()", number=1, globals=globals()
)
# print(times_multi_threaded_external)
plt.figure(figsize=(10, 4))
ax = plt.axes()
threads = range(1, max_num_processes + 1)
ax.plot(threads, times_multi_processes_python, "r--", label="Python multiprocess")
ax.plot(
threads,
times_multi_threaded_external,
"b--",
label="C++ external library with OpenMP",
)
plt.title(
"Strong scaling with "
+ str(n_samples)
+ " samples, "
+ str(num_clusters)
+ " clusters",
fontsize=14,
)
plt.xlabel("Number of processes", fontsize=16)
plt.ylabel("Wall clock time (s)", fontsize=16)
plt.xlim(1, max_num_processes)
plt.ylim(0, 20)
import matplotlib.ticker as mticker
plt.gca().xaxis.set_major_locator(mticker.MultipleLocator(1))
plt.legend(loc=2)
def plot_results(n_samples, num_clusters, num_processes, external_kernel):
from sklearn.datasets.samples_generator import make_blobs
input_points, labels, true_centroids = make_blobs(
n_samples=n_samples,
centers=num_clusters,
cluster_std=0.4,
random_state=0,
return_centers=True,
)
kMeansMultiProcess = KMeansMultiProcess(
num_clusters,
input_points,
num_iterations=100,
num_processes=num_processes,
external_kernel=external_kernel,
)
kMeansMultiProcess.fit()
print("Plotting...")
cross_color = "k"
calculated_centroids = np.reshape(
kMeansMultiProcess.centroids,
(kMeansMultiProcess.num_clusters, kMeansMultiProcess.num_dimension),
)
plt.scatter(
calculated_centroids[:, 0],
calculated_centroids[:, 1],
marker="x",
s=50,
linewidths=50,
color=cross_color,
zorder=11,
alpha=1,
)
plt.scatter(
true_centroids[:, 0],
true_centroids[:, 1],
marker="+",
s=50,
linewidths=50,
color="r",
zorder=11,
alpha=1,
)
plt.plot(input_points[:, 0], input_points[:, 1], "k.", markersize=2)