The SKIRT project
advanced radiative transfer for astrophysics
Public Member Functions | Private Attributes | List of all members
ChunkMaker Class Reference

#include <ChunkMaker.hpp>

Public Member Functions

 ChunkMaker ()
 
bool callForNext (const std::function< void(size_t firstIndex, size_t numIndices)> &target)
 
void initialize (size_t maxIndex, int numThreads, int numProcs=1)
 
bool next (size_t &firstIndex, size_t &numIndices)
 

Private Attributes

size_t _chunkSize
 
size_t _maxIndex
 
std::atomic< size_t > _nextIndex
 

Detailed Description

The ChunkMaker class implements a heuristic for chopping a range of indices from zero to \(N-1\) into smaller ranges of consecutive indices called chunks. An instance of the ChunkMaker class is used, for example, by some Parallel subclasses to distribute a range of tasks over parallel execution threads and/or processes. Assuming that each index maps to a particular task, the function performing the tasks is repeatedly handed the first index of the chunk and the number of indices in the chunk, and it is expected to iterate over the specified index range. The chunk sizes are determined by the heuristic in the ChunkMaker object to achieve optimal load balancing given the available parallel resources, while still maximally reducing the overhead of handing out the chunks.

Constructor & Destructor Documentation

◆ ChunkMaker()

ChunkMaker::ChunkMaker ( )

The default (and only) constructor initializes the ChunkMaker object to an empty range.

Member Function Documentation

◆ callForNext()

bool ChunkMaker::callForNext ( const std::function< void(size_t firstIndex, size_t numIndices)> &  target)

This function gets the next chunk, and if one is still available, it calls the specified target with the corresponding first index and number of indices, and returns true. If no more chunks are available, the target is not invoked and this function returns false. This function uses an atomic operation to obtain the next chunk so it can safely be called from multiple concurrent execution threads, as long as the target function is thread-safe as well.

◆ initialize()

void ChunkMaker::initialize ( size_t  maxIndex,
int  numThreads,
int  numProcs = 1 
)

This function initializes the ChunkMaker object to the specified range (from zero to \(N-1\)), using the specified number of threads and processes to help determine an appropriate chunk size.

◆ next()

bool ChunkMaker::next ( size_t &  firstIndex,
size_t &  numIndices 
)

This function gets the next chunk, in the form of the first index and the number of indices in the chunk. If a chunk is still available, the function places a chunk index range in its arguments and returns true. If no more chunks are available, the output arguments remain unchanged and the function returns false. This function uses an atomic operation to obtain the next chunk so it can safely be called from multiple concurrent execution threads.


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