-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
78 lines (65 loc) · 2.06 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include "file_io.h"
#include "kmeans.h"
static void usage(char *argv0, float threshold) {
char *help =
"Usage: %s [switches] -i filename -n num_clusters\n"
" -i filename : file containing data to be clustered\n"
" -n num_clusters: number of clusters (K must > 1)\n"
" -t threshold : threshold value (default %.4f)\n"
" -c iteration : end after iterations\n";
fprintf(stderr, help, argv0, threshold);
exit(-1);
}
int main(int argc, char **argv) {
// read flags
int c;
float threshold = 0.001;
int iterations = 100;
int num_clusters = 0;
char *infile = NULL;
while ( (c=getopt(argc,argv,"i:n:t:c:"))!= EOF) {
switch (c) {
case 'i': infile=optarg;
break;
case 't': threshold=atof(optarg);
break;
case 'n': num_clusters = atoi(optarg);
break;
case 'c': iterations = atoi(optarg);
break;
case '?': usage(argv[0], threshold);
break;
default: usage(argv[0], threshold);
break;
}
}
if (infile == 0 || num_clusters <= 1)
usage(argv[0], threshold);
// read data from input file
int num_points, num_coords;
float **points = file_read(infile, &num_points, &num_coords);
assert(points);
// K-means calculation
int *membership = (int*) malloc(num_points * sizeof(int));
assert(membership);
#ifdef TIMING
printf("Timer start....\n");
int64_t start = GetTimeMius64();
#endif
float **clusters = kmeans(points, num_points, num_coords, num_clusters,
threshold, iterations, membership);
#ifdef TIMING
int64_t duration = GetTimeMiusFrom(start);
printf("K-means calculation time = %lld microseconds\n", (long long) duration);
#endif
free(points[0]);
free(points);
// write results to output file
file_write(infile, num_clusters, num_points, num_coords, clusters, membership);
free(membership);
free(clusters[0]);
free(clusters);
}