-
Notifications
You must be signed in to change notification settings - Fork 120
/
omp_main.c
164 lines (140 loc) · 6.38 KB
/
omp_main.c
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* File: omp_main.c (an OpenMP version) */
/* Description: This program shows an example on how to call a subroutine */
/* that implements a simple k-means clustering algorithm */
/* based on Euclid distance. */
/* Input file format: */
/* ascii file: each line contains 1 data object */
/* binary file: first 4-byte integer is the number of data */
/* objects and 2nd integer is the no. of features (or */
/* coordinates) of each object */
/* */
/* Author: Wei-keng Liao */
/* ECE Department Northwestern University */
/* email: [email protected] */
/* Copyright, 2005, Wei-keng Liao */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* strtok() */
#include <sys/types.h> /* open() */
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h> /* getopt() */
#include <omp.h>
int _debug;
#include "kmeans.h"
/*---< usage() >------------------------------------------------------------*/
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"
" -b : input file is in binary format (default no)\n"
" -n num_clusters: number of clusters (K must > 1)\n"
" -t threshold : threshold value (default %.4f)\n"
" -p nproc : number of threads (default system allocated)\n"
" -a : perform atomic OpenMP pragma (default no)\n"
" -o : output timing results (default no)\n"
" -d : enable debug mode\n";
fprintf(stderr, help, argv0, threshold);
exit(-1);
}
/*---< main() >-------------------------------------------------------------*/
int main(int argc, char **argv) {
int opt;
extern char *optarg;
extern int optind;
int i, j, nthreads;
int isBinaryFile, is_perform_atomic, is_output_timing;
int numClusters, numCoords, numObjs;
int *membership; /* [numObjs] */
char *filename;
float **objects; /* [numObjs][numCoords] data objects */
float **clusters; /* [numClusters][numCoords] cluster center */
float threshold;
double timing, io_timing, clustering_timing;
/* some default values */
_debug = 0;
nthreads = 0;
numClusters = 0;
threshold = 0.001;
numClusters = 0;
isBinaryFile = 0;
is_output_timing = 0;
is_perform_atomic = 0;
filename = NULL;
while ( (opt=getopt(argc,argv,"p:i:n:t:abdo"))!= EOF) {
switch (opt) {
case 'i': filename=optarg;
break;
case 'b': isBinaryFile = 1;
break;
case 't': threshold=atof(optarg);
break;
case 'n': numClusters = atoi(optarg);
break;
case 'p': nthreads = atoi(optarg);
break;
case 'a': is_perform_atomic = 1;
break;
case 'o': is_output_timing = 1;
break;
case 'd': _debug = 1;
break;
case '?': usage(argv[0], threshold);
break;
default: usage(argv[0], threshold);
break;
}
}
if (filename == 0 || numClusters <= 1) usage(argv[0], threshold);
/* set the no. threads if specified in command line, else use all
threads allocated by run-time system */
if (nthreads > 0)
omp_set_num_threads(nthreads);
if (is_output_timing) io_timing = omp_get_wtime();
/* read data points from file ------------------------------------------*/
objects = file_read(isBinaryFile, filename, &numObjs, &numCoords);
if (objects == NULL) exit(1);
if (is_output_timing) {
timing = omp_get_wtime();
io_timing = timing - io_timing;
clustering_timing = timing;
}
/* start the core computation -------------------------------------------*/
/* membership: the cluster id for each data object */
membership = (int*) malloc(numObjs * sizeof(int));
assert(membership != NULL);
clusters = omp_kmeans(is_perform_atomic, objects, numCoords, numObjs,
numClusters, threshold, membership);
free(objects[0]);
free(objects);
if (is_output_timing) {
timing = omp_get_wtime();
clustering_timing = timing - clustering_timing;
}
/* output: the coordinates of the cluster centres ----------------------*/
file_write(filename, numClusters, numObjs, numCoords, clusters, membership);
free(membership);
free(clusters[0]);
free(clusters);
/*---- output performance numbers ---------------------------------------*/
if (is_output_timing) {
io_timing += omp_get_wtime() - timing;
printf("\nPerforming **** Regular Kmeans (OpenMP) ----");
if (is_perform_atomic)
printf(" using atomic pragma ******\n");
else
printf(" using array reduction ******\n");
printf("Number of threads = %d\n", omp_get_max_threads());
printf("Input file: %s\n", filename);
printf("numObjs = %d\n", numObjs);
printf("numCoords = %d\n", numCoords);
printf("numClusters = %d\n", numClusters);
printf("threshold = %.4f\n", threshold);
printf("I/O time = %10.4f sec\n", io_timing);
printf("Computation timing = %10.4f sec\n", clustering_timing);
}
return(0);
}