PetaVision  Alpha
 All Data Structures Namespaces Functions Variables
PV::PV_Arguments Class Reference

#include <PV_Arguments.hpp>

Public Member Functions

 PV_Arguments (int argc, char *argv[], bool allowUnrecognizedArguments)
 
char const *const * getArgsConst () const
 
char ** getArgsCopy () const
 
char const * getArgument (int i) const
 
int getBatchWidth () const
 
char const * getCheckpointReadDir () const
 
bool getDryRunFlag () const
 
char const * getGPUDevices () const
 
char const * getLogFile () const
 
int getNumArgs () const
 
int getNumColumns () const
 
int getNumRows () const
 
int getNumThreads () const
 
char const * getOutputPath () const
 
char const * getParamsFile () const
 
char const * getProgramName () const
 
unsigned int getRandomSeed () const
 
bool getRequireReturnFlag () const
 
bool getRestartFlag () const
 
bool getUseDefaultNumThreads () const
 
char const * getWorkingDir () const
 
int printState () const
 
int resetState (int argc, char *argv[], bool allowUnrecognizedArguments)
 
int resetState ()
 
int setBatchWidth (int val)
 
char const * setCheckpointReadDir (char const *val)
 
bool setDryRunFlag (bool val)
 
char const * setGPUDevices (char const *val)
 
char const * setLogFile (char const *val)
 
int setNumColumns (int val)
 
int setNumRows (int val)
 
int setNumThreads (int val)
 
char const * setOutputPath (char const *val)
 
char const * setParamsFile (char const *val)
 
unsigned int setRandomSeed (unsigned int val)
 
bool setRequireReturnFlag (bool val)
 
bool setRestartFlag (bool val)
 
bool setUseDefaultNumThreads (bool val)
 
char const * setWorkingDir (char const *val)
 

Static Public Member Functions

static void freeArgs (int argc, char **argv)
 

Private Member Functions

int clearState ()
 
int initialize (int argc, char *argv[], bool allowUnrecognizedArguments)
 
int initialize_base ()
 
int initializeState ()
 
int setStateFromCmdLineArgs (bool allowUnrecognizedArguments)
 
char const * setString (char **parameter, char const *string, char const *parameterName)
 

Static Private Member Functions

static char ** copyArgs (int argc, char const *const *argv)
 

Private Attributes

char ** args
 
int batchWidth
 
char * checkpointReadDir
 
bool dryRunFlag
 
char * gpuDevices
 
char * logFile
 
int numArgs
 
int numColumns
 
int numRows
 
int numThreads
 
char * outputPath
 
char * paramsFile
 
unsigned int randomSeed
 
bool requireReturnFlag
 
bool restartFlag
 
bool useDefaultNumThreads
 
char * workingDir
 

Detailed Description

A class for parsing the command line arguments and storing the results. Internally, the PV_Arguments object contains a require-return flag, an output path string, a params file string, a log file string, a gpu devices string, a working directory string, a checkpointRead directory string, a restart flag, an unsigned integer indicating a random seed, and integers indicating the number of threads, the number of MPI rows, the number of MPI columns, and the batch width. After initialization, individual settings can be modified with set-methods, or reset to the original argc/argv settings.

It is an error to set both the restart flag and the checkpointRead directory string. PV_Arguments does not check whether directory strings point at existing directories, or do any other sanity checking of the arguments. Typically under MPI, each mpi process will call PV_Init with the same arguments, and each process's PV_Init object creates a PV_Arguments object that it, HyPerCol, and other objects can use to get the command-line arguments.

Definition at line 31 of file PV_Arguments.hpp.

Constructor & Destructor Documentation

PV::PV_Arguments::PV_Arguments ( int  argc,
char *  argv[],
bool  allowUnrecognizedArguments 
)

