Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
Teuchos_YamlParameterListHelpers.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 
45 #include "Teuchos_CommHelpers.hpp"
46 
47 void Teuchos::updateParametersFromYamlFileAndBroadcast(
48  const std::string &yamlFileName,
49  const Teuchos::Ptr<Teuchos::ParameterList> &paramList,
50  const Teuchos::Comm<int> &comm,
51  bool overwrite)
52 {
53  struct SafeFile
54  {
55  SafeFile(const char* fname, const char* options)
56  {
57  handle = fopen(fname, options);
58  }
59  ~SafeFile()
60  {
61  if(handle)
62  fclose(handle);
63  }
64  FILE* handle;
65  };
66  //BMK note: see teuchos/comm/src/Teuchos_XMLParameterListHelpers.cpp
67  if(comm.getSize() == 1)
68  {
69  updateParametersFromYamlFile(yamlFileName, paramList);
70  }
71  else
72  {
73  if(comm.getRank() == 0)
74  {
75  //BMK: TODO! //reader.setAllowsDuplicateSublists(false);
76  //create a string and load file contents into it
77  //C way for readability and speed, same thing with C++ streams is slow & ugly
78  SafeFile yamlFile(yamlFileName.c_str(), "rb");
79  if(!yamlFile.handle)
80  {
81  throw std::runtime_error(std::string("Failed to open YAML file \"") + yamlFileName + "\"for reading.");
82  }
83  fseek(yamlFile.handle, 0, SEEK_END);
84  int strsize = ftell(yamlFile.handle) + 1;
85  rewind(yamlFile.handle);
86  //Make the array raii
87  Teuchos::ArrayRCP<char> contents(new char[strsize], 0, strsize, true);
88  fread((void*) contents.get(), strsize - 1, 1, yamlFile.handle);
89  contents.get()[strsize - 1] = 0;
90  Teuchos::broadcast<int, int>(comm, 0, &strsize);
91  Teuchos::broadcast<int, char>(comm, 0, strsize, contents.get());
92  updateParametersFromYamlCString(contents.get(), paramList, overwrite);
93  }
94  else
95  {
96  int strsize;
97  Teuchos::broadcast<int, int>(comm, 0, &strsize);
98  Teuchos::ArrayRCP<char> contents(new char[strsize], 0, strsize, true);
99  Teuchos::broadcast<int, char>(comm, 0, strsize, contents.get());
100  updateParametersFromYamlCString(contents.get(), paramList, overwrite);
101  }
102  }
103 }
TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void updateParametersFromYamlCString(const char *const data, const Teuchos::Ptr< Teuchos::ParameterList > &paramList, bool overwrite)
TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void updateParametersFromYamlFile(const std::string &yamlFileName, const Ptr< ParameterList > &paramList)
Reads Yaml parameters from a file and updates those already in the given parameter list...
virtual int getRank() const =0
Returns the rank of this process.
Abstract interface for distributed-memory communication.
Definition of XMLInputSource derived class for reading XML from a file.
Simple wrapper class for raw pointers to single objects where no persisting relationship exists...
virtual int getSize() const =0
Returns the number of processes that make up this communicator.
Reference-counted smart pointer for managing arrays.