Skip to content

Commit

Permalink
added td2 and td4 (not done) and finishing docs
Browse files Browse the repository at this point in the history
  • Loading branch information
adelbke committed Jan 19, 2022
1 parent fc8fea3 commit 275eb86
Show file tree
Hide file tree
Showing 25 changed files with 645 additions and 14 deletions.
Binary file modified a.out
Binary file not shown.
Binary file added execMPI
Binary file not shown.
4 changes: 4 additions & 0 deletions mpi_cae.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mkdir -p ./dist/$(dirname ${1%".c"})

mpicc -w ./scripts/$1 -fopenmp -lm -o ./dist/${1%".c"}
mpirun -np $2 ./dist/${1%".c"}
21 changes: 21 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,24 @@ l’exécution).

#### Exercice 3

1. Implémentation de la multiplication matricielle séquentielle [code](scripts/td2/exo3/produit_matrice_2d.c)
- Produit Matricielle parallèle


### TD 4

#### Exercice 1: inter-bloquage de communication

1. Le programme risque un interblocage de communication car les deux noeuds attendent un message de l'autre dès le début de l'exécution. Car la fonction MPI_Recv est bloquante

2. Le programme risque un interblocage également. Les deux machines Envoient un message en même temps en utilisant une procèdure bloquante synchrone et ils finissent par s'attendre mutuellement (interblocage)

3. les solutions aux problèmes sont [Deadlock 1](scripts/td4/deadlock_1.c) [Deadlock 2](scripts/td4/deadlock_2.c)

#### Exercice 2:

1. Tout se passe comme prévu, le noeud rang 0 envoi 2 messages au noeuds 1 et 2, le noeud 1 ensuite envoi un message au noeud 2 et le noeud 2 attends 2 messages de n'importe quel source

#### Exercice 3:


