forked from fmihpc/vlasiator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readparameters.cpp
805 lines (687 loc) · 30.3 KB
/
readparameters.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
/*
* This file is part of Vlasiator.
* Copyright 2010-2016 Finnish Meteorological Institute
*
* For details of usage, see the COPYING file and read the "Rules of the Road"
* at http://www.physics.helsinki.fi/vlasiator/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <boost/program_options.hpp>
#include <boost/lexical_cast.hpp>
#include <cmath>
#include <limits>
#include <iomanip>
#include <string>
#include <fstream>
#include <iostream>
#include "readparameters.h"
#include "version.h"
using namespace std;
// Handles parameter processing from the user
namespace PO = boost::program_options;
static bool initialized = false;
static PO::options_description* descriptions = NULL;
static PO::variables_map* variables = NULL;
static map<string,string> options;
static map<string,bool> isOptionParsed;
static map< string,vector<string> > vectorOptions;
static map<string,bool> isVectorOptionParsed;
bool Readparameters::helpRequested = false;
static string global_config_file_name = "";
static string user_config_file_name = "";
static string run_config_file_name = "";
int Readparameters::argc;
char** Readparameters::argv;
int Readparameters::rank;
MPI_Comm Readparameters::comm;
/** Constructor for class ReadParameters. The constructor defines some
* default parameters and parses the input files.
* @param argc Command line argc.
* @param argv Command line argv.
* @param mpicomm Communicator for processes that will access options
*/
Readparameters::Readparameters(int argc, char* argv[],MPI_Comm mpicomm) {
Readparameters::argc = argc;
Readparameters::argv = argv;
MPI_Comm_dup(mpicomm,&(Readparameters::comm));
MPI_Comm_rank(Readparameters::comm,&(Readparameters::rank));
if (Readparameters::rank==MASTER_RANK){
if (initialized == false) {
descriptions = new PO::options_description("Usage: main [options (options given on the command line override options given everywhere else)], where options are:", 160);
variables = new PO::variables_map;
initialized = true;
addDefaultParameters();
// Read options from command line, first time for help message parsing, second time in parse() below.
PO::store(PO::command_line_parser(argc, argv).options(*descriptions).allow_unregistered().run(), *variables);
PO::notify(*variables);
helpRequested=(variables->count("help") > 0);
MPI_Bcast(&helpRequested,sizeof(bool),MPI_BYTE,0,MPI_COMM_WORLD);
}
}
else{
descriptions = NULL;
variables = NULL;
MPI_Bcast(&helpRequested,sizeof(bool),MPI_BYTE,0,MPI_COMM_WORLD);
}
//send as Int as MPI_BOOL is only in C++ bindings
int init_int=initialized;
MPI_Bcast(&init_int,1,MPI_INT,0,Readparameters::comm);
initialized=(init_int==1);
}
/** Add a new input parameter to Readparameters. Note that Readparameters::parse must be called
* in order for the input file(s) to be re-read. This functions only needs to be called by root process.
* Other processes can call it but those calls have no effect.
* @param name The name of the parameter, as given in the input file(s).
* @param desc Description for the parameter.
* @param defValue Default value for variable var.
* @return If true, the new parameter was added successfully.
*/
bool Readparameters::add(const string& name,const string& desc,const std::string& defValue) {
if (initialized == false) return false;
if(rank==MASTER_RANK){
options[name]="";
isOptionParsed[name]=false;
descriptions->add_options()(name.c_str(), PO::value<string>(&(options[name]))->default_value(defValue), desc.c_str());
}
return true;
}
/** Add a new input parameter to Readparameters. Note that Readparameters::parse must be called
* in order for the input file(s) to be re-read. This functions only needs to be called by root process.
* Other processes can call it but those calls have no effect.
* @param name The name of the parameter, as given in the input file(s).
* @param desc Description for the parameter.
* @param defValue Default value for variable var.
* @return If true, the new parameter was added successfully.
*/
bool Readparameters::add(const string& name,const string& desc,const bool& defValue) {
if (initialized == false) return false;
stringstream ss;
ss<<defValue;
if(rank==MASTER_RANK){
options[name]="";
isOptionParsed[name]=false;
descriptions->add_options()(name.c_str(), PO::value<string>(&(options[name]))->default_value(ss.str()), desc.c_str());
}
return true;
}
/** Add a new input parameter to Readparameters. Note that Readparameters::parse must be called
* in order for the input file(s) to be re-read. This functions only needs to be called by root process.
* Other processes can call it but those calls have no effect.
* @param name The name of the parameter, as given in the input file(s).
* @param desc Description for the parameter.
* @param defValue Default value for variable var.
* @return If true, the new parameter was added successfully.
*/
bool Readparameters::add(const string& name,const string& desc,const int& defValue) {
if (initialized == false) return false;
stringstream ss;
ss<<defValue;
if(rank==MASTER_RANK){
options[name]="";
isOptionParsed[name]=false;
descriptions->add_options()(name.c_str(), PO::value<string>(&(options[name]))->default_value(ss.str()), desc.c_str());
}
return true;
}
/** Add a new input parameter to Readparameters. Note that Readparameters::parse must be called
* in order for the input file(s) to be re-read. This functions only needs to be called by root process.
* Other processes can call it but those calls have no effect.
* @param name The name of the parameter, as given in the input file(s).
* @param desc Description for the parameter.
* @param defValue Default value for variable var.
* @return If true, the new parameter was added successfully.
*/
bool Readparameters::add(const string& name,const string& desc,const unsigned int& defValue) {
if (initialized == false) return false;
stringstream ss;
ss<<defValue;
if(rank==MASTER_RANK){
options[name]="";
isOptionParsed[name]=false;
descriptions->add_options()(name.c_str(), PO::value<string>(&(options[name]))->default_value(ss.str()), desc.c_str());
}
return true;
}
/** Add a new input parameter to Readparameters. Note that Readparameters::parse must be called
* in order for the input file(s) to be re-read. This functions only needs to be called by root process.
* Other processes can call it but those calls have no effect.
* @param name The name of the parameter, as given in the input file(s).
* @param desc Description for the parameter.
* @param defValue Default value for variable var.
* @return If true, the new parameter was added successfully.
*/
bool Readparameters::add(const string& name,const string& desc,const float& defValue) {
if (initialized == false) return false;
stringstream ss;
//set full precision
ss<<setprecision(numeric_limits<float>::digits10 + 1) <<defValue;
if(rank==MASTER_RANK){
options[name]="";
isOptionParsed[name]=false;
descriptions->add_options()(name.c_str(), PO::value<string>(&(options[name]))->default_value(ss.str()), desc.c_str());
}
return true;
}
/** Add a new input parameter to Readparameters. Note that Readparameters::parse must be called
* in order for the input file(s) to be re-read. This functions only needs to be called by root process.
* Other processes can call it but those calls have no effect.
* @param name The name of the parameter, as given in the input file(s).
* @param desc Description for the parameter.
* @param defValue Default value for variable var.
* @return If true, the new parameter was added successfully.
*/
bool Readparameters::add(const string& name,const string& desc,const double& defValue) {
if (initialized == false) return false;
stringstream ss;
//set full precision
ss<<setprecision(numeric_limits<double>::digits10 + 1) <<defValue;
if(rank==MASTER_RANK){
options[name]="";
isOptionParsed[name]=false;
descriptions->add_options()(name.c_str(), PO::value<string>(&(options[name]))->default_value(ss.str()), desc.c_str());
}
return true;
}
/** Add a new composing input parameter to Readparameters. Note that Readparameters::parse must be called
* in order for the input file(s) to be re-read. This functions only needs to be called by root process.
* Other processes can call it but those calls have no effect.
* @param name The name of the parameter, as given in the input file(s).
* @param desc Description for the parameter.
* @return If true, the new parameter was added successfully.
*/
bool Readparameters::addComposing(const string& name,const string& desc) {
if (initialized == false) return false;
if(rank==MASTER_RANK){
isVectorOptionParsed[name]=false;
descriptions->add_options()(name.c_str(), PO::value<vector<string> >(&(vectorOptions[name]))->composing(), desc.c_str());
}
return true;
}
//add names of input files
bool Readparameters::addDefaultParameters() {
if (initialized == false) return false;
if(rank==MASTER_RANK){
descriptions->add_options()
("help", "print this help message");
descriptions->add_options()
("version", "print version information");
// Parameters which set the names of the configuration file(s):
descriptions->add_options()
("global_config", PO::value<string>(&global_config_file_name)->default_value(""),"read options from the global configuration file arg (relative to the current working directory). Options given in this file are overridden by options given in the user's and run's configuration files and by options given in environment variables (prefixed with MAIN_) and the command line")
("user_config", PO::value<string>(&user_config_file_name)->default_value(""), "read options from the user's configuration file arg (relative to the current working directory). Options given in this file override options given in the global configuration file. Options given in this file are overridden by options given in the run's configuration file and by options given in environment variables (prefixed with MAIN_) and the command line")
("run_config", PO::value<string>(&run_config_file_name)->default_value(""), "read options from the run's configuration file arg (relative to the current working directory). Options given in this file override options given in the user's and global configuration files. Options given in this override options given in the user's and global configuration files. Options given in this file are overridden by options given in environment variables (prefixed with MAIN_) and the command line");
}
return true;
}
/** Deallocate memory reserved by Parameters.
* @return If true, class Parameters finalized successfully.
*/
bool Readparameters::finalize() {
if(rank==MASTER_RANK){
delete descriptions;
delete variables;
descriptions = NULL;
variables = NULL;
}
initialized = false;
return true;
}
/** Get the value of the given parameter added with addComposing(). This may be called after having called Parse, and it may be called
* by any process, in any order.
* This one returns true in all cases to handle cases where no option is in the cfg files
* but could be (e.g. system boundary conditions).
* @param name The name of the parameter.
* @param value A variable where the value of the parameter is written.
* @return If true, the given parameter was found and its value was written to value.
*/
bool Readparameters::get(const std::string& name,std::vector<std::string>& value) {
if(vectorOptions.find(name) != vectorOptions.end() ){ //check if it exists
value = vectorOptions[name];
}
return true;
}
/** Get the value of the given parameter added with addComposing(). This may be called after having called Parse, and it may be called
* by any process, in any order.
* @param name The name of the parameter.
* @param value A variable where the value of the parameter is written.
* @return If true, the given parameter was found and its value was written to value.
*/
bool Readparameters::get(const std::string& name,std::vector<int>& value) {
vector<string> stringValue;
bool ret;
using boost::lexical_cast;
ret=Readparameters::get(name,stringValue);
if (ret) {
for (vector<string>::iterator i = stringValue.begin(); i!=stringValue.end(); ++i) {
try {
value.push_back(lexical_cast<int>(*i));
}
catch (...){
if(Readparameters::rank==0) cerr << "Problems casting value to int " << name <<" = " << *i <<endl;
return false;
}
}
}
return ret;
}
/** Get the value of the given parameter added with addComposing(). This may be called after having called Parse, and it may be called
* by any process, in any order.
* @param name The name of the parameter.
* @param value A variable where the value of the parameter is written.
* @return If true, the given parameter was found and its value was written to value.
*/
bool Readparameters::get(const std::string& name,std::vector<unsigned int>& value) {
vector<string> stringValue;
bool ret;
using boost::lexical_cast;
ret=Readparameters::get(name,stringValue);
if (ret) {
for (vector<string>::iterator i = stringValue.begin(); i!=stringValue.end(); ++i) {
try {
value.push_back(lexical_cast<unsigned int>(*i));
}
catch (...){
if(Readparameters::rank==0) cerr << "Problems casting value to unsigned int " << name <<" = " << *i <<endl;
return false;
}
}
}
return ret;
}
/** Get the value of the given parameter added with addComposing(). This may be called after having called Parse, and it may be called
* by any process, in any order.
* @param name The name of the parameter.
* @param value A variable where the value of the parameter is written.
* @return If true, the given parameter was found and its value was written to value.
*/
bool Readparameters::get(const std::string& name,std::vector<float>& value) {
vector<string> stringValue;
bool ret;
using boost::lexical_cast;
ret=Readparameters::get(name,stringValue);
if (ret) {
for (vector<string>::iterator i = stringValue.begin(); i!=stringValue.end(); ++i) {
try {
value.push_back(lexical_cast<float>(*i));
}
catch (...){
if(Readparameters::rank==0) cerr << "Problems casting value to float " << name <<" = " << *i <<endl;
return false;
}
}
}
return ret;
}
/** Get the value of the given parameter added with addComposing(). This may be called after having called Parse, and it may be called
* by any process, in any order.
* @param name The name of the parameter.
* @param value A variable where the value of the parameter is written.
* @return If true, the given parameter was found and its value was written to value.
*/
bool Readparameters::get(const std::string& name,std::vector<double>& value) {
vector<string> stringValue;
bool ret;
using boost::lexical_cast;
ret=Readparameters::get(name,stringValue);
if (ret) {
for (vector<string>::iterator i = stringValue.begin(); i!=stringValue.end(); ++i) {
try {
value.push_back(lexical_cast<double>(*i));
}
catch (...){
if(Readparameters::rank==0) cerr << "Problems casting value to double " << name <<" = " << *i <<endl;
return false;
}
}
}
return ret;
}
/** Get the value of the given parameter added with add(). This may be called after having called Parse, and it may be called
* by any process, in any order.
* @param name The name of the parameter.
* @param value A variable where the value of the parameter is written.
* @return If true, the given parameter was found and its value was written to value.
*/
bool Readparameters::get(const std::string& name,std::string& value) {
if(options.find(name) != options.end() ){ //check if it exists
value = options[name];
return true;
}
return false;
}
/** Get the value of the given parameter added with add(). This may be called after having called Parse, and it may be called
* by any process, in any order.
* @param name The name of the parameter.
* @param value A variable where the value of the parameter is written.
* @return If true, the given parameter was found and its value was written to value.
*/
bool Readparameters::get(const std::string& name,bool& value) {
string sval;
bool ret;
using boost::lexical_cast;
ret=Readparameters::get(name,sval);
if (ret) {
try {
value = lexical_cast<bool>(sval);
}
catch (...){
if(Readparameters::rank==0) cerr << "Problems casting value to bool " << name <<" = " << sval <<endl;
return false;
}
}
return ret;
}
/** Get the value of the given parameter added with add(). This may be called after having called Parse, and it may be called
* by any process, in any order.
* @param name The name of the parameter.
* @param value A variable where the value of the parameter is written.
* @return If true, the given parameter was found and its value was written to value.
*/
bool Readparameters::get(const std::string& name,int& value) {
string sval;
bool ret;
using boost::lexical_cast;
ret=Readparameters::get(name,sval);
if (ret) {
try {
value = lexical_cast<int>(sval);
}
catch (...){
if(Readparameters::rank==0) cerr << "Problems casting value to int " << name <<" = " << sval <<endl;
return false;
}
}
return ret;
}
/** Get the value of the given parameter added with add(). This may be called after having called Parse, and it may be called
* by any process, in any order.
* @param name The name of the parameter.
* @param value A variable where the value of the parameter is written.
* @return If true, the given parameter was found and its value was written to value.
*/
bool Readparameters::get(const std::string& name,unsigned int& value) {
string sval;
bool ret;
using boost::lexical_cast;
ret=Readparameters::get(name,sval);
if (ret) {
try {
value = lexical_cast<unsigned int>(sval);
}
catch (...){
if(Readparameters::rank==0) cerr << "Problems casting value to unsigned int " << name <<" = " << sval <<endl;
return false;
}
}
return ret;
}
/** Get the value of the given parameter added with add(). This may be called after having called Parse, and it may be called
* by any process, in any order.
* @param name The name of the parameter.
* @param value A variable where the value of the parameter is written.
* @return If true, the given parameter was found and its value was written to value.
*/
bool Readparameters::get(const std::string& name,unsigned long& value) {
string sval;
bool ret;
using boost::lexical_cast;
ret=Readparameters::get(name,sval);
if (ret) {
try {
value = lexical_cast<unsigned long>(sval);
}
catch (...){
if(Readparameters::rank==0) cerr << "Problems casting value to unsigned long " << name <<" = " << sval <<endl;
return false;
}
}
return ret;
}
/** Get the value of the given parameter added with add(). This may be called after having called Parse, and it may be called
* by any process, in any order.
* @param name The name of the parameter.
* @param value A variable where the value of the parameter is written.
* @return If true, the given parameter was found and its value was written to value.
*/
bool Readparameters::get(const std::string& name,float& value) {
string sval;
bool ret;
using boost::lexical_cast;
ret=Readparameters::get(name,sval);
if (ret) {
try {
value = lexical_cast<float>(sval);
}
catch (...){
if(Readparameters::rank==0) cerr << "Problems casting value to float " << name <<" = " << sval <<endl;
return false;
}
}
return ret;
}
/** Get the value of the given parameter added with add(). This may be called after having called Parse, and it may be called
* by any process, in any order.
* @param name The name of the parameter.
* @param value A variable where the value of the parameter is written.
* @return If true, the given parameter was found and its value was written to value.
*/
bool Readparameters::get(const std::string& name,double& value) {
string sval;
bool ret;
using boost::lexical_cast;
ret=Readparameters::get(name,sval);
if (ret) {
try {
value = lexical_cast<double>(sval);
}
catch (...){
if(Readparameters::rank==0) cerr << "Problems casting value to double " << name <<" = " << sval <<endl;
return false;
}
}
return ret;
}
/** Write the descriptions of known input options to standard output if
* an option called "help" has been read, and exit in that case.
*/
void Readparameters::helpMessage() {
if (helpRequested) {
if (rank==MASTER_RANK) {
cout << *descriptions <<endl;
}
MPI_Finalize();
exit(0);
}
}
/** Write version information to standard output if
* an option called "version" has been read.
* @return If true, option called "version" was found and descriptions were written
* to standard output.
*/
bool Readparameters::versionMessage() {
if(rank==MASTER_RANK){
if (variables->count("version") > 0) {
printVersion();
return true;
}
return false;
}
return true;
}
/** Query is Parameters has initialized successfully.
* @return If true, Parameters is ready for use.
*/
bool Readparameters::isInitialized() {return initialized;}
/** Request Parameters to reparse input file(s). This function needs
* to be called after new options have been added via Parameters:add functions.
* Otherwise the values of the new options are not read. This is a collective function, all processes have to all it.
* @param needsRunConfig Whether or not this program can run without a runconfig file (esm: vlasiator can't, but particle pusher can)
* @return If true, input file(s) were parsed successfully.
*/
bool Readparameters::parse(const bool needsRunConfig) {
if (initialized == false) return false;
// Tell Boost to allow undescribed options (throws exception otherwise)
if(rank==MASTER_RANK){
const bool ALLOW_UNKNOWN = true;
// Read options from command line: Duplicated in constructor for help message processing
PO::store(PO::parse_command_line(argc, argv, *descriptions), *variables);
PO::notify(*variables);
// Read options from environment variables:
PO::store(PO::parse_environment(*descriptions, "MAIN_"), *variables);
PO::notify(*variables);
// Read options from run config file:
if (run_config_file_name.size() > 0) {
ifstream run_config_file(run_config_file_name.c_str(), fstream::in);
if (run_config_file.good() == true) {
PO::store(PO::parse_config_file(run_config_file, *descriptions, ALLOW_UNKNOWN), *variables);
PO::notify(*variables);
run_config_file.close();
} else {
if(Readparameters::rank==0) cerr << "Couldn't open or read run config file " << run_config_file_name << endl;
abort();
}
}
// Read options from user config file:
if (user_config_file_name.size() > 0) {
ifstream user_config_file(user_config_file_name.c_str(), fstream::in);
if (user_config_file.good() == true) {
PO::store(PO::parse_config_file(user_config_file, *descriptions, ALLOW_UNKNOWN), *variables);
PO::notify(*variables);
user_config_file.close();
} else {
if(Readparameters::rank==0) cerr << "Couldn't open or read user config file " << user_config_file_name << endl;
abort();
}
}
// Read options from global config file:
if (global_config_file_name.size() > 0) {
ifstream global_config_file(global_config_file_name.c_str(), fstream::in);
if (global_config_file.good() == true) {
PO::store(PO::parse_config_file(global_config_file, *descriptions, ALLOW_UNKNOWN), *variables);
PO::notify(*variables);
global_config_file.close();
} else {
if(Readparameters::rank==0) cerr << "Couldn't open or read global config file " << global_config_file_name << endl;
abort();
}
}
}
//Check if the user has specified --version
bool hasVersionOption=versionMessage();
MPI_Bcast(&hasVersionOption,sizeof(bool),MPI_BYTE,0,MPI_COMM_WORLD);
if(hasVersionOption){
MPI_Finalize();
exit(0);
}
//Require that there is a run config file. There are so many options so it is unlikely
//that one would like to define all on the command line, or only in global/user run files
//If no arguments are given the program (currently r155) would crash later on with nasty error messages
bool hasRunConfigFile=(run_config_file_name.size() > 0);
MPI_Bcast(&hasRunConfigFile,sizeof(bool),MPI_BYTE,0,MPI_COMM_WORLD);
if(needsRunConfig && !hasRunConfigFile && !helpRequested){
if(Readparameters::rank==MASTER_RANK){
cout << "Run config file required. Use --help to list all options" <<endl;
}
MPI_Finalize();
exit(0);
}
int nOptionsToBroadcast;
int vectorSize;
const int maxStringLength=1024;
char value[maxStringLength];
char name[maxStringLength];
value[maxStringLength-1]='\0';
name[maxStringLength-1]='\0';
/*
loop through options and broadcast all options from root rank to the others
Separate bcasts not optimal from performance point of view, but parse is normally just
called a few times so it should not matter
*/
//count number of options not parsed/broarcasted previously
if(rank==MASTER_RANK){
nOptionsToBroadcast=0;
for( map<string,bool>::iterator ip=isOptionParsed.begin(); ip!=isOptionParsed.end();++ip){
if(! ip->second) nOptionsToBroadcast++;
}
}
MPI_Bcast(&nOptionsToBroadcast,1,MPI_INT,0,MPI_COMM_WORLD);
if(rank==MASTER_RANK){
//iterate through map and bcast cstrings of key/value pairs not parsed before
for( map<string,string>::iterator p=options.begin(); p!=options.end();++p){
if(! isOptionParsed[p->first]) {
strncpy(name,p->first.c_str(),maxStringLength-1);
strncpy(value,p->second.c_str(),maxStringLength-1);
MPI_Bcast(name,maxStringLength,MPI_CHAR,0,MPI_COMM_WORLD);
MPI_Bcast(value,maxStringLength,MPI_CHAR,0,MPI_COMM_WORLD);
isOptionParsed[p->first]=true;
}
}
}
else{
//reveive new options
for(int p=0;p<nOptionsToBroadcast;p++){
MPI_Bcast(name,maxStringLength,MPI_CHAR,0,MPI_COMM_WORLD);
MPI_Bcast(value,maxStringLength,MPI_CHAR,0,MPI_COMM_WORLD);
string sName(name);
string sValue(value);
options[sName]=sValue;
}
}
/*
loop through vector options and broadcast all vector options from root rank to the others
Separate bcasts not optimal from performance point of view, but parse is normally just
called a few times so it should not matter
*/
//count number of vector options not parsed/broarcasted previously
if(rank==MASTER_RANK){
nOptionsToBroadcast=0;
for( map<string,bool>::iterator ip=isVectorOptionParsed.begin(); ip!=isVectorOptionParsed.end();++ip){
if(! ip->second) nOptionsToBroadcast++;
}
}
//root broadcasts its new vector values
MPI_Bcast(&nOptionsToBroadcast,1,MPI_INT,0,MPI_COMM_WORLD);
if(rank==MASTER_RANK){
//iterate through map and bcast cstrings of key/value pairs not parsed before
for( map< string,vector<string> >::iterator p=vectorOptions.begin(); p!=vectorOptions.end();++p){
if(! isVectorOptionParsed[p->first]) {
strncpy(name,p->first.c_str(),maxStringLength-1);
MPI_Bcast(name,maxStringLength,MPI_CHAR,0,MPI_COMM_WORLD);
vectorSize=vectorOptions[p->first].size();
MPI_Bcast(&vectorSize,1,MPI_INT,0,MPI_COMM_WORLD);
for( vector<string>::iterator v=vectorOptions[p->first].begin(); v!=vectorOptions[p->first].end();++v){
strncpy(value,v->c_str(),maxStringLength-1);
MPI_Bcast(value,maxStringLength,MPI_CHAR,0,MPI_COMM_WORLD);
}
isVectorOptionParsed[p->first]=true;
}
}
}
else{
//others receive new options
for(int p=0;p<nOptionsToBroadcast;p++){
MPI_Bcast(name,maxStringLength,MPI_CHAR,0,MPI_COMM_WORLD);
string sName(name);
MPI_Bcast(&vectorSize,1,MPI_INT,0,MPI_COMM_WORLD);
for (int i=0;i<vectorSize;i++){
MPI_Bcast(value,maxStringLength,MPI_CHAR,0,MPI_COMM_WORLD);
string sValue(value);
vectorOptions[sName].push_back(sValue);
}
}
}
return true;
}