Xmipp  v3.23.11-Nereus
Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
ParallelTaskDistributor Class Referenceabstract

#include <xmipp_threads.h>

Inheritance diagram for ParallelTaskDistributor:
Inheritance graph
[legend]

Public Member Functions

 ParallelTaskDistributor (size_t nTasks, size_t bSize)
 
virtual ~ParallelTaskDistributor ()
 
void clear ()
 
void setBlockSize (size_t bSize)
 
int getBlockSize () const
 
bool getTasks (size_t &first, size_t &last)
 
bool setAssignedTasks (size_t tasks)
 

Public Attributes

size_t numberOfTasks
 

Protected Member Functions

virtual void lock ()=0
 
virtual void unlock ()=0
 
virtual bool distribute (size_t &first, size_t &last)=0
 

Protected Attributes

size_t blockSize
 
size_t assignedTasks
 

Detailed Description

This class distributes dynamically N tasks between parallel workers.

This class is a generalization of a common task in a parallel environment of dynamically distribute N tasks between workers(threads or mpi process). Each worker will ask for a group of tasks, process it and ask for more tasks until there is not more task to process.

This class is abstract and only serves as base for concrete implementations, which will provides the specific lock mechanisms and the way of distribution.

Definition at line 366 of file xmipp_threads.h.

Constructor & Destructor Documentation

◆ ParallelTaskDistributor()

ParallelTaskDistributor::ParallelTaskDistributor ( size_t  nTasks,
size_t  bSize 
)

Constructor. The number of jobs and block size should be provided.Constructor for Master node.

Definition at line 289 of file xmipp_threads.cpp.

290 {
291  if (!(nTasks && bSize && bSize <= nTasks))
292  REPORT_ERROR(ERR_ARG_INCORRECT, "nTasks and bSize should be > 0, also bSize <= nTasks");
293 
294  numberOfTasks = nTasks;
295  blockSize = bSize;
296  assignedTasks = 0;
297 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
Incorrect argument received.
Definition: xmipp_error.h:113

◆ ~ParallelTaskDistributor()

virtual ParallelTaskDistributor::~ParallelTaskDistributor ( )
inlinevirtual

Destructor.

Definition at line 386 of file xmipp_threads.h.

387  {}

Member Function Documentation

◆ clear()

void ParallelTaskDistributor::clear ( )

Restart the number of assigned tasks and distribution again. This method should only be called in the main thread before start distributing the tasks between the workers threads.

Definition at line 299 of file xmipp_threads.cpp.

300 {
301  lock();
302  assignedTasks = 0;
303  unlock();
304 }
virtual void unlock()=0
virtual void lock()=0

◆ distribute()

virtual bool ParallelTaskDistributor::distribute ( size_t &  first,
size_t &  last 
)
protectedpure virtual

◆ getBlockSize()

int ParallelTaskDistributor::getBlockSize ( ) const

Return the number of tasks assigned in each request

Definition at line 313 of file xmipp_threads.cpp.

314 {
315  return blockSize;
316 }

◆ getTasks()

bool ParallelTaskDistributor::getTasks ( size_t &  first,
size_t &  last 
)

Gets parallel tasks.

This function will be called by workers for asking tasks until there are not more tasks to process. Example:

//...
//...
//function to perform some operation
//to N images executed in parellel
void processSeveralImages()
{
size_t firstImage, lastImage;
while (td->getTasks(firstImage, lastImage))
for (size_t image = firstImage; image <= lastImage; ++image)
{
//...
processOneImage(image);
//...
}
}

Definition at line 318 of file xmipp_threads.cpp.

319 {
320  lock();
321  bool result = distribute(first, last);
322  unlock();
323  return result;
324 }
virtual bool distribute(size_t &first, size_t &last)=0
glob_log first
virtual void unlock()=0
virtual void lock()=0

◆ lock()

virtual void ParallelTaskDistributor::lock ( )
protectedpure virtual

Implemented in ThreadTaskDistributor.

◆ setAssignedTasks()

bool ParallelTaskDistributor::setAssignedTasks ( size_t  tasks)

Definition at line 326 of file xmipp_threads.cpp.

327 {
328  if (tasks < 0 || tasks >= numberOfTasks)
329  return false;
330  lock();
331  assignedTasks = tasks;
332  unlock();
333  return true;
334 }
virtual void unlock()=0
virtual void lock()=0

◆ setBlockSize()

void ParallelTaskDistributor::setBlockSize ( size_t  bSize)

Set the number of tasks assigned in each request

Definition at line 306 of file xmipp_threads.cpp.

307 {
308  lock();
309  blockSize = bSize;
310  unlock();
311 }
virtual void unlock()=0
virtual void lock()=0

◆ unlock()

virtual void ParallelTaskDistributor::unlock ( )
protectedpure virtual

Implemented in ThreadTaskDistributor.

Member Data Documentation

◆ assignedTasks

size_t ParallelTaskDistributor::assignedTasks
protected

Definition at line 372 of file xmipp_threads.h.

◆ blockSize

size_t ParallelTaskDistributor::blockSize
protected

Definition at line 370 of file xmipp_threads.h.

◆ numberOfTasks

size_t ParallelTaskDistributor::numberOfTasks

Definition at line 376 of file xmipp_threads.h.


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