-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpi_kcenter.c
140 lines (120 loc) · 3.52 KB
/
mpi_kcenter.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
#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <mpi.h>
#define MAX_POINTS 2000
typedef struct vec2_s {
double x, y;
} vec2_type;
double vec2_dist_sq (vec2_type u, vec2_type v) {
double diff_x = u.x - v.x;
double diff_y = u.y - v.y;
return diff_x*diff_x + diff_y*diff_y;
}
typedef struct vec2set_s {
vec2_type v[MAX_POINTS];
int size;
} vec2set_type;
void vec2set_read_file (vec2set_type* set, char* filename) {
vec2_type u;
set->size = 0;
FILE* file_ptr;
file_ptr = fopen(filename,"r");
if (file_ptr == NULL) {
printf ("error : could not open file %s for reading\n",filename);
exit(1);
}
while (fscanf (file_ptr,"%lf %lf",&(u.x),&(u.y)) == 2) {
if (set->size < MAX_POINTS) {
set->v[set->size] = u;
set->size += 1;
} else {
printf ("Too many points in file %s\n",filename);
fclose (file_ptr);
exit(1);
}
}
fclose (file_ptr);
}
/* calculate the cost of a given set of center locations */
double center_cost (vec2set_type* set, int* centers, int k) {
double cost = 0;
for (int i=0;i<set->size;i++) {
double min_dist_sq = DBL_MAX;
for (int j=0;j<k;j++) {
double dist_sq = vec2_dist_sq(set->v[i],set->v[centers[j]]);
if (dist_sq < min_dist_sq) {
min_dist_sq = dist_sq;
}
}
if (min_dist_sq > cost) {
cost = min_dist_sq;
}
}
return cost;
}
int main (int argc, char** argv) {
/* get filename, k, m, and s from command line */
if (argc < 5) {
printf ("Command usage : %s %s %s %s %s\n",argv[0],"filename","k","m","s");
return 1;
}
MPI_Init(&argc, &argv);
double start_time, end_time;
start_time = MPI_Wtime();
int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
vec2set_type set;
vec2set_read_file (&set,argv[1]);
int k = atoi(argv[2]);
int m = atoi(argv[3]);
/* seed the random number generator */
srandom(atoi(argv[4]));
/* check the cost of m random sets of k centers */
int centers[k];
int optimal_centers[k];
double optimal_cost = DBL_MAX;
double cost;
for (int i=0;i<m;i++) {
for (int j=0;j<k;j++) {
centers[j] = random() % set.size;
}
cost = center_cost(&set,centers,k);
if (cost < optimal_cost) {
optimal_cost = cost;
for (int j=0;j<k;j++) {
optimal_centers[j] = centers[j];
}
}
}
int total_cost[k*size];
MPI_Gather(optimal_centers,k,MPI_INT, total_cost, k, MPI_INT, 0, MPI_COMM_WORLD);
double total_opt_cost = DBL_MAX;
if(rank == 0) {
for(int i = 0; i <size; i++) {
double cost = center_cost(&set, &total_cost[i*k],k);
if( cost < total_opt_cost) {
total_opt_cost = cost;
for(int j = 0; j<k; j++) {
optimal_centers[j] = total_cost[i*k + j];
}
}
}
end_time = MPI_Wtime();
/* print out the number of k-tuples checked */
printf ("number of %d-tuples checked = %d\n",k,m*size);
/* wall time */
printf("wall time used = %.4f sec\n", (end_time - start_time));
/* print the approximate optimal cost for the k-center problem */
printf ("approximate optimal cost = %g\n",total_opt_cost);
/* print an approx optimal solution to the k-center problem */
printf ("approx optimal centers : ");
for (int j=0;j<k;j++) {
printf ("%d ",optimal_centers[j]);
}
printf ("\n");
}
MPI_Finalize();
return 0;
}