forked from spdomin/Nalu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nalu.C
236 lines (191 loc) · 7.92 KB
/
nalu.C
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
/*------------------------------------------------------------------------*/
/* Copyright 2014 Sandia Corporation. */
/* This software is released under the license detailed */
/* in the file, LICENSE, which is located in the top-level Nalu */
/* directory structure */
/*------------------------------------------------------------------------*/
#include <mpi.h>
#include <stk_util/diag/PrintTimer.hpp>
// nalu
#include <NaluParsing.h>
#include <Simulation.h>
#include <NaluEnv.h>
// util
#include <stk_util/environment/CPUTime.hpp>
#include <stk_util/environment/perf_util.hpp>
#include <stk_util/parallel/ParallelReduce.hpp>
// boost for input params
#include <boost/program_options.hpp>
// yaml for parsing..
#include <yaml-cpp/yaml.h>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdexcept>
static std::string human_bytes_double(double bytes)
{
const double K = 1024;
const double M = K*1024;
const double G = M*1024;
std::ostringstream out;
if (bytes < K) {
out << bytes << " B";
} else if (bytes < M) {
bytes /= K;
out << bytes << " K";
} else if (bytes < G) {
bytes /= M;
out << bytes << " M";
} else {
bytes /= G;
out << bytes << " G";
}
return out.str();
}
int main( int argc, char ** argv )
{
// start up MPI
if ( MPI_SUCCESS != MPI_Init( &argc , &argv ) ) {
throw std::runtime_error("MPI_Init failed");
}
// NaluEnv singleton
sierra::nalu::NaluEnv &naluEnv = sierra::nalu::NaluEnv::self();
stk::diag::setEnabledTimerMetricsMask(stk::diag::METRICS_CPU_TIME | stk::diag::METRICS_WALL_TIME);
sierra::nalu::Simulation::rootTimer().start();
// start initial time
double start_time = stk::cpu_time();
// command line options.
std::string inputFileName, logFileName;
bool debug = false;
int serializedIOGroupSize = 0;
boost::program_options::options_description desc("Nalu Supported Options");
desc.add_options()
("help,h","Help message")
("version,v", "Code Version 1.0")
("input-deck,i", boost::program_options::value<std::string>(&inputFileName)->default_value("nalu.i"),
"Analysis input file")
("log-file,o", boost::program_options::value<std::string>(&logFileName),
"Analysis log file")
("serialized-io-group-size,s",
boost::program_options::value<int>(&serializedIOGroupSize)->default_value(0),
"Specifies the number of processors which can concurrently perform I/O. Specifying zero disables serialization.")
("debug,D", "debug print on");
boost::program_options::variables_map vm;
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
boost::program_options::notify(vm);
// deal with some default parameters
if ( vm.count("help") ) {
if (!naluEnv.parallel_rank())
std::cerr << desc << std::endl;
return 0;
}
if (vm.count("version")) {
if (!naluEnv.parallel_rank())
std::cerr << "Version: Nalu1.0" << std::endl;
return 0;
}
if (vm.count("debug")) {
debug = true;
}
std::ifstream fin(inputFileName.c_str());
if (!fin.good()) {
if (!naluEnv.parallel_rank())
std::cerr << "Input file is not specified or does not exist: user specified (or default) name= " << inputFileName << std::endl;
return 0;
}
// deal with logfile name; if none supplied, go with inputFileName.log
if (!vm.count("log-file")) {
int dotPos = inputFileName.rfind(".");
if ( -1 == dotPos ) {
// lacking extension
logFileName = inputFileName + ".log";
}
else {
// with extension; swap with .log
logFileName = inputFileName.substr(0, dotPos) + ".log";
}
}
// deal with log file stream
naluEnv.set_log_file_stream(logFileName);
// proceed with reading input file "document" from YAML
YAML::Parser parser(fin);
YAML::Node doc;
try {
parser.GetNextDocument(doc);
if (debug) {
if (!naluEnv.parallel_rank())
sierra::nalu::NaluParsingHelper::emit(std::cout, doc);
}
}
catch (YAML::ParserException &e) {
std::cout << e.what() << std::endl;
}
sierra::nalu::Simulation sim(doc);
if (serializedIOGroupSize) {
naluEnv.naluOutputP0() << "Info: found non-zero serialized_io_group_size on command-line= "
<< serializedIOGroupSize << " (takes precedence over input file value)."
<< std::endl;
sim.setSerializedIOGroupSize(serializedIOGroupSize);
}
sim.debug_ = debug;
sim.load(doc);
sim.breadboard();
sim.initialize();
sim.run();
// stop timer
const double stop_time = stk::cpu_time();
const double total_time = stop_time - start_time;
const char* timer_name = "Total Time";
// parallel reduce overall times
double g_sum, g_min, g_max;
stk::all_reduce_min(naluEnv.parallel_comm(), &total_time, &g_min, 1);
stk::all_reduce_max(naluEnv.parallel_comm(), &total_time, &g_max, 1);
stk::all_reduce_sum(naluEnv.parallel_comm(), &total_time, &g_sum, 1);
const int nprocs = naluEnv.parallel_size();
// output total time
naluEnv.naluOutputP0() << "Timing for Simulation: nprocs= " << nprocs << std::endl;
naluEnv.naluOutputP0() << " main() -- " << " \tavg: " << g_sum/double(nprocs)
<< " \tmin: " << g_min << " \tmax: " << g_max << std::endl;
// output memory usage
{
size_t now, hwm;
stk::get_memory_usage(now, hwm);
// min, max, sum
size_t global_now[3] = {now,now,now};
size_t global_hwm[3] = {hwm,hwm,hwm};
stk::all_reduce(naluEnv.parallel_comm(), stk::ReduceSum<1>( &global_now[2] ) );
stk::all_reduce(naluEnv.parallel_comm(), stk::ReduceMin<1>( &global_now[0] ) );
stk::all_reduce(naluEnv.parallel_comm(), stk::ReduceMax<1>( &global_now[1] ) );
stk::all_reduce(naluEnv.parallel_comm(), stk::ReduceSum<1>( &global_hwm[2] ) );
stk::all_reduce(naluEnv.parallel_comm(), stk::ReduceMin<1>( &global_hwm[0] ) );
stk::all_reduce(naluEnv.parallel_comm(), stk::ReduceMax<1>( &global_hwm[1] ) );
naluEnv.naluOutputP0() << "Memory Overview: " << std::endl;
naluEnv.naluOutputP0() << "nalu memory: total (over all cores) current/high-water mark= "
<< std::setw(15) << human_bytes_double(global_now[2])
<< std::setw(15) << human_bytes_double(global_hwm[2])
<< std::endl;
naluEnv.naluOutputP0() << "nalu memory: min (over all cores) current/high-water mark= "
<< std::setw(15) << human_bytes_double(global_now[0])
<< std::setw(15) << human_bytes_double(global_hwm[0])
<< std::endl;
naluEnv.naluOutputP0() << "nalu memory: max (over all cores) current/high-water mark= "
<< std::setw(15) << human_bytes_double(global_now[1])
<< std::setw(15) << human_bytes_double(global_hwm[1])
<< std::endl;
}
sierra::nalu::Simulation::rootTimer().stop();
//output timings consistent w/ rest of Sierra
stk::diag::Timer & sierra_timer = sierra::nalu::Simulation::rootTimer();
const double elapsed_time = sierra_timer.getMetric<stk::diag::CPUTime>().getAccumulatedLap(false);
stk::diag::Timer & mesh_output_timer = sierra::nalu::Simulation::outputTimer();
double mesh_output_time = mesh_output_timer.getMetric<stk::diag::CPUTime>().getAccumulatedLap(false);
double time_without_output = elapsed_time-mesh_output_time;
stk::parallel_print_time_without_output_and_hwm(naluEnv.parallel_comm(), time_without_output, naluEnv.naluOutputP0());
if (!naluEnv.parallel_rank())
stk::print_timers_and_memory(&timer_name, &total_time, 1 /*num timers*/);
stk::diag::printTimersTable(naluEnv.naluOutputP0(), sierra::nalu::Simulation::rootTimer(),
stk::diag::METRICS_CPU_TIME | stk::diag::METRICS_WALL_TIME,
false, naluEnv.parallel_comm());
// all done
return 0;
}