-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseline.cc
277 lines (228 loc) · 8.61 KB
/
baseline.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
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
/*
g++ -fopenmp -std=c++11 -o baseline.exe baseline.cc
*/
#include <sched.h>
#include <omp.h>
#include <climits>
#include <iostream>
#include <fstream>
#include <sstream>
#include <queue>
#include <string>
//OMP_PLACES='{0,1}, {2,3}';
int dinics_DFS(
int curr_vertex,
int available_flow,
int **graph,
int *levels,
int s,
int t,
int num_v
);
int main(int argc, char **argv) {
double start_sequential_time = omp_get_wtime();
// Read file
std::ifstream input_file(argv[1]);
std::string line;
std::string cell;
std::getline(input_file, line);
const int NUM_V = std::stoi(line);
// initialise arrays
int **original_graph = (int **)malloc(NUM_V * sizeof(int *));
for (int i=0; i < NUM_V; i++) {
original_graph[i] = (int *)malloc(NUM_V * sizeof(int));
}
// read input file into array
for (int y=0; y < NUM_V; y++) {
std::getline(input_file, line);
std::stringstream line_stream(line);
int *curr_vector = original_graph[y];
for (int x=0; x < NUM_V; x++) {
std::getline(line_stream, cell, ',');
curr_vector[x] = std::stoi(cell);
}
}
input_file.close();
const int NUM_GRAPHS = NUM_V * NUM_V - NUM_V; //number of problems to be solved in parallel
// Create source_sink_pairs array
int *source_sink_pairs = (int *)malloc(2*NUM_GRAPHS*sizeof(int));
int ind=0;
for (int i=0; i < NUM_V; i++) {
for (int j=0; j < NUM_V; j++) {
if (i != j) {
source_sink_pairs[ind++] = i;
source_sink_pairs[ind++] = j;
}
}
}
int min_max_flow = INT_MAX;
double end_sequential_time = omp_get_wtime();
// initialise performance data arrays
double *perf_parallel_timings = (double *)malloc(NUM_GRAPHS*sizeof(double));
double *perf_critical_timings = (double *)malloc(NUM_GRAPHS*sizeof(double));
double *perf_memalloc_timings = (double *)malloc(NUM_GRAPHS*sizeof(double));
int *perf_thread_ids = (int *)malloc(NUM_GRAPHS*sizeof(int));
int *perf_cpu_ids = (int *)malloc(NUM_GRAPHS*sizeof(int));
double start_parallel_time = omp_get_wtime();
for (ind=0; ind < NUM_GRAPHS; ind++)
{
perf_thread_ids[ind] = omp_get_thread_num();
perf_cpu_ids[ind] = sched_getcpu();
perf_parallel_timings[ind] = omp_get_wtime(); //start recording time
int s = source_sink_pairs[ind*2], t = source_sink_pairs[ind*2+1];
std::queue<int> vertex_queue;
int max_flow = -1;
int curr_vertex;
int curr_level;
int i;
int *levels = (int *)calloc(NUM_V, sizeof(int));
//initialise private graph to original_graph values
int **graph = (int **)malloc(NUM_V * sizeof(int *));
for (int i=0; i < NUM_V; i++) {
graph[i] = (int *)malloc(NUM_V * sizeof(int));
}
for (int x=0; x < NUM_V; x++) {
for (int y=0; y < NUM_V; y++) {
graph[x][y] = original_graph[x][y];
}
}
perf_memalloc_timings[ind] = omp_get_wtime() - perf_parallel_timings[ind];
// Remove all edges from the sink. These will sum together to become our Max Flow.
for (i=0; i < NUM_V; i++) {
graph[t][i] = 0;
}
while(max_flow == -1) {
vertex_queue.push(s); // source vertex
vertex_queue.push(1); // initial distance from source vertex
// Run BFS to construct levels array, where level is min distance from s
while (vertex_queue.size() > 0) {
curr_vertex = vertex_queue.front();
vertex_queue.pop();
curr_level = vertex_queue.front();
vertex_queue.pop();
// Check all connected vertices for unvisited vertices
for (i=0; i < NUM_V; i++) {
if (i != s
&& i != curr_vertex
&& graph[curr_vertex][i] > 0
&& levels[i] == 0
) {
// Set level
levels[i] = curr_level;
// Add non-sink vertex to queue
if (i != t) {
vertex_queue.push(i);
vertex_queue.push(curr_level + 1);
}
}
}
}
// No route to sink. Return Max Flow.
if (levels[t] == 0) {
// Sum all
max_flow = graph[t][0];
for (i=1; i < NUM_V; i++) {
max_flow += graph[t][i];
}
perf_critical_timings[ind] = omp_get_wtime();
// printf("Time %f Thread %d: Max Flow = %d. Waiting for critical region\n", , omp_get_thread_num(), max_flow);
// printf("Time %f Thread %d: Enter critical region\n", omp_get_wtime(), omp_get_thread_num());
if (max_flow < min_max_flow) {
min_max_flow = max_flow;
}
// Parallel region complete
double curr_end_time = omp_get_wtime();
perf_critical_timings[ind] = curr_end_time - perf_critical_timings[ind];
perf_parallel_timings[ind] = curr_end_time - perf_parallel_timings[ind];
//free memory
for (i=0; i < NUM_V; i++) {
free(graph[i]);
}
free(graph);
free(levels);
} else {
// Run DFS. Each step in a path moves from a lower-level to higher-level vertex
dinics_DFS(s, INT_MAX, graph, levels, s, t, NUM_V);
// Reset levels array
for (i=0; i < NUM_V; i++) {
levels[i] = 0;
}
}
}
}
double end_parallel_time = omp_get_wtime();
printf("\nRESULT -- Minimum of max flows = %d --\n", min_max_flow);
double total_sequential_time = end_sequential_time - start_sequential_time;
double total_parallel_time = end_parallel_time - start_parallel_time;
printf("Total Sequential time: %f\n", total_sequential_time);
printf("Total Parallel time: %f\n", total_parallel_time);
printf("--Parallel timings--\n");
printf("INDEX CPU_ID THREAD_ID TOTAL_TIME MALLOC_TIME CRITICAL_TIME\n");
// for (ind=0; ind < NUM_GRAPHS; ind++) {
// printf("%d %d %d %f %f %f\n",
// ind,
// perf_cpu_ids[ind],
// perf_thread_ids[ind],
// perf_parallel_timings[ind],
// perf_memalloc_timings[ind],
// perf_critical_timings[ind]
// );
// }
// free arrays
for (int i=0; i < NUM_V; i++) {
free(original_graph[i]);
}
free(original_graph);
free(source_sink_pairs);
free(perf_parallel_timings);
free(perf_critical_timings);
free(perf_memalloc_timings);
free(perf_thread_ids);
free(perf_cpu_ids);
return 0;
}
int dinics_DFS(
int curr_vertex,
int available_flow,
int **graph,
int *levels,
int s,
int t,
int num_v
) {
// Base case: Sink
if (curr_vertex == t) {
return available_flow;
}
// Recursive case
int i;
int newly_consumed_flow;
int total_consumed_flow = 0;
for (i=0; i < num_v; i++) {
// already pushed all flow
if (available_flow == total_consumed_flow) {
return available_flow;
}
// check for edge to sink or higher-level vertices
if ((graph[curr_vertex][i] > 0) && (i == t || levels[i] > levels[curr_vertex])) {
newly_consumed_flow = dinics_DFS(
i,
std::min(available_flow - total_consumed_flow, graph[curr_vertex][i]),
graph,
levels,
s,
t,
num_v
);
if (newly_consumed_flow == 0) { // No path to sink: rule out that vertex
levels[i] = 0;
}
else { // Reached sink: consume flow
graph[curr_vertex][i] -= newly_consumed_flow; // reduce forward capacity
graph[i][curr_vertex] += newly_consumed_flow; // increase backward capacity
total_consumed_flow += newly_consumed_flow;
}
}
}
return total_consumed_flow;
}