Skip to content

Instantly share code, notes, and snippets.

@BillyONeal
Created June 9, 2017 03:41
Show Gist options
  • Save BillyONeal/1badf182873420d1ea99c4d21eb2d653 to your computer and use it in GitHub Desktop.
Save BillyONeal/1badf182873420d1ea99c4d21eb2d653 to your computer and use it in GitHub Desktop.
HPX Comparative
#include <hpx/hpx.hpp>
#include <hpx/hpx_init.hpp>
#include <hpx/parallel/algorithms/for_each.hpp>
#include <stdio.h>
#include <chrono>
#include <cstddef>
#include <iterator>
#include <vector>
#include "stopwatch.hpp"
using namespace std;
using namespace std::chrono;
int A[3000];
template<class ExecutionPolicy>
void test(ExecutionPolicy&& exec) {
hpx::parallel::for_each(std::forward<ExecutionPolicy>(exec), begin(A), end(A), [](int& r) {
const int j = static_cast<int>(&r - &A[0]);
int red = 0;
for (int i=0; i<100000; i++) {
red += static_cast<int>(sin(i+j) + cos(i+j));
}
r = red;
});
}
///////////////////////////////////////////////////////////////////////////////
#pragma optimize("", off)
int hpx_main(boost::program_options::variables_map& vm)
{
/*
PS TEST WCFB01 C:\Users\bion\Desktop>.\hpx_for_each.exe
Testing sequential
A[70] is 1
Sequential took 3053ms
Testing parallel
A[70] is 1
Parallel took 3051ms
Speedup: 1.000580x
*/
stopwatch sequential;
stopwatch parallel;
puts("Testing sequential");
fill(begin(A), end(A), int{});
sequential.start();
test(hpx::parallel::execution::seq);
sequential.stop();
printf("A[70] is %d\n", A[70]);
sequential.print("Sequential");
puts("Testing parallel");
fill(begin(A), end(A), int{});
parallel.start();
test(hpx::parallel::execution::par_unseq);
parallel.stop();
printf("A[70] is %d\n", A[70]);
parallel.print("Parallel");
printf("Speedup: %fx\n", parallel.compare(sequential));
return hpx::finalize();
}
#pragma optimize("", on)
///////////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
return hpx::init(argc, argv);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment