Skip to content

Instantly share code, notes, and snippets.

@sithhell
Created August 7, 2014 08:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sithhell/365481b3055bf43f72f9 to your computer and use it in GitHub Desktop.
Save sithhell/365481b3055bf43f72f9 to your computer and use it in GitHub Desktop.
double run_parallel_algorithm(std::size_t n)
{
std::vector<double> d = make_vector(n);
hpx::util::high_resolution_timer t;
std::size_t cores = hpx::get_os_thread_count();
std::size_t chunk_size = (d.size() + cores - 1) / cores;
std::size_t count = n + chunk_size - 1;
std::vector<hpx::future<void>> chunks;
for(std::size_t p = 0; p < count; p += chunk_size)
{
std::vector<double>::iterator s = std::begin(d);
std::advance(s, p);
std::vector<double>::iterator e = std::begin(d);
std::advance(e, std::min(n, p + chunk_size));
hpx::future<void> f = hpx::parallel::for_each(hpx::parallel::task, s, e, f1);
chunks.push_back(f.then(
hpx::util::unwrapped([s, e]()
{
hpx::parallel::for_each(hpx::parallel::par, s, e, f2);
})
));
}
hpx::wait_all(chunks);
return t.elapsed();
}
double run_serial_algorithm(std::size_t n)
{
std::vector<double> d = make_vector(n);
hpx::util::high_resolution_timer t;
std::size_t cores = hpx::get_os_thread_count();
std::size_t chunk_size = ((d.size() + cores - 1) / cores) / 2;
std::size_t count = n + chunk_size - 1;
std::vector<hpx::future<void>> chunks;
for(std::size_t p = 0; p < count; p += chunk_size)
{
std::vector<double>::iterator s = std::begin(d);
std::advance(s, p);
std::vector<double>::iterator e = std::begin(d);
std::advance(e, std::min(n, p + chunk_size));
hpx::future<void> f = hpx::async([s, e](){std::for_each(s, e, f1);});
chunks.push_back(f.then(
hpx::util::unwrapped([s, e]()
{
std::for_each(s, e, f2);
})
));
}
hpx::wait_all(chunks);
return t.elapsed();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment