Last active
June 9, 2017 03:34
-
-
Save BillyONeal/14d8bbf94bf766287df06b3fed0ca30f to your computer and use it in GitHub Desktop.
Hello World Parallel Bench Derived From OpenMP Hello World Example
This file contains hidden or 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
#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) | |
{ | |
stopwatch sw; | |
sw.start(); | |
for (int idx = 0; idx < 1; ++idx) | |
test(hpx::parallel::execution::par_unseq); | |
sw.stop(); | |
printf("A[70] is %d\n", A[70]); | |
sw.print("parallel for_each"); | |
return hpx::finalize(); | |
} | |
#pragma optimize("", on) | |
/////////////////////////////////////////////////////////////////////////////// | |
int main(int argc, char* argv[]) | |
{ | |
return hpx::init(argc, argv); | |
} |
This file contains hidden or 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
#include <math.h> | |
#include <chrono> | |
#include "stopwatch.hpp" | |
int A[3000]; | |
void test() { | |
#pragma omp parallel for | |
for (int j=0; j<3000; j++) { | |
int red = 0; | |
for (int i=0; i<100000; i++) { | |
red += static_cast<int>(sin(i+j) + cos(i+j)); | |
} | |
A[j] = red; | |
} | |
} | |
#pragma optimize("", off) | |
#include <stdio.h> | |
int main() { | |
stopwatch sw; | |
sw.start(); | |
for (int idx = 0; idx < 1; ++idx) | |
test(); | |
sw.stop(); | |
printf("A[70] is %d\n", A[70]); | |
sw.print("openmp"); | |
} | |
#pragma optimize("", on) |
This file contains hidden or 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
#include <math.h> | |
#include <stdio.h> | |
#include <chrono> | |
#include <execution> | |
#include <iterator> | |
#include "stopwatch.hpp" | |
using namespace std; | |
using namespace std::chrono; | |
using namespace std::execution; | |
int A[3000]; | |
template<class ExecutionPolicy> | |
void test(ExecutionPolicy&& exec) { | |
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 main() { | |
stopwatch sequential; | |
stopwatch parallel; | |
puts("Testing sequential"); | |
fill(begin(A), end(A), int{}); | |
sequential.start(); | |
test(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(execution::par); | |
parallel.stop(); | |
printf("A[70] is %d\n", A[70]); | |
parallel.print("Parallel"); | |
printf("Speedup: %fx\n", parallel.compare(sequential)); | |
} | |
#pragma optimize("", on) |
This file contains hidden or 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
cl /Zi /std:c++latest /MD /EHsc /O2 /openmp .\openmp.cpp | |
cl /Zi /std:c++latest /MD /EHsc /O2 .\parallel_for_each_comparative.cpp | |
cl /D_HAS_AUTO_PTR_ETC=1 /std:c++latest /MD /EHsc /O2 /IC:\Users\bion\Desktop\vcpkg-export-20170608-100432\installed\x86-windows\include .\hpx_for_each.cpp /link /libpath:C:\Users\bion\Desktop\vcpkg-export-20170608-100432\installed\x86-windows\lib hpx.lib hpx_init.lib boost_program_options-vc140-mt-1_64.lib boost_system-vc140-mt-1_64.lib boost_timer-vc140-mt-1_64.lib boost_date_time-vc140-mt-1_64.lib boost_thread-vc140-mt-1_64.lib | |
PS TEST WCFB01 C:\Users\bion\Desktop>.\parallel_for_each_comparative.exe | |
Testing sequential | |
A[70] is 1 | |
Sequential took 2874ms | |
Testing parallel | |
A[70] is 1 | |
Parallel took 300ms | |
Speedup: 9.574882x | |
PS TEST WCFB01 C:\Users\bion\Desktop>.\openmp.exe | |
A[70] is 1 | |
openmp took 303ms | |
PS TEST WCFB01 C:\Users\bion\Desktop>.\hpx_for_each.exe | |
A[70] is 1 | |
parallel for_each took 2918ms | |
PS TEST WCFB01 C:\Users\bion\Desktop> |
This file contains hidden or 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
#pragma once | |
#include <stdio.h> | |
#include <chrono> | |
class stopwatch { | |
std::chrono::high_resolution_clock::time_point startTime; | |
std::chrono::high_resolution_clock::time_point stopTime; | |
public: | |
stopwatch() | |
: startTime() | |
, stopTime() | |
{ } | |
stopwatch(const stopwatch&) = delete; | |
stopwatch& operator=(const stopwatch&) = delete; | |
void start() { | |
startTime = std::chrono::high_resolution_clock::now(); | |
} | |
void stop() { | |
stopTime = std::chrono::high_resolution_clock::now(); | |
} | |
std::chrono::high_resolution_clock::duration time_taken() const { | |
return stopTime - startTime; | |
} | |
double compare(const stopwatch& s) { | |
return static_cast<double>(s.time_taken().count()) / time_taken().count(); | |
} | |
void print(const char * const action) const { | |
printf("%s took %lldms\n\n", action, | |
static_cast<long long>(std::chrono::duration_cast<std::chrono::milliseconds>(time_taken()).count())); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment