Reducers are a special kind of symmetric object that, when read, compute a reduction over all instances. More...
#include <Reducer.hpp>
Additional Inherited Members | |
Public Member Functions inherited from ReducerImpl< T, collective_add > | |
| ReducerImpl () | |
| operator T () const | |
| Read out value; does expensive global reduce. More... | |
| void | operator= (const T &v) |
| Globally set the value; expensive global synchronization. More... | |
| void | reset () |
| Globally reset to default value for the type. More... | |
Protected Attributes inherited from ReducerImpl< T, collective_add > | |
| T | local_value |
Reducers are a special kind of symmetric object that, when read, compute a reduction over all instances.
They can be inexpensively updated (incremented/decremented, etc), so operations that modify the value without observing it are cheap and require no communication. However, reading the value forces a global reduction operation each time, and directly setting the value forces global synchronization.
Reducers must be declared in the C++ global scope ("global variables") so they have identical pointers on each core. Declaring one as a local variable (on the stack), or in a symmetric allocation will lead to segfaults or worse.
By default, Reducers do a global "sum", using the + operator; other operations can be specified by ReducerType (second template parameter).
Example usage (count non-negative values in an array):
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // declare symmetric global object Reducer<int> non_negative_count;
int main(int argc, char* argv[]) { Grappa::init(&argc, &argv); Grappa::run([]{ GlobalAddress<double> A; // allocate & initialize array A...
// set global count to 0 (expensive global sync.) non_negative_count = 0;
forall(A, N, [](double& A_i){ if (A_i > 0) { // cheap local increment non_negative_count += 1; } });
// read out total value and print it (expensive global reduction) LOG(INFO) << non_negative_count << " non negative values"; }); Grappa::finalize(); }
Definition at line 308 of file Reducer.hpp.