The standard constructor for PV_Arguments. The strings argv[0], argv[1], ..., argv[argc-1] are processed as follows: argv[0] is ignored (but can be retrieved using the getProgramName method). argv[1] through argv[argc-1] are scanned for an exact match with each of the following: "-c": the next argument is copied into the checkpointRead directory string. "-d": the next argument is copied into the gpu devices argument. "-l": the next argument is copied into the log file string. "-o": the next argument is copied into the output path string. "-p": the next argument is copied into the params file string. "-r": the restart flag is set to true. "-s": the next argument is parsed as an unsigned integer and stored into the random seed. "-t": the next argument is parsed as an integer and stored as the number of threads. "-w": the next argument is copied into the working directory string. "-rows": the next argument is parsed as an integer and stored as the number of MPI rows. "-columns": the next argument is parsed as an integer and stored as the number of MPI columns. "-batchwidth": the next argument is parsed as an integer and stored as the batch width. "-n": the dry-run flag is set to true. "--require-return": the require-return flag is set to true. If "-t" appears but is not followed by an integer, numThreads is set to -1 and useDefaultNumThreads is set to true (this behavior is governed by pv_getoptionalopt_int() in io/io.c) If "-t" is followed by an integer, useDefaultNumThreads is set to fals. It is an error to have both the -r and -c options.

Note that all arguments have a single hyphen, except for "--require-return".

If an option depends on the next argument but there is no next argument, the corresponding internal variable is not set, with the exception of "-t", as noted above.

If an option occurs more than once, only the first occurrence is used to set the value.

If allowUnrecognizedArguments is set to false, the constructor fails if any string in the argv array is not recoginzed.

Definition at line 20 of file PV_Arguments.cpp.

Member Function Documentation

int PV::PV_Arguments::clearState ( )
private

clearState() frees all memory allocated for member variables except for the copy of argv, and resets all member variables to their

Definition at line 184 of file PV_Arguments.cpp.

char ** PV::PV_Arguments::copyArgs ( int  argc,
char const *const *  argv 
)
staticprivate

copyArgs() is used internally both by the constructor, to copy the argv array into a member variable, and by the getArgs get-method to return a copy of the args array. It returns an array of length argc+1, where the first argc strings are copied from argv and the last one is NULL (i.e. copyArgs()[argc]==NULL).

Definition at line 60 of file PV_Arguments.cpp.

void PV::PV_Arguments::freeArgs ( int  argc,
char **  argv 
)
static

Deallocates an array, assuming it was created by a call to getArgs(). It frees argv[0], argv[1], ..., argv[argc-1], and then frees argv.

Definition at line 84 of file PV_Arguments.cpp.

char const* const* PV::PV_Arguments::getArgsConst ( ) const
inline

Returns a read-only pointer to the args array. Note that getArgsConst()[i] and getArgsConst()[i][j] are both const; this method does not provide a way to modify the args array.

Definition at line 91 of file PV_Arguments.hpp.

char ** PV::PV_Arguments::getArgsCopy ( ) const

Returns a copy of the args array. It uses malloc and strdup, so the caller is responsible for freeing getArgs()[k] for each k and for freeing getArgs() (the simplest way to free all the memory at once is to call the static method PV_Argument::freeArgs) The length of the returned array is argc+1, and getArgs()[argc] is NULL.

Definition at line 90 of file PV_Arguments.cpp.

char const* PV::PV_Arguments::getArgument ( int  i) const
inline

getArgument(i) returns the string passed as argv[i] to the constructor. If i is out of bounds, returns null.

Definition at line 84 of file PV_Arguments.hpp.

int PV::PV_Arguments::getBatchWidth ( ) const
inline

Returns the batch width.

Definition at line 181 of file PV_Arguments.hpp.

char const* PV::PV_Arguments::getCheckpointReadDir ( ) const
inline

Returns the checkpointRead directory string.

Definition at line 156 of file PV_Arguments.hpp.

bool PV::PV_Arguments::getDryRunFlag ( ) const
inline

Returns true if the dry-run flag was set.

Definition at line 186 of file PV_Arguments.hpp.

char const* PV::PV_Arguments::getGPUDevices ( ) const
inline

Returns the gpu devices string.

Definition at line 136 of file PV_Arguments.hpp.

