LAMA
lama::Task Class Reference

Class for asynchronous tasks. More...

#include <Task.hpp>

Inheritance diagram for lama::Task:

Public Types

typedef unsigned int Id

Public Member Functions

 Task (boost::function< void()> function, int numOmpThreads=0)
 Construct a new task that will execute a void function, no arguments.
 ~Task ()
 Destructor.
void synchronize ()
 Method synchronize waits for finalization of the task.
bool probe () const
 Method test if this Task is already finished.
Id getId () const
 Query a task for its id.
virtual void writeAt (std::ostream &stream) const
 Override method of Printable.

Private Member Functions

 LAMA_LOG_DECL_STATIC_LOGGER (logger)

Static Private Member Functions

static LAMAThreadPoolgetThreadPool ()

Private Attributes

boost::shared_ptr< LAMAThreadTaskmTask

Static Private Attributes

static boost::shared_ptr
< LAMAThreadPool
theThreadPool
 Thread pool will be allocated on need, shared pointer guarantees deallocation.

Detailed Description

Class for asynchronous tasks.

Task represents a asynchronous task, which allows synchronization. To start an asynchronous task one simply have to construct a Task object and pass a pointer to the function to execute to its constructor. If the signature of the function does not fit boost::bind can be used to build an appropriate function pointer. Be careful with reference because they need special wrapper objects. E.g.

 void foo( Vector& y, const Matrix& A, Scalar alpha, const Vector& x);

 ...

 void bar( Vector& y, const Matrix& A, const Scalar alpha, const Vector& x)
 {
     //construct function pointer
     boost::function<void()> function = boost::bind( foo, boost::ref(y), alpha ,boost::cref(A), boost::cref(x) );

     //start asynchronous tasks
     Task task( function );

     //do other work

     //wait for asynchronous task to finish
     task.synchronize();
 }

Member Typedef Documentation

typedef unsigned int lama::Task::Id

Constructor & Destructor Documentation

lama::Task::Task ( boost::function< void()>  function,
int  numOmpThreads = 0 
)

Construct a new task that will execute a void function, no arguments.

Its safe to start a recursive Task, e.g. to start a Task from a Task. The recursive Task might execute synchronously to avoid deadlock situations.

Parameters:
[in]functionis void() function executed by the new created task.
[in]numOmpThreadsnumber of openmp threads the task should use (if numOmpThreads == 0 omp_get_max_threads() is used)

By using boost::function, it is possible to bind arguments via boost::bind.

References getThreadPool(), mTask, and lama::LAMAThreadPool::schedule().

Destructor.

Implicitly synchronizes the Task.


Member Function Documentation

Query a task for its id.

References lama::max(), and mTask.

Referenced by writeAt().

LAMAThreadPool & lama::Task::getThreadPool ( ) [static, private]

References theThreadPool.

Referenced by synchronize(), and Task().

bool lama::Task::probe ( ) const

Method test if this Task is already finished.

Returns:
if this Task is already finished.

References lama::LAMAThreadTask::FINISHED, and mTask.

Method synchronize waits for finalization of the task.

References getThreadPool(), LAMA_THROWEXCEPTION, mTask, and lama::LAMAThreadPool::wait().

void lama::Task::writeAt ( std::ostream &  stream) const [virtual]

Override method of Printable.

Parameters:
[out]streamthe stream to write to.

Reimplemented from Printable.

References getId().


Field Documentation

boost::shared_ptr<LAMAThreadTask> lama::Task::mTask [private]

Referenced by getId(), probe(), synchronize(), and Task().

boost::shared_ptr< LAMAThreadPool > lama::Task::theThreadPool [static, private]

Thread pool will be allocated on need, shared pointer guarantees deallocation.

Referenced by getThreadPool().


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