diff --git a/mpi.cpp b/mpi.cpp index ff6b28e..180c68b 100644 --- a/mpi.cpp +++ b/mpi.cpp @@ -3,9 +3,15 @@ #include #include #include +#include int main (int argc, char ** argv) { + int rank, numprocs; + MPI_Init (&argc, &argv); + MPI_Comm_size(MPI_COMM_WORLD, &numprocs); + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + const long n = 1024*1024*16; std::vector unsort(n); diff --git a/openmp.cpp b/openmp.cpp index ac42f84..48d39d3 100644 --- a/openmp.cpp +++ b/openmp.cpp @@ -5,10 +5,14 @@ #include #include #include +#include +#include + //Matrix multiplication int main(int argc, char *argv[]) { + time_t start,end; int n = 1024; std::vector a(n*n); @@ -20,10 +24,26 @@ int main(int argc, char *argv[]) std::generate(a.begin(), a.end(), std::rand); std::generate(b.begin(), b.end(), std::rand); + time (&start); + #pragma omp parallel for shared(a,b,c,n) + 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 (&end); + double tm = difftime (end,start); + std::cout << "Parallel time:" << std::endl; + std::cout << tm << std::endl; + std::cout << "=====" << std::endl; + + time (&start); 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 (&end); + tm = difftime (end,start); + std::cout << "Sequential time:" << std::endl; + std::cout << tm << std::endl; return 0; } diff --git a/piOMP.cpp b/piOMP.cpp new file mode 100644 index 0000000..e01838a --- /dev/null +++ b/piOMP.cpp @@ -0,0 +1,54 @@ +#include +#include +#include +#include +#include +using namespace std; + +double calculatexi(int i, int n) +{ + return (i-0.5)/(double)n; +} + +double calculatefi(double xi) +{ + return 4/(1+xi*xi); +} + +double calculate(int n) +{ + double pi; + double xi; + int i; + // remove pragma to get sequential version + #pragma omp parallel for private(xi,i) reduction(+:pi) + for(i=1;i<=n;i++) + { + xi = calculatexi(i,n); + pi+= calculatefi(xi); + + } + + return pi/(double)n; +} + +int main(int argc, char *argv[]) +{ + time_t start,end; + int n = atoi(argv[2]); + + time (&start); + double result = calculate(n); + time (&end); + + cout << "GOT" << endl; + cout << result << endl; + cout << "MATH PI" << endl; + cout << M_PI << endl; + + double tm = difftime (end,start); + cout << "PTime:" << endl; + cout << tm << endl; + + return 0; +} \ No newline at end of file