11 #include "columns/Communicator.hpp" 13 #include "include/pv_common.h" 15 #include "utils/PVAssert.hpp" 16 #include "utils/PVLog.hpp" 23 #define MAX_PARAMS 100 // maximum number of parameters in a group 25 #undef HAS_MAIN // define if provides a main function 31 Parameter(
const char *name,
double value);
34 const char *name() {
return paramName; }
36 hasBeenReadFlag =
true;
39 const float *valuePtr() {
40 hasBeenReadFlag =
true;
43 const double *valueDblPtr() {
44 hasBeenReadFlag =
true;
45 return ¶mDblValue;
47 bool hasBeenRead() {
return hasBeenReadFlag; }
48 void clearHasBeenRead() { hasBeenReadFlag =
false; }
49 void setValue(
double v) {
50 paramValue = (float)v;
66 int getArraySize() {
return arraySize; }
67 const char *name() {
return paramName; }
68 int setName(
const char *name);
69 const float *getValues(
int *sz) {
70 hasBeenReadFlag =
true;
74 const double *getValuesDbl(
int *sz) {
75 hasBeenReadFlag =
true;
79 int pushValue(
double value);
80 void resetArraySize() { arraySize = 0; }
81 bool hasBeenRead() {
return hasBeenReadFlag; }
82 void clearHasBeenRead() { hasBeenReadFlag =
false; }
83 double peek(
int index) {
return valuesDbl[index]; }
101 const char *getName() {
return paramName; }
102 const char *getValue() {
103 hasBeenReadFlag =
true;
106 bool hasBeenRead() {
return hasBeenReadFlag; }
107 void clearHasBeenRead() { hasBeenReadFlag =
false; }
108 void setValue(
const char *s) {
110 paramValue = s ? strdup(s) : NULL;
117 bool hasBeenReadFlag;
127 Parameter *peek(
int index) {
return parameters[index]; }
128 int size() {
return count; }
141 int size() {
return count; }
143 return index >= 0 && index < count ? parameterArrays[index] : NULL;
160 return index >= 0 && index < count ? parameterStrings[index] : NULL;
162 int size() {
return count; }
163 const char *lookup(
const char *targetname);
181 const char *name() {
return groupName; }
182 const char *getGroupKeyword() {
return groupKeyword; }
183 int setGroupKeyword(
const char *keyword);
185 int present(
const char *name);
186 double value(
const char *name);
187 bool arrayPresent(
const char *name);
188 const float *arrayValues(
const char *name,
int *size);
189 const double *arrayValuesDbl(
const char *name,
int *size);
190 int stringPresent(
const char *stringName);
191 const char *stringValue(
const char *stringName);
193 bool hasBeenRead(
const char *paramName);
194 int clearHasBeenReadFlags();
197 int setValue(
const char *param_name,
double value);
198 int setStringValue(
const char *param_name,
const char *svalue);
212 enum SweepType { SWEEP_UNDEF = 0, SWEEP_NUMBER = 1, SWEEP_STRING = 2 };
219 int setGroupAndParameter(
const char *groupname,
const char *paramname);
220 int pushNumericValue(
double val);
221 int pushStringValue(
const char *sval);
222 int getNumValues() {
return numValues; }
223 SweepType getType() {
return type; }
224 int getNumericValue(
int n,
double *val);
225 const char *getStringValue(
int n);
226 const char *getGroupName() {
return groupName; }
227 const char *getParamName() {
return paramName; }
234 int currentBufferSize;
235 double *valuesNumber;
243 PVParams(
const char *buffer,
long int bufferLength,
size_t initialSize,
Communicator *inIcComm);
246 bool getParseStatus() {
return parseStatus; }
248 template <
typename T>
249 void ioParamValueRequired(
250 enum ParamsIOFlag ioFlag,
251 const char *groupName,
252 const char *paramName,
254 template <
typename T>
256 enum ParamsIOFlag ioFlag,
257 const char *groupName,
258 const char *paramName,
261 bool warnIfAbsent =
true);
263 enum ParamsIOFlag ioFlag,
264 const char *groupName,
265 const char *paramName,
266 char **paramStringValue,
267 const char *defaultValue,
268 bool warnIfAbsent =
true);
270 void ioParamStringRequired(
271 enum ParamsIOFlag ioFlag,
272 const char *groupName,
273 const char *paramName,
274 char **paramStringValue);
275 template <
typename T>
277 enum ParamsIOFlag ioFlag,
278 const char *groupName,
279 const char *paramName,
282 template <
typename T>
283 void writeParam(
const char *paramName, T paramValue);
284 template <
typename T>
285 void writeParamArray(
const char *paramName,
const T *array,
int arraysize);
286 void writeParamString(
const char *paramName,
const char *svalue);
288 int present(
const char *groupName,
const char *paramName);
289 double value(
const char *groupName,
const char *paramName);
291 value(
const char *groupName,
292 const char *paramName,
294 bool warnIfAbsent =
true);
295 int valueInt(
const char *groupName,
const char *paramName);
297 const char *groupName,
298 const char *paramName,
300 bool warnIfAbsent =
true);
301 bool arrayPresent(
const char *groupName,
const char *paramName);
302 const float *arrayValues(
303 const char *groupName,
304 const char *paramName,
306 bool warnIfAbsent =
true);
307 const double *arrayValuesDbl(
308 const char *groupName,
309 const char *paramName,
311 bool warnIfAbsent =
true);
312 int stringPresent(
const char *groupName,
const char *paramStringName);
314 stringValue(
const char *groupName,
const char *paramStringName,
bool warnIfAbsent =
true);
316 const char *groupNameFromIndex(
int index);
317 const char *groupKeywordFromIndex(
int index);
318 const char *groupKeywordFromName(
const char *name);
320 bool hasBeenRead(
const char *group_name,
const char *param_name);
321 bool presentAndNotBeenRead(
const char *group_name,
const char *param_name);
322 void handleUnnecessaryParameter(
const char *group_name,
const char *param_name);
323 template <
typename T>
324 void handleUnnecessaryParameter(
const char *group_name,
const char *param_name, T correct_value);
331 void handleUnnecessaryStringParameter(
const char *group_name,
const char *param_name);
341 void handleUnnecessaryStringParameter(
342 const char *group_name,
343 const char *param_name,
344 const char *correctValue,
345 bool case_insensitive_flag =
false);
347 void setPrintLuaStream(
FileStream *printLuaStream) { mPrintLuaStream = printLuaStream; }
348 void setPrintParamsStream(
FileStream *printParamsStream) {
349 mPrintParamsStream = printParamsStream;
351 int setParameterSweepValues(
int n);
353 void action_pvparams_directive(
char *
id,
double val);
354 void action_parameter_group_name(
char *keyword,
char *name);
355 void action_parameter_group();
356 void action_parameter_def(
char *
id,
double val);
357 void action_parameter_def_overwrite(
char *
id,
double val);
358 void action_parameter_array(
char *
id);
359 void action_parameter_array_overwrite(
char *
id);
360 void action_parameter_array_value(
double val);
361 void action_parameter_string_def(
const char *
id,
const char *stringval);
362 void action_parameter_string_def_overwrite(
const char *
id,
const char *stringval);
363 void action_parameter_filename_def(
const char *
id,
const char *stringval);
364 void action_parameter_filename_def_overwrite(
const char *
id,
const char *stringval);
365 void action_include_directive(
const char *stringval);
367 void action_parameter_sweep_open(
const char *groupname,
const char *paramname);
368 void action_parameter_sweep_close();
369 void action_parameter_sweep_values_number(
double val);
370 void action_parameter_sweep_values_string(
const char *stringval);
371 void action_parameter_sweep_values_filename(
const char *stringval);
373 int numberOfGroups() {
return numGroups; }
374 int numberOfParameterSweeps() {
return numParamSweeps; }
375 int getParameterSweepSize() {
return parameterSweepSize; }
380 size_t groupArraySize;
396 int parameterSweepSize;
400 char *currGroupKeyword;
403 char *currSweepGroupName;
404 char *currSweepParamName;
409 int initialize(
size_t initialSize);
410 int parseFile(
const char *filename);
411 void loadParamBuffer(
char const *filename, std::string ¶msFileString);
412 int parseBuffer(
const char *buffer,
long int bufferLength);
413 int setParameterSweepSize();
414 void addGroup(
char *keyword,
char *name);
415 void addActiveParamSweep(
const char *group_name,
const char *param_name);
416 void checkDuplicates(
const char *paramName);
417 int newActiveParamSweep();
418 int clearHasBeenReadFlags();
419 static char *stripQuotationMarks(
const char *s);
420 static char *stripOverwriteTag(
const char *s);
421 bool hasSweepValue(
const char *paramName);
422 int convertParamToInt(
double value);
425 template <
typename T>
426 void PVParams::handleUnnecessaryParameter(
427 const char *group_name,
428 const char *param_name,
430 int status = PV_SUCCESS;
431 if (present(group_name, param_name)) {
432 if (worldRank == 0) {
433 const char *class_name = groupKeywordFromName(group_name);
435 "%s \"%s\" does not use parameter %s, but it is present in the parameters file.\n",
440 T params_value = (T)value(
443 if (params_value != correct_value) {
445 if (worldRank == 0) {
446 ErrorLog() <<
" Value " << params_value <<
" is inconsistent with correct value " 447 << correct_value << std::endl;
451 MPI_Barrier(icComm->globalCommunicator());
452 if (status != PV_SUCCESS)
456 template <
typename T>
457 void PVParams::ioParamValueRequired(
458 enum ParamsIOFlag ioFlag,
459 const char *groupName,
460 const char *paramName,
463 case PARAMS_IO_READ: *paramValue = (T)value(groupName, paramName);
break;
464 case PARAMS_IO_WRITE: writeParam(paramName, *paramValue);
break;
468 template <
typename T>
469 void PVParams::ioParamValue(
470 enum ParamsIOFlag ioFlag,
471 const char *groupName,
472 const char *paramName,
478 *paramValue = (T)value(groupName, paramName, defaultValue, warnIfAbsent);
480 case PARAMS_IO_WRITE: writeParam(paramName, *paramValue);
break;
483 template <
typename T>
484 void PVParams::ioParamArray(
485 enum ParamsIOFlag ioFlag,
486 const char *groupName,
487 const char *paramName,
490 if (ioFlag == PARAMS_IO_READ) {
491 const double *paramArray = arrayValuesDbl(groupName, paramName, arraysize);
492 pvAssert(*arraysize >= 0);
493 if (*arraysize > 0) {
494 *paramArrayValue = (T *)calloc((
size_t)*arraysize,
sizeof(T));
496 paramArrayValue ==
nullptr,
497 "%s \"%s\": global rank %d process unable to copy array parameter %s: %s\n",
498 groupKeywordFromName(groupName),
500 icComm->globalCommRank(),
503 for (
int k = 0; k < *arraysize; k++) {
504 (*paramArrayValue)[k] = (T)paramArray[k];
508 *paramArrayValue =
nullptr;
511 else if (ioFlag == PARAMS_IO_WRITE) {
512 writeParamArray(paramName, *paramArrayValue, *arraysize);
516 template <
typename T>
517 void PVParams::writeParam(
const char *paramName, T paramValue) {
518 if (mPrintParamsStream) {
519 pvAssert(mPrintLuaStream);
520 std::stringstream vstr(
"");
521 if (std::numeric_limits<T>::has_infinity) {
522 if (paramValue == std::numeric_limits<T>::min()) {
525 else if (paramValue == std::numeric_limits<T>::max()) {
535 mPrintParamsStream->printf(
" %-35s = %s;\n", paramName, vstr.str().c_str());
536 mPrintLuaStream->printf(
" %-35s = %s;\n", paramName, vstr.str().c_str());
540 template <
typename T>
541 void PVParams::writeParamArray(
const char *paramName,
const T *array,
int arraysize) {
542 if (mPrintParamsStream) {
543 pvAssert(mPrintLuaStream !=
nullptr);
544 pvAssert(arraysize >= 0);
546 mPrintParamsStream->printf(
" %-35s = [", paramName);
547 mPrintLuaStream->printf(
" %-35s = {", paramName);
548 for (
int k = 0; k < arraysize - 1; k++) {
549 mPrintParamsStream->printf(
"%f,", (
double)array[k]);
550 mPrintLuaStream->printf(
"%f,", (
double)array[k]);
552 mPrintParamsStream->printf(
"%f];\n", (
double)array[arraysize - 1]);
553 mPrintLuaStream->printf(
"%f};\n", (
double)array[arraysize - 1]);