-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
133 lines (110 loc) · 4.54 KB
/
main.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
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
/*
* Copyright (c) 2024 Maksim Masterov, SURF
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <string>
#include <metis.h>
#include <cmath>
#include "src/field.h"
#include "src/helpers.h"
#include "src/common.h"
#include "src/MPI/Decomposition/decomposition.h"
#include "src/MPI/Decomposition/decompositionMetis.h"
#include "src//MPI/topologies.h"
#include "src/graph.h"
void reportElapsedTime(double start, double end, const std::string &message) {
findGlobalMin(start);
findGlobalMax(end);
printByRoot("Elapsed time (" + message + "): " + std::to_string(end - start) + "s.");
}
int main(int argc, char** argv) {
Helpers helper;
Field field;
IndicesIJ elts_glob; // Number of global cells in each direction
IndicesIJ struct_part; // Number of processes in each direction (for structured decomposition only)
DecompositionStruct decomp_struct;
DecompositionMetis decomp_metis;
Graph graph(ADJ_LIST);
int8_t type = STRUCTURED;
int root_pid = 0;
int32_t* partitioning;
int32_t num_glob_elts;
double elp_times[2];
Topologies topology;
double res = 0.;
/* Initialize MPI region */
initialize(argc, argv);
/* Parse input from the CL */
helper.parseInput(argc, argv, elts_glob, struct_part, type);
num_glob_elts = elts_glob.i * elts_glob.j;
/* Generate initial field and decompose the data by the root process */
if (getMyRank() == root_pid) {
field.initialize(elts_glob, elts_glob);
field.generate();
/* Print field to the file */
field.print("original");
if (type == STRUCTURED) {
/* Call for structured decomposition */
if (decomp_struct.decompose(struct_part, elts_glob) == EXIT_FAILURE) {
terminateExecution();
}
/* Print structured decomposition to the file */
decomp_struct.print("struct.dat", elts_glob);
partitioning = decomp_struct.getPartitioning().data();
} else if (type == METIS) {
graph.generateStructured(elts_glob);
/* Uncomment this line to print the graph to the terminal */
// graph.print();
std::vector<int32_t> weights;
weights.resize(graph.getRows());
for (int32_t n = 0; n < weights.size(); ++n) {
/* Uncomment this line to change the weight distribution */
// weights[n] = 100 * field(n);
weights[n] = field.getLocalLoad(field(n));
}
/* Call for graph decomposition */
if (decomp_metis.decompose(graph, weights.data()) == EXIT_FAILURE) {
terminateExecution();
}
/* Print graph decomposition to the file */
decomp_metis.print("graph.dat", elts_glob);
partitioning = decomp_metis.getPartitioning().data();
}
else {
printByRoot("Unknown decomposition type");
terminateExecution();
}
}
/* Distribute the field */
field.distribute(partitioning, num_glob_elts, root_pid);
/* Print local field for debugging */
field.print("output");
/* Perform some calculations and report the elapsed time */
elp_times[0] = helper.tic();
res = field.performDummyWork();
elp_times[1] = helper.toc();
/* Print result for the verification */
findGlobalSum(res);
printByRoot("result: " + std::to_string(res));
reportElapsedTime(elp_times[0], elp_times[1], "Work");
/* Finalize MPI region */
finalize();
return 0;
}