8 #include "ConfigParser.hpp" 9 #include "utils/PVLog.hpp" 20 initialize(configStream, allowUnrecognizedArguments);
21 if (!configStream.eof() && configStream.fail()) {
22 Fatal() <<
"Reading configuration stream failed.\n";
27 mAllowUnrecognizedArguments = allowUnrecognizedArguments;
29 unsigned int lineNumber = 0;
30 while (std::getline(configStream, line)) {
32 auto start = line.begin();
34 while (start != line.end() && std::isspace((
int)(*start))) {
37 if (start >= line.end()) {
43 auto colonPosition = line.find(
':', (std::size_t)(start - line.begin()));
45 colonPosition == std::string::npos,
46 "configuration line \"%s\" does not have the format \"argument:value\".\n");
48 std::string argument{start, line.begin() + colonPosition};
50 std::string value{line.begin() + colonPosition + 1, line.end()};
53 bool status = mConfig.setArgumentUsingString(argument, value);
58 Fatal() <<
"Unable to parse config file line number " << lineNumber <<
", " << argument
59 <<
":" << value <<
"\n";
62 bool restartFlag = mConfig.getBooleanArgument(
"Restart");
63 std::string checkpointReadDirectory = mConfig.getStringArgument(
"CheckpointReadDirectory");
64 bool checkpointReadConflict = restartFlag && !checkpointReadDirectory.empty();
66 checkpointReadConflict,
67 "ConfigParser: cannot set both the restart flag and the checkpoint read directory.\n");
71 std::string
const &argument,
72 std::string
const &value,
74 if (!mAllowUnrecognizedArguments) {
75 Fatal() <<
"allowUnrecognizedArguments is false but line number " << lineNumber <<
" (" 76 << argument <<
":" << value <<
") is not a recognized argument.\n";
81 auto start = inString.begin();
82 auto stop = inString.end();
83 while (start < stop && std::isspace((
int)(*start))) {
86 while (stop > start && std::isspace((
int)(*(stop - 1)))) {
89 std::string outString{start, stop};
93 std::string ConfigParser::createString(
94 bool requireReturnFlag,
95 std::string
const &outputPath,
96 std::string
const ¶msFile,
97 std::string
const &logFile,
98 std::string
const &gpuDevices,
99 unsigned int randomSeed,
100 std::string
const &workingDir,
102 std::string
const &checkpointReadDir,
103 bool useDefaultNumThreads,
109 std::string configString;
111 restartFlag && !checkpointReadDir.empty(),
112 "ConfigParser::createStream called with both Restart and CheckpointReadDirectory set.\n");
113 if (requireReturnFlag) {
114 configString.append(
"RequireReturn:true\n");
116 if (!outputPath.empty()) {
117 configString.append(
"OutputPath:").append(outputPath).append(
"\n");
119 if (!paramsFile.empty()) {
120 configString.append(
"ParamsFile:").append(paramsFile).append(
"\n");
122 if (!logFile.empty()) {
123 configString.append(
"LogFile:").append(logFile).append(
"\n");
125 if (!gpuDevices.empty()) {
126 configString.append(
"GPUDevices:").append(gpuDevices).append(
"\n");
129 configString.append(
"RandomSeed:").append(std::to_string(randomSeed)).append(
"\n");
131 if (!workingDir.empty()) {
132 configString.append(
"WorkingDirectory:").append(workingDir).append(
"\n");
135 configString.append(
"Restart:true\n");
137 if (!checkpointReadDir.empty()) {
138 configString.append(
"CheckpointReadDirectory:").append(checkpointReadDir).append(
"\n");
140 if (useDefaultNumThreads) {
141 configString.append(
"NumThreads:-\n");
143 else if (numThreads >= 0) {
144 configString.append(
"NumThreads:").append(std::to_string(numThreads)).append(
"\n");
147 configString.append(
"NumRows:").append(std::to_string(numRows)).append(
"\n");
150 configString.append(
"NumColumns:").append(std::to_string(numColumns)).append(
"\n");
153 configString.append(
"BatchWidth:").append(std::to_string(batchWidth)).append(
"\n");
156 configString.append(
"DryRun:true\n");
ConfigParser(std::istream &configStream, bool allowUnrecognizedArguments)
void initialize(std::istream &inputStream, bool allowUnrecognizedArguments)
std::string stripLeadingTrailingWhitespace(std::string const &inString)
void handleUnrecognized(std::string const &argument, std::string const &value, int linenumber)