HILA
|
hila::input - Class for parsing runtime parameter files. More...
#include <input.h>
Classes | |
class | returntype |
returntype is a special class for resolving get("label") return type More... | |
Public Member Functions | |
bool | open (const std::string &fname, bool use_cmdline=true, bool exit_on_error=true) |
Open file that parameters are read from. | |
void | close () |
Closes input parameter file. | |
void | quiet (bool really=true) |
Silence print output during file reading. | |
returntype | get (const std::string &key) |
Get next value in stack of read in input string from parameters file. | |
template<typename T > | |
bool | get_value (T &val, const std::string &label, bool bcast=true) |
Read input (alternative to get()) | |
int | get_item (const std::string &label, const std::vector< std::string > &items, bool bcast=true) |
Identify item from a list. | |
hila::input - Class for parsing runtime parameter files.
Input files consist normally of "key <value>" -pairs.
It is important to note that the data structure employed by the input class is not a dictionary, but technically a stack. This means that the values are read in sequentially, thus they must be in the file in the order they are read in.
In examples of other methods we will refer to the input object as f. Creating the input object is done with:
One can also initialize the input object without specifying the filename:
in which case the hila::input::open
method needs to be called separately.
Comment character '#': everything after # in input file is a comment to the end of the line.
NOTE: methods which broadcast to all nodes (default) must be called from all nodes synchronously. These include open(), get(), get_value() with bcast=true, get_item with bcast=true.
Thus:
deadlocks (if there are more than 1 rank). Method f.get_value(v,"a value",false)
can be used in this context.
void hila::input::close | ( | ) |
|
inline |
Get next value in stack of read in input string from parameters file.
Use as
reads in a key-value pair
from the input file f, and returns the value of type of variable var. The value is broadcast to all MPI nodes. The method infers the type of the returned variable from the type of the assignment.
Key is an alphanumeric string, which may contain words separated by whitespace.
The get method simply traverses the parsed in stack of input values in the given parameters file. A functionality of the method is that the given argument key is skipped and the value that this key is assigned to is then fetched and returned.
Technically the key's can also be read in if no argument is given to get. The main functionality of the key argument is to book keep by the user that all parameters are read in. If the key that is wanting to be read doesn't exist, then get throws an error and the program is stopped.
With this logic one could construct a parameters file of only values:
and read in the values in a loop by simply calling hila::input::get() consecutively, but this is not advised.
The type of the value to be read in is inferred by the variable the value is fetched into. If the value cannot be casted into the variable, then the parser will throw an error and crash.
Multiple items are separated by commas, whitespace is not significant.
matches " number of cats 5 "
matches "complex phase (0.4, 0.5)"
complex values are given in pairs within ( , )
matches "key <string value>" where string value is either
matches "lattice size 32, 32, 32, 32" (if NDIM == 4).
matches "initial vector 1, 2, 3, 4, 5"
matches "vec 3,4, 5.5, 7.8, 4" and returns a vector of double values. The numbers are read until they are not followed by a comma. If comma is the last non-whitespace character on the line, reading continues to the next line.
T is one of the other supported types.
NOTE: The parser will crash if during reading in a multi valued line (Vector, std::vector) the line ends with a ",". The parser will interpret that there is data on the next line to read in and will fail during the next get call.
key | Parameter to be read in. |
int hila::input::get_item | ( | const std::string & | label, |
const std::vector< std::string > & | items, | ||
bool | bcast = true |
||
) |
Identify item from a list.
"items" contains the allowed entries. Return value is the index of the item found in input file.
If the value of the optional bool parameter broadcast is:
Special item values:
If one of these is matched, it has to be read again with corresponding get() or get_value() -method. get_item doesn't progress the stack, so get() will fetch the next value which the item result corresponds to
Examples:
will return value 1 if f contains "colour green", and quits the program if none of the 3 alternatives are found.
If file contains:
NOTE: "%s" matches anything. It should be the last item in the list. (The items are tested in order and first to match is returned.)
label | key to match in parameters file |
items | list of items to identify with |
bcast | Default true, if true broadcast to all MPI ranks |
|
inline |
Read input (alternative to get())
Val can be any value used in get()-method above. If broadcast==false, the value is not broadcast to other nodes. The return value is false if the value could not be read successfully, true otherwise. This method does not exit on error (but an error message may be printed) Example:
NOTE: if broadcast == false the return value is correct only on node 0.
Supported types same as for hila::input::get
T |
val | variable to store gotten value in. Infers the type for the fetched value |
label | key to fetch value for |
bcast | default true. If true the value will be broadcasted to all nodes |
bool hila::input::open | ( | const std::string & | fname, |
bool | use_cmdline = true , |
||
bool | exit_on_error = true |
||
) |
Open file that parameters are read from.
If input class is initialized with path to file that needs to be opened, then the file will be opened automatically, and this method does not need to be called.
If no argument is given then filename will be interperated as default input file name
In the case that use_cmdline is True and if no argument is given and filename is given with -i {alternative_file} cmdline argument then {alternative_file} will be the specified file to be opened.
fname | Path to file that needs to be read |
use_cmdline | Default True |
exit_on_error | If true exit on error. Return value is passed to all MPI nodes. Default True |
|
inline |
Silence print output during file reading.
By default hila::input methods print everything read to hila::out0 for logging. f.quiet()
disables this.
really |