Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Parallel reductions
template<typename R, typename A, template<typename> class C>
R reduce(function<R(R,A)> f, R z, C<A> c)
R r(z);
for (e: c) r = f(r, e);
return r;
template<typename R, typename A1, typename A2, template<typename> class C>
R reduce(function<R(R,A,B)> f, R z, C<A1> c1, C<A2> c2)
auto s = c1.size();
assert(c2.size() == s);
if (s == 0) {
return z;
} else if (s == 1) {
return f(z, c1[0], c2[0]);
} else {
auto m = s/2;
return f(z, reduce(f, z, c1[0..m/2], c2[0..m/2]),
reduce(f, z, c1[m/2..s], c2[m/2..2]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment