From a053d8e4445609e854108c92cb56f39957ffcd5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Doblas=20Jim=C3=A9nez?= Date: Mon, 12 Apr 2021 14:38:28 +0200 Subject: [PATCH] Ex. 1 ping_pong solution --- Exercises/ex_1/ping_pong/ping-pong.c | 58 ++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 Exercises/ex_1/ping_pong/ping-pong.c diff --git a/Exercises/ex_1/ping_pong/ping-pong.c b/Exercises/ex_1/ping_pong/ping-pong.c new file mode 100644 index 0000000..b09ba41 --- /dev/null +++ b/Exercises/ex_1/ping_pong/ping-pong.c @@ -0,0 +1,58 @@ +#include +#include +#include + +#define MAX_I 20 +#define MAX_COUNT (1024 * 1024) + +double buf[MAX_COUNT]; + +int main(int argc, char *argv[]) // ping pong +{ + int size, rank, partner, i, count, r, repetitions; + double time, bandwidth; + + MPI_Init(&argc, &argv); + MPI_Comm_size(MPI_COMM_WORLD, &size); + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + + if (size != 2) { + fprintf(stderr, "comm_size != 2\n"); + MPI_Abort(MPI_COMM_WORLD, 1); + } + + if (rank == 0) { + puts("message size number of time bandwidth"); + puts(" [bytes] repetitions [us] [MB/s]"); + puts("---------------------------------------------------"); + } + + partner = 1 - rank; + count = 1; + repetitions = MAX_COUNT; + + for (i = 0; i <= MAX_I; i++) { + time = MPI_Wtime(); + for (r = 0; r < repetitions; r++) { + if (rank == 0) { + MPI_Send(buf, count, MPI_DOUBLE, partner, 0, MPI_COMM_WORLD); + MPI_Recv(buf, count, MPI_DOUBLE, partner, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + } else { + MPI_Recv(buf, count, MPI_DOUBLE, partner, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + MPI_Send(buf, count, MPI_DOUBLE, partner, 0, MPI_COMM_WORLD); + } + } + time = MPI_Wtime() - time; + time = time / (2.0 * repetitions); + time = 1e6 * time; // micro-seconds + bandwidth = count * sizeof(double) / time; // MByte/s + + if (rank == 0) + printf("%12zd %12d %12.2f %12.2f\n", count * sizeof(double), repetitions, time, bandwidth); + count = count * 2; + repetitions = repetitions / 2; + } + + MPI_Finalize(); + return 0; +}