Commit d33bf880 authored by Valeriu Codreanu's avatar Valeriu Codreanu
Browse files

Âfixed indentation problems

parent 07961bc8
......@@ -51,17 +51,17 @@
/** **/
/** Edited by: Jay Pisharath, Wei-keng Liao **/
/** Northwestern University. **/
/** **/
/** **/
/** ================================================================ **/
/** **/
/** Edited by: Shuai Che, David Tarjan, Sang-Ha Lee **/
/** University of Virginia **/
/** **/
/** Description: No longer supports fuzzy c-means clustering; **/
/** only regular k-means clustering. **/
/** No longer performs "validity" function to analyze **/
/** compactness and separation crietria; instead **/
/** calculate root mean squared error. **/
/** **/
/** Edited by: Shuai Che, David Tarjan, Sang-Ha Lee **/
/** University of Virginia **/
/** **/
/** Description: No longer supports fuzzy c-means clustering; **/
/** only regular k-means clustering. **/
/** No longer performs "validity" function to analyze**/
/** compactness and separation crietria; instead **/
/** calculate root mean squared error. **/
/** **/
/*************************************************************************/
......@@ -78,24 +78,24 @@ extern double wtime(void);
float min_rmse_ref = FLT_MAX; /* reference min_rmse value */
/*---< cluster() >-----------------------------------------------------------*/
int cluster(int npoints, /* number of data points */
int nfeatures, /* number of attributes for each point */
float **features, /* array: [npoints][nfeatures] */
int min_nclusters, /* range of min to max number of clusters */
int max_nclusters,
float threshold, /* loop terminating factor */
int *best_nclusters, /* out: number between min and max with lowest RMSE */
float ***cluster_centres, /* out: [best_nclusters][nfeatures] */
float *min_rmse, /* out: minimum RMSE */
int isRMSE, /* calculate RMSE */
int nloops /* number of iteration for each number of clusters */
)
{
int cluster(int npoints, /* number of data points */
int nfeatures, /* number of attributes for each point */
float **features, /* array: [npoints][nfeatures] */
int min_nclusters, /* range of min to max number of clusters */
int max_nclusters,
float threshold, /* loop terminating factor */
int *best_nclusters, /* out: number between min and max with lowest RMSE */
float ***cluster_centres, /* out: [best_nclusters][nfeatures] */
float *min_rmse, /* out: minimum RMSE */
int isRMSE, /* calculate RMSE */
int nloops /* number of iteration for each number of clusters */
)
{
int nclusters; /* number of clusters k */
int index =0; /* number of iteration to reach the best RMSE */
int rmse; /* RMSE for each clustering */
int *membership; /* which cluster a data point belongs to */
float **tmp_cluster_centres; /* hold coordinates of cluster centers */
int *membership; /* which cluster a data point belongs to */
float **tmp_cluster_centres; /* hold coordinates of cluster centers */
int i;
/* allocate memory for membership */
......@@ -125,7 +125,7 @@ int cluster(int npoints, /* number of data points */
free(*cluster_centres);
}
*cluster_centres = tmp_cluster_centres;
/* find the number of clusters with the best RMSE */
if(isRMSE)
......@@ -142,7 +142,7 @@ int cluster(int npoints, /* number of data points */
*best_nclusters = nclusters; //update optimum number of clusters
index = i; //update number of iteration to reach best RMSE
}
}
}
}
deallocateMemory(); /* free device memory (@ kmeans_cuda.cu) */
......
......@@ -57,15 +57,15 @@
/** Northwestern University. **/
/** **/
/** ================================================================ **/
/** **/
/** Edited by: Shuai Che, David Tarjan, Sang-Ha Lee **/
/** University of Virginia **/
/** **/
/** Description: No longer supports fuzzy c-means clustering; **/
/** only regular k-means clustering. **/
/** No longer performs "validity" function to analyze **/
/** compactness and separation crietria; instead **/
/** calculate root mean squared error. **/
/** **/
/** Edited by: Shuai Che, David Tarjan, Sang-Ha Lee **/
/** University of Virginia **/
/** **/
/** Description: No longer supports fuzzy c-means clustering; **/
/** only regular k-means clustering. **/
/** no longer performs "validity" function to analyze**/
/** compactness and separation crietria; instead **/
/** calculate root mean squared error. **/
/** **/
/*************************************************************************/
#define _CRT_SECURE_NO_DEPRECATE 1
......@@ -91,11 +91,11 @@ void usage(char *argv0) {
"\nUsage: %s [switches] -i filename\n\n"
" -i filename :file containing data to be clustered\n"
" -m max_nclusters :maximum number of clusters allowed [default=5]\n"
" -n min_nclusters :minimum number of clusters allowed [default=5]\n"
" -n min_nclusters :minimum number of clusters allowed [default=5]\n"
" -t threshold :threshold value [default=0.001]\n"
" -l nloops :iteration for each number of clusters [default=1]\n"
" -b :input file is in binary format\n"
" -r :calculate RMSE [default=off]\n"
" -r :calculate RMSE [default=off]\n"
" -o :output cluster center coordinates [default=off]\n";
fprintf(stderr, help, argv0);
exit(-1);
......@@ -104,7 +104,7 @@ void usage(char *argv0) {
/*---< main() >-------------------------------------------------------------*/
int setup(int argc, char **argv) {
int opt;
extern char *optarg;
extern char *optarg;
char *filename = 0;
float *buf;
char line[1024];
......@@ -131,35 +131,35 @@ int setup(int argc, char **argv) {
float cluster_timing, io_timing;
#endif
/* obtain command line arguments and change appropriate options */
while ( (opt=getopt(argc,argv,"i:t:m:n:l:bro"))!= EOF) {
while ( (opt=getopt(argc,argv,"i:t:m:n:l:bro"))!= EOF) {
switch (opt) {
case 'i': filename=optarg;
break;
break;
case 'b': isBinaryFile = 1;
break;
break;
case 't': threshold=atof(optarg);
break;
break;
case 'm': max_nclusters = atoi(optarg);
break;
break;
case 'n': min_nclusters = atoi(optarg);
break;
case 'r': isRMSE = 1;
break;
case 'o': isOutput = 1;
break;
case 'l': nloops = atoi(optarg);
break;
break;
case 'r': isRMSE = 1;
break;
case 'o': isOutput = 1;
break;
case 'l': nloops = atoi(optarg);
break;
case '?': usage(argv[0]);
break;
break;
default: usage(argv[0]);
break;
break;
}
}
if (filename == 0) usage(argv[0]);
/* ============== I/O begin ==============*/
/* get nfeatures and npoints */
/* get nfeatures and npoints */
#ifdef _OPENMP
io_timing = omp_get_wtime();
#endif
......@@ -199,7 +199,7 @@ int setup(int argc, char **argv) {
while (strtok(NULL, " ,\t\n") != NULL) nfeatures++;
break;
}
}
}
/* allocate space for features[] and read attributes of all objects */
buf = (float*) malloc(npoints*nfeatures*sizeof(float));
......@@ -214,7 +214,7 @@ int setup(int argc, char **argv) {
for (j=0; j<nfeatures; j++) {
buf[i] = atof(strtok(NULL, " ,\t\n"));
i++;
}
}
}
fclose(infile);
}
......@@ -234,7 +234,7 @@ int setup(int argc, char **argv) {
exit(0);
}
srand(7); /* seed for future random number generator */
srand(7); /* seed for future random number generator */
memcpy(features[0], buf, npoints*nfeatures*sizeof(float)); /* now features holds 2-dimensional array of features */
free(buf);
......@@ -243,17 +243,17 @@ int setup(int argc, char **argv) {
cluster_timing = omp_get_wtime(); /* Total clustering time */
#endif
cluster_centres = NULL;
index = cluster(npoints, /* number of data points */
nfeatures, /* number of features for each point */
features, /* array: [npoints][nfeatures] */
min_nclusters, /* range of min to max number of clusters */
max_nclusters,
threshold, /* loop termination factor */
&best_nclusters, /* return: number between min and max */
&cluster_centres, /* return: [best_nclusters][nfeatures] */
&rmse, /* Root Mean Squared Error */
isRMSE, /* calculate RMSE */
nloops); /* number of iteration for each number of clusters */
index = cluster(npoints, /* number of data points */
nfeatures, /* number of features for each point */
features, /* array: [npoints][nfeatures] */
min_nclusters, /* range of min to max number of clusters */
max_nclusters,
threshold, /* loop termination factor */
&best_nclusters, /* return: number between min and max */
&cluster_centres, /* return: [best_nclusters][nfeatures] */
&rmse, /* Root Mean Squared Error */
isRMSE, /* calculate RMSE */
nloops); /* number of iteration for each number of clusters */
#ifdef _OPENMP
cluster_timing = omp_get_wtime() - cluster_timing;
#endif
......@@ -281,30 +281,30 @@ int setup(int argc, char **argv) {
printf("Time for Entire Clustering: %.5fsec\n", cluster_timing);
#endif
if(min_nclusters != max_nclusters){
if(nloops != 1){ //range of k, multiple iteration
if(nloops != 1){//range of k, multiple iteration
#ifdef _OPENMP
//printf("Average Clustering Time: %fsec\n",
// cluster_timing / len);
//cluster_timing / len);
#endif
printf("Best number of clusters is %d\n", best_nclusters);
printf("Best number of clusters is %d\n", best_nclusters);
}
else{ //range of k, single iteration
else{ //range of k, single iteration
//printf("Average Clustering Time: %fsec\n",
// cluster_timing / len);
printf("Best number of clusters is %d\n", best_nclusters);
printf("Best number of clusters is %d\n", best_nclusters);
}
}
else{
if(nloops != 1){ // single k, multiple iteration
if(nloops != 1){// single k, multiple iteration
#ifdef _OPENMP
printf("Average Clustering Time: %.5fsec\n",
cluster_timing / nloops);
#endif
if(isRMSE) // if calculated RMSE
if(isRMSE)// if calculated RMSE
printf("Number of trials to approach the best RMSE of %.3f is %d\n", rmse, index + 1);
}
else{ // single k, single iteration
if(isRMSE) // if calculated RMSE
else{// single k, single iteration
if(isRMSE)// if calculated RMSE
printf("Root Mean Squared Error: %.3f\n", rmse);
}
}
......@@ -312,7 +312,7 @@ int setup(int argc, char **argv) {
/* free up memory */
free(features[0]);
free(features);
return(0);
free(features);
return(0);
}
......@@ -49,15 +49,15 @@
/** Northwestern University. **/
/** **/
/** ================================================================ **/
/** **/
/** Edited by: Shuai Che, David Tarjan, Sang-Ha Lee **/
/** University of Virginia **/
/** **/
/** Description: No longer supports fuzzy c-means clustering; **/
/** only regular k-means clustering. **/
/** No longer performs "validity" function to analyze **/
/** compactness and separation crietria; instead **/
/** calculate root mean squared error. **/
/** **/
/** Edited by: Shuai Che, David Tarjan, Sang-Ha Lee **/
/** University of Virginia **/
/** **/
/** Description: No longer supports fuzzy c-means clustering; **/
/** only regular k-means clustering. **/
/** No longer performs "validity" function to analyze**/
/** compactness and separation crietria; instead **/
/** calculate root mean squared error. **/
/** **/
/*************************************************************************/
......@@ -80,22 +80,22 @@ float** kmeans_clustering(float **feature, /* in: [npoints][nfeatures] */
float threshold,
int *membership) /* out: [npoints] */
{
int i, j, n = 0; /* counters */
int loop=0, temp;
int *new_centers_len; /* [nclusters]: no. of points in each cluster */
float delta; /* if the point moved */
float **clusters; /* out: [nclusters][nfeatures] */
float **new_centers; /* [nclusters][nfeatures] */
int *initial; /* used to hold the index of points not yet selected
prevents the "birthday problem" of dual selection (?)
considered holding initial cluster indices, but changed due to
possible, though unlikely, infinite loops */
int initial_points;
int c = 0;
int i, j, n = 0; /* counters */
int loop=0, temp;
int *new_centers_len; /* [nclusters]: no. of points in each cluster */
float delta; /* if the point moved */
float **clusters; /* out: [nclusters][nfeatures] */
float **new_centers; /* [nclusters][nfeatures] */
int *initial; /* used to hold the index of points not yet selected
prevents the "birthday problem" of dual selection (?)
considered holding initial cluster indices, but changed due to
possible, though unlikely, infinite loops */
int initial_points;
int c = 0;
/* nclusters should never be > npoints
that would guarantee a cluster without points */
that would guarantee a cluster without points */
if (nclusters > npoints)
nclusters = npoints;
......@@ -146,14 +146,14 @@ float** kmeans_clustering(float **feature, /* in: [npoints][nfeatures] */
delta = 0.0;
// CUDA
delta = (float) kmeansCuda(feature, /* in: [npoints][nfeatures] */
nfeatures, /* number of attributes for each point */
npoints, /* number of data points */
nclusters, /* number of clusters */
membership, /* which cluster the point belongs to */
clusters, /* out: [nclusters][nfeatures] */
new_centers_len, /* out: number of points in each cluster */
new_centers /* sum of points in each cluster */
);
nfeatures, /* number of attributes for each point */
npoints, /* number of data points */
nclusters, /* number of clusters */
membership, /* which cluster the point belongs to */
clusters, /* out: [nclusters][nfeatures] */
new_centers_len, /* out: number of points in each cluster */
new_centers /* sum of points in each cluster */
);
/* replace old cluster centers with new_centers */
/* CPU side of reduction */
......
......@@ -143,38 +143,38 @@ kmeansCuda(float **feature, /* in: [npoints][nfeatures] */
float **clusters, /* coordinates of cluster centers */
int *new_centers_len, /* number of elements in each cluster */
float **new_centers /* sum of elements in each cluster */
)
)
{
int delta = 0; /* if point has moved */
int i,j; /* counters */
int delta = 0; /* if point has moved */
int i,j; /* counters */
cudaSetDevice(1);
cudaSetDevice(1);
/* copy membership (host to device) */
cudaMemcpy(membership_d, membership_new, npoints*sizeof(int), cudaMemcpyHostToDevice);
/* copy membership (host to device) */
cudaMemcpy(membership_d, membership_new, npoints*sizeof(int), cudaMemcpyHostToDevice);
/* copy clusters (host to device) */
cudaMemcpy(clusters_d, clusters[0], nclusters*nfeatures*sizeof(float), cudaMemcpyHostToDevice);
/* copy clusters (host to device) */
cudaMemcpy(clusters_d, clusters[0], nclusters*nfeatures*sizeof(float), cudaMemcpyHostToDevice);
/* set up texture */
/* set up texture */
cudaChannelFormatDesc chDesc0 = cudaCreateChannelDesc<float>();
t_features.filterMode = cudaFilterModePoint;
t_features.normalized = false;
t_features.channelDesc = chDesc0;
if(cudaBindTexture(NULL, &t_features, feature_d, &chDesc0, npoints*nfeatures*sizeof(float)) != (cudaError_t) CUDA_SUCCESS)
printf("Couldn't bind features array to texture!\n");
if(cudaBindTexture(NULL, &t_features, feature_d, &chDesc0, npoints*nfeatures*sizeof(float)) != (cudaError_t) CUDA_SUCCESS)
printf("Couldn't bind features array to texture!\n");
cudaChannelFormatDesc chDesc1 = cudaCreateChannelDesc<float>();
cudaChannelFormatDesc chDesc1 = cudaCreateChannelDesc<float>();
t_features_flipped.filterMode = cudaFilterModePoint;
t_features_flipped.normalized = false;
t_features_flipped.channelDesc = chDesc1;
if(cudaBindTexture(NULL, &t_features_flipped, feature_flipped_d, &chDesc1, npoints*nfeatures*sizeof(float)) != (cudaError_t) CUDA_SUCCESS)
printf("Couldn't bind features_flipped array to texture!\n");
if(cudaBindTexture(NULL, &t_features_flipped, feature_flipped_d, &chDesc1, npoints*nfeatures*sizeof(float)) != (cudaError_t) CUDA_SUCCESS)
printf("Couldn't bind features_flipped array to texture!\n");
cudaChannelFormatDesc chDesc2 = cudaCreateChannelDesc<float>();
cudaChannelFormatDesc chDesc2 = cudaCreateChannelDesc<float>();
t_clusters.filterMode = cudaFilterModePoint;
t_clusters.normalized = false;
t_clusters.channelDesc = chDesc2;
......
......@@ -129,10 +129,10 @@ kmeansPoint(float *features, /* in: [npoints*nfeatures] */
// primitve reduction follows
unsigned int threadids_participating = THREADS_PER_BLOCK / 2;
for(;threadids_participating > 1; threadids_participating /= 2) {
if(threadIdx.x < threadids_participating) {
if(threadIdx.x < threadids_participating) {
deltas[threadIdx.x] += deltas[threadIdx.x + threadids_participating];
}
__syncthreads();
__syncthreads();
}
if(threadIdx.x < 1) {deltas[threadIdx.x] += deltas[threadIdx.x + 1];}
__syncthreads();
......
/*************************************************************************/
/** File: rmse.c **/
/** File: rmse.c **/
/** Description: calculate root mean squared error of particular **/
/** clustering. **/
/** Author: Sang-Ha Lee **/
/** University of Virginia. **/
/** **/
/** clustering. **/
/** Author: Sang-Ha Lee **/
/** University of Virginia. **/
/** **/
/** Note: euclid_dist_2() and find_nearest_point() adopted from **/
/** Minebench code. **/
/** Minebench code. **/
/** **/
/*************************************************************************/
......@@ -62,15 +62,15 @@ int find_nearest_point(float *pt, /* [nfeatures] */
/*----< rms_err(): calculates RMSE of clustering >-------------------------------------*/
float rms_err (float **feature, /* [npoints][nfeatures] */
int nfeatures,
int npoints,
float **cluster_centres, /* [nclusters][nfeatures] */
int nclusters)
int nfeatures,
int npoints,
float **cluster_centres, /* [nclusters][nfeatures] */
int nclusters)
{
int i;
int nearest_cluster_index; /* cluster center id with min distance to pt */
float sum_euclid = 0.0; /* sum of Euclidean distance squares */
float ret; /* return value */
int i;
int nearest_cluster_index; /* cluster center id with min distance to pt */
float sum_euclid = 0.0; /* sum of Euclidean distance squares */
float ret; /* return value */
/* calculate and sum the sqaure of euclidean distance*/
#pragma omp parallel for \
......@@ -79,19 +79,18 @@ float rms_err (float **feature, /* [npoints][nfeatures] */
private(i, nearest_cluster_index) \
schedule (static)
for (i=0; i<npoints; i++) {
nearest_cluster_index = find_nearest_point(feature[i],
nfeatures,
cluster_centres,
nclusters);
nearest_cluster_index = find_nearest_point(feature[i],
nfeatures,
cluster_centres,
nclusters);
sum_euclid += euclid_dist_2(feature[i],
cluster_centres[nearest_cluster_index],
nfeatures);
}
/* divide by n, then take sqrt */
ret = sqrt(sum_euclid / npoints);
cluster_centres[nearest_cluster_index],
nfeatures);
}
/* divide by n, then take sqrt */
ret = sqrt(sum_euclid / npoints);
return(ret);
}
......@@ -52,14 +52,14 @@
/** Northwestern University. **/
/** **/
/** ================================================================ **/
/** **/
/** Edited by: Sang-Ha Lee **/
/** University of Virginia **/
/** **/
/** Description: No longer supports fuzzy c-means clustering; **/
/** only regular k-means clustering. **/
/** Simplified for main functionality: regular k-means **/
/** clustering. **/
/** **/
/**Edited by: Sang-Ha Lee **/
/**University of Virginia **/
/** **/
/**Description: No longer supports fuzzy c-means clustering; **/
/** only regular k-means clustering. **/
/** Simplified for main functionality: regular k-means **/
/** clustering. **/
/** **/
/*************************************************************************/
......@@ -88,25 +88,23 @@ int cluster(int numObjects, /* number of input objects */
float **tmp_cluster_centres;
membership = (int*) malloc(numObjects * sizeof(int));
srand(7);
/* perform regular Kmeans */
/* perform regular Kmeans */
tmp_cluster_centres = kmeans_clustering(attributes,
numAttributes,
numObjects,
nclusters,
threshold,
membership);
if (*cluster_centres) {
free((*cluster_centres)[0]);
free((*cluster_centres)[0]);
free(*cluster_centres);
}
*cluster_centres = tmp_cluster_centres;
free(membership);
free(membership);
return 0;
}
......@@ -48,14 +48,14 @@
/** Northwestern University. **/
/** **/
/** ================================================================ **/
/** **/
/** Edited by: Sang-Ha Lee **/
/** University of Virginia **/
/** **/
/** Description: No longer supports fuzzy c-means clustering; **/
/** only regular k-means clustering. **/
/** Simplified for main functionality: regular k-means **/
/** clustering. **/
/** **/
/**Edited by: Sang-Ha Lee **/
/**University of Virginia **/
/** **/
/**Description: No longer supports fuzzy c-means clustering; **/
/** only regular k-means clustering. **/
/** Simplified for main functionality: regular k-means **/
/** clustering. **/
/** **/
/*************************************************************************/
......@@ -123,13 +123,13 @@ float** kmeans_clustering(float **feature, /* in: [npoints][nfeatures] */
int i, j, k, n=0, index, loop=0;
int *new_centers_len; /* [nclusters]: no. of points in each cluster */
float **new_centers; /* [nclusters][nfeatures] */
float **clusters; /* out: [nclusters][nfeatures] */
float **new_centers; /* [nclusters][nfeatures] */
float **clusters; /* out: [nclusters][nfeatures] */
float delta;
double timing;
int nthreads;
int nthreads;
int **partial_new_centers_len;
float ***partial_new_centers;
......@@ -219,7 +219,7 @@ float** kmeans_clustering(float **feature, /* in: [npoints][nfeatures] */
partial_new_centers[j][i][k] = 0.0;
<