TINC
Toolkit for Interactive Computation
tinc::ProcessorAsyncWrapper Class Reference

Wraps a Processor object to run it asynchronously. More...

#include <ProcessorAsyncWrapper.hpp>

Inheritance diagram for tinc::ProcessorAsyncWrapper:
tinc::Processor tinc::IdObject

Public Types

typedef std::map< std::string, al::VariantValue > Configuration
 

Public Member Functions

 ProcessorAsyncWrapper (std::string id)
 
 ProcessorAsyncWrapper (Processor *processor=nullptr)
 
 ~ProcessorAsyncWrapper ()
 
 ProcessorAsyncWrapper (const ProcessorAsyncWrapper &other)
 
 ProcessorAsyncWrapper (ProcessorAsyncWrapper &&other) noexcept
 
ProcessorAsyncWrapperoperator= (const ProcessorAsyncWrapper &other)
 
ProcessorAsyncWrapperoperator= (ProcessorAsyncWrapper &&other) noexcept
 
bool process (bool forceRecompute=false) override
 override this function to determine how subclasses should process More...
 
bool waitUntilDone ()
 
Processorprocessor () const
 
void setProcessor (Processor *processor)
 
bool isRunning ()
 returns true is the process() function is currently running More...
 
void setDataDirectory (std::string directory)
 Convenience function to set input and output directories in one call. More...
 
void setOutputDirectory (std::string outputDirectory)
 Set the directory for output files. More...
 
std::string getOutputDirectory ()
 Get the directory for output files. More...
 
void setInputDirectory (std::string inputDirectory)
 Set the directory for input files. More...
 
std::string getInputDirectory ()
 Get the directory for input files. More...
 
void setOutputFileNames (std::vector< std::string > outputFiles)
 Set the names of output files. More...
 
std::vector< std::string > getOutputFileNames ()
 Query the current output filenames. More...
 
void setInputFileNames (std::vector< std::string > inputFiles)
 Set the names of input files. More...
 
std::vector< std::string > getInputFileNames ()
 Query the current input filenames. More...
 
void setRunningDirectory (std::string directory)
 Set the current directory for process to run in. More...
 
std::string getRunningDirectory ()
 Get the directory for input files. More...
 
void registerStartCallback (std::function< void(void)> func)
 Register a function to be called at the start of process() More...
 
void registerDoneCallback (std::function< void(bool)> func)
 Register a function to be called at the end of process() More...
 
void setVerbose (bool verbose=true)
 
ProcessorregisterDimension (ParameterSpaceDimension &dim)
 Register a dimension so that the Processor is executed on changes. More...
 
Processoroperator<< (ParameterSpaceDimension &dim)
 Convenient syntax for registerDimension() More...
 
void useCache (bool use=true)
 Enable simple file based caching. More...
 
ProcessorregisterDependency (ParameterSpaceDimension &param)
 Register a dependency. More...
 
ProcessorregisterDependency (al::ParameterMeta *param)
 Convenient syntax for registerParameter() More...
 
std::vector< ParameterSpaceDimension * > getDependencies ()
 Return the dependencies registered with this Processor. More...
 
std::string getId ()
 get this object's id More...
 
void setId (std::string id)
 set this object's id More...
 
std::string getDocumentation () const
 
void setDocumentation (const std::string &documentation)
 

Public Attributes

bool ignoreFail {false}
 
bool enabled {true}
 
std::function< bool(void)> prepareFunction
 Set a function to be called before computing to prepare data. More...
 
Configuration configuration
 Current internal configuration key value pairs. More...
 
std::function< void(al::Socket *src)> modified = [](al::Socket * ) {}
 

Protected Member Functions

void startThread ()
 
void callStartCallbacks ()
 
void callDoneCallbacks (bool result)
 
virtual bool writeMeta ()
 
virtual bool needsRecompute ()
 
std::string metaFilename ()
 

Protected Attributes

std::string mInputDirectory
 
std::string mOutputDirectory
 
std::string mRunningDirectory
 
std::vector< std::string > mOutputFileNames
 
std::vector< std::string > mInputFileNames
 
bool mVerbose
 
bool mUseCache {false}
 
std::vector< ParameterSpaceDimension * > mParameters
 
std::vector< ParameterSpaceDimension * > mDependencies
 
std::vector< std::unique_ptr< ParameterSpaceDimension > > mInternalDimensions
 
std::mutex mProcessLock
 
std::string mId
 
std::string mDocumentation
 

Detailed Description

Wraps a Processor object to run it asynchronously.

This class is needed as there are cases where the processor might be running both synchronously and asynchronously, for example when requesting a synchronous run while there is an asynchronous parameter sweep taking place.

Definition at line 53 of file ProcessorAsyncWrapper.hpp.

Member Typedef Documentation

◆ Configuration

typedef std::map<std::string, al::VariantValue> tinc::Processor::Configuration
inherited

Definition at line 272 of file Processor.hpp.

Constructor & Destructor Documentation

◆ ProcessorAsyncWrapper() [1/4]

tinc::ProcessorAsyncWrapper::ProcessorAsyncWrapper ( std::string  id)

◆ ProcessorAsyncWrapper() [2/4]

tinc::ProcessorAsyncWrapper::ProcessorAsyncWrapper ( Processor processor = nullptr)

◆ ~ProcessorAsyncWrapper()

tinc::ProcessorAsyncWrapper::~ProcessorAsyncWrapper ( )

◆ ProcessorAsyncWrapper() [3/4]

tinc::ProcessorAsyncWrapper::ProcessorAsyncWrapper ( const ProcessorAsyncWrapper other)
inline

Definition at line 62 of file ProcessorAsyncWrapper.hpp.

◆ ProcessorAsyncWrapper() [4/4]

tinc::ProcessorAsyncWrapper::ProcessorAsyncWrapper ( ProcessorAsyncWrapper &&  other)
inlinenoexcept

Definition at line 66 of file ProcessorAsyncWrapper.hpp.

Member Function Documentation

◆ callDoneCallbacks()

void tinc::Processor::callDoneCallbacks ( bool  result)
protectedinherited

◆ callStartCallbacks()

void tinc::Processor::callStartCallbacks ( )
protectedinherited

◆ getDependencies()

std::vector<ParameterSpaceDimension *> tinc::Processor::getDependencies ( )
inlineinherited

Return the dependencies registered with this Processor.

Returns

Definition at line 268 of file Processor.hpp.

◆ getDocumentation()

std::string tinc::IdObject::getDocumentation ( ) const
inherited

◆ getId()

std::string tinc::IdObject::getId ( )
inherited

get this object's id

◆ getInputDirectory()

std::string tinc::Processor::getInputDirectory ( )
inlineinherited

Get the directory for input files.

Definition at line 119 of file Processor.hpp.

◆ getInputFileNames()

std::vector<std::string> tinc::Processor::getInputFileNames ( )
inherited

Query the current input filenames.

This function contains the filenames set in C++ using setInputFileNames or the filenames set by the Processor after running, see for example ProcessorScript

◆ getOutputDirectory()

std::string tinc::Processor::getOutputDirectory ( )
inlineinherited

Get the directory for output files.

Definition at line 109 of file Processor.hpp.

◆ getOutputFileNames()

std::vector<std::string> tinc::Processor::getOutputFileNames ( )
inherited

Query the current output filenames.

This function contains the filenames set in C++ using setOutputFileNames or the filenames set by the Processor after running, see for example ProcessorScript

◆ getRunningDirectory()

std::string tinc::Processor::getRunningDirectory ( )
inlineinherited

Get the directory for input files.

Definition at line 163 of file Processor.hpp.

◆ isRunning()

bool tinc::Processor::isRunning ( )
inherited

returns true is the process() function is currently running

This is useful for soft queries, for example for user feedback. The processor might have started running before this function exits even if it reports it is idle and viceversa.

◆ metaFilename()

std::string tinc::Processor::metaFilename ( )
protectedinherited

◆ needsRecompute()

virtual bool tinc::Processor::needsRecompute ( )
protectedvirtualinherited

◆ operator<<()

Processor& tinc::Processor::operator<< ( ParameterSpaceDimension dim)
inlineinherited

