Mercurial > cgi-bin > hgwebdir.cgi > PR > Applications > Vthread > Vthread__KMeans__Bench
view pthreads_main.c @ 1:8e7bdab2840f
VPThread version workinh
| author | Merten Sach <msach@mailbox.tu-berlin.de> |
|---|---|
| date | Tue, 16 Aug 2011 20:32:55 +0200 |
| parents | e69e4c2d612a |
| children | 467746c73fd0 |
line source
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* File: pthreads_main.c (an OpenMP version) */
3 /* Description: This program shows an example on how to call a subroutine */
4 /* that implements a simple k-means clustering algorithm */
5 /* based on Euclid distance. */
6 /* Input file format: */
7 /* ascii file: each line contains 1 data object */
8 /* binary file: first 4-byte integer is the number of data */
9 /* objects and 2nd integer is the no. of features (or */
10 /* coordinates) of each object */
11 /* */
12 /* Author: Wei-keng Liao */
13 /* ECE Department Northwestern University */
14 /* email: wkliao@ece.northwestern.edu */
15 /* Copyright, 2005, Wei-keng Liao */
16 /* */
17 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h> /* strtok() */
22 #include <sys/types.h> /* open() */
23 #include <sys/stat.h>
24 #include <sys/time.h>
25 #include <fcntl.h>
26 #include <unistd.h> /* getopt() */
27 #include <time.h>
28 #include "kmeans.h"
30 #include "VPThread_lib/VPThread.h"
32 char __ProgrammName[] = "kmeans";
33 char __DataSet[255];
35 #define seconds(tm) gettimeofday(&tp,(struct timezone *)0);\
36 tm=tp.tv_sec+tp.tv_usec/1000000.0
38 struct timeval tp;
40 int numClusters, numCoords, numObjs, nthreads;
42 /*
43 * Function: usage
44 * ---------------
45 * Prints information on how to call the program.
46 */
47 static void usage(char *argv0) {
48 char *help =
49 "Usage: %s [switches] -i filename -n num_clusters [OPTIONS]\n"
50 " -i filename : file containing data to be clustered\n"
51 " -b : input file is in binary format (default no)\n"
52 " -n num_clusters: number of clusters (K must be > 1)\n"
53 " -p nproc : number of threads (default 1)\n"
54 " -o filename : write output to file\n";
55 fprintf(stderr, help, argv0);
56 exit(-1);
57 }
59 /*---< main() >-------------------------------------------------------------*/
60 int main(int argc, char **argv) {
61 int opt;
62 extern char *optarg;
63 extern int optind;
64 int j;
65 int isBinaryFile;
67 int *membership; /* [numObjs] */
68 char *filename, *outfile;
69 double **objects; /* [numObjs][numCoords] data objects */
70 double **clusters; /* [numClusters][numCoords] cluster center */
71 double threshold;
72 double timing, io_timing, clustering_timing;
74 /* some default values */
75 nthreads = 1; /* Amount of threads to use */
76 numClusters = 1; /* Amount of cluster centers */
77 threshold = 0.001; /* Percentage of objects that need to change membership for the clusting to continue */
78 isBinaryFile = 0; /* 0 if the input file is in ASCII format, 1 for binary format */
79 filename = NULL; /* Name of the input file */
80 outfile = NULL; /* Name of the output file */
82 /* Parse command line options */
83 while ( (opt=getopt(argc,argv,"o:p:i:n:t:bh"))!= EOF) {
84 switch (opt) {
85 case 'i': filename=optarg;
86 break;
87 case 'b': isBinaryFile = 1;
88 break;
89 case 'n': numClusters = atoi(optarg);
90 break;
91 case 'p': nthreads = atoi(optarg);
92 break;
93 case 'h': usage(argv[0]);
94 break;
95 case 'o': outfile=optarg;
96 break;
97 default: usage(argv[0]);
98 break;
99 }
100 }
102 if (filename == NULL) usage(argv[0]);
104 seconds(io_timing);
106 /* Read input data points from given input file */
107 objects = file_read(isBinaryFile, filename, &numObjs, &numCoords);
108 assert(objects != NULL);
110 seconds(timing);
111 io_timing = timing - io_timing;
112 clustering_timing = timing;
114 membership = (int*) malloc(numObjs * sizeof(int));
115 assert(membership != NULL);
117 clusters = malloc(numClusters * sizeof(double*));
118 assert(clusters != NULL);
119 clusters[0] = malloc(numClusters * numCoords * sizeof(double));
120 assert(clusters[0] != NULL);
122 struct call_data data = { 0, objects, numCoords, numObjs,
123 numClusters, threshold, membership, clusters };
125 /* Launch the core computation algorithm */
126 VPThread__create_seed_procr_and_do_work(pthreads_kmeans, (void*)&data);
128 free(objects[0]);
129 free(objects);
131 seconds(timing);
132 clustering_timing = timing - clustering_timing;
134 /* Memory cleanup */
135 free(membership);
137 if(outfile != NULL) {
138 int l;
139 FILE* fp = fopen(outfile, "w");
140 for(j = 0; j < numClusters; j++) {
141 fprintf(fp, "Cluster %d: ", j);
142 for(l = 0; l < numCoords; l++)
143 fprintf(fp, "%f ", clusters[j][l]);
144 fprintf(fp, "\n");
145 }
146 fclose(fp);
147 }
149 free(clusters[0]);
150 free(clusters);
152 /* Print performance numbers on stdout */
153 double t1;
154 io_timing += seconds(t1) - timing;
156 printf("\n---- kMeans Clustering ----\n");
157 printf("Number of threads = %d\n", nthreads);
158 printf("Input file: %s\n", filename);
159 printf("numObjs = %d\n", numObjs);
160 printf("numCoords = %d\n", numCoords);
161 printf("numClusters = %d\n", numClusters);
162 printf("threshold = %.4f\n", threshold);
164 printf("I/O time = %10.4f sec\n", io_timing);
165 printf("Computation timing = %10.4f sec\n", clustering_timing);
167 return(0);
168 }
