-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtsp.c
366 lines (314 loc) · 9.13 KB
/
tsp.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/**
* Copyright (c) <2012>, <Victor Mateevitsi> www.vmateevitsi.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the <organization>.
* 4. Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <float.h>
#include <getopt.h>
#include <limits.h>
#include <math.h>
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
int n;
char locations_file[255];
int computerStats = 0;
int local_rank, num_procs;
// Timing
double gen_time, proc_time, comm_time, total_time;
typedef enum {
linear,
nn
} TYPE;
TYPE type;
typedef struct {
int line;
float x;
float y;
int visited;
} LOCATION;
LOCATION *locations;
typedef struct {
float cost;
int pointA;
int pointB;
} COST;
int parse_arguments(int argc, char **argv);
void parse_file();
void swap(int *p1, int *p2);
void permute();
void nearest_neighbor();
void display(int *a, int *count, float cost);
float calculate_cost(int *a);
float dist(LOCATION a, LOCATION b);
int find_nearest(int current, int start, int end);
int main(int argc, char **argv) {
double t_start, t_end;
int i;
gen_time = 0.0; proc_time = 0.0; comm_time = 0.0; total_time = 0.0;
// Parse the arguments
if( parse_arguments(argc, argv) ) return 1;
parse_file();
// Initialize MPI
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &local_rank);
MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
t_start = MPI_Wtime();
if( type == linear ) {
permute();
} else if( type == nn ) {
nearest_neighbor();
}
t_end = MPI_Wtime();
total_time = t_end - t_start;
if( computerStats ) {
printf("%d\tg\t%d\t%d\t%f\n", n, local_rank, num_procs, gen_time);
printf("%d\tp\t%d\t%d\t%f\n", n, local_rank, num_procs, proc_time);
printf("%d\tc\t%d\t%d\t%f\n", n, local_rank, num_procs, comm_time);
printf("%d\tt\t%d\t%d\t%f\n", n, local_rank, num_procs, total_time);
}
free(locations);
MPI_Finalize(); // Exit MPI
return 0;
}
void display(int *a, int *count, float cost) {
int x;
for( x = 0; x < n; x++ )
printf("%d ",a[x]);
printf("cost:%f count:%d\n", cost, *count);
}
void swap(int *p1, int *p2) {
int temp;
temp = *p1;
*p1 = *p2;
*p2 = temp;
}
float dist(LOCATION a, LOCATION b) {
return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2));
}
float calculate_cost(int *a) {
int i;
float cost = 0.0f;
for( i = 0; i < n - 1; i++ ) {
cost += dist(locations[a[i] - 1], locations[a[i + 1] - 1]);
}
return cost;
}
void permute() {
int count, i, x, y;
int n_perm = 1;
int *a;
float cost = 0.0;;
double start, end, dt;
start = MPI_Wtime();
for( i = 1; i <= n; i++ ) {
n_perm *= i;
}
a = (int*) malloc(sizeof(int) * n * n_perm);
for( i = 0; i < n; i++ ) a[i] = i + 1;
while( count < n_perm ) {
for( y = 0; y < n - 1; y++) {
swap(&a[y], &a[y + 1]);
cost = calculate_cost(a);
if( !computerStats) display(a, &count, cost);
count++;
}
swap(&a[0], &a[1]);
cost = calculate_cost(a);
if( !computerStats) display(a, &count, cost);
count++;
for( y = n - 1; y > 0; y-- ) {
swap(&a[y], &a[y - 1]);
cost = calculate_cost(a);
if( !computerStats) display(a, &count, cost);
count++;
}
swap(&a[n - 1], &a[n - 2]);
cost = calculate_cost(a);
if( !computerStats) display(a, &count, cost);
count++;
}
end = MPI_Wtime();
dt = end - start;
proc_time += dt;
}
int find_nearest(int current, int start, int end) {
int i, index;
float min = FLT_MAX;
float distance;
index = -1;
for( i = start; i<= end; ++i ) {
distance = dist(locations[current], locations[i]);
if( distance < min && i != current && locations[i].visited == 0 ) {
min = distance;
index = i;
}
}
return index;
}
void nearest_neighbor() {
int i, j, index;
float cost = 0.0f;
int starting_loc, ending_loc;
int loc_per_node = n / num_procs;
int next;
int *index_of_min;
float distance;
float min = FLT_MAX;
int final_path[n];
double start, end, dt;
index_of_min = (int*)malloc(sizeof(int) * num_procs);
starting_loc = loc_per_node * local_rank;
ending_loc = starting_loc + loc_per_node - 1;
if( local_rank == num_procs - 1 ) ending_loc += n % num_procs;
next = 0;
final_path[0] = 0;
for( i = 0; i < n - 1; i++ ) {
// Find the nearest neighbor to
start = MPI_Wtime();
MPI_Bcast(&next, 1, MPI_INT, 0, MPI_COMM_WORLD);
end = MPI_Wtime();
dt = end - start;
comm_time += dt;
start = MPI_Wtime();
locations[next].visited = 1;
int index = find_nearest(next, starting_loc, ending_loc);
end = MPI_Wtime();
dt = end - start;
proc_time += dt;
start = MPI_Wtime();
MPI_Gather(&index, 1, MPI_INT, index_of_min, 1, MPI_INT, 0, MPI_COMM_WORLD);
end = MPI_Wtime();
dt = end - start;
comm_time += dt;
if( local_rank == 0 ) {
start = MPI_Wtime();
index = index_of_min[0];
// find the nearest
min = FLT_MAX;
for( j = 0; j < num_procs; ++j ) {
if( index_of_min[j] < 0 ) continue;
distance = dist(locations[next], locations[index_of_min[j]]);
if( distance < min ) {
min = distance;
index = index_of_min[j];
}
}
next = index;
final_path[i + 1] = index;
end = MPI_Wtime();
dt = end - start;
proc_time += dt;
}
MPI_Barrier(MPI_COMM_WORLD);
}
if( local_rank == 0 && !computerStats ) {
for( i = 0; i < n; ++i ) {
printf("%d ", final_path[i]);
}
printf("\n");
}
free(index_of_min);
}
void parse_file() {
FILE *fp;
int i, line;
char buffer[1024];
float x, y;
fp = fopen(locations_file, "r");
for(i = 0; i < 7; i++) {
fgets(buffer, 1024, fp);
}
while(fscanf(fp, "%d %f %f", &line, &x, &y) > 0 ) {
if(line == n) break;
//printf("%d %f %f\n", line, x, y);
}
// Line has the number of elements
locations = (LOCATION *) malloc(sizeof(LOCATION) * line);
rewind(fp);
for(i = 0; i < 7; i++) {
fgets(buffer, 1024, fp);
}
while(fscanf(fp, "%d %f %f", &line, &x, &y) > 0 ) {
locations[line - 1].line = line;
locations[line - 1].x = x;
locations[line - 1].y = y;
locations[line - 1].visited = 0;
if(line == n) break;
//printf("%d %f %f\n", locations[line - 1].line, locations[line - 1].x, locations[line - 1].y);
}
fclose(fp);
}
int parse_arguments(int argc, char **argv) {
int i, c;
int option_index = 0;
static struct option long_options[] =
{
{"input_method1", required_argument, 0, 'q'},
{"input_method2", required_argument, 0, 'w'},
{0, 0, 0, 0}
};
char *result = NULL;
char delims[] = "m";
while( (c = getopt_long (argc, argv, "f:n:t:c", long_options, &option_index)) != -1 ) {
switch(c) {
case 'f':
strcpy(locations_file, optarg);
break;
case 'n':
n = atoi(optarg);
break;
case 'c':
computerStats = 1;
break;
case 't':
if( strcmp(optarg, "linear" ) == 0 ) type = linear;
else if( strcmp(optarg, "nn" ) == 0 ) type = nn;
else {
fprintf( stderr, "Option -%c %s in incorrect. Allowed values are: linear, nn\n", optopt, optarg);
return 1;
}
break;
case '?':
if( optopt == 'n' )
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);
return 1;
default:
fprintf(stderr, "Usage: %s -n <number of numbers> \n", argv[0]);
fprintf(stderr, "\tExample: %s -n 1000\n", argv[0]);
return 1;
}
}
return 0;
}