char const* PV::PV_Arguments::getLogFile ( ) const
inline

Returns the log file string.

Definition at line 131 of file PV_Arguments.hpp.

int PV::PV_Arguments::getNumArgs ( ) const
inline

Returns the length of the array returned by getUnusedArgArray(); i.e. the argc argument passed to the constructor.

Definition at line 111 of file PV_Arguments.hpp.

int PV::PV_Arguments::getNumColumns ( ) const
inline

Returns the number of columns.

Definition at line 176 of file PV_Arguments.hpp.

int PV::PV_Arguments::getNumRows ( ) const
inline

Returns the number of rows.

Definition at line 171 of file PV_Arguments.hpp.

int PV::PV_Arguments::getNumThreads ( ) const
inline

Returns the number of threads.

Definition at line 166 of file PV_Arguments.hpp.

char const* PV::PV_Arguments::getOutputPath ( ) const
inline

Returns the output path string.

Definition at line 121 of file PV_Arguments.hpp.

char const* PV::PV_Arguments::getParamsFile ( ) const
inline

Returns the params file string.

Definition at line 126 of file PV_Arguments.hpp.

char const* PV::PV_Arguments::getProgramName ( ) const
inline

Returns the string passed as argv[0] to the constructor, typically the name of the program being run.

Definition at line 78 of file PV_Arguments.hpp.

unsigned int PV::PV_Arguments::getRandomSeed ( ) const
inline

Returns the random seed.

Definition at line 141 of file PV_Arguments.hpp.

bool PV::PV_Arguments::getRequireReturnFlag ( ) const
inline

Returns true if the require-return flag was set.

Definition at line 116 of file PV_Arguments.hpp.

bool PV::PV_Arguments::getRestartFlag ( ) const
inline

Returns true if the restart flag was set.

Definition at line 151 of file PV_Arguments.hpp.

bool PV::PV_Arguments::getUseDefaultNumThreads ( ) const
inline

Returns the useDefaultNumThreads flag.

Definition at line 161 of file PV_Arguments.hpp.

char const* PV::PV_Arguments::getWorkingDir ( ) const
inline

Returns the working directory string.

Definition at line 146 of file PV_Arguments.hpp.

int PV::PV_Arguments::initialize ( int  argc,
char *  argv[],
bool  allowUnrecognizedArguments 
)
private

initialize() is called by the constructor to parse the argv array and set internal variables accordingly.

Definition at line 51 of file PV_Arguments.cpp.

int PV::PV_Arguments::initialize_base ( )
private

initialize_base() is called by the constructor to initialize the internal variables to false for flags, zero for integers, and NULL for strings.

Definition at line 25 of file PV_Arguments.cpp.

int PV::PV_Arguments::initializeState ( )
private

initializeState() sets all member variables except numArgs and args (the copies of argc and argv passed to the constructor) to zero or NULL. It is called by initialize_base() and clearState().

Definition at line 32 of file PV_Arguments.cpp.

int PV::PV_Arguments::printState ( ) const

Prints the effective command line based on the argc/argv arguments used in instantiation, and any set-methods used since then.

Definition at line 241 of file PV_Arguments.cpp.

int PV::PV_Arguments::resetState ( int  argc,
char *  argv[],
bool  allowUnrecognizedArguments 
)

Reinitializes the object's state based on the new argc and argv array. The allowUnrecognizedArguments flag has the same effect as in the constructor. Any previous pointers returned by get-methods become invalid.

Definition at line 169 of file PV_Arguments.cpp.

int PV::PV_Arguments::resetState ( )

Resets all member variables to their state at the time the object was instantiated. That is, the arguments in argv are parsed but the effect of any set-method that had been called is discarded. Any previous pointers returned by get-methods, except for getArgs or getArgsCopy are no longer valid.

Definition at line 176 of file PV_Arguments.cpp.

int PV::PV_Arguments::setBatchWidth ( int  val)

Sets the batch width to the input argument. The old value is discarded. The return value is the new batch width.

Definition at line 142 of file PV_Arguments.cpp.

