ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Farm.h File Reference

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). More...

#include "ADS/Contract.h"
#include "ADS/concurrent/MPMCQueue.h"
#include "ADS/concurrent/MPSCQueue.h"
#include "ADS/parallel/Executor.h"
#include "ADS/parallel/FastFlowExecutor.h"
#include <atomic>
#include <cstddef>
#include <deque>
#include <exception>
#include <memory>
#include <optional>
#include <stdexcept>
#include <type_traits>
#include <utility>
#include <vector>
#include "ADS/parallel/impl/Farm.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

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 [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).

feedbackFarm< T , R >( ex , w , start , onResult , work ) calls start( emit ) once, where emit( t ) emits the task \( t \), then onResult( r , emit ) on each result \( r \), which returns false to stop the farm, while the workers call work( t ) on the tasks; the farm ends when the master stops it or when no task is left (none emitted and not yet worked out), and returns when all the calls have returned. The tasks reach the workers on demand, i.e., a task goes to the first worker that is free, and the master runs on its own thread (see The farm with feedback).

The workers have indices \( 0 , \ldots , w - 1 \), and the master the index \( w \): a work callable as work( t , i ) is called so, with \( i \) the index of the worker, and a start callable as start( emit , m ) is called so, with \( m = w \), so that each of them may keep a state of its own in a vector of \( w + 1 \) places (an evaluator per worker, the processor to which it pins itself); the forms work( t ) and start( emit ) stay. During a farm each index runs on one thread, the same for all its calls, and distinct indices on distinct threads; with \( w = 0 \) the master is also the worker 0, both on the calling thread. Which thread an index is depends on the executor: on FastFlow the master and each worker are nodes of the farm, each with a thread of FastFlow of its own, none of them the caller, which waits; on the others the master is the call 0 of run(), i.e., the caller on ThreadPool and on OpenMPExecutor, and the worker \( i \) the call \( i + 1 \); isRunCaller() [see Executor.h] tells each of them whether it is the caller.

On FastFlowExecutor the farm is a farm of FastFlow (ff_Farm) with an emitter, \( w \) workers, no collector, the scheduling on demand and the channel of feedback (wrap_around()), the tasks and the results passed as pointers, as FastFlow passes them; on the other executors it is emulated on their run(), with the master as the call 0 and the workers as the calls \( 1 , \ldots , w \), the tasks on a queue of many producers and many consumers [see MPMCQueue.h] from which the free workers take them, and the results on a queue with a lane per worker [see MPSCQueue.h]; with \( w = 0 \) (or on Sequential) the master works out its tasks itself, in the order of their emission. When the master stops the farm, the tasks not yet taken are dropped, the workers end after the task they hold, and its result is dropped.

The contracts [see Contract.h]: \( w + 1 \le \) concurrency() but on FastFlow, where \( w \le \) concurrency() and the emitter has a thread of its own; the first exception thrown by onResult() or by work() stops the farm and is thrown again once all the calls have returned.

See also
M. Aldinucci, M. Danelutto, P. Kilpatrick, M. Torquati, FastFlow: high-level and efficient streaming on multicore, in S. Pllana, F. Xhafa (eds.), Programming Multi-core and Many-core Computing Systems, Wiley, 261-280, 2017, doi:10.1002/9781119332015.ch13
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa