-
Notifications
You must be signed in to change notification settings - Fork 89
/
initialization.cpp
292 lines (264 loc) · 9.14 KB
/
initialization.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
* ------------------------------------------------------------------------------------------------------------
* SPDX-License-Identifier: LGPL-2.1-only
*
* Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
* Copyright (c) 2018-2024 Total, S.A
* Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
* Copyright (c) 2023-2024 Chevron
* Copyright (c) 2019- GEOS/GEOSX Contributors
* All rights reserved
*
* See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
* ------------------------------------------------------------------------------------------------------------
*/
#include "initialization.hpp"
#include "version.hpp"
#include "common/DataTypes.hpp"
#include "common/Path.hpp"
#include "LvArray/src/system.hpp"
#include "linearAlgebra/interfaces/InterfaceTypes.hpp"
// TPL includes
#include <optionparser.h>
namespace geos
{
/**
* @class Arg a class inheriting from option::Arg that can parse a command line argument.
*/
struct Arg : public option::Arg
{
/**
* @brief Parse an unknown option. Unknown options aren't supported so this throws an error.
* @param option the option to parse.
* @return option::ARG_ILLEGAL.
*/
static option::ArgStatus unknown( option::Option const & option, bool )
{
GEOS_UNUSED_VAR( option ); // unused if geos_error_if is nulld
GEOS_LOG_RANK( "Unknown option: " << option.name );
return option::ARG_ILLEGAL;
}
/**
* @brief Parse a non-empty string option.
* @param option the option to parse.
* @return option::ARK_OK if the parse was successful, option::ARG_ILLEGAL otherwise.
*/
static option::ArgStatus nonEmpty( const option::Option & option, bool )
{
if((option.arg != nullptr) && (option.arg[0] != 0))
{
return option::ARG_OK;
}
GEOS_LOG_RANK( "Error: " << option.name << " requires a non-empty argument!" );
return option::ARG_ILLEGAL;
}
/**
* @brief Parse a numeric string option.
* @param option the option to parse.
* @return option::ARK_OK if the parse was successful, option::ARG_ILLEGAL otherwise.
*/
static option::ArgStatus numeric( const option::Option & option, bool )
{
char * endptr = nullptr;
if((option.arg != nullptr) && strtol( option.arg, &endptr, 10 )) {}
if((endptr != option.arg) && (*endptr == 0))
{
return option::ARG_OK;
}
GEOS_LOG_RANK( "Error: " << option.name << " requires a long-int argument!" );
return option::ARG_ILLEGAL;
}
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::unique_ptr< CommandLineOptions > parseCommandLineOptions( int argc, char * * argv )
{
std::unique_ptr< CommandLineOptions > commandLineOptions = std::make_unique< CommandLineOptions >();
// Set the options structs and parse
enum optionIndex
{
UNKNOWN,
HELP,
INPUT,
RESTART,
XPAR,
YPAR,
ZPAR,
SCHEMA,
NONBLOCKING_MPI,
SUPPRESS_PINNED,
PROBLEMNAME,
OUTPUTDIR,
TIMERS,
TRACE_DATA_MIGRATION,
MEMORY_USAGE,
PAUSE_FOR,
};
const option::Descriptor usage[] =
{
{ UNKNOWN, 0, "", "", Arg::unknown, "USAGE: geosx -i input.xml [options]\n\nOptions:" },
{ HELP, 0, "?", "help", Arg::None, "\t-?, --help" },
{ INPUT, 0, "i", "input", Arg::nonEmpty, "\t-i, --input, \t Input xml filename (required)" },
{ RESTART, 0, "r", "restart", Arg::nonEmpty, "\t-r, --restart, \t Target restart filename" },
{ XPAR, 0, "x", "xpartitions", Arg::numeric, "\t-x, --x-partitions, \t Number of partitions in the x-direction" },
{ YPAR, 0, "y", "ypartitions", Arg::numeric, "\t-y, --y-partitions, \t Number of partitions in the y-direction" },
{ ZPAR, 0, "z", "zpartitions", Arg::numeric, "\t-z, --z-partitions, \t Number of partitions in the z-direction" },
{ SCHEMA, 0, "s", "schema", Arg::nonEmpty, "\t-s, --schema, \t Name of the output schema" },
{ NONBLOCKING_MPI, 0, "b", "use-nonblocking", Arg::None, "\t-b, --use-nonblocking, \t Use non-blocking MPI communication" },
{ PROBLEMNAME, 0, "n", "name", Arg::nonEmpty, "\t-n, --name, \t Name of the problem, used for output" },
{ SUPPRESS_PINNED, 0, "s", "suppress-pinned", Arg::None, "\t-s, --suppress-pinned, \t Suppress usage of pinned memory for MPI communication buffers" },
{ OUTPUTDIR, 0, "o", "output", Arg::nonEmpty, "\t-o, --output, \t Directory to put the output files" },
{ TIMERS, 0, "t", "timers", Arg::nonEmpty, "\t-t, --timers, \t String specifying the type of timer output" },
{ TRACE_DATA_MIGRATION, 0, "", "trace-data-migration", Arg::None, "\t--trace-data-migration, \t Trace host-device data migration" },
{ MEMORY_USAGE, 0, "m", "memory-usage", Arg::nonEmpty, "\t-m, --memory-usage, \t Minimum threshold for printing out memory allocations in a member of the data repository." },
{ PAUSE_FOR, 0, "", "pause-for", Arg::numeric, "\t--pause-for, \t Pause geosx for a given number of seconds before starting execution" },
{ 0, 0, nullptr, nullptr, nullptr, nullptr }
};
argc -= ( argc > 0 );
argv += ( argc > 0 );
option::Stats stats( usage, argc, argv );
option::Option options[ 100 ];//stats.options_max];
option::Option buffer[ 100 ];//stats.buffer_max];
option::Parser parse( usage, argc, argv, options, buffer );
// Handle special cases
bool const noXML = options[INPUT].count() == 0 && options[SCHEMA].count() == 0;
if( parse.error() || options[HELP] || (argc == 0) || noXML )
{
int columns = getenv( "COLUMNS" ) ? atoi( getenv( "COLUMNS" )) : 120;
option::printUsage( fwrite, stdout, usage, columns );
if( options[HELP] )
{
throw NotAnError();
}
GEOS_THROW( "Bad command line arguments.", InputError );
}
// Iterate over the remaining inputs
for( int ii=0; ii<parse.optionsCount(); ++ii )
{
option::Option & opt = buffer[ii];
switch( opt.index() )
{
case UNKNOWN:
{}
break;
case HELP:
{}
break;
case INPUT:
{
commandLineOptions->inputFileNames.emplace_back( opt.arg );
}
break;
case RESTART:
{
commandLineOptions->restartFileName = opt.arg;
commandLineOptions->beginFromRestart = true;
}
break;
case XPAR:
{
commandLineOptions->xPartitionsOverride = std::stoi( opt.arg );
commandLineOptions->overridePartitionNumbers = 1;
}
break;
case YPAR:
{
commandLineOptions->yPartitionsOverride = std::stoi( opt.arg );
commandLineOptions->overridePartitionNumbers = 1;
}
break;
case ZPAR:
{
commandLineOptions->zPartitionsOverride = std::stoi( opt.arg );
commandLineOptions->overridePartitionNumbers = 1;
}
break;
case NONBLOCKING_MPI:
{
commandLineOptions->useNonblockingMPI = true;
}
break;
case SUPPRESS_PINNED:
{
commandLineOptions->suppressPinned = true;
}
break;
case SCHEMA:
{
commandLineOptions->schemaName = opt.arg;
}
break;
case PROBLEMNAME:
{
commandLineOptions->problemName = opt.arg;
}
break;
case OUTPUTDIR:
{
commandLineOptions->outputDirectory = opt.arg;
}
break;
case TIMERS:
{
commandLineOptions->timerOutput = opt.arg;
}
break;
case TRACE_DATA_MIGRATION:
{
commandLineOptions->traceDataMigration = true;
}
break;
case MEMORY_USAGE:
{
commandLineOptions->printMemoryUsage = std::stod( opt.arg );
}
break;
case PAUSE_FOR:
{
// we should store this in commandLineOptions and sleep in main
integer const duration = std::stoi( opt.arg );
GEOS_LOG_RANK_0( "Paused for " << duration << " s" );
std::this_thread::sleep_for( std::chrono::seconds( duration ) );
}
break;
}
}
if( commandLineOptions->problemName.empty() && options[INPUT].count() > 0 )
{
string & inputFileName = commandLineOptions->inputFileNames[0];
if( inputFileName.length() > 4 && inputFileName.substr( inputFileName.length() - 4, 4 ) == ".xml" )
{
string::size_type start = inputFileName.find_last_of( '/' ) + 1;
if( start >= inputFileName.length())
{
start = 0;
}
commandLineOptions->problemName.assign( inputFileName, start, inputFileName.length() - 4 - start );
}
else
{
commandLineOptions->problemName.assign( inputFileName );
}
}
return commandLineOptions;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::unique_ptr< CommandLineOptions > basicSetup( int argc, char * argv[], bool const parseCommandLine )
{
setupEnvironment( argc, argv );
setupLAI();
if( parseCommandLine )
{
return parseCommandLineOptions( argc, argv );
}
else
{
return std::make_unique< CommandLineOptions >();
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void basicCleanup()
{
finalizeLAI();
cleanupEnvironment();
}
} // namespace geos