gatb.core-API-1.4.2
IDispatcher Class Referenceabstract

Dispatching of commands. More...

#include <ICommand.hpp>

Inheritance diagram for IDispatcher:
Inheritance graph

Public Member Functions

virtual size_t dispatchCommands (std::vector< ICommand * > &commands, ICommand *postTreatment=0)=0
 
virtual size_t getExecutionUnitsNumber ()=0
 
template<typename Item , typename Functor >
Status iterate (Iterator< Item > *iterator, const Functor &functor, size_t groupSize=1000, bool deleteSynchro=false)
 
template<typename Item , typename Functor >
Status iterate (const Iterator< Item > &iterator, const Functor &functor, size_t groupSize=1000, bool deleteSynchro=false)
 
virtual void setGroupSize (size_t groupSize)=0
 
virtual size_t getGroupSize () const =0
 
- Public Member Functions inherited from SmartPointer
void use ()
 
void forget ()
 
- Public Member Functions inherited from ISmartPointer
virtual ~ISmartPointer ()
 

Protected Member Functions

virtual system::ISynchronizernewSynchro ()=0
 
template<typename Item , typename Functor >
Status iterate (Iterator< Item > *iterator, std::vector< Functor * > &functors, size_t groupSize=1000, bool deleteSynchro=false)
 
- Protected Member Functions inherited from SmartPointer
 SmartPointer ()
 
virtual ~SmartPointer ()
 

Detailed Description

Dispatching of commands.

Interface that launches several commands.

According to the implementation, the dispatching can be done in a serial or in a parallelized way.

Note that a post treatment command can be provided and will be launched when all the commands have finished.

Note that this interface could also be implemented for dispatching commands over a network in order to use a grid of computers. We could imagine that the commands dispatching consists in launching some RCP calls, or creating some web services calls. The important point is that, from the client point of view, her/his code should not change, except the actual kind of IDispatcher instance provided to the algorithm.

Sample of use:

// We define a command class
class MyCommand : public ICommand, public SmartPointer
{
public:
MyCommand (int i) : _i(i) {}
void execute () { printf ("Going to sleep %d...\n", _i); sleep (_i); }
private:
int _i;
};
int main (int argc, char* argv[])
{
// We create a list of commands
std::vector<ICommand*> commands;
commands.push_back (new MyCommand(2));
commands.push_back (new MyCommand(5));
commands.push_back (new MyCommand(3));
// We create a commands dispatcher that parallelizes the execution of the commands.
IDispatcher* dispatcher = new Dispatcher ();
// We launch the 3 commands.
dispatcher->dispatchCommands (commands, 0);
// Here, we should be waiting until the second command (that waits for 5 seconds) is finished.
return 0;
}
See also
ICommand

Member Function Documentation

virtual size_t dispatchCommands ( std::vector< ICommand * > &  commands,
ICommand postTreatment = 0 
)
pure virtual

Dispatch commands execution in some separate contexts (threads for instance). Once the commands are launched, this dispatcher waits for the commands finish. Then, it may have to execute a post treatment command (if any).

Parameters
[in]commands: commands to be executed
[in]postTreatment: command to be executed after all the commands are done
Returns
time elapsed in msec

Implemented in Dispatcher, and SerialDispatcher.

virtual size_t getExecutionUnitsNumber ( )
pure virtual

Returns the number of execution units for this dispatcher. For instance, it could be the number of cores in a multi cores architecture.

Returns
the number of execution units.

Implemented in Dispatcher, and SerialDispatcher.

virtual size_t getGroupSize ( ) const
pure virtual

Get the number of items to be retrieved from the iterator by one thread in a synchronized way.

Returns
the number of items.

Implemented in Dispatcher, and SerialDispatcher.

Status iterate ( Iterator< Item > *  iterator,
const Functor &  functor,
size_t  groupSize = 1000,
bool  deleteSynchro = false 
)
inline

Iterate a provided instance. The provided functor is cloned N times, where N is the number of threads to be created; each thread will use its own instance of functor.

One has to be aware that the copy constructor of the Functor type has to be well defined. For instance, if one wants to share a resource hold by the 'functor' argument, the copied Functor object may have a direct access to the shared resource, which potential issues with concurrent accesses on it. A way to avoid this kind of issue is to define a copy constructor that will create an instance of Functor with specific synchronization process (see ISynchronizer for that).

Parameters
[in]iterator: the iterator to be iterated
[in]functor: functor object to be cloned N times, one per thread
[in]groupSize: number of items to be retrieved in a single lock/unlock block
[in]deleteSynchro: if false, destructor of functors are called in each thread; if true, destructor of functors are called synchronously
Status iterate ( const Iterator< Item > &  iterator,
const Functor &  functor,
size_t  groupSize = 1000,
bool  deleteSynchro = false 
)
inline

Iterate a provided instance. The provided functor is cloned N times, where N is the number of threads to be created; each thread will use its own instance of functor.

One has to be aware that the copy constructor of the Functor type has to be well defined. For instance, if one wants to share a resource hold by the 'functor' argument, the copied Functor object may have a direct access to the shared resource, which potential issues with concurrent accesses on it. A way to avoid this kind of issue is to define a copy constructor that will create an instance of Functor with specific synchronization process (see ISynchronizer for that).

Parameters
[in]iterator: the iterator to be iterated
[in]functor: functor object to be cloned N times, one per thread
[in]groupSize: number of items to be retrieved in a single lock/unlock block
[in]deleteSynchro: if false, destructor of functors are called in each thread; if true, destructor of functors are called synchronously
Status iterate ( Iterator< Item > *  iterator,
std::vector< Functor * > &  functors,
size_t  groupSize = 1000,
bool  deleteSynchro = false 
)
inlineprotected

Iterate a provided Iterator instance; each iterated items are processed through some functors. According to the dispatcher implementation, we can hence iterate in a parallel way on several cores. Since we can have concurrent access, the iteration is protected by a synchronizer. Note that it is possible to group items to be given to one functor; grouping may be important for performance issues since we reduce the number of synchronizer lock/unlock calls and so use in an optimal way the available cores.

Parameters
[in]iterator: the iterator to be iterated
[in]functors: vector of functors that are fed with iterated items
[in]groupSize: number of items to be retrieved in a single lock/unlock block
[in]deleteSynchro: if false, destructor of functors are called in each thread; if true, destructor of functors are called synchronously
virtual system::ISynchronizer* newSynchro ( )
protectedpure virtual

Factory method for synchronizer instantiation.

Returns
the created synchronizer.
virtual void setGroupSize ( size_t  groupSize)
pure virtual

Set the number of items to be retrieved from the iterator by one thread in a synchronized way.

Parameters
[in]groupSize: number of items to be retrieved.

Implemented in Dispatcher, and SerialDispatcher.


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