Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
205 lines (166 loc) · 5.96 KB
/
Copy pathmain.cpp
File metadata and controls
205 lines (166 loc) · 5.96 KB
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
#include <mpi.h>
#include <omp.h>
#include <getopt.h>
#include <iostream>
#include <vector>
#include <string>
#include <stdlib.h>
#include <stdio.h>
// TODO change spacial parameters type to float with corresponding validation changes
int param_x_bound = 100;
int param_y_bound = 100;
int param_z_bound = 100;
int param_t_bound = 10;
// steps number
int param_steps = 100;
// threads number
int param_nt = 1;
// total number of processes involved and current process id
int nproc, rank;
const int MASTER_PROCESS = 0;
typedef enum {
INVALID_PARAMETERS = 1
} errcode;
bool validate_parameters() {
bool is_valid = true;
if (param_x_bound <= 0) {
std::cerr << "input parameters error:"
" --xbound (-x) must be positive integer, but " <<
param_x_bound << " was given" << std::endl;
is_valid = false;
}
if (param_y_bound <= 0) {
std::cerr << "input parameters error:"
" --ybound (-y) must be positive integer, but " <<
param_y_bound << " was given" << std::endl;
is_valid = false;
}
if (param_z_bound <= 0) {
std::cerr << "input parameters error:"
" --zbound (-z) must be positive integer, but " <<
param_z_bound << " was given" << std::endl;
is_valid = false;
}
if (param_t_bound <= 0) {
std::cerr << "input parameters error:"
" --tbound (-t) must be positive integer, but " <<
param_t_bound << " was given" << std::endl;
is_valid = false;
}
if (param_steps <= 0) {
std::cerr << "input parameters error:"
" --steps (-s) must be positive integer, but " <<
param_steps << " was given" << std::endl;
is_valid = false;
}
if (param_nt <= 0) {
std::cerr << "input parameters error:"
" --nt (-n) must be positive integer, but " <<
param_nt << " was given" << std::endl;
is_valid = false;
}
return is_valid;
}
int main(int argc, char *argv[]) {
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &nproc);
// array for parameters transition from the master to slaves
int params_storage[4];
// processing and validating input values
if (rank == MASTER_PROCESS) {
int option_char = -1,
option_idx = 0;
std::string help_message =
"Usage: <binary> [OPTIONS], where OPTIONS include\n"
"[--help | -h] \n"
"[--xbound=<upper X boundary for computational grid> | -x <...>] \n"
"[--ybound=<upper Y boundary for computational grid> | -y <...>] \n"
"[--zbound=<upper Z boundary for computational grid> | -z <...>] \n"
"[--tbound=<temporal upper limit for the model> | -t <...>] \n"
"[--steps=<number of iterations to perform> | -s <...>] \n"
"[--nt=<thread number> | -n <...>] \n";
while (true) {
static struct option long_options[] = {
{ "xbound", required_argument, 0, 'x' },
{ "ybound", required_argument, 0, 'y' },
{ "zbound", required_argument, 0, 'z' },
{ "tbound", required_argument, 0, 't' },
{ "steps", required_argument, 0, 's' },
{ "nt", required_argument, 0, 'n' },
{ "help", no_argument, 0, 'h' },
{ 0, 0, 0, 0 }
};
option_char = getopt_long(argc, argv, "x:y:z:t:s:n:h",
long_options, &option_idx);
if (option_char == -1) {
break;
}
switch (option_char) {
case 'x':
param_x_bound = atoi(optarg);
break;
case 'y':
param_y_bound = atoi(optarg);
break;
case 'z':
param_z_bound = atoi(optarg);
break;
case 't':
param_t_bound = atoi(optarg);
break;
case 's':
param_steps = atoi(optarg);
break;
case 'n':
param_nt = atoi(optarg);
break;
case 'h':
std::cout << help_message << std::endl;
break;
case '?':
break;
}
}
if (argc == 1) {
std::cout << help_message << std::endl;
}
if (!validate_parameters()) {
MPI_Abort(MPI_COMM_WORLD, INVALID_PARAMETERS);
exit(INVALID_PARAMETERS);
}
params_storage[0] = param_x_bound;
params_storage[1] = param_y_bound;
params_storage[2] = param_z_bound;
params_storage[3] = param_nt;
}
// receiving parameters from MASTER
// TODO beware of param_XXX_bound type changes as MPI_INT is strictly set
MPI_Bcast(params_storage, 4, MPI_INT, MASTER_PROCESS, MPI_COMM_WORLD);
param_x_bound = params_storage[0];
param_y_bound = params_storage[1];
param_z_bound = params_storage[2];
param_nt = params_storage[3];
// <test_code>
std::cout << "x=" << param_x_bound << std::endl <<
"y=" << param_y_bound << std::endl <<
"z=" << param_z_bound << std::endl <<
"threads=" << param_nt << std::endl <<
"in rank #" << rank << std::endl;
std::cout << "settings threads number to " << param_nt << " in process #" <<
rank << std::endl;
omp_set_num_threads(param_nt);
int accum = 0;
#pragma omp parallel for reduction(+: accum)
for (int i = 0; i < param_x_bound * param_y_bound * param_z_bound; ++i) {
if (i % 2) {
accum += i;
} else {
accum -= i;
}
}
std::cout << "accumulator = " << accum << std::endl;
// </test_code>
MPI_Finalize();
exit(0);
}