Skip to content

Instantly share code, notes, and snippets.

@parsa
Forked from BillyONeal/hpx_reverse.cpp
Created June 10, 2020 02:46
Show Gist options
  • Save parsa/43a0ee7b2ac9d564d043472c042a3e9c to your computer and use it in GitHub Desktop.
Save parsa/43a0ee7b2ac9d564d043472c042a3e9c to your computer and use it in GitHub Desktop.

Revisions

  1. @BillyONeal BillyONeal created this gist Jun 13, 2017.
    75 changes: 75 additions & 0 deletions hpx_reverse.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    #include <hpx/hpx.hpp>
    #include <hpx/hpx_init.hpp>
    #include <hpx/parallel/algorithms/reverse.hpp>

    #include <algorithm>
    #include <execution>
    #include <functional>
    #include <random>
    #include <vector>
    #include "stopwatch.hpp"

    using namespace std;

    constexpr size_t dataSize = 10'000;
    constexpr size_t iterations = 1000;

    template<class ExecutionPolicy>
    void test(ExecutionPolicy&& exec, vector<unsigned int>& v) {
    for (size_t iter = 0; iter < iterations; ++iter) {
    std::reverse(std::forward<ExecutionPolicy>(exec), v.begin(), v.end());
    }
    }

    template<class ExecutionPolicy>
    void test_hpx(ExecutionPolicy&& exec, vector<unsigned int>& v) {
    for (size_t iter = 0; iter < iterations; ++iter) {
    hpx::parallel::reverse(std::forward<ExecutionPolicy>(exec), v.begin(), v.end());
    }
    }

    #pragma optimize("", off)
    int hpx_main(boost::program_options::variables_map& vm)
    {
    puts("reverse parallelism test");
    printf("iterations: %zu elements: %zu size: %zu\n", iterations, dataSize, dataSize * sizeof(unsigned int));

    random_device rd{};
    vector<unsigned int> data(dataSize);
    generate(data.begin(), data.end(), ref(rd));

    stopwatch std_sequential;
    std_sequential.start();
    test(execution::seq, data);
    std_sequential.stop();
    std_sequential.print("std seq");

    stopwatch std_parallel;
    std_parallel.start();
    test(execution::par, data);
    std_parallel.stop();
    std_parallel.print("std par");

    printf("std par speedup: %f\n", std_parallel.compare(std_sequential));

    stopwatch sequential;
    sequential.start();
    test_hpx(hpx::parallel::execution::seq, data);
    sequential.stop();
    sequential.print("HPX seq");

    stopwatch parallel;
    parallel.start();
    test_hpx(hpx::parallel::execution::par_unseq, data);
    parallel.stop();
    parallel.print("HPX par_unseq");

    printf("HPX par speedup: %f\n", parallel.compare(sequential));
    return hpx::finalize();
    }
    #pragma optimize("", on)

    int main(int argc, char* argv[])
    {
    return hpx::init(argc, argv);
    }