-
Notifications
You must be signed in to change notification settings - Fork 1
/
DistanceCalculator.cpp
62 lines (50 loc) · 1.64 KB
/
DistanceCalculator.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
#if defined(_WIN32)
#define DISTANCE_CALCULATOR_API __declspec(dllexport)
#else
#define DISTANCE_CALCULATOR_API __attribute__((visibility("default")))
#endif
#include "omp.h"
// contains all mesh instances
namespace DistanceCalculator
{
extern "C"
{
DISTANCE_CALCULATOR_API void distance_calculator(
int num_clusters,
int num_points,
int num_dimensions,
int num_threads,
double* centroids_coordinates,
double* points,
double* min_distance,
int* min_distance_index)
{
if (num_threads < 1)
{
return;
}
// set the numebr of threads
omp_set_num_threads(num_threads);
#pragma omp parallel for schedule(static,1)
for (int p = 0; p < num_points; ++p)
{
const int point_position = p * num_dimensions;
for (int c = 0; c < num_clusters; ++c)
{
const int cluster_position = c * num_dimensions;
double squared_distance = 0.0;
for (int d = 0; d < num_dimensions; ++d)
{
double delta = centroids_coordinates[cluster_position + d] - points[point_position + d];
squared_distance += delta * delta;
}
if (squared_distance < min_distance[p])
{
min_distance[p] = squared_distance;
min_distance_index[p] = c;
}
}
}
}
}
}