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

Generated on Fri Jun 17 2016 14:17:40 for queso-0.55.0 by  doxygen 1.8.5