Algorithm::GaMultithreadingAlgorithm Class Reference

This class implements features to allow parallel execution of some operations of genetic algorithm. It runs multiple threads which execute work which can be done in parallel, one control thread which controls workers and executes all operations which must be executed sequentially. More...

#include <D:/Temp/vs/gal/source/MultithreadingAlgorithm.h>

Inherits Algorithm::GaBaseAlgorithm.

Inherited by Algorithm::SimpleAlgorithms::GaIncrementalAlgorithm, and Algorithm::SimpleAlgorithms::GaSimpleAlgorithm.

List of all members.

Public Member Functions

GAL_API GaMultithreadingAlgorithm (const GaMultithreadingAlgorithmParams &parameters)
 This constructor initializes control structures with user-defined parameters.
virtual GAL_API ~GaMultithreadingAlgorithm ()
 Destructors releases acquired resources such as threads and synchronization objects and memory used by control structures.
virtual GAL_API void GACALL SetAlgorithmParameters (const GaAlgorithmParams &parameters)
 This method should be called from classes that inherit this class when user changes algorithm parameters. It updates structures which control multithreading execution.
virtual GAL_API bool GACALL WaitForThreads ()
 This method waits for control and working thread to finish their execution.

Protected Member Functions

virtual void GACALL BeforeWorkers ()
 This method is called before workers execute their steps. It is executed in control thread context.
virtual void GACALL WorkStep (int workerId)
 After control thread executes BeforeWork method, working threads starts execution and steps in this method. Working threads get their identification numbers which are assigned sequentially to them.
virtual void GACALL AfterWorkers ()
 This method is called after workers finish their steps. It is executed in control thread context.
virtual bool GACALL OnStart ()
 This method starts control and working threads.
virtual bool GACALL OnStop ()
 This method waits for control and working thread to finish their execution.
virtual bool GACALL OnPause ()
 This method waits for control and working thread to finish their execution.
virtual bool GACALL OnResume ()
 This method starts control and working threads.
virtual GAL_API void GACALL ControlFlow ()
 This method implements work flow of control thread.
virtual GAL_API void GACALL WorkFlow ()
 This method implements work flow of working thread.
virtual GAL_API bool GACALL StartThreads ()
 This method starts control and working threads.

Protected Attributes

SysSemaphoreObject _workerForkSync
 Object for synchronization of working step start.
SysEventObject _workerJoinSync
 Object for synchronization of worker threads with control thread after end of work step.
int _workersThreadIn
 Number of workers thread currently executing working section of code.
bool _parametersChange
 This flag indicates that user has changed some aspect of the algorithm, but algorithm didn't refresh its control structures.
int _numberOfThreads
 Number of threads which are used by algorithm (working and control threads).
volatile int _workerIdCounter
 This attribute is used for assigning identifiers to working threads when algorithm starts execution.
GaThread ** _threads
 Array of threads which are used by the algorithm (working and control threads).

Static Private Member Functions

static ThreadFunctionReturn GACALL ControlFlowWrapper (GaThread *thread, void *params)
 This method is used as wrapper for control flow method and is used when algorithm starts control thread.
static ThreadFunctionReturn GACALL WorkFlowWrapper (GaThread *thread, void *params)
 This method is used as wrapper for working flow method and is used when algorithm starts working thread.


Detailed Description

This class implements features to allow parallel execution of some operations of genetic algorithm. It runs multiple threads which execute work which can be done in parallel, one control thread which controls workers and executes all operations which must be executed sequentially.

This class has no built-in synchronizator, so LOCK_OBJECT and LOCK_THIS_OBJECT macros cannot be used with instances of this class, but all public method and operators are thread-safe.


Constructor & Destructor Documentation

Algorithm::GaMultithreadingAlgorithm::GaMultithreadingAlgorithm ( const GaMultithreadingAlgorithmParams parameters  ) 

This constructor initializes control structures with user-defined parameters.

Parameters:
parameters reference to algorithm parameters.

Algorithm::GaMultithreadingAlgorithm::~GaMultithreadingAlgorithm (  )  [virtual]

Destructors releases acquired resources such as threads and synchronization objects and memory used by control structures.


Member Function Documentation

void Algorithm::GaMultithreadingAlgorithm::SetAlgorithmParameters ( const GaAlgorithmParams parameters  )  [virtual]

This method should be called from classes that inherit this class when user changes algorithm parameters. It updates structures which control multithreading execution.

More details are given in specification of GaAlgorithm::SetAlgorithmParameters method.

This method is thread-safe.

Implements Algorithm::GaAlgorithm.

Reimplemented in Algorithm::SimpleAlgorithms::GaIncrementalAlgorithm, and Algorithm::SimpleAlgorithms::GaSimpleAlgorithm.

bool Algorithm::GaMultithreadingAlgorithm::WaitForThreads (  )  [virtual]

This method waits for control and working thread to finish their execution.

Returns:
Method returns true if ther were no errors during waiting.

virtual void GACALL Algorithm::GaMultithreadingAlgorithm::BeforeWorkers (  )  [inline, protected, virtual]

This method is called before workers execute their steps. It is executed in control thread context.

Reimplemented in Algorithm::SimpleAlgorithms::GaIncrementalAlgorithm, and Algorithm::SimpleAlgorithms::GaSimpleAlgorithm.

virtual void GACALL Algorithm::GaMultithreadingAlgorithm::WorkStep ( int  workerId  )  [inline, protected, virtual]

After control thread executes BeforeWork method, working threads starts execution and steps in this method. Working threads get their identification numbers which are assigned sequentially to them.

Parameters:
workerId identification number of working thread

Reimplemented in Algorithm::SimpleAlgorithms::GaIncrementalAlgorithm, and Algorithm::SimpleAlgorithms::GaSimpleAlgorithm.

virtual void GACALL Algorithm::GaMultithreadingAlgorithm::AfterWorkers (  )  [inline, protected, virtual]

This method is called after workers finish their steps. It is executed in control thread context.

Reimplemented in Algorithm::SimpleAlgorithms::GaIncrementalAlgorithm, and Algorithm::SimpleAlgorithms::GaSimpleAlgorithm.

virtual bool GACALL Algorithm::GaMultithreadingAlgorithm::OnStart (  )  [inline, protected, virtual]

This method starts control and working threads.

Implements Algorithm::GaBaseAlgorithm.

virtual bool GACALL Algorithm::GaMultithreadingAlgorithm::OnStop (  )  [inline, protected, virtual]

This method waits for control and working thread to finish their execution.

Implements Algorithm::GaBaseAlgorithm.

virtual bool GACALL Algorithm::GaMultithreadingAlgorithm::OnPause (  )  [inline, protected, virtual]

This method waits for control and working thread to finish their execution.

Implements Algorithm::GaBaseAlgorithm.

virtual bool GACALL Algorithm::GaMultithreadingAlgorithm::OnResume (  )  [inline, protected, virtual]

This method starts control and working threads.

Implements Algorithm::GaBaseAlgorithm.

void Algorithm::GaMultithreadingAlgorithm::ControlFlow (  )  [protected, virtual]

This method implements work flow of control thread.

void Algorithm::GaMultithreadingAlgorithm::WorkFlow (  )  [protected, virtual]

This method implements work flow of working thread.

bool Algorithm::GaMultithreadingAlgorithm::StartThreads (  )  [protected, virtual]

This method starts control and working threads.

Returns:
Method returs true if all threads are started successafully.

ThreadFunctionReturn Algorithm::GaMultithreadingAlgorithm::ControlFlowWrapper ( GaThread thread,
void *  params 
) [static, private]

This method is used as wrapper for control flow method and is used when algorithm starts control thread.

Parameters:
thread pointer to control thread.
params pointer to parameters which are passed by algorithm to the control thread.
Returns:
Returns status of the thread when it finishes its execution.

ThreadFunctionReturn Algorithm::GaMultithreadingAlgorithm::WorkFlowWrapper ( GaThread thread,
void *  params 
) [static, private]

This method is used as wrapper for working flow method and is used when algorithm starts working thread.

Parameters:
thread pointer to working thread.
params pointer to parameters which are passed by algorithm to the working thread.
Returns:
Returns status of the thread when it finishes its execution.


Member Data Documentation

Object for synchronization of working step start.

Object for synchronization of worker threads with control thread after end of work step.

Number of workers thread currently executing working section of code.

This flag indicates that user has changed some aspect of the algorithm, but algorithm didn't refresh its control structures.

Number of threads which are used by algorithm (working and control threads).

This attribute is used for assigning identifiers to working threads when algorithm starts execution.

Array of threads which are used by the algorithm (working and control threads).


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

Genetic Algorithm Library
Coolsoft Software Development