|
ADS
Algorithms and Data Structures
|
The executor [see Executor.h] on the threads of FastFlow, beyond the book, compiled only with ADS_WITH_FASTFLOW (the CMake option of the same name, OFF by default) and the headers of FastFlow on the path: bulk( n , f ) is a parallel_for of a ParallelFor of FastFlow on \( n \) indices in chunks of 1, scheduled dynamically by the workers themselves (the scheduler thread is disabled, since the pool already has as many workers as threads), so that the parallel algorithms of ADS run on FastFlow unchanged. More...
#include "ADS/parallel/Affinity.h"#include <algorithm>#include <thread>#include "ADS/Contract.h"#include "ADS/concurrent/Backoff.h"#include "ADS/parallel/Executor.h"#include <ff/ff.hpp>#include <ff/parallel_for.hpp>#include <ff/spin-lock.hpp>#include <atomic>#include <cstddef>#include <exception>#include <memory>#include <mutex>Go to the source code of this file.
Classes | |
| class | ADS::FastFlowLock |
| the spin lock of FastFlow, ff::lock_t, as a mutex of the standard More... | |
| class | ADS::FastFlowExecutor |
| the executor on a ParallelFor of FastFlow More... | |
Namespaces | |
| namespace | ADS |
Macros | |
| #define | NO_DEFAULT_MAPPING |
| #define | NUM_CORES ::ADS::detail::fastFlowCores() |
| #define | NUM_REAL_CORES ::ADS::detail::fastFlowRealCores() |
The executor [see Executor.h] on the threads of FastFlow, beyond the book, compiled only with ADS_WITH_FASTFLOW (the CMake option of the same name, OFF by default) and the headers of FastFlow on the path: bulk( n , f ) is a parallel_for of a ParallelFor of FastFlow on \( n \) indices in chunks of 1, scheduled dynamically by the workers themselves (the scheduler thread is disabled, since the pool already has as many workers as threads), so that the parallel algorithms of ADS run on FastFlow unchanged.
Each call is given all the \( p \) workers and at least \( p \) indices, those from \( n \) on doing nothing: the spinning workers of FastFlow meet at a barrier of all the workers of the call, but only those given an index are woken up, and FastFlow resets the barrier whenever the number of workers changes from a call to the next, also while a worker of the previous call may still be spinning in it; either way the barrier is never passed. The ParallelFor is built at the first call of bulk() that needs it and kept with the executor, and its \( p \) workers wait between the calls by spinning if spin is true (the default of the executor, not of FastFlow), or sleeping otherwise; the thread that calls bulk() waits for them and does not work. A call of bulk() from a call of bulk() runs its calls in order on that thread, since a ParallelFor does not nest. run( p , f ) is a parallel_for of another ParallelFor, also built at its first call, whose workers sleep between the calls (those of bulk() are put to sleep before a run): the workers of a run are long-lived, gain nothing by spinning between runs, and threads that spin unpinned may share a core with the thread that a run waits for, which then waits a quantum of the scheduler of the system (milliseconds). On the \( p \) calls of long-lived workers FastFlow gives each of its first \( p \) workers its own index before any worker looks at the indices of the others, so that the calls run at the same time, one per worker, and the caller only waits for them (no call runs on it, isRunCaller() is false in each). Its lock is the spin lock of FastFlow (ff::lock_t, FastFlowLock), the one its runtime takes, whose try_lock() is one test-and-set of its flag; lock() retries it after the exponential backoff of Backoff [see Backoff.h], i.e., after pauses of the processor and then after yields of the thread, so that the threads that wait for a holder that the system has preempted do not spin through their quanta (the spin_lock() of FastFlow retries with no pause). The farm with feedback runs on a farm of FastFlow [see Farm.h].
Unless ADS_FASTFLOW_MAPPING is defined, the header defines NO_DEFAULT_MAPPING before it includes FastFlow: by default FastFlow pins its \( i \)-th thread to the \( i \)-th core of the machine, regardless of the cores the process may run on (e.g., under taskset), and two programs pinned on the same cores slow each other down. Unless they are defined, it also defines NUM_CORES and NUM_REAL_CORES, the numbers of processors and of physical cores with which FastFlow skips counting them by itself, as functions of ADS that count them once: FastFlow counts the physical cores with a shell per processor (a popen() of cat on its siblings in /sys) at the construction of each ParallelFor, i.e., of each executor, which takes most of a second on a machine of hundreds of processors, while the executor is otherwise built in about a millisecond. These macros reach FastFlow only if the header comes before every header of FastFlow, which reads them when its ff/config.hpp is first included: a program that includes a header of FastFlow first does not compile, and the error of the preprocessor says why, unless it has defined NUM_CORES, NUM_REAL_CORES and NO_DEFAULT_MAPPING (or ADS_FASTFLOW_MAPPING) itself before them.
The contracts [see Contract.h]: \( p \ge 1 \); one thread at a time calls the executor (the others wait on a mutex); an exception thrown by \( f \) stops the claims and is thrown again by bulk().
| #define NO_DEFAULT_MAPPING |
| #define NUM_CORES ::ADS::detail::fastFlowCores() |
| #define NUM_REAL_CORES ::ADS::detail::fastFlowRealCores() |