Convenient syntax for registerDimension()

Definition at line 225 of file Processor.hpp.

◆ operator=() [1/2]

ProcessorAsyncWrapper& tinc::ProcessorAsyncWrapper::operator= ( const ProcessorAsyncWrapper other)
inline

Definition at line 72 of file ProcessorAsyncWrapper.hpp.

◆ operator=() [2/2]

ProcessorAsyncWrapper& tinc::ProcessorAsyncWrapper::operator= ( ProcessorAsyncWrapper &&  other)
inlinenoexcept

Definition at line 78 of file ProcessorAsyncWrapper.hpp.

◆ process()

bool tinc::ProcessorAsyncWrapper::process ( bool  forceRecompute = false)
overridevirtual

override this function to determine how subclasses should process

You must call prepareFunction(), callDoneCallbacks() , callStartCallbacks() and test for 'enabled' within the process() function of all child classes.

Implements tinc::Processor.

◆ processor()

Processor* tinc::ProcessorAsyncWrapper::processor ( ) const

◆ registerDependency() [1/2]

Processor& tinc::Processor::registerDependency ( al::ParameterMeta *  param)
inherited

Convenient syntax for registerParameter()

Parameters
param
Returns

◆ registerDependency() [2/2]

Processor& tinc::Processor::registerDependency ( ParameterSpaceDimension param)
inherited

Register a dependency.

Parameters
param
Returns

A dependency is a parameter that affects the result of this processor but does not trigger computation

◆ registerDimension()

Processor& tinc::Processor::registerDimension ( ParameterSpaceDimension dim)
inherited

Register a dimension so that the Processor is executed on changes.

You should only register dimensions in this way if you are not using the processor through ParameterSpace::process() or ParameterSpace::sweep(). The tow modes are provided for flexibility but should not be used concurrently.

Processor is executed on changes of this parameter

◆ registerDoneCallback()

void tinc::Processor::registerDoneCallback ( std::function< void(bool)>  func)
inherited

Register a function to be called at the end of process()

This function is passed true if the process() function has been sucessful.

These callbacks should only be used for data related actions, as they will be skipped when reusing cache in ParameterSpace.

◆ registerStartCallback()

void tinc::Processor::registerStartCallback ( std::function< void(void)>  func)
inherited

Register a function to be called at the start of process()

These callbacks should only be used for data related actions, as they will be skipped when reusing cache in ParameterSpace.

◆ setDataDirectory()

void tinc::Processor::setDataDirectory ( std::string  directory)
inherited

Convenience function to set input and output directories in one call.

◆ setDocumentation()

void tinc::IdObject::setDocumentation ( const std::string &  documentation)
inherited

◆ setId()

void tinc::IdObject::setId ( std::string  id)
inherited

set this object's id

◆ setInputDirectory()

void tinc::Processor::setInputDirectory ( std::string  inputDirectory)
inherited

Set the directory for input files.

◆ setInputFileNames()

void tinc::Processor::setInputFileNames ( std::vector< std::string >  inputFiles)
inherited

Set the names of input files.

Parameters
outputFileslist of output file names.

◆ setOutputDirectory()

void tinc::Processor::setOutputDirectory ( std::string  outputDirectory)
inherited

Set the directory for output files.

◆ setOutputFileNames()

void tinc::Processor::setOutputFileNames ( std::vector< std::string >  outputFiles)
inherited

Set the names of output files.

Parameters
outputFileslist of output file names.

◆ setProcessor()

void tinc::ProcessorAsyncWrapper::setProcessor ( Processor processor)

◆ setRunningDirectory()

void tinc::Processor::setRunningDirectory ( std::string  directory)
inherited

Set the current directory for process to run in.

You must ensure you don;t change the running directory while the processor is running, otherwise different parts of the processing (the callbacks and the processing function) might execute in a different directory

◆ setVerbose()

void tinc::Processor::setVerbose ( bool  verbose = true)
inlineinherited

Definition at line 183 of file Processor.hpp.

◆ startThread()

void tinc::ProcessorAsyncWrapper::startThread ( )
protected

◆ useCache()

