queso-0.51.1
Environment.C
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // QUESO - a library to support the Quantification of Uncertainty
5 // for Estimation, Simulation and Optimization
6 //
7 // Copyright (C) 2008,2009,2010,2011,2012,2013 The PECOS Development Team
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the Version 2.1 GNU Lesser General
11 // Public License as published by the Free Software Foundation.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301 USA
22 //
23 //-----------------------------------------------------------------------el-
24 
25 #include <queso/queso.h>
26 #include <queso/Environment.h>
27 #include <queso/EnvironmentOptions.h>
28 #include <queso/RngGsl.h>
29 #include <queso/RngBoost.h>
30 #include <queso/BasicPdfsGsl.h>
31 #include <queso/BasicPdfsBoost.h>
32 #include <queso/Miscellaneous.h>
33 #include <sys/time.h>
34 #ifdef HAVE_GRVY
35 #include <grvy.h>
36 #endif
37 
38 // queso error handling
39 #include <queso/asserts.h>
40 
41 //-----------------------------
42 // Library versioning routines
43 //-----------------------------
44 
45 namespace QUESO {
46 
47  void QUESO_version_print(std::ostream &os)
48  {
49  {
50  os << "------------------------------------------------------------------------------------------" ;
51  os << "--------------------" << std::endl;
52  os << "QUESO Library: Version = " << QUESO_LIB_VERSION;
53  os << " (" << QUESO_get_numeric_version() << ")" << std::endl << std::endl;
54 
55  os << QUESO_LIB_RELEASE << std::endl << std::endl;
56 
57  os << "Build Date = " << QUESO_BUILD_DATE << std::endl;
58  os << "Build Host = " << QUESO_BUILD_HOST << std::endl;
59  os << "Build User = " << QUESO_BUILD_USER << std::endl;
60  os << "Build Arch = " << QUESO_BUILD_ARCH << std::endl;
61  os << "Build Rev = " << QUESO_BUILD_VERSION << std::endl << std::endl;
62 
63  os << "C++ Config = " << QUESO_CXX << " " << QUESO_CXXFLAGS << std::endl;
64  os << std::endl;
65  os << "Trilinos DIR = " << QUESO_TRILINOS_DIR << std::endl;
66  os << "GSL Libs = " << QUESO_GSL_DIR << std::endl;
67  os << "GRVY DIR = " << QUESO_GRVY_DIR << std::endl;
68  os << "GLPK DIR = " << QUESO_GLPK_DIR << std::endl;
69  os << "HDF5 DIR = " << QUESO_HDF5_DIR << std::endl;
70  os << "------------------------------------------------------------------------------------------" ;
71  os << "--------------------" << std::endl;
72  }
73 
74  return;
75  }
76 
78  {
79  // Note: return format follows the versioning convention xx.yy.z where
80  //
81  // xx = major version number
82  // yy = minor version number
83  // zz = micro version number
84  //
85  // For example:
86  // v. 0.23 -> 002300 = 2300
87  // v. 0.23.1 -> 002301 = 2301
88  // v. 10.23.2 -> 102302
89 
90  int major_version = 0;
91  int minor_version = 0;
92  int micro_version = 0;
93 
94 #ifdef QUESO_MAJOR_VERSION
95  major_version = QUESO_MAJOR_VERSION;
96 #endif
97 
98 #ifdef QUESO_MINOR_VERSION
99  minor_version = QUESO_MINOR_VERSION;
100 #endif
101 
102 #ifdef QUESO_MICRO_VERSION
103  micro_version = QUESO_MICRO_VERSION;
104 #endif
105 
106  return(major_version*10000 + minor_version*100 + micro_version);
107  }
108 
110  :
111  ofsVar(NULL),
112  ifsVar(NULL)
113 {
114 }
115 
117 {
118 }
119 
120  //
121  // queso terminate handler will be invoked for unhandled exceptions
122  // needs to be invoked in FullEnvironment constructor
123  //
124  std::terminate_handler old_terminate_handler;
125 
126 //*****************************************************
127 // Base class
128 //*****************************************************
129 // Default constructor --------------------------------
131  const char* passedOptionsInputFileName,
132  const EnvOptionsValues* alternativeOptionsValues)
133  :
134  m_fullEnvIsReady (false),
135  m_worldRank (-1),
136  m_fullComm (NULL),
137  m_fullRank (-1),
138  m_fullCommSize (1),
139  m_optionsInputFileName (""),
140  m_optionsInputFileAccessState(true),
141  m_allOptionsDesc (NULL),
142  m_allOptionsMap (NULL),
143  m_subComm (NULL),
144  m_subRank (-1),
145  m_subCommSize (1),
146  m_selfComm (NULL),
147  m_inter0Comm (NULL),
148  m_inter0Rank (-1),
149  m_inter0CommSize (1),
150  m_subDisplayFile (NULL),
151  m_rngObject (NULL),
152  m_basicPdfs (NULL),
153  m_exceptionalCircumstance (false),
154  m_alternativeOptionsValues (),
155  m_optionsObj (NULL)
156 {
157  if (passedOptionsInputFileName) m_optionsInputFileName = passedOptionsInputFileName;
158  if (alternativeOptionsValues ) m_alternativeOptionsValues = *alternativeOptionsValues;
159 }
160 // Copy constructor -------------------------------------
162 {
164  obj.worldRank(),
165  "BaseEnvironment::constructor(), copy",
166  "code should not execute through here");
167 }
168 // Destructor -------------------------------------------
170 {
171  //if (m_subDisplayFile) {
172  // *m_subDisplayFile << "Entering BaseEnvironment::destructor()"
173  // << std::endl;
174  //}
175 
176  struct timeval timevalNow;
177  /*int iRC = 0;*/
178  /*iRC = */gettimeofday(&timevalNow, NULL);
179 
180  if( this->displayVerbosity() > 0 )
181  {
182  if (m_subDisplayFile) {
183  *m_subDisplayFile << "Ending run at " << ctime(&timevalNow.tv_sec)
184  << "Total run time = " << timevalNow.tv_sec - m_timevalBegin.tv_sec
185  << " seconds"
186  << std::endl;
187  }
188 
189  if (m_fullRank == 0) {
190  std::cout << "Ending run at " << ctime(&timevalNow.tv_sec)
191  << "Total run time = " << timevalNow.tv_sec - m_timevalBegin.tv_sec
192  << " seconds"
193  << std::endl;
194  }
195  }
196 
197  if (m_optionsObj) delete m_optionsObj;
198  if (m_allOptionsMap) {
199  delete m_allOptionsMap;
200  delete m_allOptionsDesc;
201  }
202 
203  if (m_basicPdfs) delete m_basicPdfs;
204  if (m_rngObject) delete m_rngObject;
205 
206  //if (m_subDisplayFile) {
207  // *m_subDisplayFile << "Leaving BaseEnvironment::destructor()"
208  // << std::endl;
209  //}
210 
212  if (m_inter0Comm ) delete m_inter0Comm;
213  if (m_selfComm ) delete m_selfComm;
214  if (m_subComm ) delete m_subComm;
215  if (m_fullComm ) delete m_fullComm;
216 }
217 // Set methods ------------------------------------------
220 {
222  rhs.worldRank(),
223  "BaseEnvironment::operator=()",
224  "code should not execute through here");
225  return *this;
226 }
227 // Environment, Communicator and Options Input File methods
228 bool
230 {
231  return m_fullEnvIsReady;
232 }
233 //-------------------------------------------------------
234 int
236 {
237  return m_worldRank;
238 }
239 //-------------------------------------------------------
240 int
242 {
243  return m_fullRank;
244 }
245 //-------------------------------------------------------
246 const MpiComm&
248 {
250  m_worldRank,
251  "BaseEnvironment::fullComm()",
252  "m_fullComm variable is NULL");
253  return *m_fullComm;
254 }
255 //-------------------------------------------------------
258 {
259  return m_subGroup;
260 }
261 //-------------------------------------------------------
262 int
264 {
265  return m_subRank;
266 }
267 //-------------------------------------------------------
268 const MpiComm&
270 {
272  m_worldRank,
273  "BaseEnvironment::subComm()",
274  "m_subComm variable is NULL");
275  return *m_subComm;
276 }
277 //-------------------------------------------------------
278 const MpiComm&
280 {
282  m_worldRank,
283  "BaseEnvironment::selfComm()",
284  "m_selfComm variable is NULL");
285  return *m_selfComm;
286 }
287 //-------------------------------------------------------
288 int
290 {
291  return m_inter0Rank;
292 }
293 //-------------------------------------------------------
294 const MpiComm&
296 {
298  m_worldRank,
299  "BaseEnvironment::inter0Comm()",
300  "m_inter0Comm variable is NULL");
301  return *m_inter0Comm;
302 }
303 //-------------------------------------------------------
304 std::ofstream*
306 {
307  return m_subDisplayFile;
308 }
309 //-------------------------------------------------------
310 std::string
312 {
313  if (m_optionsObj == NULL) return ".";
314 
316 }
317 //-------------------------------------------------------
318 unsigned int
320 {
322  m_worldRank,
323  "BaseEnvironment::numSubEnvironments()",
324  "m_optionsObj variable is NULL");
326 }
327 //-------------------------------------------------------
328 unsigned int
330 {
331  return m_subId;
332 }
333 //-------------------------------------------------------
334 const std::string&
336 {
337  return m_subIdString;
338 }
339 //-------------------------------------------------------
340 std::string
342 {
344  return m_optionsInputFileName;
345  }
346  else {
347  return "";
348  }
349 }
350 //-------------------------------------------------------
351 void
353 {
355 
356  return;
357 }
358 //-------------------------------------------------------
359 #ifdef UQ_USES_COMMAND_LINE_OPTIONS
360 const po::options_description&
361 BaseEnvironment::allOptionsDesc() const
362 {
363  return *m_allOptionsDesc;
364 }
365 #endif
366 //-------------------------------------------------------
367 po::variables_map&
369 {
371  m_worldRank,
372  "BaseEnvironment::allOptionsMap()",
373  "m_allOptionsMap variable is NULL");
374  return *m_allOptionsMap;
375 }
376 //-------------------------------------------------------
377 void
378 BaseEnvironment::scanInputFileForMyOptions(const po::options_description& optionsDesc) const
379 {
380 #ifdef UQ_USES_COMMAND_LINE_OPTIONS
381  // If you want to use command line options, the following line does *not* work outside 'main.C',
382  // e.g., in the constructor of FullEnvironment:
383  // Line: po::store(po::parse_command_line(argc, argv, *m_allOptionsDesc), *m_allOptionsMap);
384  //
385  // Instead, put the following three lines *immediately after* instantianting the UQ environment
386  // variable "FullEnvironment* env":
387  // Line 1: po::store(po::parse_command_line(argc, argv, env->allOptionsDesc()), env->allOptionsMap());
388  // Line 2: po::notify(env->allOptionsMap());
389  // Line 3: env->getMyOptionValues();
390 #endif
391 
392 #ifdef QUESO_MEMORY_DEBUGGING
393  std::cout << "Entering BaseEnv::scanInputFileForMyOptions()" << std::endl;
394 #endif
395 
397  m_worldRank,
398  "BaseEnvironment::scanInputFileForMyOptions()",
399  "m_allOptionsDesc variable is NULL");
400  m_allOptionsDesc->add(optionsDesc);
401  //if (m_subDisplayFile) {
402  // *m_subDisplayFile << *m_allOptionsDesc
403  // << std::endl;
404  //}
405 
407  m_worldRank,
408  "BaseEnvironment::scanInputFileForMyOptions()",
409  "m_optionsInputFileName is 'nothing'");
410  //std::ifstream ifs(m_optionsInputFileName.c_str());
411  std::ifstream* ifs = new std::ifstream(m_optionsInputFileName.c_str());
412 #ifdef QUESO_MEMORY_DEBUGGING
413  std::cout << "in BaseEnv::scanInputFileForMyOptions(), before store(a)" << std::endl;
414 #endif
415 
417  m_worldRank,
418  "BaseEnvironment::scanInputFileForMyOptions()",
419  "m_allOptionsMap variable is NULL");
420  po::store(po::parse_config_file(*ifs, *m_allOptionsDesc, true), *m_allOptionsMap);
421 #ifdef QUESO_MEMORY_DEBUGGING
422  std::cout << "in BaseEnv::scanInputFileForMyOptions(), after store(a)" << std::endl;
423 #endif
424  po::notify(*m_allOptionsMap);
425 
426  //ifs.close();
427  delete ifs;
428 #ifdef QUESO_MEMORY_DEBUGGING
429  std::cout << "Leaving BaseEnv::scanInputFileForMyOptions()" << std::endl;
430 #endif
431 
432  return;
433 }
434 //-----------------------------------------------------
435 unsigned int
437 {
439  m_worldRank,
440  "BaseEnvironment::displayVerbosity()",
441  "m_optionsObj variable is NULL");
443 }
444 //-------------------------------------------------------
445 unsigned int
447 {
449  m_worldRank,
450  "BaseEnvironment::displayVerbosity()",
451  "m_optionsObj variable is NULL");
453 }
454 //-------------------------------------------------------
455 unsigned int
457 {
459  m_worldRank,
460  "BaseEnvironment::checkingLevel()",
461  "m_optionsObj variable is NULL");
463 }
464 //-------------------------------------------------------
465 const RngBase*
467 {
468  return m_rngObject;
469 }
470 //-------------------------------------------------------
471 int
473 {
474  return m_rngObject->seed();
475 }
476 //-------------------------------------------------------
477 void
478 BaseEnvironment::resetSeed(int newSeedOption)
479 {
480  m_rngObject->resetSeed(newSeedOption);
481  return;
482 }
483 //-------------------------------------------------------
484 const BasicPdfsBase*
486 {
487  return m_basicPdfs;
488 }
489 //-------------------------------------------------------
490 std::string
492 {
494 }
495 //-------------------------------------------------------
496 std::string
498 {
500 }
501 //-------------------------------------------------------
502 void
503 BaseEnvironment::resetIdentifyingString(const std::string& newString) const // Yes, const
504 {
505  m_optionsObj->m_ov.m_identifyingString = newString;
506  return;
507 }
508 //-------------------------------------------------------
509 struct timeval
510 BaseEnvironment::timevalBegin() const
511 {
512  return m_timevalBegin;
513 }
514 //-------------------------------------------------------
515 bool
517  const std::string& baseFileName,
518  const std::string& inputFileType,
519  const std::set<unsigned int>& allowedSubEnvIds,
520  bool writeOver,
521  FilePtrSetStruct& filePtrSet) const
522 {
523  std::string fileType(inputFileType);
524 #ifdef QUESO_HAS_HDF5
525  // Do nothing
526 #else
527  if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
528  if (m_subDisplayFile) {
529  *this->subDisplayFile() << "WARNING in BaseEnvironment::openOutputFile()"
530  << ": file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
531  << "' has been requested, but this QUESO library has not been built with 'hdf5'"
532  << ". Code will therefore process the file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
533  << "' instead..."
534  << std::endl;
535  }
536  if (this->subRank() == 0) {
537  std::cerr << "WARNING in BaseEnvironment::openOutputFile()"
538  << ": file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
539  << "' has been requested, but this QUESO library has not been built with 'hdf5'"
540  << ". Code will therefore process the file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
541  << "' instead..."
542  << std::endl;
543  }
545  }
546 #endif
547 
548  bool returnValue = true;
549  filePtrSet.ofsVar = NULL;
550  if ((baseFileName == UQ_ENV_FILENAME_FOR_NO_OUTPUT_FILE) ||
551  (allowedSubEnvIds.find(this->subId()) == allowedSubEnvIds.end() )) {
552  if ((m_subDisplayFile) && (this->displayVerbosity() > 10)) { // output debug
553  *this->subDisplayFile() << "In BaseEnvironment::openOutputFile()"
554  << ", subId = " << this->subId()
555  << ": no output file opened with base name '" << baseFileName << "." << fileType
556  << "'"
557  << ", writeOver = " << writeOver
558  << std::endl;
559  }
560  returnValue = false;
561  }
562  else {
564  // Open file
566  if ((m_subDisplayFile) && (this->displayVerbosity() > 10)) { // output debug
567  *this->subDisplayFile() << "In BaseEnvironment::openOutputFile()"
568  << ", subId = " << this->subId()
569  << ": opening output file with base name '" << baseFileName << "." << fileType
570  << "'"
571  << ", writeOver = " << writeOver
572  << std::endl;
573  }
574 
575  if (this->subRank() == 0) {
576 #if 0
577  std::cout << "In BaseEnvironment::openOutputFile()"
578  << ": opening output file with base name '" << baseFileName << "." << fileType
579  << "'"
580  << ", writeOver = " << writeOver
581  << std::endl;
582 #endif
583 
585  // Verify parent directory exists (for cases when a user
586  // specifies a relative path for the desired output file).
588  // std::cout << "checking " << baseFileName+"_sub"+this->subIdString()+"."+fileType << std::endl;
589  if ((m_subDisplayFile) && (this->displayVerbosity() > 10)) { // output debug
590  *this->subDisplayFile() << "In BaseEnvironment::openOutputFile()"
591  << ", subId = " << this->subId()
592  << ", trying to open output file with base name '" << baseFileName << "." << fileType
593  << "'"
594  << ", writeOver = " << writeOver
595  << ": calling CheckFilePath()..."
596  << std::endl;
597  }
598  int irtrn = CheckFilePath((baseFileName+"_sub"+this->subIdString()+"."+fileType).c_str());
599  if ((m_subDisplayFile) && (this->displayVerbosity() > 10)) { // output debug
600  *this->subDisplayFile() << "In BaseEnvironment::openOutputFile()"
601  << ", subId = " << this->subId()
602  << ", trying to open output file with base name '" << baseFileName << "." << fileType
603  << "'"
604  << ", writeOver = " << writeOver
605  << ": returned from CheckFilePath() with irtrn = " << irtrn
606  << std::endl;
607  }
608  UQ_FATAL_TEST_MACRO(irtrn < 0,
609  m_worldRank,
610  "BaseEnvironment::openOutputFile()",
611  "unable to verify output path");
612 
613  if (writeOver) {
615  // Write over an eventual pre-existing file
617  if (fileType == UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT) {
618  filePtrSet.ofsVar = new std::ofstream((baseFileName+"_sub"+this->subIdString()+"."+fileType).c_str(),
619  std::ofstream::out | std::ofstream::trunc);
620  }
621  else if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
622  UQ_FATAL_TEST_MACRO(true,
623  m_worldRank,
624  "BaseEnvironment::openOutputFile(), writeOver=true",
625  "hdf file type not supported yet");
626  }
627  else {
628  UQ_FATAL_TEST_MACRO(true,
629  m_worldRank,
630  "BaseEnvironment::openOutputFile(), writeOver=true",
631  "invalid file type");
632  }
633  if ((m_subDisplayFile) && (this->displayVerbosity() > 10)) { // output debug
634  *this->subDisplayFile() << "In BaseEnvironment::openOutputFile()"
635  << ": just opened output file with base name '" << baseFileName << "." << fileType
636  << "'"
637  << ", writeOver = " << writeOver
638  << ", options 'out|trunc'"
639  << ", osfvar = " << filePtrSet.ofsVar
640  << std::endl;
641  }
642  }
643  else {
645  // Write at the end of an eventual pre-existing file
647  if (fileType == UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT) {
648 #if 0
649  filePtrSet.ofsVar = new std::ofstream((baseFileName+"_sub"+this->subIdString()+"."+fileType).c_str(),
650  std::ofstream::in);
651  std::cout << "filePtrSet.ofsVar(1) = " << filePtrSet.ofsVar << std::endl;
652  if (filePtrSet.ofsVar) std::cout << "filePtrSet.ofsVar(1)->is_open() = " << filePtrSet.ofsVar->is_open() << std::endl;
653  if (filePtrSet.ofsVar) delete filePtrSet.ofsVar;
654 #endif
655  // 'm' and Ranger nodes behave differently on ofstream constructor... prudenci 2010/03/05
656  filePtrSet.ofsVar = new std::ofstream((baseFileName+"_sub"+this->subIdString()+"."+fileType).c_str(),
657  std::ofstream::out /*| std::ofstream::in*/ | std::ofstream::app);
658  }
659  else if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
660  UQ_FATAL_TEST_MACRO(true,
661  m_worldRank,
662  "BaseEnvironment::openOutputFile(), writeOver=false",
663  "hdf file type not supported yet");
664  }
665  else {
666  UQ_FATAL_TEST_MACRO(true,
667  m_worldRank,
668  "BaseEnvironment::openOutputFile(), writeOver=false",
669  "invalid file type");
670  }
671  if ((m_subDisplayFile) && (this->displayVerbosity() > 10)) { // output debug
672  *this->subDisplayFile() << "In BaseEnvironment::openOutputFile()"
673  << ": just opened output file with base name '" << baseFileName << "." << fileType
674  << "'"
675  << ", writeOver = " << writeOver
676  << ", options 'out|in|app'"
677  << ", osfvar = " << filePtrSet.ofsVar
678  << std::endl;
679  }
680 
681  if (fileType == UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT) {
682  //std::cout << "filePtrSet.ofsVar(2) = " << filePtrSet.ofsVar << std::endl;
683  //if (filePtrSet.ofsVar) std::cout << "filePtrSet.ofsVar(2)->is_open() = " << filePtrSet.ofsVar->is_open() << std::endl;
684  if ((filePtrSet.ofsVar == NULL ) ||
685  (filePtrSet.ofsVar->is_open() == false)) {
686  //std::cout << "Retrying 1..." << std::endl;
687  delete filePtrSet.ofsVar;
688  filePtrSet.ofsVar = new std::ofstream((baseFileName+"_sub"+this->subIdString()+"."+fileType).c_str(),
689  std::ofstream::out | std::ofstream::trunc);
690  if ((m_subDisplayFile) && (this->displayVerbosity() > 10)) { // output debug
691  *this->subDisplayFile() << "In BaseEnvironment::openOutputFile()"
692  << ": just opened output file with base name '" << baseFileName << "." << fileType
693  << "'"
694  << ", writeOver = " << writeOver
695  << ", options 'out|trunc'"
696  << ", osfvar = " << filePtrSet.ofsVar
697  << std::endl;
698  }
699  }
700  } // only for matlab formats
701  }
702  if (filePtrSet.ofsVar != NULL) {
703  if ((m_subDisplayFile) && (this->displayVerbosity() > 10)) { // output debug
704  *this->subDisplayFile() << "In BaseEnvironment::openOutputFile()"
705  << ", subId = " << this->subId()
706  << ": succeeded on opening output file with base name '" << baseFileName << "." << fileType
707  << "'"
708  << ", writeOver = " << writeOver
709  << std::endl;
710  }
711  }
712  else {
713  std::cerr << "In BaseEnvironment::openOutputFile()"
714  << ": failed to open output file with base name '" << baseFileName << "." << fileType
715  << "'"
716  << std::endl;
717  }
718  UQ_FATAL_TEST_MACRO((filePtrSet.ofsVar && filePtrSet.ofsVar->is_open()) == false,
719  this->worldRank(),
720  "openOutputFile()",
721  "failed to open output file");
722  }
723  else {
724  returnValue = false;
725  }
726  //this->subComm().Barrier(); // prudenci-2011-01-17
727  }
728 
729  return returnValue;
730 }
731 //-------------------------------------------------------
732 bool
734  const std::string& baseFileName,
735  const std::string& inputFileType,
736  bool writeOver,
737  FilePtrSetStruct& filePtrSet) const
738 {
739  std::string fileType(inputFileType);
740 #ifdef QUESO_HAS_HDF5
741  // Do nothing
742 #else
743  if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
744  if (m_subDisplayFile) {
745  *this->subDisplayFile() << "WARNING in BaseEnvironment::openUnifiedOutputFile()"
746  << ": file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
747  << "' has been requested, but this QUESO library has not been built with 'hdf5'"
748  << ". Code will therefore process the file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
749  << "' instead..."
750  << std::endl;
751  }
752  if (this->subRank() == 0) {
753  std::cerr << "WARNING in BaseEnvironment::openUnifiedOutputFile()"
754  << ": file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
755  << "' has been requested, but this QUESO library has not been built with 'hdf5'"
756  << ". Code will therefore process the file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
757  << "' instead..."
758  << std::endl;
759  }
761  }
762 #endif
763 
764  bool returnValue = true;
765  filePtrSet.ofsVar = NULL;
766  if (baseFileName == ".") {
767  if ((m_subDisplayFile) && (this->displayVerbosity() > 10)) { // output debug
768  *this->subDisplayFile() << "In BaseEnvironment::openUnifiedOutputFile()"
769  << ": no unified output file opened with base name '" << baseFileName << "." << fileType
770  << "'"
771  << ", writeOver = " << writeOver
772  << std::endl;
773  }
774  returnValue = false;
775  }
776  else {
778  // Open file
780  if ((m_subDisplayFile) && (this->displayVerbosity() > 10)) { // output debug
781  *this->subDisplayFile() << "In BaseEnvironment::openUnifiedOutputFile()"
782  << ": opening unified output file with base name '" << baseFileName << "." << fileType
783  << "'"
784  << ", writeOver = " << writeOver
785  << std::endl;
786  }
787 
788 
789  //if ((this->subRank () == 0) &&
790  // (this->inter0Rank() == 0)) {
791 #if 0
792  std::cout << "In BaseEnvironment::openUnifiedOutputFile()"
793  << ": opening output file with base name '" << baseFileName << "." << fileType
794  << "'"
795  << ", writeOver = " << writeOver
796  << std::endl;
797 #endif
798  // Verify parent directory exists (for cases when a user
800  // specifies a relative path for the desired output file). prudenci 2010/06/26
802  // std::cout << "checking " << baseFileName+"."+fileType << std::endl;
803  int irtrn = CheckFilePath((baseFileName+"."+fileType).c_str());
804  UQ_FATAL_TEST_MACRO(irtrn < 0,
805  m_worldRank,
806  "BaseEnvironment::openUnifiedOutputFile()",
807  "unable to verify output path");
808 
809  if (writeOver) {
811  // Write over an eventual pre-existing file
813  if (fileType == UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT) {
814  filePtrSet.ofsVar = new std::ofstream((baseFileName+"."+fileType).c_str(),
815  std::ofstream::out | std::ofstream::trunc);
816  }
817 #ifdef QUESO_HAS_HDF5
818  else if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
819  filePtrSet.h5Var = H5Fcreate((baseFileName+"."+fileType).c_str(),
820  H5F_ACC_TRUNC,
821  H5P_DEFAULT,
822  H5P_DEFAULT);
823  }
824 #endif
825  else {
826  UQ_FATAL_TEST_MACRO(true,
827  m_worldRank,
828  "BaseEnvironment::openUnifiedOutputFile(), writeOver=true",
829  "invalid file type");
830  }
831  if ((m_subDisplayFile) && (this->displayVerbosity() > 10)) { // output debug
832  *this->subDisplayFile() << "In BaseEnvironment::openUnifiedOutputFile()"
833  << ": just opened output file with base name '" << baseFileName << "." << fileType
834  << "'"
835  << ", writeOver = " << writeOver
836  << ", options 'out|trunc'"
837  << ", osfvar = " << filePtrSet.ofsVar
838  << std::endl;
839  }
840  }
841  else {
843  // Write at the end of an eventual pre-existing file
844  // 'm' and Ranger nodes behave differently on ofstream constructor... prudenci 2010/03/05
846  if (fileType == UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT) {
847  filePtrSet.ofsVar = new std::ofstream((baseFileName+"."+fileType).c_str(),
848  std::ofstream::out /*| std::ofstream::in*/ | std::ofstream::app);
849  }
850 #ifdef QUESO_HAS_HDF5
851  else if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
852  filePtrSet.h5Var = H5Fcreate((baseFileName+"."+fileType).c_str(), // TEMPORARY - FIX ME
853  H5F_ACC_TRUNC,
854  H5P_DEFAULT,
855  H5P_DEFAULT);
856  //UQ_FATAL_TEST_MACRO(true,
857  // m_worldRank,
858  // "BaseEnvironment::openUnifiedOutputFile(), writeOver=false",
859  // "hdf file type not supported yet");
860  }
861 #endif
862  else {
863  UQ_FATAL_TEST_MACRO(true,
864  m_worldRank,
865  "BaseEnvironment::openUnifiedOutputFile(), writeOver=false",
866  "invalid file type");
867  }
868  if ((m_subDisplayFile) && (this->displayVerbosity() > 10)) { // output debug
869  *this->subDisplayFile() << "In BaseEnvironment::openUnifiedOutputFile()"
870  << ": just opened output file with base name '" << baseFileName << "." << fileType
871  << "'"
872  << ", writeOver = " << writeOver
873  << ", options 'out|in|app'"
874  << ", osfvar = " << filePtrSet.ofsVar
875  << std::endl;
876  }
877  if ((filePtrSet.ofsVar == NULL ) ||
878  (filePtrSet.ofsVar->is_open() == false)) {
879 #if 0
880  std::cout << "Retrying 2..." << std::endl;
881 #endif
882  delete filePtrSet.ofsVar;
883  filePtrSet.ofsVar = new std::ofstream((baseFileName+"."+fileType).c_str(),
884  std::ofstream::out | std::ofstream::trunc);
885  if ((m_subDisplayFile) && (this->displayVerbosity() > 10)) { // output debug
886  *this->subDisplayFile() << "In BaseEnvironment::openUnifiedOutputFile()"
887  << ": just opened output file with base name '" << baseFileName << "." << fileType
888  << "'"
889  << ", writeOver = " << writeOver
890  << ", options 'out|trunc'"
891  << ", osfvar = " << filePtrSet.ofsVar
892  << std::endl;
893  }
894  }
895  }
896  if (filePtrSet.ofsVar == NULL) {
897  std::cerr << "In BaseEnvironment::openUnifiedOutputFile()"
898  << ": failed to open unified output file with base name '" << baseFileName << "." << fileType
899  << "'"
900  << std::endl;
901  }
902  UQ_FATAL_TEST_MACRO((filePtrSet.ofsVar && filePtrSet.ofsVar->is_open()) == false,
903  this->worldRank(),
904  "openUnifiedOutputFile()",
905  "failed to open output file");
906  //}
907  }
908 
909  return returnValue;
910 }
911 //-------------------------------------------------------
912 bool
914  const std::string& baseFileName,
915  const std::string& inputFileType,
916  const std::set<unsigned int>& allowedSubEnvIds,
917  FilePtrSetStruct& filePtrSet) const
918 {
919  std::string fileType(inputFileType);
920 #ifdef QUESO_HAS_HDF5
921  // Do nothing
922 #else
923  if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
924  if (m_subDisplayFile) {
925  *this->subDisplayFile() << "WARNING in BaseEnvironment::openInputFile()"
926  << ": file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
927  << "' has been requested, but this QUESO library has not been built with 'hdf5'"
928  << ". Code will therefore process the file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
929  << "' instead..."
930  << std::endl;
931  }
932  if (this->subRank() == 0) {
933  std::cerr << "WARNING in BaseEnvironment::openInputFile()"
934  << ": file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
935  << "' has been requested, but this QUESO library has not been built with 'hdf5'"
936  << ". Code will therefore process the file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
937  << "' instead..."
938  << std::endl;
939  }
941  }
942 #endif
943 
944  bool returnValue = true;
945  filePtrSet.ifsVar = NULL;
946  if ((baseFileName == UQ_ENV_FILENAME_FOR_NO_INPUT_FILE) ||
947  (allowedSubEnvIds.find(this->subId()) == allowedSubEnvIds.end() )) {
948  if ((m_subDisplayFile) && (this->displayVerbosity() >= 10)) {
949  *this->subDisplayFile() << "In BaseEnvironment::openInputFile()"
950  << ": no input file opened with base name '" << baseFileName << "." << fileType
951  << "'"
952  << std::endl;
953  }
954  returnValue = false;
955  }
956  else {
958  // Open file
960  if ((m_subDisplayFile) && (this->displayVerbosity() >= 10)) {
961  *this->subDisplayFile() << "In BaseEnvironment::openInputFile()"
962  << ": opening input file with base name '" << baseFileName << "." << fileType
963  << "'"
964  << std::endl;
965  }
966  if (this->subRank() == 0) {
968  // Verify parent directory exists (for cases when a user
969  // specifies a relative path for the desired output file). prudenci 2010/06/26
971  // std::cout << "checking " << baseFileName+"."+fileType << std::endl;
972  int irtrn = CheckFilePath((baseFileName+"."+fileType).c_str());
973  UQ_FATAL_TEST_MACRO(irtrn < 0,
974  m_worldRank,
975  "BaseEnvironment::openInputFile()",
976  "unable to verify input path");
977 
978  if (fileType == UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT) {
979  filePtrSet.ifsVar = new std::ifstream((baseFileName+"."+fileType).c_str(),
980  std::ofstream::in);
981  if ((filePtrSet.ifsVar == NULL) || (filePtrSet.ifsVar->is_open() == false)) {
982  std::cerr << "In BaseEnvironment::openInputFile()"
983  << ": failed to open input file with base name '" << baseFileName << "." << fileType
984  << "'"
985  << std::endl;
986  }
987  UQ_FATAL_TEST_MACRO((filePtrSet.ifsVar == NULL) || (filePtrSet.ifsVar->is_open() == false),
988  this->worldRank(),
989  "BaseEnvironment::openInputFile()",
990  "file with fileName could not be found");
991  }
992 #ifdef QUESO_HAS_HDF5
993  else if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
994  filePtrSet.h5Var = H5Fopen((baseFileName+"."+fileType).c_str(),
995  H5F_ACC_RDONLY,
996  H5P_DEFAULT);
997  }
998 #endif
999  else {
1000  UQ_FATAL_TEST_MACRO(true,
1001  m_worldRank,
1002  "BaseEnvironment::openInputFile()",
1003  "invalid file type");
1004  }
1005  }
1006  else {
1007  returnValue = false;
1008  }
1009  //this->subComm().Barrier(); // prudenci-2011-01-17
1010  }
1011 
1012  return returnValue;
1013 }
1014 //-------------------------------------------------------
1015 bool
1017  const std::string& baseFileName,
1018  const std::string& inputFileType,
1019  FilePtrSetStruct& filePtrSet) const
1020 {
1021  std::string fileType(inputFileType);
1022 #ifdef QUESO_HAS_HDF5
1023  // Do nothing
1024 #else
1025  if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
1026  if (m_subDisplayFile) {
1027  *this->subDisplayFile() << "WARNING in BaseEnvironment::openUnifiedInputFile()"
1028  << ": file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
1029  << "' has been requested, but this QUESO library has not been built with 'hdf5'"
1030  << ". Code will therefore process the file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
1031  << "' instead..."
1032  << std::endl;
1033  }
1034  if (this->subRank() == 0) {
1035  std::cerr << "WARNING in BaseEnvironment::openUnifiedInputFile()"
1036  << ": file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
1037  << "' has been requested, but this QUESO library has not been built with 'hdf5'"
1038  << ". Code will therefore process the file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
1039  << "' instead..."
1040  << std::endl;
1041  }
1043  }
1044 #endif
1045 
1046  bool returnValue = true;
1047  filePtrSet.ifsVar = NULL;
1048  if (baseFileName == UQ_ENV_FILENAME_FOR_NO_INPUT_FILE) {
1049  if ((m_subDisplayFile) && (this->displayVerbosity() >= 10)) {
1050  *this->subDisplayFile() << "In BaseEnvironment::openUnifiedInputFile()"
1051  << ": no input file opened with base name '" << baseFileName << "." << fileType
1052  << "'"
1053  << std::endl;
1054  }
1055  returnValue = false;
1056  }
1057  else {
1059  // Open file
1061  if ((m_subDisplayFile) && (this->displayVerbosity() >= 10)) {
1062  *this->subDisplayFile() << "In BaseEnvironment::openUnifiedInputFile()"
1063  << ": opening input file with base name '" << baseFileName << "." << fileType
1064  << "'"
1065  << std::endl;
1066  }
1067  if (this->subRank() == 0) { // Needed ???????? prudenci 2010-11-11
1069  // Verify parent directory exists (for cases when a user
1070  // specifies a relative path for the desired output file). prudenci 2010/06/26
1072  // std::cout << "checking " << baseFileName+"."+fileType << std::endl;
1073  int irtrn = CheckFilePath((baseFileName+"."+fileType).c_str());
1074  UQ_FATAL_TEST_MACRO(irtrn < 0,
1075  m_worldRank,
1076  "BaseEnvironment::openUnifiedInputFile()",
1077  "unable to verify input path");
1078 
1079  if (fileType == UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT) {
1080  filePtrSet.ifsVar = new std::ifstream((baseFileName+"."+fileType).c_str(),
1081  std::ofstream::in);
1082  if ((filePtrSet.ifsVar == NULL) || (filePtrSet.ifsVar->is_open() == false)) {
1083  std::cerr << "In BaseEnvironment::openUnifiedInputFile()"
1084  << ": failed to open input file with base name '" << baseFileName << "." << fileType
1085  << "'"
1086  << std::endl;
1087  }
1088  UQ_FATAL_TEST_MACRO((filePtrSet.ifsVar == NULL) || (filePtrSet.ifsVar->is_open() == false),
1089  this->worldRank(),
1090  "BaseEnvironment::openUnifiedInputFile()",
1091  "file with fileName could not be found");
1092  }
1093 #ifdef QUESO_HAS_HDF5
1094  else if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
1095  filePtrSet.h5Var = H5Fopen((baseFileName+"."+fileType).c_str(),
1096  H5F_ACC_RDONLY,
1097  H5P_DEFAULT);
1098  }
1099 #endif
1100  else {
1101  UQ_FATAL_TEST_MACRO(true,
1102  m_worldRank,
1103  "BaseEnvironment::openUnifiedInputFile()",
1104  "invalid file type");
1105  }
1106  }
1107  //else {
1108  // returnValue = false;
1109  //}
1110  //this->subComm().Barrier();
1111  }
1112 
1113  return returnValue;
1114 }
1115 //-------------------------------------------------------
1116 void
1118  FilePtrSetStruct& filePtrSet,
1119  const std::string& inputFileType) const
1120 {
1121  std::string fileType(inputFileType);
1122 #ifdef QUESO_HAS_HDF5
1123  // Do nothing
1124 #else
1125  if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
1126  if (m_subDisplayFile) {
1127  *this->subDisplayFile() << "WARNING in BaseEnvironment::closeFile()"
1128  << ": file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
1129  << "' has been requested, but this QUESO library has not been built with 'hdf5'"
1130  << ". Code will therefore process the file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
1131  << "' instead..."
1132  << std::endl;
1133  }
1134  if (this->subRank() == 0) {
1135  std::cerr << "WARNING in BaseEnvironment::closeFile()"
1136  << ": file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
1137  << "' has been requested, but this QUESO library has not been built with 'hdf5'"
1138  << ". Code will therefore process the file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
1139  << "' instead..."
1140  << std::endl;
1141  }
1143  }
1144 #endif
1145 
1146  if (fileType == UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT) {
1147  //filePtrSet.ofsVar->close(); // close() crashes on Mac; need to use delete(); why? prudenci 2010/June
1148  delete filePtrSet.ofsVar;
1149  filePtrSet.ofsVar = NULL;
1150 
1151  //filePtrSet.ifsVar->close();
1152  delete filePtrSet.ifsVar;
1153  filePtrSet.ifsVar = NULL;
1154  }
1155 #ifdef QUESO_HAS_HDF5
1156  else if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
1157  H5Fclose(filePtrSet.h5Var);
1158  }
1159 #endif
1160  else {
1161  UQ_FATAL_TEST_MACRO(true,
1162  m_worldRank,
1163  "BaseEnvironment::closeFile()",
1164  "invalid file type");
1165  }
1166 
1167  return;
1168 }
1169 //-------------------------------------------------------
1170 void
1172 {
1173  m_exceptionalCircumstance = value;
1174  return;
1175 }
1176 //-------------------------------------------------------
1177 bool
1179 {
1181 }
1182 
1183 
1184 //*****************************************************
1185 // Empty Environment
1186 //*****************************************************
1188  :
1189  BaseEnvironment("",NULL)
1190 {
1191 }
1192 //-------------------------------------------------------
1194 {
1195 }
1196 //-------------------------------------------------------
1197 void
1198 EmptyEnvironment::print(std::ostream& os) const
1199 {
1200  os.flush(); // just to avoid icpc warnings
1201  return;
1202 }
1203 
1204 //*****************************************************
1205 // Full Environment
1206 //*****************************************************
1208  RawType_MPI_Comm inputComm,
1209  const char* passedOptionsInputFileName,
1210  const char* prefix,
1211  const EnvOptionsValues* alternativeOptionsValues)
1212  :
1213  BaseEnvironment(passedOptionsInputFileName,alternativeOptionsValues)
1214 {
1215 #ifdef QUESO_MEMORY_DEBUGGING
1216  std::cout << "Entering FullEnv" << std::endl;
1217 #endif
1218 
1220  // Initialize "full" communicator -- Not necessarily MPI_COMM_WORLD
1222  int mpiRC = MPI_Comm_rank(inputComm,&m_worldRank);
1223  UQ_FATAL_TEST_MACRO(mpiRC != MPI_SUCCESS,
1225  "FullEnvironment::commonConstructor()",
1226  "failed to get world fullRank()");
1227 
1228  m_fullComm = new MpiComm(*this,inputComm);
1231 
1232  mpiRC = MPI_Comm_group(m_fullComm->Comm(), &m_fullGroup);
1233  UQ_FATAL_TEST_MACRO(mpiRC != MPI_SUCCESS,
1234  m_worldRank,
1235  "FullEnvironment::commonConstructor()",
1236  "failed MPI_Comm_group()");
1237 
1238  // saving old uncaught exception handler, invoking queso_terminate
1239  old_terminate_handler = std::set_terminate(queso_terminate_handler);
1240 
1241 #ifdef QUESO_MEMORY_DEBUGGING
1242  std::cout << "In FullEnv, finished dealing with MPI initially" << std::endl;
1243 #endif
1244 
1246  // Read options
1248  if (m_optionsInputFileName == "") {
1250  }
1251  else {
1252  m_allOptionsMap = new po::variables_map();
1253  m_allOptionsDesc = new po::options_description("Allowed options");
1254  m_optionsObj = new EnvironmentOptions(*this,prefix);
1255 
1257 
1259  }
1260 
1261 #ifdef QUESO_MEMORY_DEBUGGING
1262  std::cout << "In FullEnv, finished scanning options" << std::endl;
1263 #endif
1264 
1265  // Only display these messages if the user wants them
1266  // NOTE: This got moved below the Read Options section
1267  // because we need the options to be read to know what
1268  // the verbosity level is.
1269  if (this->displayVerbosity() > 0) {
1271  // Display main initial messages
1272  // 'std::cout' is for: main trace messages + synchronized trace messages + error messages prior to 'exit()' or 'abort()'
1274  /*int iRC = 0;*/
1275  /*iRC = */gettimeofday(&m_timevalBegin, NULL);
1276 
1277  if (m_fullRank == 0) {
1278  QUESO_version_print(std::cout);
1279  }
1280 
1281  if (m_fullRank == 0) {
1282  std::cout << "Beginning run at " << ctime(&m_timevalBegin.tv_sec)
1283  << std::endl;
1284  }
1285  }
1286 
1288  // Deal with multiple subEnvironments: create the sub communicators, one for each subEnvironment
1290  unsigned int numRanksPerSubEnvironment = m_fullCommSize/m_optionsObj->m_ov.m_numSubEnvironments;
1291 
1292  m_subId = m_fullRank/numRanksPerSubEnvironment;
1293  char tmpSubId[16];
1294  sprintf(tmpSubId,"%u",m_subId);
1295  m_subIdString = tmpSubId;
1296 
1298  m_optionsObj->m_ov.m_subDisplayAllowedSet.insert((unsigned int) m_subId);
1299  }
1300 
1301  std::vector<int> fullRanksOfMySubEnvironment(numRanksPerSubEnvironment,0);
1302  for (unsigned int i = 0; i < numRanksPerSubEnvironment; ++i) {
1303  fullRanksOfMySubEnvironment[i] = m_subId * numRanksPerSubEnvironment + i;
1304  }
1305 
1306  mpiRC = MPI_Group_incl(m_fullGroup, (int) numRanksPerSubEnvironment, &fullRanksOfMySubEnvironment[0], &m_subGroup);
1307  UQ_FATAL_TEST_MACRO(mpiRC != MPI_SUCCESS,
1308  m_worldRank,
1309  "FullEnvironment::commonConstructor()",
1310  "failed MPI_Group_incl() for a subEnvironment");
1311 
1312  RawType_MPI_Comm subRawComm;
1313  mpiRC = MPI_Comm_create(m_fullComm->Comm(), m_subGroup, &subRawComm);
1314  UQ_FATAL_TEST_MACRO(mpiRC != MPI_SUCCESS,
1315  m_worldRank,
1316  "FullEnvironment::commonConstructor()",
1317  "failed MPI_Comm_group() for a subEnvironment");
1318  m_subComm = new MpiComm(*this,subRawComm);
1319  m_subRank = m_subComm->MyPID();
1321 
1323  // Deal with multiple subEnvironments: create the self communicator
1326 
1328  // Deal with multiple subEnvironments: create the inter0 communicator
1330  std::vector<int> fullRanksOfInter0(m_optionsObj->m_ov.m_numSubEnvironments,0);
1331  for (unsigned int i = 0; i < m_optionsObj->m_ov.m_numSubEnvironments; ++i) {
1332  fullRanksOfInter0[i] = i * numRanksPerSubEnvironment;
1333  }
1334  mpiRC = MPI_Group_incl(m_fullGroup, (int) m_optionsObj->m_ov.m_numSubEnvironments, &fullRanksOfInter0[0], &m_inter0Group);
1335  UQ_FATAL_TEST_MACRO(mpiRC != MPI_SUCCESS,
1336  m_worldRank,
1337  "FullEnvironment::commonConstructor()",
1338  "failed MPI_Group_incl() for inter0");
1339  RawType_MPI_Comm inter0RawComm;
1340  mpiRC = MPI_Comm_create(m_fullComm->Comm(), m_inter0Group, &inter0RawComm);
1341  UQ_FATAL_TEST_MACRO(mpiRC != MPI_SUCCESS,
1342  m_worldRank,
1343  "FullEnvironment::commonConstructor()",
1344  "failed MPI_Comm_group() for inter0");
1345  if (m_fullRank%numRanksPerSubEnvironment == 0) {
1346  m_inter0Comm = new MpiComm(*this,inter0RawComm);
1349  }
1350 
1352  // This situation has been already taken care of above
1353  }
1355  if (m_inter0Rank >= 0) {
1356  m_optionsObj->m_ov.m_subDisplayAllowedSet.insert((unsigned int) m_subId);
1357  }
1358  }
1359 
1360 
1362  // Open "screen" file
1364  bool openFile = false;
1365  if ((m_subRank == 0 ) &&
1368  openFile = true;
1369  }
1370 
1371  if (openFile && m_worldRank == 0) {
1373  // Verify parent directory exists (for cases when a user
1374  // specifies a relative path for the desired output file).
1376  int irtrn = CheckFilePath((m_optionsObj->m_ov.m_subDisplayFileName+"_sub"+m_subIdString+".txt").c_str());
1377  UQ_FATAL_TEST_MACRO(irtrn < 0,
1378  m_worldRank,
1379  "Environment::constructor()",
1380  "unable to verify output path");
1381  }
1382 
1384  // Ensure that rank 0 has created path, if necessary, before other tasks use it
1386  m_fullComm->Barrier();
1387 
1388  if (openFile) {
1390  // Always write over an eventual pre-existing file
1392  m_subDisplayFile = new std::ofstream((m_optionsObj->m_ov.m_subDisplayFileName+"_sub"+m_subIdString+".txt").c_str(),
1393  std::ofstream::out | std::ofstream::trunc);
1394  UQ_FATAL_TEST_MACRO((m_subDisplayFile && m_subDisplayFile->is_open()) == false,
1395  m_worldRank,
1396  "Environment::constructor()",
1397  "failed to open sub screen file");
1398 
1400 
1401  *m_subDisplayFile << "Beginning run at " << ctime(&m_timevalBegin.tv_sec)
1402  << std::endl;
1403  }
1404 
1406  // Debug message related to subEnvironments
1408  if (this->displayVerbosity() >= 2) {
1409  for (int i = 0; i < m_fullCommSize; ++i) {
1410  if (i == m_fullRank) {
1411  //std::cout << "In FullEnvironment::commonConstructor()"
1412  std::cout << "MPI node of worldRank " << m_worldRank
1413  << " has fullRank " << m_fullRank
1414  << ", belongs to subEnvironment of id " << m_subId
1415  << ", and has subRank " << m_subRank
1416  << std::endl;
1417 
1418  std::cout << "MPI node of worldRank " << m_worldRank
1419  << " belongs to sub communicator with full ranks";
1420  for (unsigned int j = 0; j < fullRanksOfMySubEnvironment.size(); ++j) {
1421  std::cout << " " << fullRanksOfMySubEnvironment[j];
1422  }
1423  std::cout << "\n";
1424 
1425  if (m_inter0Comm) {
1426  std::cout << "MPI node of worldRank " << m_worldRank
1427  << " also belongs to inter0 communicator with full ranks";
1428  for (unsigned int j = 0; j < fullRanksOfInter0.size(); ++j) {
1429  std::cout << " " << fullRanksOfInter0[j];
1430  }
1431  std::cout << ", and has inter0Rank " << m_inter0Rank;
1432  }
1433  std::cout << "\n";
1434 
1435  std::cout << std::endl;
1436  }
1437  m_fullComm->Barrier();
1438  }
1439  //if (this->fullRank() == 0) std::cout << "Sleeping 3 seconds..."
1440  // << std::endl;
1441  //sleep(3);
1442  }
1443 
1445  // Deal with seed
1447  if (m_optionsObj->m_ov.m_rngType == "gsl") {
1450  }
1451  else if (m_optionsObj->m_ov.m_rngType == "boost") {
1454  }
1455  else {
1456  std::cerr << "In Environment::constructor()"
1457  << ": rngType = " << m_optionsObj->m_ov.m_rngType
1458  << std::endl;
1459  UQ_FATAL_TEST_MACRO(true,
1460  m_worldRank,
1461  "Environment::constructor()",
1462  "the requested 'rngType' is not supported yet");
1463  }
1464 
1466  // Leave commonConstructor()
1468  m_fullComm->Barrier();
1469  m_fullEnvIsReady = true;
1470 
1471  if ((m_subDisplayFile) && (this->displayVerbosity() >= 5)) {
1472  *m_subDisplayFile << "Done with initializations at FullEnvironment::commonConstructor()"
1473  << std::endl;
1474  }
1475 
1476  return;
1477 }
1478 //-------------------------------------------------------
1480 {
1481 }
1482 //-------------------------------------------------------
1483 void
1484 FullEnvironment::print(std::ostream& os) const
1485 {
1486  os.flush(); // just to avoid icpc warnings
1487  return;
1488 }
1489 
1491 {
1492  int mpi_initialized;
1493  MPI_Initialized (&mpi_initialized);
1494 
1495  if (mpi_initialized)
1496  {
1497  //MPI_Abort(m_fullComm->Comm(), 1);
1498  MPI_Abort(MPI_COMM_WORLD, 1);
1499  }
1500  else
1501  {
1502  // The system terminate_handler may do useful things like printing
1503  // uncaught exception information, or the user may have created
1504  // their own terminate handler that we want to call.
1506  }
1507  exit(1);
1508 }
1509 
1510 
1511 //-------------------------------------------------------
1512 void
1514 {
1515  std::ifstream* ifs = new std::ifstream(m_optionsInputFileName.c_str());
1516  if (ifs->is_open()) {
1517  //ifs->close();
1518  delete ifs;
1519  }
1520  else {
1521  if (m_fullRank == 0) std::cout << "An invalid input file has been passed to the 'environment' class constructor!"
1522  << ": name of file is '" << m_optionsInputFileName.c_str() << "'"
1523  << std::endl;
1524 
1525  if (m_fullRank == 0) std::cout << "\nThis is a help message of the QUESO library."
1526  << "\nAn application using the QUESO library shall be executed by typing"
1527  << "\n '<eventual mpi commands and options> <Application> <InputFile>'"
1528  << "\nin the command line."
1529  << "\n"
1530  << std::endl;
1531  queso_error();
1532  }
1533 
1534  return;
1535 }
1536 
1537 } // End namespace QUESO
const std::string & subIdString() const
Access to the attribute m_subIdString; which stores the string for the sub-environment, and it will be used, for instance, to create the output files for each sub-environment.
Definition: Environment.C:335
#define queso_error()
Definition: asserts.h:87
bool openInputFile(const std::string &fileName, const std::string &fileType, const std::set< unsigned int > &allowedSubEnvIds, FilePtrSetStruct &filePtrSet) const
Opens an input file.
Definition: Environment.C:913
std::string m_subIdString
Definition: Environment.h:367
The QUESO MPI Communicator Class.
Definition: MpiComm.h:75
po::options_description * m_allOptionsDesc
Definition: Environment.h:363
#define UQ_ENV_FILENAME_FOR_NO_INPUT_FILE
#define QUESO_HDF5_DIR
Definition: queso.h:56
int subRank() const
Access function for sub-rank.
Definition: Environment.C:263
#define QUESO_BUILD_DATE
Definition: queso.h:41
#define QUESO_BUILD_HOST
Definition: queso.h:40
std::string m_platformName
Platform name.
Definition: Defines.h:160
std::terminate_handler old_terminate_handler
Definition: Environment.C:124
~FilePtrSetStruct()
Destructor.
Definition: Environment.C:116
const RngBase * rngObject() const
Access to the RNG object.
Definition: Environment.C:466
std::ofstream * ofsVar
Provides a stream interface to write data to files.
Definition: Environment.h:75
unsigned int syncVerbosity() const
Access function to private attribute m_syncVerbosity.
Definition: Environment.C:446
bool openUnifiedInputFile(const std::string &fileName, const std::string &fileType, FilePtrSetStruct &filePtrSet) const
Opens the unified input file.
Definition: Environment.C:1016
struct timeval m_timevalBegin
Definition: Environment.h:383
int inter0Rank() const
Returns the process inter0 rank.
Definition: Environment.C:289
void scanOptionsValues()
Scans option values from input file.
#define QUESO_CXX
Definition: queso.h:47
int seed() const
Sets the seed.
Definition: RngBase.C:54
std::string subDisplayFileName() const
Access function for m_subDisplayFileName (displays filename on stream).
Definition: Environment.C:311
void resetSeed(int newSeedOption)
Reset RNG seed.
Definition: Environment.C:478
int worldRank() const
Returns the process world rank.
Definition: Environment.C:235
#define UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT
Definition: Defines.h:89
EmptyEnvironment()
Default constructor. Does nothing.
Definition: Environment.C:1187
unsigned int checkingLevel() const
Access function to private attribute m_checkingLevel.
Definition: Environment.C:456
const MpiComm & subComm() const
Access function for MpiComm sub communicator.
Definition: Environment.C:269
BaseEnvironment(const char *passedOptionsInputFileName, const EnvOptionsValues *alternativeOptionsValues)
Default constructor.
Definition: Environment.C:130
int NumProc() const
Returns total number of processes.
Definition: MpiComm.C:121
TODO: Base class for basic PDFs using Gsl library.
Definition: BasicPdfsGsl.h:43
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:305
void print(std::ostream &os) const
Definition: Environment.C:1198
const MpiComm & fullComm() const
Access function for MpiComm full communicator.
Definition: Environment.C:247
const int UQ_INVALID_INTERNAL_STATE_RC
Definition: Defines.h:82
virtual void resetSeed(int newSeed)
Resets the seed with value newSeed.
Definition: RngBase.C:60
#define QUESO_BUILD_USER
Definition: queso.h:38
void QUESO_version_print(std::ostream &os)
Definition: Environment.C:47
#define QUESO_GSL_DIR
Definition: queso.h:53
#define QUESO_BUILD_ARCH
Definition: queso.h:39
virtual ~BaseEnvironment()
Destructor.
Definition: Environment.C:169
#define QUESO_TRILINOS_DIR
Definition: queso.h:52
po::variables_map & allOptionsMap() const
Definition: Environment.C:368
const int UQ_UNAVAILABLE_RANK
Definition: Defines.h:74
void resetIdentifyingString(const std::string &newString) const
Reset private attribute m_identifyingString with the value newString.
Definition: Environment.C:503
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
Definition: Environment.h:187
std::string identifyingString() const
Access function to private attribute m_identifyingString: identifying string.
Definition: Environment.C:497
bool m_subDisplayAllowAll
Allows (or not) all sub-environments to write to output file.
Definition: Defines.h:134
void print(std::ostream &os) const
Sends the environment options to the stream.
Definition: Environment.C:1484
std::string m_optionsInputFileName
Definition: Environment.h:361
bool m_subDisplayAllowInter0
Allows (or not) all inter0 nodes to write to output file.
Definition: Defines.h:137
std::ofstream * m_subDisplayFile
Definition: Environment.h:380
std::string platformName() const
Access to the platform name.
Definition: Environment.C:491
#define QUESO_MAJOR_VERSION
Definition: queso.h:34
#define QUESO_BUILD_VERSION
Definition: queso.h:42
#define QUESO_GLPK_DIR
Definition: queso.h:55
po::variables_map * m_allOptionsMap
Definition: Environment.h:364
BasicPdfsBase * m_basicPdfs
Definition: Environment.h:382
#define QUESO_LIB_VERSION
Definition: queso.h:44
void Barrier() const
Pause every process in *this communicator until all the processes reach this point.
Definition: MpiComm.C:143
void closeFile(FilePtrSetStruct &filePtrSet, const std::string &fileType) const
Closes the file.
Definition: Environment.C:1117
std::ifstream * ifsVar
Provides a stream interface to read data from files.
Definition: Environment.h:78
std::string optionsInputFileName() const
Access to the attribute m_optionsInputFileName, which stores the name of the input file passed by the...
Definition: Environment.C:341
unsigned int m_numSubEnvironments
Number of sub-environments.
Definition: Defines.h:128
Struct for handling data input and output from files.
Definition: Environment.h:66
~FullEnvironment()
Destructor.
Definition: Environment.C:1479
int QUESO_get_numeric_version()
Definition: Environment.C:77
void setOptionsInputFileAccessState(bool newState) const
Definition: Environment.C:352
RawType_MPI_Group m_inter0Group
Definition: Environment.h:375
FilePtrSetStruct()
Struct constructor.
Definition: Environment.C:109
MPI_Comm RawType_MPI_Comm
Definition: MpiComm.h:38
unsigned int m_subId
Definition: Environment.h:366
int m_seed
Seed of the random number generator.
Definition: Defines.h:157
MPI_Group RawType_MPI_Group
Definition: MpiComm.h:39
TODO: Base class for basic PDFs using Boost library.
#define QUESO_MINOR_VERSION
Definition: queso.h:35
bool fullEnvIsReady() const
Returns whether the full environment class is ready (constructor has successfully been called)...
Definition: Environment.C:229
unsigned int m_syncVerbosity
Synchronized verbosity.
Definition: Defines.h:146
const BasicPdfsBase * basicPdfs() const
Access to Basic PDFs.
Definition: Environment.C:485
void setExceptionalCircumstance(bool value) const
Set an exceptional circumstance.
Definition: Environment.C:1171
#define UQ_ENV_FILENAME_FOR_NO_OUTPUT_FILE
void readOptionsInputFile()
Checks the options input file and reads the options.
Definition: Environment.C:1513
int fullRank() const
Returns the process full rank.
Definition: Environment.C:241
unsigned int numSubEnvironments() const
Access function to the number of sub-environments.
Definition: Environment.C:319
This class provides a suite options one can pass to a QUESO environment.
Definition: Defines.h:105
#define QUESO_MICRO_VERSION
Definition: queso.h:36
int MyPID() const
Return my process ID.
Definition: MpiComm.C:112
const MpiComm & selfComm() const
Access function for MpiComm self-communicator.
Definition: Environment.C:279
EnvironmentOptions * m_optionsObj
Definition: Environment.h:387
RawType_MPI_Group subGroup() const
Access function for sub-group.
Definition: Environment.C:257
std::set< unsigned int > m_subDisplayAllowedSet
Sub-environments that will write to output.
Definition: Defines.h:140
FullEnvironment(RawType_MPI_Comm inputComm, const char *passedOptionsInputFileName, const char *prefix, const EnvOptionsValues *alternativeOptionsValues)
Default constructor.
Definition: Environment.C:1207
#define QUESO_GRVY_DIR
Definition: queso.h:54
#define UQ_FILE_EXTENSION_FOR_HDF_FORMAT
Definition: Defines.h:90
int seed() const
Access to the RNG seed.
Definition: Environment.C:472
#define RawValue_MPI_COMM_SELF
Definition: MpiComm.h:43
int CheckFilePath(const char *path)
TODO: Base class for basic PDFs (via either GSL or Boost).
Definition: BasicPdfsBase.h:44
unsigned int m_checkingLevel
Checking level.
Definition: Defines.h:149
RawType_MPI_Group m_fullGroup
Definition: Environment.h:359
Class for random number generation (base class for either GSL or Boost RNG).
Definition: RngBase.h:45
bool openUnifiedOutputFile(const std::string &fileName, const std::string &fileType, bool writeOver, FilePtrSetStruct &filePtrSet) const
Opens a unified output file, that will contain data from all sub-environments.
Definition: Environment.C:733
unsigned int displayVerbosity() const
Definition: Environment.C:436
void queso_terminate_handler()
Function for unhandled exceptions in Queso.
Definition: Environment.C:1490
~EmptyEnvironment()
Destructor.
Definition: Environment.C:1193
const MpiComm & inter0Comm() const
Access function for MpiComm inter0-communicator.
Definition: Environment.C:295
#define UQ_FATAL_TEST_MACRO(test, givenRank, where, what)
Definition: Defines.h:223
EnvOptionsValues m_alternativeOptionsValues
Definition: Environment.h:386
This class reads options one can pass to a QUESO environment through an input file.
unsigned int subId() const
Access function to the number of each sub-environment Id: m_subId.
Definition: Environment.C:329
std::string m_rngType
Type of the random number generator.
Definition: Defines.h:152
unsigned int m_displayVerbosity
Verbosity.
Definition: Defines.h:143
BaseEnvironment & operator=(const BaseEnvironment &rhs)
Assignment operator. It should not be used be the user.
Definition: Environment.C:219
bool openOutputFile(const std::string &fileName, const std::string &fileType, const std::set< unsigned int > &allowedSubEnvIds, bool writeOver, FilePtrSetStruct &filePtrSet) const
Opens an output file for each sub-environment that was chosen to send data to the file...
Definition: Environment.C:516
std::string m_subDisplayFileName
Output filename for sub-screen writing.
Definition: Defines.h:131
std::string m_identifyingString
Identifying string.
Definition: Defines.h:163
bool exceptionalCircumstance() const
Decides whether there is an exceptional circumstance.
Definition: Environment.C:1178
#define QUESO_LIB_RELEASE
Definition: queso.h:45
EnvOptionsValues m_ov
Instance of EnvOptionsValues, a class with default values for QUESO environment.
RawType_MPI_Group m_subGroup
Definition: Environment.h:368
void scanInputFileForMyOptions(const po::options_description &optionsDesc) const
This method scans the input file provided by the user to QUESO.
Definition: Environment.C:378
#define QUESO_CXXFLAGS
Definition: queso.h:48
RawType_MPI_Comm Comm() const
Extract MPI Communicator from a MpiComm object.
Definition: MpiComm.C:103

Generated on Thu Apr 23 2015 19:26:15 for queso-0.51.1 by  doxygen 1.8.5