Peano
Loading...
Searching...
No Matches
swift2::TaskEnumerator Class Reference

Task enumerator for Swift 2. More...

#include <TaskEnumerator.h>

Inheritance diagram for swift2::TaskEnumerator:
Collaboration diagram for swift2::TaskEnumerator:

Public Member Functions

 TaskEnumerator ()=default
 
- Public Member Functions inherited from tarch::multicore::TaskEnumerator
 TaskEnumerator ()
 
void reset ()
 
int getNumber ()
 Get a new task number.
 
void releaseNumber (int value)
 Release a task number.
 
int size () const
 Number of numbers handed out so far.
 
std::string toString () const
 

Static Public Member Functions

static void lockResources (const tarch::la::Vector< TwoPowerD, int > &numbers)
 Lock resources.
 
static void unlockResources (const tarch::la::Vector< TwoPowerD, int > &numbers)
 Free resources.
 

Private Types

using ResourcesSemaphore = tarch::multicore::MultiReadSingleWriteSemaphore
 Protect the _resources map.
 

Static Private Member Functions

static void insertNumber (int number)
 Create a new number.
 
static void insertNumbers (const tarch::la::Vector< TwoPowerD, int > &numbers)
 

Static Private Attributes

static std::map< int, tarch::multicore::BooleanSemaphore * > _resources
 Each task is tied to a resource, i.e.
 
static ResourcesSemaphore _resourcesSemaphore
 

Additional Inherited Members

- Static Public Attributes inherited from tarch::multicore::TaskEnumerator
static constexpr int NoNumber = -1
 

Detailed Description

Task enumerator for Swift 2.

Every particle type (species) has a marker associate to the vertices and cells. So they are all unique numbers. Therefore, each of these markers is tied to one rank-global enumerator. These enumerators may not give out the same numbers even for different species. Therefore, we make the enumerator instances (of this type) all become a decorator for tarch::Enumerator which delegates all calls to a global instance. As a result, each marker type can work with its own instance of this TaskEnumerator, but behind the scenes, all requests are funneled through to one big number generator.

Besides the administration of the tasks, the type also maintains a big hash map of resources, which is really just a hash map to boolean semaphores plus some routines that manage access to them. These can be used to ensure that no two cells access the same vertex concurrently.

We can use objects of this type as proxy to access a big global table with one lock per global single task number. So if we have a cell with four neighbours where race conditions might occur, we can use lockResources() to ensure that we exclusively access those neighbours. In Swift's original terminology, these are the conflicts that we resolve at runtime.

Thread safety

The class is thread-safe as the underlying tarch::Enumerator object is thread-safe. However, we need an additional semaphore for the underlying map of resources.

Definition at line 53 of file TaskEnumerator.h.

Member Typedef Documentation

◆ ResourcesSemaphore

Protect the _resources map.

This map holds all the boolean semaphores. An arbitrary number of threads can hit this map at the same time and try to book some semaphores. This is technically a read access, as they do not write to the map. They only read from the map.

Contrary to this, we may not change the map, i.e. insert new semaphores, if others try to read it, as new inserts might change the underlying memory arrangement and hence mess everything up.

Definition at line 122 of file TaskEnumerator.h.

Constructor & Destructor Documentation

◆ TaskEnumerator()

swift2::TaskEnumerator::TaskEnumerator ( )
default

Member Function Documentation

◆ insertNumber()

static void swift2::TaskEnumerator::insertNumber ( int number)
staticprivate

Create a new number.

This routine is robust, i.e. if the number does exist already, we do not do anything.

◆ insertNumbers()

static void swift2::TaskEnumerator::insertNumbers ( const tarch::la::Vector< TwoPowerD, int > & numbers)
staticprivate

◆ lockResources()

static void swift2::TaskEnumerator::lockResources ( const tarch::la::Vector< TwoPowerD, int > & numbers)
static

Lock resources.

This routine is used to lock a set of indices. This could be the \( 2^d \) adjacent vertices of a cell. This way, we ensure that noone else uses these vertices concurrently. It can also be used, obviously, to lock the \( 2^d \) adjacent cells of a vertex in case we know their indices.

The implementation is not a simple while loop: We loop over the entries of numbers and try to lock them. If one lock fails, we unlock all previous ones immediately again, yield, and then try again. This way, we avoid deadlocks if multiple cells next to each other each try to lock their adjacent vertices but actually only get around half of the locks they want.

Entries set to tarch::Enumerator::NoNumber are ignored.

Thread-safety

At any point throughout the calculation, another thread might insert new resource identifiers into the _resources map through getNumber() calls. If such a call happens at exactly the same time as _resources.at( numbers[counter] ), we might end up with a seg fault. Therefore, the locking itself is protected until we get all resources.

Lazy generation of indices

getNumber() explains why there's no direct 1:1 match of numbers to resources. Therefore, we create indices lazily at this point through insertNumbers().

◆ unlockResources()

static void swift2::TaskEnumerator::unlockResources ( const tarch::la::Vector< TwoPowerD, int > & numbers)
static

Free resources.

Counterpart to lockResources().

Entries set to tarch::Enumerator::NoNumber are ignored.

Field Documentation

◆ _resources

std::map< int, tarch::multicore::BooleanSemaphore* > swift2::TaskEnumerator::_resources
staticprivate

Each task is tied to a resource, i.e.

a unique number, which we can lock and unlock again.

Definition at line 108 of file TaskEnumerator.h.

◆ _resourcesSemaphore

ResourcesSemaphore swift2::TaskEnumerator::_resourcesSemaphore
staticprivate

Definition at line 124 of file TaskEnumerator.h.


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