diff --git a/exercices/pi_collect.c b/exercices/pi_collect.c index 81aa84d..6bac194 100644 --- a/exercices/pi_collect.c +++ b/exercices/pi_collect.c @@ -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) { @@ -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); diff --git a/exercices/pi_collect.in b/exercices/pi_collect.in index 86d8068..e30f9e0 100644 --- a/exercices/pi_collect.in +++ b/exercices/pi_collect.in @@ -1,4 +1,5 @@ -1000 +100 +10000 1000000 -1000000000 +100000000 0 diff --git a/exercices/pi_collect.py b/exercices/pi_collect.py new file mode 100644 index 0000000..9238cc7 --- /dev/null +++ b/exercices/pi_collect.py @@ -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() diff --git a/exercices/solutions/pi_collect.c b/exercices/solutions/pi_collect.c index 0a028a3..9b84178 100644 --- a/exercices/solutions/pi_collect.c +++ b/exercices/solutions/pi_collect.c @@ -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) { @@ -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); diff --git a/exercices/solutions/pi_collect.py b/exercices/solutions/pi_collect.py new file mode 100644 index 0000000..d28a0d7 --- /dev/null +++ b/exercices/solutions/pi_collect.py @@ -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()