Skip to content

Commit

Permalink
added timer
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaweiZhuang committed Apr 16, 2017
1 parent 167df47 commit d813e59
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
22 changes: 11 additions & 11 deletions Parallel_Algorithm/OpenMP/Kmean_seq.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "../shared/timing.h" //for timer seconds()
#include <stdio.h>
#include <stdlib.h>
#include <float.h> //for FLT_MAX
#include <netcdf.h>


/* This is the name of the data file we will read. */
#define FILE_NAME "../test_data/Blobs_smp20000_fea30_cls8.nc"
#define TOL 0.0001
Expand Down Expand Up @@ -91,6 +93,8 @@ int readX(float*** p_X,int*** p_GUESS,
if ((retval = nc_close(ncid) ))
ERR(retval);

printf("=====reading data finished======\n");

return 0;
}

Expand All @@ -114,17 +118,9 @@ int main() {
float dist,dist_min,dist_sum_old,dist_sum_new,inert_best=FLT_MAX;

// get input data and its size
double iStart1 = seconds();
readX(&X,&GUESS,&N_samples,&N_features,&N_clusters,&N_repeat);

// check the input data
/*
for (i=0; i<N_samples; i=i+N_samples-1){
printf("no.%d ",i+1);
for (j=0; j<N_features; j++)
printf("%f ",X[i][j]);
printf("\n");
}
*/
double iElaps1 = seconds() - iStart1;

// each data point belongs to which cluster
// values range from 0 to N_cluster-1
Expand All @@ -140,6 +136,7 @@ int main() {
// needed by calculating the average position of data points in each cluster
int* cluster_sizes = (int *)malloc(N_clusters*sizeof(int));

double iStart2 = seconds();
for (int i_repeat=0; i_repeat < N_repeat; i_repeat++){

// guess initial centers
Expand Down Expand Up @@ -201,14 +198,17 @@ int main() {
} while( i_iter==1 || ((dist_sum_old - dist_sum_new > TOL)&&i_iter<MAX_ITER) );
//end of K-mean stepping

printf("Final inertia: %f, iteration: %d \n",dist_sum_new,i_iter);
//printf("Final inertia: %f, iteration: %d \n",dist_sum_new,i_iter);

if (dist_sum_new < inert_best)
inert_best = dist_sum_new;

} //end of one repeated run
double iElaps2 = seconds() - iStart2;

printf("Best inertia: %f \n",inert_best);
printf("Kmean time use (ms): %f \n", iElaps2*1000.0);
printf("I/O time use (ms): %f \n", iElaps1*1000.0);

return 0;
}
9 changes: 9 additions & 0 deletions Parallel_Algorithm/shared/timing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <sys/time.h>
#include <stdlib.h>

inline double seconds()
{
struct timeval tp;
int i = gettimeofday(&tp, NULL);
return ((double)tp.tv_sec + (double)tp.tv_usec * 1.e-6);
}

0 comments on commit d813e59

Please sign in to comment.