Skip to content

Commit

Permalink
Codes pour l'approximation de pi
Browse files Browse the repository at this point in the history
  • Loading branch information
plstonge committed May 14, 2024
1 parent 92b31b2 commit 08f6f95
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 25 deletions.
23 changes: 13 additions & 10 deletions exercices/pi_collect.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

int main(int argc, char *argv[])
{
int rank, size, n, i;
double sum, pi;
int rank, size, n, debut, fin, k;
double somme, pi;

MPI_Init( &argc, &argv );
MPI_Comm_size( MPI_COMM_WORLD, &size );
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);

do {
if (rank == 0) {
Expand All @@ -22,16 +22,19 @@ int main(int argc, char *argv[])
MPI_Bcast( ... );
if (n == 0) break;

sum = 0.0;
for (i = n - 1 - rank; i >= 0; i -= size) {
sum += (4.0 - 8.0 * (i % 2)) / (2.0 * i + 1);
debut = ... (long)n ...
fin = ... (long)n ...

somme = 0.0;
for (k = fin - 1; k >= debut; --k) {
somme += (4.0 - 8.0 * (k % 2)) / (2.0 * k + 1);
}

MPI_Reduce( ... );

if (rank == 0) {
printf("PI is approximately %.16f, Error is %.16f\n",
pi, fabs(pi - M_PI));
printf("PI is approximately %.16f, difference is %.16f\n",
pi, fabs(pi - M_PI));
}
} while (1);

Expand Down
5 changes: 3 additions & 2 deletions exercices/pi_collect.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
1000
100
10000
1000000
1000000000
100000000
0
44 changes: 44 additions & 0 deletions exercices/pi_collect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env python

from mpi4py import MPI # MPI.Init() implicite
import math
import sys


def main():
"""
Programme principal
"""

comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
n = None

while True:
if rank == 0:
n = int(input('Enter the number of intervals (0 quits): '))
print(f'-> {n} iterations' if n > 0 else 'done')

bcast( ... )
if n == 0:
break

debut = ... n ...
fin = ... n ...

somme = 0.0;
for k in range(fin - 1, debut - 1, -1):
somme += (4.0 - 8.0 * (k % 2)) / (2.0 * k + 1)

reduce( ... )

if rank == 0:
print(f'PI is approximately {pi:.16f},',
f'difference is {abs(pi - math.pi):.16f}')

MPI.Finalize()


if __name__ == '__main__':
main()
29 changes: 16 additions & 13 deletions exercices/solutions/pi_collect.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

int main(int argc, char *argv[])
{
int rank, size, n, i;
double sum, pi, t1, t2;
int rank, size, n, debut, fin, k;
double somme, pi, t1, t2;

MPI_Init( &argc, &argv );
MPI_Comm_size( MPI_COMM_WORLD, &size );
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);

do {
if (rank == 0) {
Expand All @@ -20,21 +20,24 @@ int main(int argc, char *argv[])
t1 = MPI_Wtime();
}

MPI_Bcast( &n, 1, MPI_INT, 0, MPI_COMM_WORLD );
MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
if (n == 0) break;

sum = 0.0;
for (i = n - 1 - rank; i >= 0; i -= size) {
sum += (4.0 - 8.0 * (i % 2)) / (2.0 * i + 1);
debut = rank * (long)n / size;
fin = (rank + 1) * (long)n / size;

somme = 0.0;
for (k = fin - 1; k >= debut; --k) {
somme += (4.0 - 8.0 * (k % 2)) / (2.0 * k + 1);
}

MPI_Reduce( &sum, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD );
MPI_Reduce(&somme, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);

if (rank == 0) {
t2 = MPI_Wtime();
printf("PI is approximately %.16f, Error is %.16f\n",
pi, fabs(pi - M_PI));
printf("Time = %.16f sec\n", t2 - t1);
printf("PI is approximately %.16f, difference is %.16f\n",
pi, fabs(pi - M_PI));
printf("Time = %.6f sec\n", t2 - t1);
}
} while (1);

Expand Down
47 changes: 47 additions & 0 deletions exercices/solutions/pi_collect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python

from mpi4py import MPI # MPI.Init() implicite
import math
import sys


def main():
"""
Programme principal
"""

comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
n = None

while True:
if rank == 0:
n = int(input('Enter the number of intervals (0 quits): '))
print(f'-> {n} iterations' if n > 0 else 'done')
t1 = MPI.Wtime()

n = comm.bcast(n, 0)
if n == 0:
break

debut = rank * n // size
fin = (rank + 1) * n // size

somme = 0.0;
for k in range(fin - 1, debut - 1, -1):
somme += (4.0 - 8.0 * (k % 2)) / (2.0 * k + 1)

pi = comm.reduce(somme, MPI.SUM, 0)

if rank == 0:
t2 = MPI.Wtime()
print(f'PI is approximately {pi:.16f},',
f'difference is {abs(pi - math.pi):.16f}')
print(f'Time = {t2 - t1:.6f} sec')

MPI.Finalize()


if __name__ == '__main__':
main()

0 comments on commit 08f6f95

Please sign in to comment.