Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_YamlParameterListHelpers.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
12 #include "Teuchos_CommHelpers.hpp"
13 
14 #include <string>
15 #include <fstream>
16 #include <streambuf>
17 
18 void Teuchos::updateParametersFromYamlFileAndBroadcast(
19  const std::string &yamlFileName,
20  const Ptr<ParameterList> &paramList,
21  const Comm<int> &comm,
22  bool overwrite
23  )
24 {
25  if (comm.getSize()==1)
26  updateParametersFromYamlFile(yamlFileName, paramList);
27  else {
28  if (comm.getRank()==0) {
29  std::ifstream stream(yamlFileName.c_str());
30  TEUCHOS_TEST_FOR_EXCEPTION(!stream.is_open(),
31  std::runtime_error,
32  "Could not open YAML file " << yamlFileName);
33  std::istreambuf_iterator<char> stream_iter(stream);
34  std::istreambuf_iterator<char> stream_end;
35  std::string yamlString(stream_iter, stream_end);
36  int strsize = yamlString.size();
37  broadcast<int, int>(comm, 0, &strsize);
38  char* ptr = (strsize) ? (&yamlString[0]) : 0;
39  broadcast<int, char>(comm, 0, strsize, ptr);
40  updateParametersFromYamlString(yamlString, paramList,overwrite, yamlFileName);
41  }
42  else {
43  int strsize;
44  broadcast<int, int>(comm, 0, &strsize);
45  std::string yamlString;
46  yamlString.resize(strsize);
47  char* ptr = (strsize) ? (&yamlString[0]) : 0;
48  broadcast<int, char>(comm, 0, strsize, ptr);
49  updateParametersFromYamlString(yamlString, paramList,overwrite);
50  }
51  }
52 }
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
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...
Ptr< T > ptr(T *p)
Create a pointer to an object from a raw pointer.
Definition of XMLInputSource derived class for reading XML from a file.
TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void updateParametersFromYamlString(const std::string &yamlStr, const Ptr< ParameterList > &paramList, bool overwrite, const std::string &name="")
Reads Yaml parameters from a std::string and updates those already in the given parameter list...