Skip to content

Instantly share code, notes, and snippets.

@Syntaf
Last active August 29, 2015 14:03
Show Gist options
  • Save Syntaf/778f120bb44383654c37 to your computer and use it in GitHub Desktop.
Save Syntaf/778f120bb44383654c37 to your computer and use it in GitHub Desktop.
#include <hpx/hpx_init.hpp>
#include <hpx/hpx.hpp>
#include <hpx/include/algorithm.hpp>
//...
std::vector<int> c(10007);
//fill the vector with random values
std::iota(boost::begin(c), boost::end(c), std::rand());
int const val = 42;
auto op =
[val](int v1, int v2) {
return v1 + v2 + val;
};
//call reduce with a parallel execution policy
int r1 = hpx::parallel::reduce(hpx::parallel::par,
boost::begin(c), boost::end(c), val, op);
//the above execution is equivalent to:
int r2 = std::accumulate(boost::begin(c), boost::end(c), val, op);
//these values are the SAME, but the parallel execution is much faster!
HPX_TEST_EQ(r1, r2);
/////////////////////////////////////////////////////////////////////////////////////
//using futures
std::vector<int> c(10007);
//fill vector with random values
std::iota(boost::begin(c), boost::end(c), std::rand());
int const val = 42;
hpx::future<int> f =
hpx::parallel::reduce(hpx::parallel::task,
boost::begin(c), boost::end(c), val)
//wait for future
f.wait();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment