Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rustem Khabetdinov[11-301] Test #1 #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions mpi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
#include <cmath>
#include <algorithm>
#include <random>
#include <mpi.h>

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<int> unsort(n);
Expand Down
22 changes: 21 additions & 1 deletion openmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <omp.h>
#include <time.h>


//Matrix multiplication
int main(int argc, char *argv[])
{
time_t start,end;
int n = 1024;

std::vector<double> a(n*n);
Expand All @@ -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;
}
54 changes: 54 additions & 0 deletions piOMP.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <iostream>
#include <math.h>
#include <omp.h>
#include <cstdlib>
#include <time.h>
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;
}