From 0384735eb3b858166d09116e00c237347b9ccb36 Mon Sep 17 00:00:00 2001 From: Ilnur Yakupov Date: Fri, 16 Sep 2016 22:20:22 +0300 Subject: [PATCH] openmp --- openmp.cpp | 20 ++++++++++++++++++-- piopenmp.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 piopenmp.cpp diff --git a/openmp.cpp b/openmp.cpp index ac42f84..4141dbb 100644 --- a/openmp.cpp +++ b/openmp.cpp @@ -5,11 +5,14 @@ #include #include #include +#include +#include //Matrix multiplication int main(int argc, char *argv[]) { int n = 1024; + time_t startTime,endTime; std::vector a(n*n); std::vector b(n*n); @@ -20,10 +23,23 @@ int main(int argc, char *argv[]) std::generate(a.begin(), a.end(), std::rand); std::generate(b.begin(), b.end(), std::rand); + time (&startTime); for(int i = 0; i < n; ++i) for(int j = 0; j < n; ++j) for(int k = 0; k < n; ++k) c[i*n + j] += a[i*n + k] * b[k*n + j]; - + + time (&endTime); + std::cout << difftime (endTime,startTime) << std::endl; + + time (&startTime); + #pragma omp parallel for + for(int i = 0; i < n; ++i) + for(int j = 0; j < n; ++j) + for(int k = 0; k < n; ++k) + c[i*n + j] += a[i*n + k] * b[k*n + j]; + + time (&endTime); + std::cout << difftime (endTime,startTime) << std::endl; return 0; -} +} \ No newline at end of file diff --git a/piopenmp.cpp b/piopenmp.cpp new file mode 100644 index 0000000..42e1434 --- /dev/null +++ b/piopenmp.cpp @@ -0,0 +1,24 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +double f(double x){ + return 4/(1+x*x); +} + +int main(int argc, char *argv[]){ + int n = 10240; + double s; + #pragma omp parallel for reduction(+:s) + for(int i = 1; i <= n; i++) + s += f((i - 0.5)/n); + double pi = s/n; + + std::cout << pi << std::endl; + return 0; +} \ No newline at end of file