1414#include < chrono>
1515#include < iostream>
1616
17- km::KMeansOMP::KMeansOMP (const int & k, const std::vector<Point>& points)
18- : KMeansBase(k, points) {}
17+ km::KMeansOMP::KMeansOMP (const int & k, const std::vector<Point>& points)
18+ : KMeansBase(k, points) {}
1919
2020auto km::KMeansOMP::run () -> void
2121{
@@ -32,7 +32,7 @@ auto km::KMeansOMP::run() -> void
3232 counts = std::vector<int >(k, 0 );
3333 #pragma omp parallel
3434 {
35-
35+
3636 #pragma omp for
3737 for (auto & p : points)
3838 {
@@ -53,33 +53,35 @@ auto km::KMeansOMP::run() -> void
5353 change = true ;
5454 }
5555 }
56- #pragma omp master
56+ }
57+ std::vector<std::vector<double >> partial_sum (k, {0.0 , 0.0 , 0.0 });
58+ std::vector<int > cluster_size (k, 0 );
59+
60+ #pragma omp parallel
61+ {
62+ #pragma omp for
63+ for (int i = 0 ; i < k; ++i)
5764 {
58- std::vector<std::vector<double >> partial_sum (k, {0.0 , 0.0 , 0.0 });
59- std::vector<int > cluster_size (k, 0 );
60-
61- for (int i = 0 ; i < k; ++i)
65+ for (auto & point : points)
6266 {
63- for ( auto & point : points )
67+ if ( point. clusterId == i )
6468 {
65- if (point. clusterId == i )
69+ for ( int x = 0 ; x < 3 ; x++ )
6670 {
67- for (int x = 0 ; x < 3 ; x++)
68- {
69- partial_sum[i][x] += point.getFeature_int (x);
70- }
71- cluster_size[i]++;
71+ partial_sum[i][x] += point.getFeature_int (x);
7272 }
73+ cluster_size[i]++;
7374 }
7475 }
76+ }
7577
76- for (int i = 0 ; i < k; ++i)
78+ #pragma omp for
79+ for (int i = 0 ; i < k; ++i)
80+ {
81+ for (int j = 0 ; j < 3 ; j++)
7782 {
78- for (int j = 0 ; j < 3 ; j++)
79- {
80- int result = static_cast <int >(partial_sum[i][j] / cluster_size[i]);
81- centroids[i].setFeature (j, result);
82- }
83+ int result = static_cast <int >(partial_sum[i][j] / cluster_size[i]);
84+ centroids[i].setFeature (j, result);
8385 }
8486 }
8587 }
0 commit comments