PetaVision  Alpha
ConfigFileArguments.cpp
1 /*
2  * ConfigFileArguments.cpp
3  *
4  * Created on: Sep 21, 2015
5  * Author: pschultz
6  */
7 
8 #include "ConfigFileArguments.hpp"
9 #include "include/pv_common.h"
10 #include "utils/PVLog.hpp"
11 #include <cerrno>
12 #include <cstring>
13 #include <fstream>
14 #include <sstream>
15 
16 namespace PV {
17 
19  std::string const &configFile,
20  MPI_Comm communicator,
21  bool allowUnrecognizedArguments) {
23  initialize(configFile, communicator, allowUnrecognizedArguments);
24 }
25 
26 int ConfigFileArguments::initialize_base() { return PV_SUCCESS; }
27 
29  std::string const &configFile,
30  MPI_Comm communicator,
31  bool allowUnrecognizedArguments) {
32  resetState(configFile, communicator, allowUnrecognizedArguments);
33  return PV_SUCCESS;
34 }
35 
37  std::string const &configFile,
38  MPI_Comm communicator,
39  bool allowUnrecognizedArguments) {
40  std::string configContents;
41  unsigned int fileSize = 0U;
42  int rank;
43  MPI_Comm_rank(communicator, &rank);
44  if (rank == 0) {
45  errno = 0;
46  std::ifstream configFileStream{configFile};
47  FatalIf(
48  configFileStream.fail(),
49  "ConfigFileArguments unable to open \"%s\" for reading: %s\n",
50  configFile.c_str(),
51  strerror(errno));
52  configFileStream.seekg(0, std::ios_base::end);
53  fileSize = (unsigned int)configFileStream.tellg();
54  configContents.resize(fileSize);
55  configFileStream.seekg(0, std::ios_base::beg);
56  configFileStream.read(&configContents.at(0), fileSize);
57  MPI_Bcast(&fileSize, 1, MPI_UNSIGNED, 0, communicator);
58  MPI_Bcast(&configContents.at(0), (int)fileSize, MPI_CHAR, 0, communicator);
59  }
60  else {
61  MPI_Bcast(&fileSize, 1, MPI_UNSIGNED, 0, communicator);
62  configContents.resize(fileSize);
63  MPI_Bcast(&configContents.at(0), (int)fileSize, MPI_CHAR, 0, communicator);
64  }
65  std::istringstream configStream{configContents};
66  Arguments::resetState(configStream, allowUnrecognizedArguments);
67 }
68 
69 } /* namespace PV */
void resetState()
Definition: Arguments.cpp:38
ConfigFileArguments(std::string const &configFile, MPI_Comm communicator, bool allowUnrecognizedArguments)
int initialize(std::string const &configFile, MPI_Comm communicator, bool allowUnrecognizedArguments)