Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
Teuchos_YamlParameterListHelpers.cpp
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
10#include "Teuchos_YamlParameterListHelpers.hpp"
12#include "Teuchos_CommHelpers.hpp"
13
14#include <string>
15#include <fstream>
16#include <streambuf>
17
18void 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}
Definition of XMLInputSource derived class for reading XML from a file.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.