-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmainserial.cpp
85 lines (66 loc) · 2.27 KB
/
mainserial.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
79
80
81
82
83
84
85
#include <iostream>
#include <assert.h>
#include <climits>
#include <vector>
//For calculating Time
#include <chrono>
#include <ctime>
#include "stack.h"
#include "graphs.h"
using namespace std;
using namespace std::chrono;
int main(int argc, char* argv[]) {
high_resolution_clock::time_point t1 = high_resolution_clock::now();
data node;
tour_t* best_tour = new_tour();
tour_t* tour = new_tour();
strack_t* stack = new_stack();
freed_tours_t* freed_tours = new_freed_tour();
int home_city = 0;
tour->cities[0] = home_city;
tour->size = 1;
best_tour->cost = INT_MAX;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &node.comm_sz);
MPI_Comm_rank(MPI_COMM_WORLD, &node.rank);
if(node.my_rank == 0)
{
get_mpi_custom_data_type(&freed_tours, &stack, &tour, &best_tour);
}
push_copy(stack, tour, freed_tours);
while (stack->size > 0){
tour = pop(stack);
if(tour->size == n_cities){
if(is_best_tour(tour, best_tour, graph, home_city)) {
// If best tour add home city and make new best tour
add_city(graph, tour, home_city);
copy_tour(tour, best_tour);
}
} else{
for(int neighbor = n_cities -1; neighbor >= 0; neighbor--){ // book uses int neighbor; neighbor >= 1; neighbor--) Possible bug??
if(is_neighbor(graph, tour->cities[tour->size - 1], neighbor)){
if(!is_visited(tour, neighbor)){ // not in books code
add_city(graph, tour, neighbor );
push_copy(stack, tour, freed_tours);
remove_city(graph, tour);
}
}
}
}
push_freed_tour(freed_tours, tour);
}
if(my_rank == 0)
{
print_tour(best_tour);
}
delete[] graph;
delete freed_tours;
delete best_tour;
delete tour;
delete stack;
high_resolution_clock::time_point t2 = high_resolution_clock::now();
duration<double> time_span = duration_cast<duration<double> >(t2 - t1);
std::cout << "\nTime taken : " << time_span.count() << " seconds.";
std::cout << std::endl;
return 0;
}