Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Improve option smpi/wtime
[simgrid.git] / teshsuite / smpi / isp / umpire / basic-deadlock-intercomm_merge.c
1 /* -*- Mode: C; -*- */
2 /* Creator: Bronis R. de Supinski (bronis@llnl.gov) Fri Mar  17 2000 */
3 /* no-error.c -- do some MPI calls without any errors */
4
5 #include <stdio.h>
6 #include "mpi.h"
7
8 #define buf_size 128
9
10 #define INTERCOMM_CREATE_TAG 666
11
12 int
13 main (int argc, char **argv)
14 {
15   int nprocs = -1;
16   int rank = -1;
17   char processor_name[128];
18   int namelen = 128;
19   int buf0[buf_size];
20   int buf1[buf_size];
21   MPI_Status status;
22   MPI_Comm comm, temp, intercomm;
23   int drank, dnprocs, rleader;
24
25   /* init */
26   MPI_Init (&argc, &argv);
27   MPI_Comm_size (MPI_COMM_WORLD, &nprocs);
28   MPI_Comm_rank (MPI_COMM_WORLD, &rank);
29   MPI_Get_processor_name (processor_name, &namelen);
30   printf ("(%d) is alive on %s\n", rank, processor_name);
31   fflush (stdout);
32
33   MPI_Barrier (MPI_COMM_WORLD);
34
35   if (nprocs < 3) {
36     printf ("not enough tasks\n");
37   }
38   else {
39     /* need to make split communicator temporarily... */
40     MPI_Comm_split (MPI_COMM_WORLD, rank % 2, nprocs - rank, &temp);
41
42     if (temp != MPI_COMM_NULL) {
43       /* create an intercommunicator temporarily so can merge it... */
44       rleader = ((rank + nprocs) % 2) ?  nprocs - 2 : nprocs - 1;
45       MPI_Intercomm_create (temp, 0, MPI_COMM_WORLD, rleader,
46                             INTERCOMM_CREATE_TAG, &intercomm);
47
48       MPI_Comm_free (&temp);
49
50       if (intercomm == MPI_COMM_NULL) {
51         printf ("(%d) MPI_Intercomm_Create returned MPI_COMM_NULL\n", rank);
52         printf ("(%d) Aborting...\n", rank);
53         MPI_Abort (MPI_COMM_WORLD, 666);
54         exit (666);
55       }
56
57       MPI_Intercomm_merge (intercomm, rank % 2, &comm);
58
59       MPI_Comm_free (&intercomm);
60
61       if (comm != MPI_COMM_NULL) {
62         MPI_Comm_size (comm, &dnprocs);
63         MPI_Comm_rank (comm, &drank);
64
65         if (dnprocs > 1) {
66           if (drank == 0) {
67             memset (buf0, 0, buf_size);
68         
69             MPI_Recv (buf1, buf_size, MPI_INT, 1, 0, comm, &status);
70         
71             MPI_Send (buf0, buf_size, MPI_INT, 1, 0, comm);
72           }
73           else if (drank == 1) {
74             memset (buf1, 1, buf_size);
75
76             MPI_Recv (buf0, buf_size, MPI_INT, 0, 0, comm, &status);
77
78             MPI_Send (buf1, buf_size, MPI_INT, 0, 0, comm);
79           }
80         }
81         else {
82           printf ("(%d) Derived communicator too small (size = %d)\n",
83                   rank, dnprocs);
84         }
85
86         MPI_Comm_free (&comm);
87       }
88       else {
89         printf ("(%d) Got MPI_COMM_NULL\n", rank);
90       }
91     }
92     else {
93       printf ("(%d) MPI_Comm_split got MPI_COMM_NULL\n", rank);
94     }
95   }
96
97   MPI_Barrier (MPI_COMM_WORLD);
98
99   MPI_Finalize ();
100   printf ("(%d) Finished normally\n", rank);
101 }
102
103 /* EOF */