1 /*
2  * Copyright (C) by Argonne National Laboratory
3  *     See COPYRIGHT in top-level directory
4  */
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include "mpi.h"
10 #include "mpitest.h"
11 
12 #define ITERS 4
13 
main(int argc,char ** argv)14 int main(int argc, char **argv)
15 {
16     int errs = 0;
17     int i, j;
18     int rank, size, rsize;
19     int in[ITERS], out[ITERS], sol[ITERS], cnt;
20     int isLeft;
21     MPI_Comm newcomm[ITERS], testcomm;
22     MPI_Request *sreq;
23 
24     MTest_Init(&argc, &argv);
25     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
26     MPI_Comm_size(MPI_COMM_WORLD, &size);
27     if (size < 2) {
28         printf("this test requires at least 2 processes\n");
29         MPI_Abort(MPI_COMM_WORLD, 1);
30     }
31 
32     sreq = (MPI_Request *) malloc(sizeof(MPI_Request) * (size + 1) * ITERS);
33 
34     while (MTestGetIntracommGeneral(&testcomm, 1, 1)) {
35         if (testcomm == MPI_COMM_NULL)
36             continue;
37 
38         MPI_Comm_rank(testcomm, &rank);
39         MPI_Comm_size(testcomm, &size);
40         cnt = 0;
41         for (j = 0; j < ITERS; j++) {
42             if (rank == 0) {
43                 out[j] = 815;
44                 in[j] = 815;
45                 sol[j] = 815;
46                 for (i = 1; i < size; i++)
47                     MPI_Isend(&out[j], 1, MPI_INT, i, 0, testcomm, &sreq[cnt++]);
48                 MPI_Comm_idup(testcomm, &newcomm[j], &sreq[cnt++]);
49             } else {
50                 out[j] = 0;
51                 in[j] = 0;
52                 sol[j] = 815;
53                 MPI_Comm_idup(testcomm, &newcomm[j], &sreq[cnt++]);
54                 MPI_Irecv(&in[j], 1, MPI_INT, 0, 0, testcomm, &sreq[cnt++]);
55             }
56         }
57         MPI_Waitall(cnt, sreq, MPI_STATUS_IGNORE);
58 
59         for (j = 0; j < ITERS; j++) {
60             if (sol[j] != in[j])
61                 errs++;
62             errs += MTestTestComm(newcomm[j]);
63             MPI_Comm_free(&newcomm[j]);
64         }
65         MTestFreeComm(&testcomm);
66     }
67     while (MTestGetIntercomm(&testcomm, &isLeft, 1)) {
68         if (testcomm == MPI_COMM_NULL)
69             continue;
70 
71         MPI_Comm_rank(testcomm, &rank);
72         MPI_Comm_size(testcomm, &size);
73         MPI_Comm_remote_size(testcomm, &rsize);
74         cnt = 0;
75         for (j = 0; j < ITERS; j++) {
76             if (rank == 0) {
77                 out[j] = 815;
78                 in[j] = 815;
79                 sol[j] = 815;
80                 for (i = 1; i < rsize; i++)
81                     MPI_Isend(&out[j], 1, MPI_INT, i, 0, testcomm, &sreq[cnt++]);
82                 MPI_Comm_idup(testcomm, &newcomm[j], &sreq[cnt++]);
83             } else {
84                 out[j] = 0;
85                 in[j] = 0;
86                 sol[j] = 815;
87                 MPI_Comm_idup(testcomm, &newcomm[j], &sreq[cnt++]);
88                 MPI_Irecv(&in[j], 1, MPI_INT, 0, 0, testcomm, &sreq[cnt++]);
89             }
90         }
91         MPI_Waitall(cnt, sreq, MPI_STATUS_IGNORE);
92 
93         for (j = 0; j < ITERS; j++) {
94             if (sol[j] != in[j])
95                 errs++;
96             errs += MTestTestComm(newcomm[j]);
97             MPI_Comm_free(&newcomm[j]);
98         }
99         MTestFreeComm(&testcomm);
100     }
101 
102     free(sreq);
103     MTest_Finalize(errs);
104     return MTestReturnValue(errs);
105 }
106