-
-
Save tbennun/8ab43c6aec75187dbb59b195de21a131 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
//////////////////////////////////////////////////////////////////////////////// | |
// Copyright (c) 2011 Bryce Lelbach | |
// | |
// Distributed under the Boost Software License, Version 1.0. (See accompanying | |
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
//////////////////////////////////////////////////////////////////////////////// | |
// Naive SMP version implemented with futures. | |
#include <hpx/hpx_init.hpp> | |
#include <hpx/include/actions.hpp> | |
#include <hpx/include/async.hpp> | |
#include <hpx/include/util.hpp> | |
#include <hpx/parallel/algorithms/copy.hpp> | |
#include <hpx/parallel/execution_policy.hpp> | |
#include <cstdint> | |
#include <iostream> | |
#include <random> | |
#include <boost/format.hpp> | |
/////////////////////////////////////////////////////////////////////////////// | |
//[fib_hpx_main | |
int hpx_main(boost::program_options::variables_map& vm) | |
{ | |
// extract command line argument, i.e. fib(N) | |
std::uint64_t n = vm["n-value"].as<std::uint64_t>(); | |
{ | |
std::random_device rd; | |
std::mt19937 dev(rd()); | |
std::uniform_real_distribution<float> ud(0.0f,1.0f); | |
float *arr = new float[n]; | |
float *out = new float[n]; | |
float *regression = new float[n]; | |
int reg_items = 0, items = 0; | |
for(int i = 0; i < n; ++i) { | |
arr[i] = ud(dev); | |
out[i] = 999.0f; | |
if (arr[i] >= 0.5f) | |
regression[reg_items++] = arr[i]; | |
} | |
{ | |
// Keep track of the time required to execute. | |
hpx::util::high_resolution_timer t; | |
auto res = hpx::parallel::copy_if( | |
hpx::parallel::execution::par, | |
//hpx::parallel::sequential_execution_policy(), | |
arr, arr+n, out, | |
[&](const auto& a) { return a >= 0.5f; }); | |
/* | |
std::cout << "in: "; | |
for(int i = 0; i < n; ++i) | |
std::cout << arr[i] << ", "; | |
std::cout << std::endl; | |
std::cout << "out: "; | |
for(int i = 0; i < n; ++i) | |
std::cout << out[i] << ", "; | |
std::cout << std::endl; | |
*/ | |
items = res.second - out; | |
std::cout << "Items: " << items << ", elapsed time: " << t.elapsed() << std::endl; | |
} | |
if (items != reg_items) | |
printf("REGRESSION FAILED (items %d != actual %d)\n", items, | |
reg_items); | |
else { | |
std::sort(out, out+items); | |
std::sort(regression, regression+items); | |
for (int i = 0; i< items; ++i) { | |
if (out[i] != regression[i]) { | |
printf("REGRESSION FAILED AT INDEX %d\n", i); | |
break; | |
} | |
} | |
} | |
printf("OK\n"); | |
delete[] arr; | |
delete[] out; | |
} | |
return hpx::finalize(); // Handles HPX shutdown | |
} | |
//] | |
/////////////////////////////////////////////////////////////////////////////// | |
//[fib_main | |
int main(int argc, char* argv[]) | |
{ | |
// Configure application-specific options | |
boost::program_options::options_description | |
desc_commandline("Usage: " HPX_APPLICATION_STRING " [options]"); | |
desc_commandline.add_options() | |
( "n-value", | |
boost::program_options::value<std::uint64_t>()->default_value(10), | |
"n value for the Fibonacci function") | |
; | |
// Initialize and run HPX | |
return hpx::init(desc_commandline, argc, argv); | |
} | |
//] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment