The SKIRT project
advanced radiative transfer for astrophysics
Protected Member Functions | Private Member Functions | Private Attributes | List of all members
MultiParallel Class Referenceabstract

#include <MultiParallel.hpp>

Inheritance diagram for MultiParallel:
Inheritance graph
[legend]

Protected Member Functions

void activateThreads ()
 
void constructThreads (int numThreads)
 
void destroyThreads ()
 
int numThreads ()
 
void waitForThreads ()
 
- Protected Member Functions inherited from Parallel
 Parallel ()
 

Private Member Functions

virtual bool doSomeWork ()=0
 
void reportException (FatalError *exception)
 
void run (int threadIndex)
 
bool threadsActive ()
 

Private Attributes

std::vector< bool > _active
 
std::condition_variable _conditionChildren
 
std::condition_variable _conditionParent
 
FatalError_exception
 
std::mutex _mutex
 
int _numThreads
 
std::atomic< bool > _terminate
 
std::vector< std::thread > _threads
 

Additional Inherited Members

- Public Member Functions inherited from Parallel
virtual ~Parallel ()
 
virtual void call (size_t maxIndex, std::function< void(size_t firstIndex, size_t numIndices)> target)=0
 

Detailed Description

MultiParallel is an intermediate, abstract class that offers facilities for implementing parallel subclasses that use multiple parallel execution threads.

Specifically, the class offers functions to create a given number of child threads and hand out work to them. The parent thread is never used to perform actual work, so that it remains available for communicating with other processes. Because the parent thread is supposed to consume very little resources, it is not counted towards the number of threads configured by the user.

When an exception is thrown by one of the child threads, all other threads are gracefully shut down and a FatalError exception is thrown in the context of the parent thread. If the original exception was a FatalError instance, the newly thrown exception is a copy thereof. Otherwise a fresh FatalError instance is created with a generic error message.

This class uses the standard low-level C++ multi-threading capabilities. It is designed to minimize the run-time overhead for handing out parallel tasks. Between invocations of the call() function, the parallel threads are put in wait so that they consume no CPU cycles (and very little memory).

Member Function Documentation

◆ activateThreads()

void MultiParallel::activateThreads ( )
protected

This function activates the child threads so they start doing work (i.e. calling the doSomeWork() function until it returns false).

◆ constructThreads()

void MultiParallel::constructThreads ( int  numThreads)
protected

This function constructs the specified number of parallel child threads (not including the parent thread) and waits for them to become ready (in the inactive state).

◆ destroyThreads()

void MultiParallel::destroyThreads ( )
protected

This function destructs the child threads constucted with the constructThreads() function.

◆ doSomeWork()

virtual bool MultiParallel::doSomeWork ( )
privatepure virtual

The function to do the actual work; called from within run(). The function should perform some limited amount of work and then return true if more work might be available for this cycle, and false if not.

Implemented in MultiHybridParallel, and MultiThreadParallel.

◆ numThreads()

int MultiParallel::numThreads ( )
inlineprotected

This function returns the number of parallel child threads (not including the parent thread) specified to constructThreads().

◆ reportException()

void MultiParallel::reportException ( FatalError exception)
private

This function reports an exception from within any thread.

◆ run()

void MultiParallel::run ( int  threadIndex)
private

This function gets executed inside each of the parallel threads.

◆ threadsActive()

bool MultiParallel::threadsActive ( )
private

This function returns true if at least one of the child threads is still active, and false if not. This function does not perform any locking; callers should lock the shared data members of this class instance.

◆ waitForThreads()

void MultiParallel::waitForThreads ( )
protected

This function blocks until all child threads have become inactive (i.e. the doSomeWork() function has returned false for all threads).


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