Skip to content

Commit

Permalink
OpenMP
Browse files Browse the repository at this point in the history
  • Loading branch information
eliazonta committed Aug 18, 2023
1 parent 47e6518 commit abff81b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
4 changes: 2 additions & 2 deletions include/k-means.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
#define __KMEANS__

#include <vector>
#include "Point.h"
#include "point.h"
#include "readWrite.h"
#include <iostream> // debug

void KMeans(std::vector<Point>* p,int epochs, int k);

#endif
#endif
4 changes: 2 additions & 2 deletions include/readWrite.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#include <sstream>
#include <vector>

#include "Point.h"
#include "point.h"

std::vector<Point> readCsv(std::string path);
void writeCsv(std::vector<Point>* p, std::string s);

#endif
#endif
22 changes: 15 additions & 7 deletions src/k-means.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,23 @@
#include "../include/k-means.h"
#include <omp.h>
// #define DEBUG
// #define OMP
//#define OMP

void KMeans(std::vector<Point>* p,int epochs, int k){
std::vector<Point> centroids;
std::srand(time(nullptr));
std::vector<int> npts(p->size());
std::vector<double> sx(k), sy(k), sz(k);
std::vector<double> sx(k), sy(k), sz(k); //cumulative x, y, x
#ifdef DEBUG
std::cout << "=== DEBUG ===" << std::endl;
#endif
// init clusters with some random point
#ifdef OMP
// omp_set_num_threads(k);
#pragma omp num_threads(k)
#ifdef DEBUG
std::cout << "N threads : [" << omp_get_num_threads() << std::cout << "]\n";
#endif
#pragma omp parallel for
#endif
for(size_t i = 0; i < k; i++){
Expand All @@ -39,18 +42,15 @@ void KMeans(std::vector<Point>* p,int epochs, int k){
std::cout << "epoch - " << it << "/" << epochs << std::endl;
#endif
// assign pts to a cluster
size_t j;
#ifdef OMP
// fix different directive needed
// #pragma omp parallel for
#pragma omp parallel for private(j)
#endif
for(size_t i = 0; i < centroids.size(); ++i)
{
#ifdef DEBUG
std::cout<<"centroid N: [" << i << "]" << std::endl;
#endif
#ifdef OMP
#pragma omp parallel for
#endif
for(size_t j = 0;j < p->size(); ++j)
{
#ifdef DEBUG
Expand All @@ -77,6 +77,14 @@ void KMeans(std::vector<Point>* p,int epochs, int k){
p->at(i).setMinDist(__DBL_MAX__); // reset
}
//compute new centroids
#ifdef OMP
omp_set_num_threads(centroids.size());
int cthreads = omp_get_num_threads();
#ifdef DEBUG
std::cout << "N cthreads : [" << cthreads << std::cout << "]\n";
#endif
#pragma omp parallel for
#endif
for(size_t i = 0;i < centroids.size(); ++i)
{
centroids.at(i).setX(sx[i] / npts[i]);
Expand Down
12 changes: 6 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <vector>
#include <string>

#include "../include/Point.h"
#include "../include/point.h"
#include "../include/k-means.h"
#include "../include/readWrite.h"

Expand All @@ -26,12 +26,12 @@ int main(int argc, char** argv){
std::cerr << e.what() << '\n';
return EXIT_FAILURE;
}
clock_t stop = clock();
clock_t elapsed = (((float)(stop - start)) / CLOCKS_PER_SEC) * 1000; // ms
std::cout<< "[ K-Means clustering algorithm ]" << std::endl;
std::cout<< "-> Details : data [ " << path << "] | " << epochs << " epochs | "
clock_t stop = clock();
clock_t elapsed = (((float)(stop - start)) / CLOCKS_PER_SEC) * 1000; // s
std::cout<< "[ K-Means clustering algorithm ]\n" <<
"-> Details : data [ " << path << "] | " << epochs << " epochs | "
<< clusters << " clusters;" << std::endl;
std::cout << "Sequential running time : "<< elapsed << " ms" << std::endl;
std::cout << "Run time : "<< elapsed << " s" << std::endl;
writeCsv(&points, "data/out.csv");
}
return EXIT_SUCCESS;
Expand Down
2 changes: 1 addition & 1 deletion src/readWrite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ void writeCsv(std::vector<Point>* p, std::string s){
file << it->getX() << ", " << it->getY() << ", " << it->getZ() <<", " << it->getK() << std::endl;
}
file.close();
}
}

0 comments on commit abff81b

Please sign in to comment.