ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SpeculativeFor.h

The deterministic reservations of Blelloch, Fineman, Gibbons and Shun, beyond the book. More...

Files

file  ADS/parallel/SpeculativeFor.h
 The deterministic reservations of Blelloch, Fineman, Gibbons and Shun, beyond the book: speculativeFor( ex , step , s , e ) runs, on the threads of an executor [see Executor.h], a loop whose iterations \( s , s + 1 , \ldots , e - 1 \) a sequential program would run in this order, each depending on the ones before it, and gives the result of the sequential loop whatever the executor and the number of its threads (see Deterministic reservations, greedy algorithms).

Concepts

concept  ADS::SpeculativeStep
 the two steps of an iteration [see speculativeFor()]

Classes

class  ADS::Reservation
 a place reserved by the least of the iterations that want it More...
struct  ADS::SpeculativeCounts
 what speculativeFor() did: its rounds and the iterations tried, the failed ones counted again More...

Functions

template<Executor E, SpeculativeStep S>
void ADS::speculativeFor (E &ex, S &step, std::size_t s, std::size_t e, std::size_t g=50, SpeculativeCounts *c=nullptr)
 the iterations \( [ s , e ) \) of step by deterministic reservations, with the result of the sequential loop; the rounds of at most \( r_{\mathrm{max}} = \lfloor ( e - s ) / g \rfloor + 1 \) iterations; its work in *c, if given

Detailed description

The deterministic reservations of Blelloch, Fineman, Gibbons and Shun, beyond the book.

The definitions, the invariants and the costs of the area are in Theory of parallelism.

Function documentation

◆ speculativeFor()

template<Executor E, SpeculativeStep S>
void ADS::speculativeFor ( E & ex,
S & step,
std::size_t s,
std::size_t e,
std::size_t g = 50,
SpeculativeCounts * c = nullptr )

the iterations \( [ s , e ) \) of step by deterministic reservations, with the result of the sequential loop; the rounds of at most \( r_{\mathrm{max}} = \lfloor ( e - s ) / g \rfloor + 1 \) iterations; its work in *c, if given

In pseudocode (Blelloch, Fineman, Gibbons and Shun, beyond the book; it is correct by Lemma 4 of Deterministic reservations, greedy algorithms):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{speculativeFor}( \mathit{ex} , \mathit{step} , s , e , g ) \cr 2 & \quad r \gets r_{\mathrm{max}};\ I \gets \text{an empty sequence}; \ d \gets s \cr 3 & \quad \textbf{while}\ I \ne \emptyset \textbf{ or } d < e \ \textbf{do} \cr 4 & \qquad m \gets \min( r , |I| + e - d );\ \text{append } d , d + 1 , \ldots \text{ to } I \text{ up to } m \text{ iterations}; \ \text{advance } d \cr 5 & \qquad \textbf{foreach}\ i \in I \text{ in parallel}\ \textbf{do} \ k_i \gets \mathit{step}.\mathrm{reserve}( i ) \cr 6 & \qquad \textbf{foreach}\ i \in I \text{ with } k_i \text{ in parallel} \ \textbf{do}\ k_i \gets \textbf{not}\ \mathit{step}.\mathrm{commit}( i ) \cr 7 & \qquad I \gets \text{the } i \in I \text{ with } k_i \text{, in their} \text{ order (a parallel pack)} \cr 8 & \qquad \textbf{if}\ |I| > m / 5\ \textbf{then}\ r \gets \max( r / 2 , r_{\mathrm{max}} / 64 + 1 , |I| ) \cr 9 & \qquad \textbf{else if}\ |I| < m / 10\ \textbf{then}\ r \gets \min( 2 r , r_{\mathrm{max}} ) \end{array}} \]

Precondition
\( s \le e \), \( g \ge 1 \), the least iteration of a round commits