Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
test --cfg=smpi/shared-malloc:local
[simgrid.git] / teshsuite / smpi / pt2pt-pingpong / pt2pt-pingpong.c
1 /* A simple example ping-pong program to test MPI_Send and MPI_Recv */
2
3 /* Copyright (c) 2009-2019. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include <stdio.h>
10 #include <mpi.h>
11
12 int main(int argc, char *argv[])
13 {
14   const int tag1 = 42;
15   const int tag2 = 43; /* Message tag */
16   int size;
17   int rank;
18   int msg = 99;
19   MPI_Status status;
20
21   int err = MPI_Init(&argc, &argv); /* Initialize MPI */
22   if (err != MPI_SUCCESS) {
23     printf("MPI initialization failed!\n");
24     exit(1);
25   }
26   MPI_Comm_size(MPI_COMM_WORLD, &size);   /* Get nr of tasks */
27   MPI_Comm_rank(MPI_COMM_WORLD, &rank);   /* Get id of this process */
28   if (size < 2) {
29     printf("run this program with exactly 2 processes (-np 2)\n");
30     MPI_Finalize();
31     exit(0);
32   }
33   if (0 == rank) {
34     printf("\n    *** Ping-pong test (MPI_Send/MPI_Recv) ***\n\n");
35   }
36
37   /* start ping-pong tests between several pairs */
38   for (int pivot = 0; pivot < size - 1; pivot++) {
39     if (pivot == rank) {
40       printf("\n== pivot=%d : pingpong [%d] <--> [%d]\n", pivot, pivot, pivot + 1);
41
42       int dst = rank + 1;
43       printf("[%d] About to send 1st message '%d' to process [%d]\n", rank, msg, dst);
44       MPI_Send(&msg, 1, MPI_INT, dst, tag1, MPI_COMM_WORLD);
45
46       MPI_Recv(&msg, 1, MPI_INT, dst, tag2, MPI_COMM_WORLD, &status);     /* Receive a message */
47       printf("[%d] Received reply message '%d' from process [%d]\n", rank, msg, dst);
48     }
49     if ((pivot + 1) == rank) {
50       int src = rank - 1;
51       MPI_Recv(&msg, 1, MPI_INT, src, tag1, MPI_COMM_WORLD, &status);     /* Receive a message */
52       printf("[%d] Received 1st message '%d' from process [%d]\n", rank, msg, src);
53       msg++;
54       printf("[%d] increment message's value to  '%d'\n", rank, msg);
55       printf("[%d] About to send back message '%d' to process [%d]\n", rank, msg, src);
56       MPI_Send(&msg, 1, MPI_INT, src, tag2, MPI_COMM_WORLD);
57     }
58   }
59   MPI_Finalize();
60   return 0;
61 }