-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
160 lines (133 loc) · 3.94 KB
/
main.cc
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
#include <deque>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <memory>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <optional>
#include <assert.h>
#define OMPI_SKIP_MPICXX 1
#include <mpi.h>
#include <string_view>
using std::cerr;
using std::cout;
using std::endl;
using std::nullopt;
using std::optional;
using std::string;
using std::string_view;
using std::unordered_map;
using std::vector;
using adjacency_matrix = std::vector<std::vector<size_t>>;
// Definition for simulate
void simulate(size_t num_stations, const vector<string> &station_names, const std::vector<size_t> &popularities,
const adjacency_matrix &mat, const unordered_map<char, vector<string>> &station_lines, size_t ticks,
const unordered_map<char, size_t> num_trains, size_t num_ticks_to_print, size_t mpi_rank,
size_t total_processes);
enum LineColor {
GREEN = 'g',
YELLOW = 'y',
BLUE = 'b',
RED = 'r',
BROWN = 'w',
PURPLE = 'p',
TURQUOISE = 't',
PINK = 'k',
LIME = 'l',
GREY = 'e'
};
const LineColor colors[] = {GREEN, YELLOW, BLUE, RED, BROWN, PURPLE, TURQUOISE, PINK, LIME, GREY};
vector<string> extract_station_names(string &line) {
constexpr char space_delimiter = ' ';
vector<string> stations{};
line += ' ';
size_t pos;
while ((pos = line.find(space_delimiter)) != string::npos) {
stations.push_back(line.substr(0, pos));
line.erase(0, pos + 1);
}
return stations;
}
int main(int argc, char *argv[]) {
if (argc < 2) {
std::cerr << argv[0] << " <input_file>\n";
std::exit(1);
}
int rank, tp;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &tp);
std::ifstream ifs(argv[1], std::ios_base::in);
if (!ifs.is_open()) {
std::cerr << "Failed to open " << argv[1] << '\n';
std::exit(2);
}
// Read S & V
size_t S;
size_t V;
ifs >> S;
ifs >> V;
// Read station names.
string station;
std::vector<string> station_names{};
station_names.reserve(S);
for (size_t i = 0; i < S; ++i) {
ifs >> station;
station_names.emplace_back(station);
}
// Read P popularity
size_t p;
std::vector<size_t> popularities{};
popularities.reserve(S);
for (size_t i = 0; i < S; ++i) {
ifs >> p;
popularities.emplace_back(p);
}
// Form adjacency mat
adjacency_matrix mat(S, std::vector<size_t>(S));
for (size_t src{}; src < S; ++src) {
for (size_t dst{}; dst < S; ++dst) {
ifs >> mat[src][dst];
}
}
ifs.ignore();
string stations_buf;
unordered_map<char, vector<string>> station_lines;
for (size_t i = 0; i < V; ++i) {
std::getline(ifs, stations_buf);
vector<string> station_names = extract_station_names(stations_buf);
station_lines[colors[i]] = std::move(station_names);
}
// N time ticks
size_t N;
ifs >> N;
// number of trains per line
unordered_map<char, size_t> num_trains;
for (size_t i = 0; i < V; ++i) {
size_t line_num_trains;
ifs >> line_num_trains;
num_trains[colors[i]] = line_num_trains;
}
size_t num_ticks_to_print;
ifs >> num_ticks_to_print;
// Start timing with MPI_Wtime
double start_time = MPI_Wtime();
// Call student implementation
simulate(S, station_names, popularities, mat, station_lines, N, num_trains, num_ticks_to_print, rank, tp);
// Barrier to make sure all processes are finished before timing
MPI_Barrier(MPI_COMM_WORLD);
// End timing with MPI_Wtime
double end_time = MPI_Wtime();
// We will use rank 0's timing in the end
double total_time;
if (rank == 0) {
total_time = end_time - start_time;
cerr << std::fixed << "mpi_time:" << total_time << "s" << endl;
}
MPI_Finalize();
return 0;
}