Binary file added scripts/td2/exo3/prod_mat_par_dynamic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added scripts/td2/exo3/prod_mat_par_static.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion scripts/td2/exo3/produit_matrice_2d.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ int main(int argc, char const *argv[])
{
for (int j = 0; j < MAX; j++)
{
a[i][j] = 1 + i + j/2;
a[i][j] = 1 + i ;
b[i][j] = j + 1 ;
c[i][j] = 0;
}
Expand Down
26 changes: 13 additions & 13 deletions scripts/td2/exo3/produit_matrice_2d_par.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,39 @@

#define MAX 10

int a[MAX][MAX], b[MAX][MAX], c[MAX][MAX];

int main(int argc, char const *argv[])
{
int a[MAX][MAX], b[MAX][MAX], c[MAX][MAX];

// initialisation
for (int i = 0; i < MAX; i++)
{
for (int j = 0; j < MAX; j++)
{
a[i][j] = 1 + i + j/2;
a[i][j] = 1 + i ;
b[i][j] = j + 1 ;
c[i][j] = 0;
}
}
int i = 0;
#pragma omp parallel for shared(i)
for (i = 0; i < MAX; i++)
{
for (int j = 0; j < MAX; j++)

#pragma omp parallel for schedule(dynamic)
for (int i = 0; i < MAX; i++)
{
for (int j = 0; j < MAX; j++)
{
for (int k = 0; k < MAX; k++)
{
for (int k = 0; k < MAX; k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
c[i][j]+=a[i][k]*b[k][j];
}

}
}

for (int i = 0; i < MAX; i++)
{
for (int j = 0; j < MAX; j++)
{
printf("| %d ");
printf("|%d",c[i][j]);
}
printf("\n");
}
Expand Down
43 changes: 43 additions & 0 deletions scripts/td2/exo4/pi_for.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define MAX 50
#define NUMTHREAD 4

int main(int argc, char* argv[]){

float step, x, sum, pi;
int i, steps;
steps = 200.0;

step = 1.0/(float)steps;

float* resultArray = malloc(sizeof(float)*NUMTHREAD);

// printf("\nwe have %d threads", omp_get_thread_num());
// omp_set_num_threads(NUMTHREAD);
// int t_num = omp_get_thread_num();
// int start = (steps/NUMTHREAD)*t_num;
// int end = (steps/NUMTHREAD) * (t_num + 1);
// printf("\nThread %d , starts: %d , ends: %d",t_num, start, end);

#pragma omp parallel for
for (int i = 0; i < steps; i++)
{
x = (i + 0.5)* step;
sum += 4.0/(1.0+x*x);
}

// printf("\nsum=%f",sum)
// printf("\nThread %d, sum: %f",t_num, sum);


pi = step * sum;
// print("pi= %f", math.pi)
printf("\npi= %.10f", pi);

return 0;

}
31 changes: 31 additions & 0 deletions scripts/td2/exo4/pi_for_critical.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define NUMTHREAD 4

int main(int argc, char* argv[]){

double step, x, sum, pi;
int i, steps;
steps = 1000.0;

step = 1.0/(double)steps;

#pragma omp parallel for private(x)
for (int i = 0; i < steps; i++)
{
x = (i + 0.5)* step;
#pragma omp critical
{
sum += 4.0/(1.0+x*x);
}
}


pi = step * sum;
printf("\npi= %.15f", pi);

return 0;
}
42 changes: 42 additions & 0 deletions scripts/td2/exo4/pi_for_iterations.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc, char *argv[])
{

double step, x, sum, pi;
int i, steps;
steps = 1000.0;

int p[4] = { 2, 4, 6, 8 };
int n[5] = { 200, 400, 600, 800, 1000 };

for (int j = 0; j < 5; j++)
{
for (int k = 0; k < 5; k++)
{
pi = 0, sum = 0.0;
step = 1.0 / (double)n[k];
omp_set_num_threads(p[j]);

#pragma omp parallel for reduction(+:sum)
for (int i = 0; i < n[k]; i++)
{
x = (i + 0.5) * step;
sum += 4.0 / (1.0 + x * x);
}

printf("\npi for num_threads %d and steps %d is %f",p[j], n[k], sum * step);
}

}



pi = step * sum;
printf("\npi= %.15f", pi);

return 0;
}
28 changes: 28 additions & 0 deletions scripts/td2/exo4/pi_for_reduction.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define NUMTHREAD 4

int main(int argc, char* argv[]){

double step, x, sum, pi;
int i, steps;
steps = 1000.0;

step = 1.0/(double)steps;

#pragma omp parallel for private(x) reduction(+:sum)
for (int i = 0; i < steps; i++)
{
x = (i + 0.5)* step;
sum += 4.0/(1.0+x*x);
}


pi = step * sum;
printf("\npi= %.15f", pi);

return 0;
}
35 changes: 35 additions & 0 deletions scripts/td2/exo4/pi_out.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include<omp.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>


void main()
{
float step,x,sum,pi;
int steps, tid;
steps=1000;
int num;
step = 1.0/(float)steps;
int numthread =4;
omp_set_num_threads(numthread);
#pragma omp parallel private(tid, num, x)
{
tid=omp_get_thread_num();
num=omp_get_num_threads();

printf("threads num %d\n",numthread);
printf("threads %d\n",num);
printf("tid= %d\n",tid);
#pragma omp for reduction(+:sum)
for(int i= 0; i<steps; i++ )
{
x=(i+0.5)*step;
sum+=4.0/(1.0+x*x);
}
}
pi=step*sum;

printf("pi=%.8f",pi);
printf("\npi=%.8f",M_PI);
}
10 changes: 10 additions & 0 deletions scripts/td4/deadlock_1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
if (rank == 0)
{
MPI_Recv(&recvbuf, count, MPI_Float, 1, tag, MPI_COMM_WORLD,&status);
MPI_Send(&sendbuf, count, MPI_Float, 1, tag, MPI_COMM_WORLD);
}
else if (rank == 1)
{
MPI_Send(&sendbuf, count, MPI_Float, 0, tag, MPI_CMM_WORLD, &status);
MPI_Recv(&recvbuf, count, MPI_Float, 0, tag, MPI_COMM_WORLD, &status);
}
10 changes: 10 additions & 0 deletions scripts/td4/deadlock_2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
if (rank == 0)
{
MPI_Ssend(&sendbuf, count, MPI_Float, 1, tag, MPI_COMM_WORLD);
MPI_Recv(&recvbuf, count, MPI_Float, 1, tag, MPI_COMM_WORLD, &status);
}
else if (rank == 1)
{
MPI_SRecv(&recvbuf, count, MPI_Float, 0, tag, MPI_COMM_WORLD, &status);
MPI_Ssend(&sendbuf, count, MPI_Float, 0, tag, MPI_CMM_WORLD);
}
36 changes: 36 additions & 0 deletions scripts/td4/exo2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <mpich/mpi.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
int numtasks, myRank, len, rc;
char hostname[MPI_MAX_PROCESSOR_NAME];
MPI_Init(&argc, &argv);

int sendbuf1, sendbuf2, count, recvbuf1, recvbuf2, tag;

MPI_Comm comm = MPI_COMM_WORLD;
MPI_Status status;


MPI_Comm_rank(comm, &myRank);
if (myRank == 0)
{
MPI_Send(sendbuf1, count, MPI_INT, 2, tag, comm);
MPI_Send(sendbuf2, count, MPI_INT, 1, tag, comm);
}
else if (myRank == 1)
{
MPI_Recv(recvbuf1, count, MPI_INT, 0, tag, comm, &status);
MPI_Send(recvbuf1, count, MPI_INT, 2, tag, comm);
}
else if (myRank == 2)
{
MPI_Recv(recvbuf1, count, MPI_INT, MPI_ANY_SOURCE, tag, comm, &status);
MPI_Recv(recvbuf2, count, MPI_INT, MPI_ANY_SOURCE, tag, comm, &status);
}

MPI_Finalize();

return 0;
}
Loading

0 comments on commit 275eb86

Please sign in to comment.