void tinc::Processor::useCache ( bool  use = true)
inlineinherited

Enable simple file based caching.

Parameters
useThis caching is a simple but brittle caching mechanism. Whenever possible use the caching provided by ParameterSpace, as it is more robust.

For this caching to work, you will need to ensure that every independent run produces unique and reproducible filenames. i.e. you will need to set the output file names on every run to include in their name all parameter values and dependencies, or have some other way of connecting the parameters/ parameter space to the file.

This will cache the output as written in the output files. You can set and query the files using getOutputFiles() setOutputFiles().

Definition at line 245 of file Processor.hpp.

◆ waitUntilDone()

bool tinc::ProcessorAsyncWrapper::waitUntilDone ( )

◆ writeMeta()

virtual bool tinc::Processor::writeMeta ( )
protectedvirtualinherited

Member Data Documentation

◆ configuration

Configuration tinc::Processor::configuration
inherited

Current internal configuration key value pairs.

Reflects the most recently used configuration (whether successful or failed) or the configuration for the currently running process.

Definition at line 280 of file Processor.hpp.

◆ enabled

bool tinc::Processor::enabled {true}
inherited

If set to false, process() has no effect and will return true;

Definition at line 194 of file Processor.hpp.

◆ ignoreFail

bool tinc::Processor::ignoreFail {false}
inherited

If set to true, ProcessorChains will continue even if this processor fails. Has no effect if running chain asychronously

Definition at line 189 of file Processor.hpp.

◆ mDependencies

std::vector<ParameterSpaceDimension *> tinc::Processor::mDependencies
protectedinherited

Definition at line 293 of file Processor.hpp.

◆ mDocumentation

std::string tinc::IdObject::mDocumentation
protectedinherited

Definition at line 69 of file IdObject.hpp.

◆ mId

std::string tinc::IdObject::mId
protectedinherited

Definition at line 68 of file IdObject.hpp.

◆ mInputDirectory

std::string tinc::Processor::mInputDirectory
protectedinherited

Definition at line 283 of file Processor.hpp.

◆ mInputFileNames

std::vector<std::string> tinc::Processor::mInputFileNames
protectedinherited

Definition at line 287 of file Processor.hpp.

◆ mInternalDimensions

std::vector<std::unique_ptr<ParameterSpaceDimension> > tinc::Processor::mInternalDimensions
protectedinherited

Definition at line 294 of file Processor.hpp.

◆ modified

std::function<void(al::Socket *src)> tinc::IdObject::modified = [](al::Socket * ) {}
inherited

Definition at line 65 of file IdObject.hpp.

◆ mOutputDirectory

std::string tinc::Processor::mOutputDirectory
protectedinherited

Definition at line 284 of file Processor.hpp.

◆ mOutputFileNames

std::vector<std::string> tinc::Processor::mOutputFileNames
protectedinherited

Definition at line 286 of file Processor.hpp.

◆ mParameters

std::vector<ParameterSpaceDimension *> tinc::Processor::mParameters
protectedinherited

Definition at line 292 of file Processor.hpp.

◆ mProcessLock

std::mutex tinc::Processor::mProcessLock
protectedinherited

Definition at line 304 of file Processor.hpp.

◆ mRunningDirectory

std::string tinc::Processor::mRunningDirectory
protectedinherited

Definition at line 285 of file Processor.hpp.

◆ mUseCache

bool tinc::Processor::mUseCache {false}
protectedinherited

Definition at line 290 of file Processor.hpp.

◆ mVerbose

bool tinc::Processor::mVerbose
protectedinherited

Definition at line 288 of file Processor.hpp.

◆ prepareFunction

std::function<bool(void)> tinc::Processor::prepareFunction
inherited

Set a function to be called before computing to prepare data.

When writing the prepare function you should access values and ids through Processor::configuration. If you access values directly from dimensions, you will likely break ParameterSpace::sweep used with this Processor as sweep() does not change the internal values of the parameter space and its dimensions.

If this function returns false, computation is not performed and process() will return false. You can use this function to avoid computation when conditions are not right.

Definition at line 209 of file Processor.hpp.


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