Created
August 7, 2014 08:21
-
-
Save sithhell/365481b3055bf43f72f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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