Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | Friends

ParallelTask Class Reference

Inheritance diagram for ParallelTask:
Task NamedObject CallBacker ArrayNDGetAll< T > MemCopier< T > MemSetter< T >

List of all members.

Public Member Functions

virtual ~ParallelTask ()
bool execute ()
virtual bool execute (bool parallel)
void setProgressMeter (ProgressMeter *)
 Must be called before execute().
void enableNrDoneCounting (bool yn)
int64_t nrDone () const
 May be -1, i.e. class does not report nrdone.
int64_t totalNr () const

Protected Member Functions

virtual int64_t nrIterations () const =0
virtual int maxNrThreads () const
virtual int minThreadSize () const
virtual bool stopAllOnFailure () const
 ParallelTask (const char *nm=0)
int64_t calculateThreadSize (int64_t totalnr, int nrthreads, int thread) const
void addToNrDone (int increment)
void reportNrDone (int nrdone)

Private Member Functions

virtual bool doWork (int64_t start, int64_t stop, int threadid)=0
virtual bool doPrepare (int nrthreads)
virtual bool doFinish (bool success)

Private Attributes

ProgressMeterprogressmeter_
Threads::Mutexnrdonemutex_
int64_t nrdone_
int64_t totalnrcache_

Friends

class ParallelTaskRunner

Detailed Description

Generalization of a task that can be run in parallel. Any task that has a fixed number of computations that are independent (i.e. they don't need to be done in a certain order) can inherit ParallelTask and be executed in parallel by calling the ParallelTask::execute().

Example of usage:

    float result[N];
    for ( int idx=0; idx<N; idx++ )
        result[idx] = input1[idx]* function( idx, other, variables );

could be made parallel by adding the class:

class CalcClass : public ParallelTask
{
public:
    od_int64    nrIterations() const { return N; }
    int         doWork( od_int64 start, od_int64 stop, int threadid )
                {
                    for ( int idx=start; idx<=stop && shouldContinue(); idx++ )
                    {
                        result[idx] = input1[idx] *
                                      function( idx, other, variables );
                        addToNrDone( 1 );
                    }

                    return true;
                }
};

and in use that instead of the for-loop:

    CalcClass myclass( N, my, parameters );
    myclass.exectute();

Constructor & Destructor Documentation

virtual ParallelTask::~ParallelTask (  )  [virtual]
ParallelTask::ParallelTask ( const char *  nm = 0  )  [protected]

Member Function Documentation

void ParallelTask::addToNrDone ( int  increment  )  [protected]

Call this from within your thread to say that you have done something.

int64_t ParallelTask::calculateThreadSize ( int64_t  totalnr,
int  nrthreads,
int  thread 
) const [protected]
virtual bool ParallelTask::doFinish ( bool  success  )  [inline, private, virtual]

Called after all doWork have finished.

Parameters:
success indicates whether all doWork returned true.
virtual bool ParallelTask::doPrepare ( int  nrthreads  )  [inline, private, virtual]

Called before any doWork is called.

Reimplemented in MemSetter< T >, and MemCopier< T >.

virtual bool ParallelTask::doWork ( int64_t  start,
int64_t  stop,
int  threadid 
) [private, pure virtual]

The functions that does the job. The function will be called with all intervals from 0 to ParallelTask::nrIterations()-1. The function must be designed to be able to run in parallel.

Parameters:
threadid gives an identifier (between 0 and nr of threads -1) that is unique to each call to doWork.

Implemented in ArrayNDGetAll< T >, MemSetter< T >, and MemCopier< T >.

void ParallelTask::enableNrDoneCounting ( bool  yn  )  [virtual]

Reimplemented from Task.

virtual bool ParallelTask::execute ( bool  parallel  )  [virtual]

Runs the process the desired number of times.

Note:
that the function has static threads (normally the same number as there are processors on the machine), and these static threads will be shared by all instances of ParallelTask::execute.
bool ParallelTask::execute (  )  [inline, virtual]

Runs the process the desired number of times.

Note:
that the function has static threads (normally the same number as there are processors on the machine), and these static threads will be shared by all instances of ParallelTask::execute.

Implements Task.

virtual int ParallelTask::maxNrThreads (  )  const [inline, protected, virtual]
virtual int ParallelTask::minThreadSize (  )  const [inline, protected, virtual]
Returns:
the minimum number of computations that effectively can be run in a separate thread. A small number will give a large overhead for when each step is quick and nrIterations is small.

Reimplemented in MemSetter< T >, and MemCopier< T >.

int64_t ParallelTask::nrDone (  )  const [virtual]

May be -1, i.e. class does not report nrdone.

Reimplemented from Task.

virtual int64_t ParallelTask::nrIterations (  )  const [protected, pure virtual]
Returns:
the number of times the process should be run.

Implemented in ArrayNDGetAll< T >, MemSetter< T >, and MemCopier< T >.

void ParallelTask::reportNrDone ( int  nrdone  )  [protected]
void ParallelTask::setProgressMeter ( ProgressMeter  )  [virtual]

Must be called before execute().

Reimplemented from Task.

virtual bool ParallelTask::stopAllOnFailure (  )  const [inline, protected, virtual]

If one thread fails, should an attempt be made to stop the others? If true, enableWorkControl will be enabled, and threads should call shouldContinue() regularly.

int64_t ParallelTask::totalNr (  )  const [inline, virtual]

Reimplemented from Task.


Friends And Related Function Documentation

friend class ParallelTaskRunner [friend]

Member Data Documentation

int64_t ParallelTask::nrdone_ [private]
int64_t ParallelTask::totalnrcache_ [private]