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

The farm with feedback on the workers of an executor, beyond the book. More...

Files

file  ADS/parallel/Farm.h
 The farm with feedback on the workers of an executor [see Executor.h], beyond the book: a master emits tasks, \( w \) workers turn each task into a result, and the results go back to the master, which may emit more tasks from them and decides when the farm ends, the pattern of the master and workers of FastFlow (its farm with the channel of feedback from the workers to the emitter).

Functions

template<class T, class R, WorkerExecutor E, class S, class O, class W>
void ADS::feedbackFarm (E &ex, std::size_t w, S &&start, O &&onResult, W &&work)
 the farm of a master and \( w \) workers with feedback: start( emit ) (or start( emit , w )), then onResult( r , emit ) on each result of work( t ) (or work( t , i ) on the worker \( i \)), until it returns false or no task is left
template<class T, class R, class S, class O, class W>
void ADS::feedbackFarm (FastFlowExecutor &ex, std::size_t w, S &&start, O &&onResult, W &&work)
 the farm with feedback of feedbackFarm() above on a farm of FastFlow, with an emitter of its own and \( w \) workers

Detailed description

The farm with feedback on the workers of an executor, beyond the book.

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

Function documentation

◆ feedbackFarm() [1/2]

template<class T, class R, WorkerExecutor E, class S, class O, class W>
void ADS::feedbackFarm ( E & ex,
std::size_t w,
S && start,
O && onResult,
W && work )

the farm of a master and \( w \) workers with feedback: start( emit ) (or start( emit , w )), then onResult( r , emit ) on each result of work( t ) (or work( t , i ) on the worker \( i \)), until it returns false or no task is left

In pseudocode (the farm with feedback of FastFlow, beyond the book), \( Q \) the tasks, \( P \) the results, \( m \) the tasks emitted and not yet back:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{feedbackFarm}( \mathit{ex} , w , \mathit{start} , \mathit{onResult} , \mathit{work} ) \cr 2 & \quad \mathit{ex}.\mathrm{run}( w + 1 , j \mapsto \textbf{if}\ j = 0 \ \textbf{then}\ \mathrm{master}()\ \textbf{else}\ \mathrm{worker}( j - 1 ) ) \cr 3 & \textbf{procedure}\ \mathrm{master}() \cr 4 & \quad \mathit{start}( t \mapsto \mathrm{enqueue}( Q , t );\ m \gets m + 1 , w ) \cr 5 & \quad \textbf{while}\ m > 0\ \textbf{do} \cr 6 & \qquad \textbf{foreach}\ r \text{ dequeued from } P\ \textbf{do} \cr 7 & \qquad\quad m \gets m - 1 \cr 8 & \qquad\quad \textbf{if}\ \textbf{not}\ \mathit{onResult}( r , t \mapsto \mathrm{enqueue}( Q , t );\ m \gets m + 1 )\ \textbf{then} \ m \gets 0;\ \textbf{break} \cr 9 & \quad \text{tell the workers to end} \cr 10 & \textbf{procedure}\ \mathrm{worker}( i ) \cr 11 & \quad \textbf{while}\ \text{not told to end}\ \textbf{do} \ \textbf{if}\ t \text{ dequeued from } Q\ \textbf{then} \ \mathrm{enqueue}( P , \mathit{work}( t , i ) ) \end{array}} \]

Precondition
\( w + 1 \le \) concurrency(), or \( w = 0 \)

◆ feedbackFarm() [2/2]

template<class T, class R, class S, class O, class W>
void ADS::feedbackFarm ( FastFlowExecutor & ex,
std::size_t w,
S && start,
O && onResult,
W && work )

the farm with feedback of feedbackFarm() above on a farm of FastFlow, with an emitter of its own and \( w \) workers

In pseudocode (beyond the book), the emitter called by FastFlow on nothing at the start and then on each result:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{emitter}( r ) \cr 2 & \quad \textbf{if}\ r = \textbf{nil}\ \textbf{then}\ \mathit{start}( \mathit{emit} ) \cr 3 & \quad \textbf{else}\ m \gets m - 1;\ \textbf{if}\ \text{not stopped } \textbf{and not}\ \mathit{onResult}( r , \mathit{emit} )\ \textbf{then} \ \text{stop} \cr 4 & \quad \textbf{if}\ m = 0\ \textbf{then return}\ \text{the end of } \text{the stream}\ \textbf{else return}\ \text{nothing} \end{array}} \]

Precondition
\( w \le \) concurrency()