char const * PV::PV_Arguments::setCheckpointReadDir ( char const *  val)

Sets the value of the checkpointRead directory string to a copy of the input argument. If there is an error setting the string, the old value is untouched and the return value is NULL. On success, the old value is deallocated and the return value is a pointer to the new checkpointRead directory string.

Definition at line 121 of file PV_Arguments.cpp.

bool PV::PV_Arguments::setDryRunFlag ( bool  val)

Sets the dry-run flag to the new argument. The old value is discarded. The return value is the new value of the dry-run flag.

Definition at line 146 of file PV_Arguments.cpp.

char const * PV::PV_Arguments::setGPUDevices ( char const *  val)

Sets the value of the gpu devices string to a copy of the input argument. If there is an error setting the string, the old value is untouched and the return value is NULL. On success, the old value is deallocated and the return value is a pointer to the new gpu devices string.

Definition at line 107 of file PV_Arguments.cpp.

char const * PV::PV_Arguments::setLogFile ( char const *  val)

Sets the value of the log file string to a copy of the input argument. If there is an error setting the string, the old value is untouched and the return value is NULL. On success, the old value is deallocated and the return value is a pointer to the new log file string.

Definition at line 104 of file PV_Arguments.cpp.

int PV::PV_Arguments::setNumColumns ( int  val)

Sets the number of columns to the input argument. The old value is discarded. The return value is the new number of columns.

Definition at line 138 of file PV_Arguments.cpp.

int PV::PV_Arguments::setNumRows ( int  val)

Sets the number of rows to the input argument. The old value is discarded. The return value is the new number of rows.

Definition at line 134 of file PV_Arguments.cpp.

int PV::PV_Arguments::setNumThreads ( int  val)

Sets the number of threads to the input argument. The old value is discarded. The return value is the new number of threads. Additionally, the useDefaultNumThreads flag is set to false.

Definition at line 129 of file PV_Arguments.cpp.

char const * PV::PV_Arguments::setOutputPath ( char const *  val)

Sets the value of the output path string. The return value is the new value of the output path string.

Definition at line 98 of file PV_Arguments.cpp.

char const * PV::PV_Arguments::setParamsFile ( char const *  val)

Sets the value of the params file string to a copy of the input argument. If there is an error setting the string, the old value is untouched and the return value is NULL. On success, the old value is deallocated and the return value is a pointer to the new params file string.

Definition at line 101 of file PV_Arguments.cpp.

unsigned int PV::PV_Arguments::setRandomSeed ( unsigned int  val)

Sets the value of the random seed to the input argument. The old value is discarded. The return value is the new value of the random seed.

Definition at line 110 of file PV_Arguments.cpp.

bool PV::PV_Arguments::setRequireReturnFlag ( bool  val)

Sets the value of the require-return flag. The return value is the new value of the require-return flag.

Definition at line 94 of file PV_Arguments.cpp.

bool PV::PV_Arguments::setRestartFlag ( bool  val)

Sets the value of the restart flag. The return value is the new value of the restart flag.

Definition at line 117 of file PV_Arguments.cpp.

char const * PV::PV_Arguments::setString ( char **  parameter,
char const *  string,
char const *  parameterName 
)
private

setString() is used internally as a common interface for setting the internal string variables. If it encounters an error, it prints an error message and returns NULL with the old value of the parameter untouched.

Definition at line 151 of file PV_Arguments.cpp.

bool PV::PV_Arguments::setUseDefaultNumThreads ( bool  val)

Sets the useDefaultNumThreads flag to the given argument. The old value is discarded. The return value is the new useDefaultNumThreads flag. If the argument is true, numThreads is set to zero.

Definition at line 124 of file PV_Arguments.cpp.

char const * PV::PV_Arguments::setWorkingDir ( char const *  val)

Sets the value of the working directory string to a copy of the input argument. If there is an error setting the string, the old value is untouched and the return value is NULL. On success, the old value is deallocated and the return value is a pointer to the new working directory string.

Definition at line 114 of file PV_Arguments.cpp.


The documentation for this class was generated from the following files: