queso-0.54.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 {
116 }
117 
119 {
120 }
121 
122  //
123  // queso terminate handler will be invoked for unhandled exceptions
124  // needs to be invoked in FullEnvironment constructor
125  //
126  std::terminate_handler old_terminate_handler;
127 
128 //*****************************************************
129 // Base class
130 //*****************************************************
131 // Default constructor --------------------------------
133  const char* passedOptionsInputFileName,
134  EnvOptionsValues* alternativeOptionsValues)
135  :
136  m_fullEnvIsReady (false),
137  m_worldRank (-1),
138  m_fullComm (NULL),
139  m_fullRank (-1),
140  m_fullCommSize (1),
141  m_optionsInputFileName (""),
142  m_optionsInputFileAccessState(true),
143  m_allOptionsDesc (NULL),
144  m_allOptionsMap (NULL),
145  m_subComm (NULL),
146  m_subRank (-1),
147  m_subCommSize (1),
148  m_selfComm (NULL),
149  m_inter0Comm (NULL),
150  m_inter0Rank (-1),
151  m_inter0CommSize (1),
152  m_subDisplayFile (NULL),
153  m_rngObject (NULL),
154  m_basicPdfs (NULL),
155  m_exceptionalCircumstance (false),
156  m_optionsObj (alternativeOptionsValues)
157 {
158  if (passedOptionsInputFileName) m_optionsInputFileName = passedOptionsInputFileName;
159 }
160 // Destructor -------------------------------------------
162 {
163  //if (m_subDisplayFile) {
164  // *m_subDisplayFile << "Entering BaseEnvironment::destructor()"
165  // << std::endl;
166  //}
167 
168  struct timeval timevalNow;
169  /*int iRC = 0;*/
170  /*iRC = */gettimeofday(&timevalNow, NULL);
171 
172  if( this->displayVerbosity() > 0 )
173  {
174  if (m_subDisplayFile) {
175  *m_subDisplayFile << "Ending run at " << ctime(&timevalNow.tv_sec)
176  << "Total run time = " << timevalNow.tv_sec - m_timevalBegin.tv_sec
177  << " seconds"
178  << std::endl;
179  }
180 
181  if (m_fullRank == 0) {
182  std::cout << "Ending run at " << ctime(&timevalNow.tv_sec)
183  << "Total run time = " << timevalNow.tv_sec - m_timevalBegin.tv_sec
184  << " seconds"
185  << std::endl;
186  }
187  }
188 
189  if (m_allOptionsMap) {
190  delete m_allOptionsMap;
191  delete m_allOptionsDesc;
192  }
193 
194  if (m_basicPdfs) delete m_basicPdfs;
195  if (m_rngObject) delete m_rngObject;
196 
197  //if (m_subDisplayFile) {
198  // *m_subDisplayFile << "Leaving BaseEnvironment::destructor()"
199  // << std::endl;
200  //}
201 
203  if (m_inter0Comm ) delete m_inter0Comm;
204  if (m_selfComm ) delete m_selfComm;
205  if (m_subComm ) delete m_subComm;
206  if (m_fullComm ) delete m_fullComm;
207 }
208 // Environment, Communicator and Options Input File methods
209 bool
211 {
212  return m_fullEnvIsReady;
213 }
214 //-------------------------------------------------------
215 int
217 {
218  return m_worldRank;
219 }
220 //-------------------------------------------------------
221 int
223 {
224  return m_fullRank;
225 }
226 //-------------------------------------------------------
227 const MpiComm&
229 {
230  queso_require_msg(m_fullComm, "m_fullComm variable is NULL");
231  return *m_fullComm;
232 }
233 //-------------------------------------------------------
236 {
237  return m_subGroup;
238 }
239 //-------------------------------------------------------
240 int
242 {
243  return m_subRank;
244 }
245 //-------------------------------------------------------
246 const MpiComm&
248 {
249  queso_require_msg(m_subComm, "m_subComm variable is NULL");
250  return *m_subComm;
251 }
252 //-------------------------------------------------------
253 const MpiComm&
255 {
256  queso_require_msg(m_selfComm, "m_selfComm variable is NULL");
257  return *m_selfComm;
258 }
259 //-------------------------------------------------------
260 int
262 {
263  return m_inter0Rank;
264 }
265 //-------------------------------------------------------
266 const MpiComm&
268 {
269  queso_require_msg(m_inter0Comm, "m_inter0Comm variable is NULL");
270  return *m_inter0Comm;
271 }
272 //-------------------------------------------------------
273 std::ofstream*
275 {
276  return m_subDisplayFile;
277 }
278 //-------------------------------------------------------
279 std::string
281 {
282  if (m_optionsObj == NULL) return ".";
283 
285 }
286 //-------------------------------------------------------
287 unsigned int
289 {
290  queso_require_msg(m_optionsObj, "m_optionsObj variable is NULL");
292 }
293 //-------------------------------------------------------
294 unsigned int
296 {
297  return m_subId;
298 }
299 //-------------------------------------------------------
300 const std::string&
302 {
303  return m_subIdString;
304 }
305 //-------------------------------------------------------
306 std::string
308 {
310  return m_optionsInputFileName;
311  }
312  else {
313  return "";
314  }
315 }
316 //-------------------------------------------------------
317 void
319 {
321 
322  return;
323 }
324 //-------------------------------------------------------
325 #ifdef UQ_USES_COMMAND_LINE_OPTIONS
326 const boost::program_options::options_description&
327 BaseEnvironment::allOptionsDesc() const
328 {
330 
331  return *m_allOptionsDesc;
332 }
333 #endif
334 //-------------------------------------------------------
335 boost::program_options::variables_map&
337 {
339 
340  queso_require_msg(m_allOptionsMap, "m_allOptionsMap variable is NULL");
341  return *m_allOptionsMap;
342 }
343 //-------------------------------------------------------
344 void
345 BaseEnvironment::scanInputFileForMyOptions(const boost::program_options::options_description& optionsDesc) const
346 {
348 
349 #ifdef UQ_USES_COMMAND_LINE_OPTIONS
350  // If you want to use command line options, the following line does *not* work outside 'main.C',
351  // e.g., in the constructor of FullEnvironment:
352  // Line: boost::program_options::store(boost::program_options::parse_command_line(argc, argv, *m_allOptionsDesc), *m_allOptionsMap);
353  //
354  // Instead, put the following three lines *immediately after* instantianting the UQ environment
355  // variable "FullEnvironment* env":
356  // Line 1: boost::program_options::store(boost::program_options::parse_command_line(argc, argv, env->allOptionsDesc()), env->allOptionsMap());
357  // Line 2: boost::program_options::notify(env->allOptionsMap());
358  // Line 3: env->getMyOptionValues();
359 #endif
360 
361 #ifdef QUESO_MEMORY_DEBUGGING
362  std::cout << "Entering BaseEnv::scanInputFileForMyOptions()" << std::endl;
363 #endif
364 
365  queso_require_msg(m_allOptionsDesc, "m_allOptionsDesc variable is NULL");
366  m_allOptionsDesc->add(optionsDesc);
367  //if (m_subDisplayFile) {
368  // *m_subDisplayFile << *m_allOptionsDesc
369  // << std::endl;
370  //}
371 
372  queso_require_not_equal_to_msg(m_optionsInputFileName, "", "m_optionsInputFileName is 'nothing'");
373  //std::ifstream ifs(m_optionsInputFileName.c_str());
374  std::ifstream* ifs = new std::ifstream(m_optionsInputFileName.c_str());
375 #ifdef QUESO_MEMORY_DEBUGGING
376  std::cout << "in BaseEnv::scanInputFileForMyOptions(), before store(a)" << std::endl;
377 #endif
378 
379  queso_require_msg(m_allOptionsMap, "m_allOptionsMap variable is NULL");
380  boost::program_options::store(boost::program_options::parse_config_file(*ifs, *m_allOptionsDesc, true), *m_allOptionsMap);
381 #ifdef QUESO_MEMORY_DEBUGGING
382  std::cout << "in BaseEnv::scanInputFileForMyOptions(), after store(a)" << std::endl;
383 #endif
384  boost::program_options::notify(*m_allOptionsMap);
385 
386  //ifs.close();
387  delete ifs;
388 #ifdef QUESO_MEMORY_DEBUGGING
389  std::cout << "Leaving BaseEnv::scanInputFileForMyOptions()" << std::endl;
390 #endif
391 
392  return;
393 }
394 //-----------------------------------------------------
395 unsigned int
397 {
398  queso_require_msg(m_optionsObj, "m_optionsObj variable is NULL");
400 }
401 //-------------------------------------------------------
402 unsigned int
404 {
405  queso_require_msg(m_optionsObj, "m_optionsObj variable is NULL");
407 }
408 //-------------------------------------------------------
409 unsigned int
411 {
412  queso_require_msg(m_optionsObj, "m_optionsObj variable is NULL");
414 }
415 //-------------------------------------------------------
416 const RngBase*
418 {
419  return m_rngObject;
420 }
421 //-------------------------------------------------------
422 int
424 {
425  return m_rngObject->seed();
426 }
427 //-------------------------------------------------------
428 void
429 BaseEnvironment::resetSeed(int newSeedOption)
430 {
431  m_rngObject->resetSeed(newSeedOption);
432  return;
433 }
434 //-------------------------------------------------------
435 const BasicPdfsBase*
437 {
438  return m_basicPdfs;
439 }
440 //-------------------------------------------------------
441 std::string
443 {
445 }
446 //-------------------------------------------------------
447 std::string
449 {
451 }
452 //-------------------------------------------------------
453 void
454 BaseEnvironment::resetIdentifyingString(const std::string& newString)
455 {
456  m_optionsObj->m_identifyingString = newString;
457  return;
458 }
459 //-------------------------------------------------------
460 struct timeval
461 BaseEnvironment::timevalBegin() const
462 {
463  return m_timevalBegin;
464 }
465 //-------------------------------------------------------
466 bool
468  const std::string& baseFileName,
469  const std::string& inputFileType,
470  const std::set<unsigned int>& allowedSubEnvIds,
471  bool writeOver,
472  FilePtrSetStruct& filePtrSet) const
473 {
474  std::string fileType(inputFileType);
475 #ifdef QUESO_HAS_HDF5
476  // Do nothing
477 #else
478  if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
479  if (m_subDisplayFile) {
480  *this->subDisplayFile() << "WARNING in BaseEnvironment::openOutputFile()"
481  << ": file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
482  << "' has been requested, but this QUESO library has not been built with 'hdf5'"
483  << ". Code will therefore process the file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
484  << "' instead..."
485  << std::endl;
486  }
487  if (this->subRank() == 0) {
488  std::cerr << "WARNING in BaseEnvironment::openOutputFile()"
489  << ": file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
490  << "' has been requested, but this QUESO library has not been built with 'hdf5'"
491  << ". Code will therefore process the file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
492  << "' instead..."
493  << std::endl;
494  }
496  }
497 #endif
498 
499  bool returnValue = true;
500  filePtrSet.ofsVar = NULL;
501  if ((baseFileName == UQ_ENV_FILENAME_FOR_NO_OUTPUT_FILE) ||
502  (allowedSubEnvIds.find(this->subId()) == allowedSubEnvIds.end() )) {
503  if ((m_subDisplayFile) && (this->displayVerbosity() > 10)) { // output debug
504  *this->subDisplayFile() << "In BaseEnvironment::openOutputFile()"
505  << ", subId = " << this->subId()
506  << ": no output file opened with base name '" << baseFileName << "." << fileType
507  << "'"
508  << ", writeOver = " << writeOver
509  << std::endl;
510  }
511  returnValue = false;
512  }
513  else {
515  // Open file
517  if ((m_subDisplayFile) && (this->displayVerbosity() > 10)) { // output debug
518  *this->subDisplayFile() << "In BaseEnvironment::openOutputFile()"
519  << ", subId = " << this->subId()
520  << ": opening output file with base name '" << baseFileName << "." << fileType
521  << "'"
522  << ", writeOver = " << writeOver
523  << std::endl;
524  }
525 
526  if (this->subRank() == 0) {
527 #if 0
528  std::cout << "In BaseEnvironment::openOutputFile()"
529  << ": opening output file with base name '" << baseFileName << "." << fileType
530  << "'"
531  << ", writeOver = " << writeOver
532  << std::endl;
533 #endif
534 
536  // Verify parent directory exists (for cases when a user
537  // specifies a relative path for the desired output file).
539  // std::cout << "checking " << baseFileName+"_sub"+this->subIdString()+"."+fileType << std::endl;
540  if ((m_subDisplayFile) && (this->displayVerbosity() > 10)) { // output debug
541  *this->subDisplayFile() << "In BaseEnvironment::openOutputFile()"
542  << ", subId = " << this->subId()
543  << ", trying to open output file with base name '" << baseFileName << "." << fileType
544  << "'"
545  << ", writeOver = " << writeOver
546  << ": calling CheckFilePath()..."
547  << std::endl;
548  }
549  int irtrn = CheckFilePath((baseFileName+"_sub"+this->subIdString()+"."+fileType).c_str());
550  if ((m_subDisplayFile) && (this->displayVerbosity() > 10)) { // output debug
551  *this->subDisplayFile() << "In BaseEnvironment::openOutputFile()"
552  << ", subId = " << this->subId()
553  << ", trying to open output file with base name '" << baseFileName << "." << fileType
554  << "'"
555  << ", writeOver = " << writeOver
556  << ": returned from CheckFilePath() with irtrn = " << irtrn
557  << std::endl;
558  }
559  queso_require_greater_equal_msg(irtrn, 0, "unable to verify output path");
560 
561  if (writeOver) {
563  // Write over an eventual pre-existing file
565  if ((fileType == UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT) ||
566  (fileType == UQ_FILE_EXTENSION_FOR_TXT_FORMAT)) {
567  filePtrSet.ofsVar = new std::ofstream((baseFileName+"_sub"+this->subIdString()+"."+fileType).c_str(),
568  std::ofstream::out | std::ofstream::trunc);
569  }
570  else if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
571  queso_error_msg("hdf file type not supported yet");
572  }
573  else {
574  queso_error_msg("invalid file type");
575  }
576  if ((m_subDisplayFile) && (this->displayVerbosity() > 10)) { // output debug
577  *this->subDisplayFile() << "In BaseEnvironment::openOutputFile()"
578  << ": just opened output file with base name '" << baseFileName << "." << fileType
579  << "'"
580  << ", writeOver = " << writeOver
581  << ", options 'out|trunc'"
582  << ", osfvar = " << filePtrSet.ofsVar
583  << std::endl;
584  }
585  }
586  else {
588  // Write at the end of an eventual pre-existing file
590  if ((fileType == UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT) ||
591  (fileType == UQ_FILE_EXTENSION_FOR_TXT_FORMAT)) {
592 #if 0
593  filePtrSet.ofsVar = new std::ofstream((baseFileName+"_sub"+this->subIdString()+"."+fileType).c_str(),
594  std::ofstream::in);
595  std::cout << "filePtrSet.ofsVar(1) = " << filePtrSet.ofsVar << std::endl;
596  if (filePtrSet.ofsVar) std::cout << "filePtrSet.ofsVar(1)->is_open() = " << filePtrSet.ofsVar->is_open() << std::endl;
597  if (filePtrSet.ofsVar) delete filePtrSet.ofsVar;
598 #endif
599  // 'm' and Ranger nodes behave differently on ofstream constructor... prudenci 2010/03/05
600  filePtrSet.ofsVar = new std::ofstream((baseFileName+"_sub"+this->subIdString()+"."+fileType).c_str(),
601  std::ofstream::out /*| std::ofstream::in*/ | std::ofstream::app);
602  }
603  else if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
604  queso_error_msg("hdf file type not supported yet");
605  }
606  else {
607  queso_error_msg("invalid file type");
608  }
609  if ((m_subDisplayFile) && (this->displayVerbosity() > 10)) { // output debug
610  *this->subDisplayFile() << "In BaseEnvironment::openOutputFile()"
611  << ": just opened output file with base name '" << baseFileName << "." << fileType
612  << "'"
613  << ", writeOver = " << writeOver
614  << ", options 'out|in|app'"
615  << ", osfvar = " << filePtrSet.ofsVar
616  << std::endl;
617  }
618 
619  if (fileType == UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT) {
620  //std::cout << "filePtrSet.ofsVar(2) = " << filePtrSet.ofsVar << std::endl;
621  //if (filePtrSet.ofsVar) std::cout << "filePtrSet.ofsVar(2)->is_open() = " << filePtrSet.ofsVar->is_open() << std::endl;
622  if ((filePtrSet.ofsVar == NULL ) ||
623  (filePtrSet.ofsVar->is_open() == false)) {
624  //std::cout << "Retrying 1..." << std::endl;
625  delete filePtrSet.ofsVar;
626  filePtrSet.ofsVar = new std::ofstream((baseFileName+"_sub"+this->subIdString()+"."+fileType).c_str(),
627  std::ofstream::out | std::ofstream::trunc);
628  if ((m_subDisplayFile) && (this->displayVerbosity() > 10)) { // output debug
629  *this->subDisplayFile() << "In BaseEnvironment::openOutputFile()"
630  << ": just opened output file with base name '" << baseFileName << "." << fileType
631  << "'"
632  << ", writeOver = " << writeOver
633  << ", options 'out|trunc'"
634  << ", osfvar = " << filePtrSet.ofsVar
635  << std::endl;
636  }
637  }
638  } // only for matlab formats
639  }
640  if (filePtrSet.ofsVar != NULL) {
641  if ((m_subDisplayFile) && (this->displayVerbosity() > 10)) { // output debug
642  *this->subDisplayFile() << "In BaseEnvironment::openOutputFile()"
643  << ", subId = " << this->subId()
644  << ": succeeded on opening output file with base name '" << baseFileName << "." << fileType
645  << "'"
646  << ", writeOver = " << writeOver
647  << std::endl;
648  }
649  }
650  else {
651  std::cerr << "In BaseEnvironment::openOutputFile()"
652  << ": failed to open output file with base name '" << baseFileName << "." << fileType
653  << "'"
654  << std::endl;
655  }
656  queso_require_msg((filePtrSet.ofsVar && filePtrSet.ofsVar->is_open()), "failed to open output file");
657  }
658  else {
659  returnValue = false;
660  }
661  //this->subComm().Barrier(); // prudenci-2011-01-17
662  }
663 
664  return returnValue;
665 }
666 //-------------------------------------------------------
667 bool
669  const std::string& baseFileName,
670  const std::string& inputFileType,
671  bool writeOver,
672  FilePtrSetStruct& filePtrSet) const
673 {
674  std::string fileType(inputFileType);
675 #ifdef QUESO_HAS_HDF5
676  // Do nothing
677 #else
678  if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
679  if (m_subDisplayFile) {
680  *this->subDisplayFile() << "WARNING in BaseEnvironment::openUnifiedOutputFile()"
681  << ": file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
682  << "' has been requested, but this QUESO library has not been built with 'hdf5'"
683  << ". Code will therefore process the file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
684  << "' instead..."
685  << std::endl;
686  }
687  if (this->subRank() == 0) {
688  std::cerr << "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  }
696  }
697 #endif
698 
699  bool returnValue = true;
700  filePtrSet.ofsVar = NULL;
701  if (baseFileName == ".") {
702  if ((m_subDisplayFile) && (this->displayVerbosity() > 10)) { // output debug
703  *this->subDisplayFile() << "In BaseEnvironment::openUnifiedOutputFile()"
704  << ": no unified output file opened with base name '" << baseFileName << "." << fileType
705  << "'"
706  << ", writeOver = " << writeOver
707  << std::endl;
708  }
709  returnValue = false;
710  }
711  else {
713  // Open file
715  if ((m_subDisplayFile) && (this->displayVerbosity() > 10)) { // output debug
716  *this->subDisplayFile() << "In BaseEnvironment::openUnifiedOutputFile()"
717  << ": opening unified output file with base name '" << baseFileName << "." << fileType
718  << "'"
719  << ", writeOver = " << writeOver
720  << std::endl;
721  }
722 
723 
724  //if ((this->subRank () == 0) &&
725  // (this->inter0Rank() == 0)) {
726 #if 0
727  std::cout << "In BaseEnvironment::openUnifiedOutputFile()"
728  << ": opening output file with base name '" << baseFileName << "." << fileType
729  << "'"
730  << ", writeOver = " << writeOver
731  << std::endl;
732 #endif
733  // Verify parent directory exists (for cases when a user
735  // specifies a relative path for the desired output file). prudenci 2010/06/26
737  // std::cout << "checking " << baseFileName+"."+fileType << std::endl;
738  int irtrn = CheckFilePath((baseFileName+"."+fileType).c_str());
739  queso_require_greater_equal_msg(irtrn, 0, "unable to verify output path");
740 
741  if (writeOver) {
743  // Write over an eventual pre-existing file
745  if ((fileType == UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT) ||
746  (fileType == UQ_FILE_EXTENSION_FOR_TXT_FORMAT)) {
747  filePtrSet.ofsVar = new std::ofstream((baseFileName+"."+fileType).c_str(),
748  std::ofstream::out | std::ofstream::trunc);
749  }
750 #ifdef QUESO_HAS_HDF5
751  else if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
752  filePtrSet.h5Var = H5Fcreate((baseFileName+"."+fileType).c_str(),
753  H5F_ACC_TRUNC,
754  H5P_DEFAULT,
755  H5P_DEFAULT);
756  }
757 #endif
758  else {
759  queso_error_msg("invalid file type");
760  }
761  if ((m_subDisplayFile) && (this->displayVerbosity() > 10)) { // output debug
762  *this->subDisplayFile() << "In BaseEnvironment::openUnifiedOutputFile()"
763  << ": just opened output file with base name '" << baseFileName << "." << fileType
764  << "'"
765  << ", writeOver = " << writeOver
766  << ", options 'out|trunc'"
767  << ", osfvar = " << filePtrSet.ofsVar
768  << std::endl;
769  }
770  }
771  else {
773  // Write at the end of an eventual pre-existing file
774  // 'm' and Ranger nodes behave differently on ofstream constructor... prudenci 2010/03/05
776  if ((fileType == UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT) ||
777  (fileType == UQ_FILE_EXTENSION_FOR_TXT_FORMAT)) {
778  filePtrSet.ofsVar = new std::ofstream((baseFileName+"."+fileType).c_str(),
779  std::ofstream::out /*| std::ofstream::in*/ | std::ofstream::app);
780  }
781 #ifdef QUESO_HAS_HDF5
782  else if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
783  filePtrSet.h5Var = H5Fcreate((baseFileName+"."+fileType).c_str(), // TEMPORARY - FIX ME
784  H5F_ACC_TRUNC,
785  H5P_DEFAULT,
786  H5P_DEFAULT);
787 
788  // m_worldRank,
789  // "BaseEnvironment::openUnifiedOutputFile(), writeOver=false",
790  // "hdf file type not supported yet");
791  }
792 #endif
793  else {
794  queso_error_msg("invalid file type");
795  }
796  if ((m_subDisplayFile) && (this->displayVerbosity() > 10)) { // output debug
797  *this->subDisplayFile() << "In BaseEnvironment::openUnifiedOutputFile()"
798  << ": just opened output file with base name '" << baseFileName << "." << fileType
799  << "'"
800  << ", writeOver = " << writeOver
801  << ", options 'out|in|app'"
802  << ", osfvar = " << filePtrSet.ofsVar
803  << std::endl;
804  }
805  if ((filePtrSet.ofsVar == NULL ) ||
806  (filePtrSet.ofsVar->is_open() == false)) {
807 #if 0
808  std::cout << "Retrying 2..." << std::endl;
809 #endif
810  delete filePtrSet.ofsVar;
811  filePtrSet.ofsVar = new std::ofstream((baseFileName+"."+fileType).c_str(),
812  std::ofstream::out | std::ofstream::trunc);
813  if ((m_subDisplayFile) && (this->displayVerbosity() > 10)) { // output debug
814  *this->subDisplayFile() << "In BaseEnvironment::openUnifiedOutputFile()"
815  << ": just opened output file with base name '" << baseFileName << "." << fileType
816  << "'"
817  << ", writeOver = " << writeOver
818  << ", options 'out|trunc'"
819  << ", osfvar = " << filePtrSet.ofsVar
820  << std::endl;
821  }
822  }
823  }
824  if (filePtrSet.ofsVar == NULL) {
825  std::cerr << "In BaseEnvironment::openUnifiedOutputFile()"
826  << ": failed to open unified output file with base name '" << baseFileName << "." << fileType
827  << "'"
828  << std::endl;
829  }
830  queso_require_msg((filePtrSet.ofsVar && filePtrSet.ofsVar->is_open()), "failed to open output file");
831  //}
832  }
833 
834  return returnValue;
835 }
836 //-------------------------------------------------------
837 bool
839  const std::string& baseFileName,
840  const std::string& inputFileType,
841  const std::set<unsigned int>& allowedSubEnvIds,
842  FilePtrSetStruct& filePtrSet) const
843 {
844  std::string fileType(inputFileType);
845 #ifdef QUESO_HAS_HDF5
846  // Do nothing
847 #else
848  if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
849  if (m_subDisplayFile) {
850  *this->subDisplayFile() << "WARNING in BaseEnvironment::openInputFile()"
851  << ": file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
852  << "' has been requested, but this QUESO library has not been built with 'hdf5'"
853  << ". Code will therefore process the file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
854  << "' instead..."
855  << std::endl;
856  }
857  if (this->subRank() == 0) {
858  std::cerr << "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  }
866  }
867 #endif
868 
869  bool returnValue = true;
870  filePtrSet.ifsVar = NULL;
871  if ((baseFileName == UQ_ENV_FILENAME_FOR_NO_INPUT_FILE) ||
872  (allowedSubEnvIds.find(this->subId()) == allowedSubEnvIds.end() )) {
873  if ((m_subDisplayFile) && (this->displayVerbosity() >= 10)) {
874  *this->subDisplayFile() << "In BaseEnvironment::openInputFile()"
875  << ": no input file opened with base name '" << baseFileName << "." << fileType
876  << "'"
877  << std::endl;
878  }
879  returnValue = false;
880  }
881  else {
883  // Open file
885  if ((m_subDisplayFile) && (this->displayVerbosity() >= 10)) {
886  *this->subDisplayFile() << "In BaseEnvironment::openInputFile()"
887  << ": opening input file with base name '" << baseFileName << "." << fileType
888  << "'"
889  << std::endl;
890  }
891  if (this->subRank() == 0) {
893  // Verify parent directory exists (for cases when a user
894  // specifies a relative path for the desired output file). prudenci 2010/06/26
896  // std::cout << "checking " << baseFileName+"."+fileType << std::endl;
897  int irtrn = CheckFilePath((baseFileName+"."+fileType).c_str());
898  queso_require_greater_equal_msg(irtrn, 0, "unable to verify input path");
899 
900  if ((fileType == UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT) ||
901  (fileType == UQ_FILE_EXTENSION_FOR_TXT_FORMAT)) {
902  filePtrSet.ifsVar = new std::ifstream((baseFileName+"."+fileType).c_str(),
903  std::ofstream::in);
904  if ((filePtrSet.ifsVar == NULL) || (filePtrSet.ifsVar->is_open() == false)) {
905  std::cerr << "In BaseEnvironment::openInputFile()"
906  << ": failed to open input file with base name '" << baseFileName << "." << fileType
907  << "'"
908  << std::endl;
909  }
910  queso_require_msg(!((filePtrSet.ifsVar == NULL) || (filePtrSet.ifsVar->is_open() == false)), "file with fileName could not be found");
911  }
912 #ifdef QUESO_HAS_HDF5
913  else if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
914  filePtrSet.h5Var = H5Fopen((baseFileName+"."+fileType).c_str(),
915  H5F_ACC_RDONLY,
916  H5P_DEFAULT);
917  }
918 #endif
919  else {
920  queso_error_msg("invalid file type");
921  }
922  }
923  else {
924  returnValue = false;
925  }
926  //this->subComm().Barrier(); // prudenci-2011-01-17
927  }
928 
929  return returnValue;
930 }
931 //-------------------------------------------------------
932 bool
934  const std::string& baseFileName,
935  const std::string& inputFileType,
936  FilePtrSetStruct& filePtrSet) const
937 {
938  std::string fileType(inputFileType);
939 #ifdef QUESO_HAS_HDF5
940  // Do nothing
941 #else
942  if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
943  if (m_subDisplayFile) {
944  *this->subDisplayFile() << "WARNING in BaseEnvironment::openUnifiedInputFile()"
945  << ": file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
946  << "' has been requested, but this QUESO library has not been built with 'hdf5'"
947  << ". Code will therefore process the file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
948  << "' instead..."
949  << std::endl;
950  }
951  if (this->subRank() == 0) {
952  std::cerr << "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  }
960  }
961 #endif
962 
963  bool returnValue = true;
964  filePtrSet.ifsVar = NULL;
965  if (baseFileName == UQ_ENV_FILENAME_FOR_NO_INPUT_FILE) {
966  if ((m_subDisplayFile) && (this->displayVerbosity() >= 10)) {
967  *this->subDisplayFile() << "In BaseEnvironment::openUnifiedInputFile()"
968  << ": no input file opened with base name '" << baseFileName << "." << fileType
969  << "'"
970  << std::endl;
971  }
972  returnValue = false;
973  }
974  else {
976  // Open file
978  if ((m_subDisplayFile) && (this->displayVerbosity() >= 10)) {
979  *this->subDisplayFile() << "In BaseEnvironment::openUnifiedInputFile()"
980  << ": opening input file with base name '" << baseFileName << "." << fileType
981  << "'"
982  << std::endl;
983  }
984  if (this->subRank() == 0) { // Needed ???????? prudenci 2010-11-11
986  // Verify parent directory exists (for cases when a user
987  // specifies a relative path for the desired output file). prudenci 2010/06/26
989  // std::cout << "checking " << baseFileName+"."+fileType << std::endl;
990  int irtrn = CheckFilePath((baseFileName+"."+fileType).c_str());
991  queso_require_greater_equal_msg(irtrn, 0, "unable to verify input path");
992 
993  if ((fileType == UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT) ||
994  (fileType == UQ_FILE_EXTENSION_FOR_TXT_FORMAT)) {
995  filePtrSet.ifsVar = new std::ifstream((baseFileName+"."+fileType).c_str(),
996  std::ofstream::in);
997  if ((filePtrSet.ifsVar == NULL) || (filePtrSet.ifsVar->is_open() == false)) {
998  std::cerr << "In BaseEnvironment::openUnifiedInputFile()"
999  << ": failed to open input file with base name '" << baseFileName << "." << fileType
1000  << "'"
1001  << std::endl;
1002  }
1003  queso_require_msg(!((filePtrSet.ifsVar == NULL) || (filePtrSet.ifsVar->is_open() == false)), "file with fileName could not be found");
1004  }
1005 #ifdef QUESO_HAS_HDF5
1006  else if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
1007  filePtrSet.h5Var = H5Fopen((baseFileName+"."+fileType).c_str(),
1008  H5F_ACC_RDONLY,
1009  H5P_DEFAULT);
1010  }
1011 #endif
1012  else {
1013  queso_error_msg("invalid file type");
1014  }
1015  }
1016  //else {
1017  // returnValue = false;
1018  //}
1019  //this->subComm().Barrier();
1020  }
1021 
1022  return returnValue;
1023 }
1024 //-------------------------------------------------------
1025 void
1027  FilePtrSetStruct& filePtrSet,
1028  const std::string& inputFileType) const
1029 {
1030  std::string fileType(inputFileType);
1031 #ifdef QUESO_HAS_HDF5
1032  // Do nothing
1033 #else
1034  if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
1035  if (m_subDisplayFile) {
1036  *this->subDisplayFile() << "WARNING in BaseEnvironment::closeFile()"
1037  << ": file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
1038  << "' has been requested, but this QUESO library has not been built with 'hdf5'"
1039  << ". Code will therefore process the file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
1040  << "' instead..."
1041  << std::endl;
1042  }
1043  if (this->subRank() == 0) {
1044  std::cerr << "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  }
1052  }
1053 #endif
1054 
1055  if ((fileType == UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT) ||
1056  (fileType == UQ_FILE_EXTENSION_FOR_TXT_FORMAT)) {
1057  //filePtrSet.ofsVar->close(); // close() crashes on Mac; need to use delete(); why? prudenci 2010/June
1058  delete filePtrSet.ofsVar;
1059  filePtrSet.ofsVar = NULL;
1060 
1061  //filePtrSet.ifsVar->close();
1062  delete filePtrSet.ifsVar;
1063  filePtrSet.ifsVar = NULL;
1064  }
1065 #ifdef QUESO_HAS_HDF5
1066  else if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
1067  H5Fclose(filePtrSet.h5Var);
1068  }
1069 #endif
1070  else {
1071  queso_error_msg("invalid file type");
1072  }
1073 
1074  return;
1075 }
1076 //-------------------------------------------------------
1077 void
1079 {
1080  m_exceptionalCircumstance = value;
1081  return;
1082 }
1083 //-------------------------------------------------------
1084 bool
1086 {
1088 }
1089 
1090 
1091 //*****************************************************
1092 // Empty Environment
1093 //*****************************************************
1095  :
1096  BaseEnvironment("",NULL)
1097 {
1098 }
1099 //-------------------------------------------------------
1101 {
1102 }
1103 //-------------------------------------------------------
1104 void
1105 EmptyEnvironment::print(std::ostream& os) const
1106 {
1107  os.flush(); // just to avoid icpc warnings
1108  return;
1109 }
1110 
1111 //*****************************************************
1112 // Full Environment
1113 //*****************************************************
1114 #ifdef QUESO_HAS_MPI
1116  RawType_MPI_Comm inputComm,
1117  const char* passedOptionsInputFileName,
1118  const char* prefix,
1119  EnvOptionsValues* alternativeOptionsValues)
1120  :
1121  BaseEnvironment(passedOptionsInputFileName,alternativeOptionsValues)
1122 {
1123 #ifdef QUESO_MEMORY_DEBUGGING
1124  std::cout << "Entering FullEnv" << std::endl;
1125 #endif
1126 
1128  // Initialize "full" communicator -- Not necessarily MPI_COMM_WORLD
1130  int mpiRC = MPI_Comm_rank(inputComm,&m_worldRank);
1131  queso_require_equal_to_msg(mpiRC, MPI_SUCCESS, "failed to get world fullRank()");
1132 
1133  m_fullComm = new MpiComm(*this,inputComm);
1134 
1137 
1138  mpiRC = MPI_Comm_group(m_fullComm->Comm(), &m_fullGroup);
1139  queso_require_equal_to_msg(mpiRC, MPI_SUCCESS, "failed MPI_Comm_group()");
1140 
1141  // saving old uncaught exception handler, invoking queso_terminate
1142  old_terminate_handler = std::set_terminate(queso_terminate_handler);
1143 
1144 #ifdef QUESO_MEMORY_DEBUGGING
1145  std::cout << "In FullEnv, finished dealing with MPI initially" << std::endl;
1146 #endif
1147 
1149  // Read options
1151  // If NULL, we create one
1152  if (m_optionsObj == NULL) {
1153  EnvOptionsValues * tempOptions = new EnvOptionsValues(this, prefix);
1154 
1155  // If there's an input file, we grab the options from there. Otherwise the
1156  // defaults are used
1157  if (m_optionsInputFileName != "") {
1158  m_allOptionsMap = new boost::program_options::variables_map();
1159  m_allOptionsDesc = new boost::program_options::options_description("Allowed options");
1160 
1161  readOptionsInputFile();
1162  }
1163 
1164  // We did this dance because scanOptionsValues is not a const method, but
1165  // m_optionsObj is a pointer to const
1166  m_optionsObj = tempOptions;
1167  }
1168 
1169  // If help option was supplied, print info
1170  if (m_optionsObj->m_help != "") {
1171  // We write to std::cout because subDisplayFile() isn't ready yet?
1172  std::cout << (*m_optionsObj) << std::endl;
1173  }
1174 
1176  fullComm().NumProc() % m_optionsObj->m_numSubEnvironments, 0,
1177  "total number of processors in environment must be multiple of the specified number of subEnvironments");
1178 
1179 #ifdef QUESO_MEMORY_DEBUGGING
1180  std::cout << "In FullEnv, finished scanning options" << std::endl;
1181 #endif
1182 
1183  // Only display these messages if the user wants them
1184  // NOTE: This got moved below the Read Options section
1185  // because we need the options to be read to know what
1186  // the verbosity level is.
1187  if (this->displayVerbosity() > 0) {
1189  // Display main initial messages
1190  // 'std::cout' is for: main trace messages + synchronized trace messages + error messages prior to 'exit()' or 'abort()'
1192  /*int iRC = 0;*/
1193  /*iRC = */gettimeofday(&m_timevalBegin, NULL);
1194 
1195  if (m_fullRank == 0) {
1196  QUESO_version_print(std::cout);
1197  }
1198 
1199  if (m_fullRank == 0) {
1200  std::cout << "Beginning run at " << ctime(&m_timevalBegin.tv_sec)
1201  << std::endl;
1202  }
1203  }
1204 
1206  // Deal with multiple subEnvironments: create the sub communicators, one for each subEnvironment
1208  unsigned int numRanksPerSubEnvironment = m_fullCommSize/m_optionsObj->m_numSubEnvironments;
1209 
1210  m_subId = m_fullRank/numRanksPerSubEnvironment;
1211  char tmpSubId[16];
1212  sprintf(tmpSubId,"%u",m_subId);
1213  m_subIdString = tmpSubId;
1214 
1216  m_optionsObj->m_subDisplayAllowedSet.insert((unsigned int) m_subId);
1217  }
1218 
1219  std::vector<int> fullRanksOfMySubEnvironment(numRanksPerSubEnvironment,0);
1220  for (unsigned int i = 0; i < numRanksPerSubEnvironment; ++i) {
1221  fullRanksOfMySubEnvironment[i] = m_subId * numRanksPerSubEnvironment + i;
1222  }
1223 
1224  mpiRC = MPI_Group_incl(m_fullGroup, (int) numRanksPerSubEnvironment, &fullRanksOfMySubEnvironment[0], &m_subGroup);
1225  queso_require_equal_to_msg(mpiRC, MPI_SUCCESS, "failed MPI_Group_incl() for a subEnvironment");
1226 
1227  RawType_MPI_Comm subRawComm;
1228  mpiRC = MPI_Comm_create(m_fullComm->Comm(), m_subGroup, &subRawComm);
1229  queso_require_equal_to_msg(mpiRC, MPI_SUCCESS, "failed MPI_Comm_group() for a subEnvironment");
1230  m_subComm = new MpiComm(*this,subRawComm);
1231  m_subRank = m_subComm->MyPID();
1233 
1235  // Deal with multiple subEnvironments: create the self communicator
1237  m_selfComm = new MpiComm(*this,RawValue_MPI_COMM_SELF);
1238 
1240  // Deal with multiple subEnvironments: create the inter0 communicator
1242  std::vector<int> fullRanksOfInter0(m_optionsObj->m_numSubEnvironments,0);
1243  for (unsigned int i = 0; i < m_optionsObj->m_numSubEnvironments; ++i) {
1244  fullRanksOfInter0[i] = i * numRanksPerSubEnvironment;
1245  }
1246  mpiRC = MPI_Group_incl(m_fullGroup, (int) m_optionsObj->m_numSubEnvironments, &fullRanksOfInter0[0], &m_inter0Group);
1247  queso_require_equal_to_msg(mpiRC, MPI_SUCCESS, "failed MPI_Group_incl() for inter0");
1248  RawType_MPI_Comm inter0RawComm;
1249  mpiRC = MPI_Comm_create(m_fullComm->Comm(), m_inter0Group, &inter0RawComm);
1250  queso_require_equal_to_msg(mpiRC, MPI_SUCCESS, "failed MPI_Comm_group() for inter0");
1251  if (m_fullRank%numRanksPerSubEnvironment == 0) {
1252  m_inter0Comm = new MpiComm(*this,inter0RawComm);
1255  }
1256 
1258  // This situation has been already taken care of above
1259  }
1261  if (m_inter0Rank >= 0) {
1262  m_optionsObj->m_subDisplayAllowedSet.insert((unsigned int) m_subId);
1263  }
1264  }
1265 
1266 
1268  // Open "screen" file
1270  bool openFile = false;
1271  if ((m_subRank == 0 ) &&
1274  openFile = true;
1275  }
1276 
1277  if (openFile && m_worldRank == 0) {
1279  // Verify parent directory exists (for cases when a user
1280  // specifies a relative path for the desired output file).
1282  int irtrn = CheckFilePath((m_optionsObj->m_subDisplayFileName+"_sub"+m_subIdString+".txt").c_str());
1283  queso_require_greater_equal_msg(irtrn, 0, "unable to verify output path");
1284  }
1285 
1287  // Ensure that rank 0 has created path, if necessary, before other tasks use it
1289  m_fullComm->Barrier();
1290 
1291  if (openFile) {
1293  // Always write over an eventual pre-existing file
1295  m_subDisplayFile = new std::ofstream((m_optionsObj->m_subDisplayFileName+"_sub"+m_subIdString+".txt").c_str(),
1296  std::ofstream::out | std::ofstream::trunc);
1297  queso_require_msg((m_subDisplayFile && m_subDisplayFile->is_open()), "failed to open sub screen file");
1298 
1300 
1301  *m_subDisplayFile << "Beginning run at " << ctime(&m_timevalBegin.tv_sec)
1302  << std::endl;
1303  }
1304 
1306  // Debug message related to subEnvironments
1308  if (this->displayVerbosity() >= 2) {
1309  for (int i = 0; i < m_fullCommSize; ++i) {
1310  if (i == m_fullRank) {
1311  //std::cout << "In FullEnvironment::commonConstructor()"
1312  std::cout << "MPI node of worldRank " << m_worldRank
1313  << " has fullRank " << m_fullRank
1314  << ", belongs to subEnvironment of id " << m_subId
1315  << ", and has subRank " << m_subRank
1316  << std::endl;
1317 
1318  std::cout << "MPI node of worldRank " << m_worldRank
1319  << " belongs to sub communicator with full ranks";
1320  for (unsigned int j = 0; j < fullRanksOfMySubEnvironment.size(); ++j) {
1321  std::cout << " " << fullRanksOfMySubEnvironment[j];
1322  }
1323  std::cout << "\n";
1324 
1325  if (m_inter0Comm) {
1326  std::cout << "MPI node of worldRank " << m_worldRank
1327  << " also belongs to inter0 communicator with full ranks";
1328  for (unsigned int j = 0; j < fullRanksOfInter0.size(); ++j) {
1329  std::cout << " " << fullRanksOfInter0[j];
1330  }
1331  std::cout << ", and has inter0Rank " << m_inter0Rank;
1332  }
1333  std::cout << "\n";
1334 
1335  std::cout << std::endl;
1336  }
1337  m_fullComm->Barrier();
1338  }
1339  //if (this->fullRank() == 0) std::cout << "Sleeping 3 seconds..."
1340  // << std::endl;
1341  //sleep(3);
1342  }
1343 
1345  // Deal with seed
1347  if (m_optionsObj->m_rngType == "gsl") {
1348  m_rngObject = new RngGsl(m_optionsObj->m_seed,m_worldRank);
1349  m_basicPdfs = new BasicPdfsGsl(m_worldRank);
1350  }
1351  else if (m_optionsObj->m_rngType == "boost") {
1352  m_rngObject = new RngBoost(m_optionsObj->m_seed,m_worldRank);
1353  m_basicPdfs = new BasicPdfsBoost(m_worldRank);
1354  }
1355  else {
1356  std::cerr << "In Environment::constructor()"
1357  << ": rngType = " << m_optionsObj->m_rngType
1358  << std::endl;
1359  queso_error_msg("the requested 'rngType' is not supported yet");
1360  }
1361 
1363  // Leave commonConstructor()
1365  m_fullComm->Barrier();
1366  m_fullEnvIsReady = true;
1367 
1368  if ((m_subDisplayFile) && (this->displayVerbosity() >= 5)) {
1369  *m_subDisplayFile << "Done with initializations at FullEnvironment::commonConstructor()"
1370  << std::endl;
1371  }
1372 
1373  return;
1374 }
1375 #endif // QUESO_HAS_MPI
1376 
1378  const char* passedOptionsInputFileName,
1379  const char* prefix,
1380  EnvOptionsValues* alternativeOptionsValues)
1381  :
1382  BaseEnvironment(passedOptionsInputFileName,alternativeOptionsValues)
1383 {
1384 #ifdef QUESO_MEMORY_DEBUGGING
1385  std::cout << "Entering FullEnv" << std::endl;
1386 #endif
1387 
1388  m_worldRank = 0;
1389 
1390  m_fullComm = new MpiComm(*this);
1391  m_fullRank = 0;
1392  m_fullCommSize = 1;
1393 
1394 #ifndef QUESO_HAS_MPI
1395  m_fullGroup = 0;
1396 #endif
1397 
1398  // saving old uncaught exception handler, invoking queso_terminate
1399  old_terminate_handler = std::set_terminate(queso_terminate_handler);
1400 
1401 #ifdef QUESO_MEMORY_DEBUGGING
1402  std::cout << "In FullEnv, finished dealing with MPI initially" << std::endl;
1403 #endif
1404 
1406  // Read options
1408  // If NULL, we create one
1409  if (m_optionsObj == NULL) {
1410  EnvOptionsValues * tempOptions = new EnvOptionsValues(this, prefix);
1411 
1412  // If there's an input file, we grab the options from there. Otherwise the
1413  // defaults are used
1414  if (m_optionsInputFileName != "") {
1415  m_allOptionsMap = new boost::program_options::variables_map();
1416  m_allOptionsDesc = new boost::program_options::options_description("Allowed options");
1417 
1419  }
1420 
1421  // We did this dance because scanOptionsValues is not a const method, but
1422  // m_optionsObj is a pointer to const
1423  m_optionsObj = tempOptions;
1424  }
1425 
1426  // If help option was supplied, print info
1427  if (m_optionsObj->m_help != "") {
1428  // We write to std::cout because subDisplayFile() isn't ready yet?
1429  std::cout << (*m_optionsObj) << std::endl;
1430  }
1431 
1433  fullComm().NumProc() % m_optionsObj->m_numSubEnvironments, 0,
1434  "total number of processors in environment must be multiple of the specified number of subEnvironments");
1435 
1436 #ifdef QUESO_MEMORY_DEBUGGING
1437  std::cout << "In FullEnv, finished scanning options" << std::endl;
1438 #endif
1439 
1440  // Only display these messages if the user wants them
1441  // NOTE: This got moved below the Read Options section
1442  // because we need the options to be read to know what
1443  // the verbosity level is.
1444  if (this->displayVerbosity() > 0) {
1446  // Display main initial messages
1447  // 'std::cout' is for: main trace messages + synchronized trace messages + error messages prior to 'exit()' or 'abort()'
1449  /*int iRC = 0;*/
1450  /*iRC = */gettimeofday(&m_timevalBegin, NULL);
1451 
1452  QUESO_version_print(std::cout);
1453 
1454  std::cout << "Beginning run at " << ctime(&m_timevalBegin.tv_sec)
1455  << std::endl;
1456  }
1457 
1458  m_subId = 0;
1459  char tmpSubId[16];
1460  sprintf(tmpSubId,"%u",m_subId);
1461  m_subIdString = tmpSubId;
1462 
1464  m_optionsObj->m_subDisplayAllowedSet.insert((unsigned int) m_subId);
1465  }
1466 
1467  int fullRanksOfMySubEnvironment = 1;
1468 
1469 #ifndef QUESO_HAS_MPI
1470  m_subGroup = 0;
1471 #endif
1472 
1473  m_subComm = new MpiComm(*this);
1474  m_subRank = 0;
1475  m_subCommSize = 1;
1476 
1478  // Deal with multiple subEnvironments: create the self communicator
1480  m_selfComm = new MpiComm(*this);
1481 
1483  // Deal with multiple subEnvironments: create the inter0 communicator
1485  int fullRanksOfInter0 = 0;
1486 #ifndef QUESO_HAS_MPI
1487  m_inter0Group = 0;
1488 #endif
1489  m_inter0Comm = new MpiComm(*this);
1490  m_inter0Rank = 0;
1491  m_inter0CommSize = 1;
1492 
1494  // This situation has been already taken care of above
1495  }
1497  if (m_inter0Rank >= 0) {
1498  m_optionsObj->m_subDisplayAllowedSet.insert((unsigned int) m_subId);
1499  }
1500  }
1501 
1502 
1504  // Open "screen" file
1506  bool openFile = false;
1507  if ((m_subRank == 0 ) &&
1510  openFile = true;
1511  }
1512 
1513  if (openFile && m_worldRank == 0) {
1515  // Verify parent directory exists (for cases when a user
1516  // specifies a relative path for the desired output file).
1518  int irtrn = CheckFilePath((m_optionsObj->m_subDisplayFileName+"_sub"+m_subIdString+".txt").c_str());
1519  queso_require_greater_equal_msg(irtrn, 0, "unable to verify output path");
1520  }
1521 
1523  // Ensure that rank 0 has created path, if necessary, before other tasks use it
1525  m_fullComm->Barrier();
1526 
1527  if (openFile) {
1529  // Always write over an eventual pre-existing file
1531  m_subDisplayFile = new std::ofstream((m_optionsObj->m_subDisplayFileName+"_sub"+m_subIdString+".txt").c_str(),
1532  std::ofstream::out | std::ofstream::trunc);
1533  queso_require_msg((m_subDisplayFile && m_subDisplayFile->is_open()), "failed to open sub screen file");
1534 
1536 
1537  *m_subDisplayFile << "Beginning run at " << ctime(&m_timevalBegin.tv_sec)
1538  << std::endl;
1539  }
1540 
1542  // Debug message related to subEnvironments
1544  if (this->displayVerbosity() >= 2) {
1545  for (int i = 0; i < m_fullCommSize; ++i) {
1546  if (i == m_fullRank) {
1547  //std::cout << "In FullEnvironment::commonConstructor()"
1548  std::cout << "MPI node of worldRank " << m_worldRank
1549  << " has fullRank " << m_fullRank
1550  << ", belongs to subEnvironment of id " << m_subId
1551  << ", and has subRank " << m_subRank
1552  << std::endl;
1553 
1554  std::cout << "MPI node of worldRank " << m_worldRank
1555  << " belongs to sub communicator with full ranks";
1556  std::cout << " " << fullRanksOfMySubEnvironment;
1557  std::cout << "\n";
1558 
1559  if (m_inter0Comm) {
1560  std::cout << "MPI node of worldRank " << m_worldRank
1561  << " also belongs to inter0 communicator with full ranks";
1562  std::cout << " " << fullRanksOfInter0;
1563  std::cout << ", and has inter0Rank " << m_inter0Rank;
1564  }
1565  std::cout << "\n";
1566 
1567  std::cout << std::endl;
1568  }
1569  m_fullComm->Barrier();
1570  }
1571  }
1572 
1574  // Deal with seed
1576  if (m_optionsObj->m_rngType == "gsl") {
1577  m_rngObject = new RngGsl(m_optionsObj->m_seed,m_worldRank);
1578  m_basicPdfs = new BasicPdfsGsl(m_worldRank);
1579  }
1580  else if (m_optionsObj->m_rngType == "boost") {
1581  m_rngObject = new RngBoost(m_optionsObj->m_seed,m_worldRank);
1582  m_basicPdfs = new BasicPdfsBoost(m_worldRank);
1583  }
1584  else {
1585  std::cerr << "In Environment::constructor()"
1586  << ": rngType = " << m_optionsObj->m_rngType
1587  << std::endl;
1588  queso_error_msg("the requested 'rngType' is not supported yet");
1589  }
1590 
1592  // Leave commonConstructor()
1594  m_fullComm->Barrier();
1595  m_fullEnvIsReady = true;
1596 
1597  if ((m_subDisplayFile) && (this->displayVerbosity() >= 5)) {
1598  *m_subDisplayFile << "Done with initializations at FullEnvironment::commonConstructor()"
1599  << std::endl;
1600  }
1601 
1602  return;
1603 }
1604 
1605 //-------------------------------------------------------
1607 {
1608 }
1609 //-------------------------------------------------------
1610 void
1611 FullEnvironment::print(std::ostream& os) const
1612 {
1613  os.flush(); // just to avoid icpc warnings
1614  return;
1615 }
1616 
1618 {
1619 #ifdef QUESO_HAS_MPI
1620  int mpi_initialized;
1621  MPI_Initialized (&mpi_initialized);
1622 
1623  if (mpi_initialized)
1624  {
1625  //MPI_Abort(m_fullComm->Comm(), 1);
1626  MPI_Abort(MPI_COMM_WORLD, 1);
1627  }
1628  else
1629  {
1630  // The system terminate_handler may do useful things like printing
1631  // uncaught exception information, or the user may have created
1632  // their own terminate handler that we want to call.
1634  }
1635 #else
1637 #endif
1638  exit(1);
1639 }
1640 
1641 
1642 //-------------------------------------------------------
1643 void
1645 {
1646  std::ifstream* ifs = new std::ifstream(m_optionsInputFileName.c_str());
1647  if (ifs->is_open()) {
1648  //ifs->close();
1649  delete ifs;
1650  }
1651  else {
1652  if (m_fullRank == 0) std::cout << "An invalid input file has been passed to the 'environment' class constructor!"
1653  << ": name of file is '" << m_optionsInputFileName.c_str() << "'"
1654  << std::endl;
1655 
1656  if (m_fullRank == 0) std::cout << "\nThis is a help message of the QUESO library."
1657  << "\nAn application using the QUESO library shall be executed by typing"
1658  << "\n '<eventual mpi commands and options> <Application> <InputFile>'"
1659  << "\nin the command line."
1660  << "\n"
1661  << std::endl;
1662  queso_error();
1663  }
1664 
1665  return;
1666 }
1667 
1668 } // End namespace QUESO
#define UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT
Definition: Defines.h:102
unsigned int displayVerbosity() const
Definition: Environment.C:396
const MpiComm & subComm() const
Access function for MpiComm sub communicator.
Definition: Environment.C:247
void readOptionsInputFile()
Checks the options input file and reads the options.
Definition: Environment.C:1644
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:241
#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:1611
boost::program_options::variables_map * m_allOptionsMap
Definition: Environment.h:361
void setOptionsInputFileAccessState(bool newState) const
Definition: Environment.C:318
bool exceptionalCircumstance() const
Decides whether there is an exceptional circumstance.
Definition: Environment.C:1085
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:345
int seed() const
Access to the RNG seed.
Definition: Environment.C:423
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:222
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:448
#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:235
#define queso_deprecated()
Definition: Defines.h:134
const MpiComm & fullComm() const
Access function for MpiComm full communicator.
Definition: Environment.C:228
void queso_terminate_handler()
Function for unhandled exceptions in Queso.
Definition: Environment.C:1617
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:403
EnvOptionsValues * m_optionsObj
Definition: Environment.h:383
unsigned int numSubEnvironments() const
Access function to the number of sub-environments.
Definition: Environment.C:288
std::string m_platformName
Platform name.
boost::program_options::variables_map & allOptionsMap() const
Definition: Environment.C:336
#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:301
int inter0Rank() const
Returns the process inter0 rank.
Definition: Environment.C:261
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:668
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:161
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:1026
std::terminate_handler old_terminate_handler
Definition: Environment.C:126
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:307
const MpiComm & selfComm() const
Access function for MpiComm self-communicator.
Definition: Environment.C:254
bool fullEnvIsReady() const
Returns whether the full environment class is ready (constructor has successfully been called)...
Definition: Environment.C:210
~FilePtrSetStruct()
Destructor.
Definition: Environment.C:118
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:274
#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:1078
#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:295
void resetSeed(int newSeedOption)
Reset RNG seed.
Definition: Environment.C:429
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:1377
int CheckFilePath(const char *path)
std::string m_identifyingString
Identifying string.
std::string platformName() const
Access to the platform name.
Definition: Environment.C:442
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:267
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:436
RawType_MPI_Group m_inter0Group
Definition: Environment.h:372
const RngBase * rngObject() const
Access to the RNG object.
Definition: Environment.C:417
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:467
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:410
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:280
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:216
TODO: Base class for basic PDFs using Gsl library.
Definition: BasicPdfsGsl.h:43
EmptyEnvironment()
Default constructor. Does nothing.
Definition: Environment.C:1094
unsigned int m_syncVerbosity
Synchronized verbosity.
void resetIdentifyingString(const std::string &newString)
Reset private attribute m_identifyingString with the value newString.
Definition: Environment.C:454
~FullEnvironment()
Destructor.
Definition: Environment.C:1606
RawType_MPI_Group m_subGroup
Definition: Environment.h:365
~EmptyEnvironment()
Destructor.
Definition: Environment.C:1100
BaseEnvironment(const char *passedOptionsInputFileName, EnvOptionsValues *alternativeOptionsValues)
Default constructor.
Definition: Environment.C:132
void print(std::ostream &os) const
Definition: Environment.C:1105
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:933
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:838

Generated on Fri Jun 17 2016 14:10:49 for queso-0.54.0 by  doxygen 1.8.5