1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
4 * (C) 2003 by Argonne National Laboratory.
5 * See COPYRIGHT in top-level directory.
13 static char MTEST_Descrip[] = "Test that communicators have reference count semantics";
19 int main(int argc, char *argv[])
22 int rank, size, source, dest, i;
24 MPI_Comm tmpComm[NCOMM];
29 MTest_Init(&argc, &argv);
31 MPI_Comm_dup(MPI_COMM_WORLD, &comm);
33 /* This is similar to the datatype test, except that we post
34 * an irecv on a simple data buffer but use a rank-reordered communicator.
35 * In this case, an error in handling the reference count will most
36 * likely cause the program to hang, so this should be run only
37 * if (a) you are confident that the code is correct or (b)
38 * a timeout is set for mpiexec
41 MPI_Comm_rank(comm, &rank);
42 MPI_Comm_size(comm, &size);
45 fprintf(stderr, "This test requires at least two processes.");
46 MPI_Abort(MPI_COMM_WORLD, 1);
53 buf = (int *) malloc(NELM * sizeof(int));
54 for (i = 0; i < NELM; i++)
56 MPI_Irecv(buf, NELM, MPI_INT, source, 0, comm, &req);
59 if (comm != MPI_COMM_NULL) {
61 printf("Freed comm was not set to COMM_NULL\n");
64 for (i = 0; i < NCOMM; i++) {
65 MPI_Comm_split(MPI_COMM_WORLD, 0, size - rank, &tmpComm[i]);
68 MPI_Sendrecv(NULL, 0, MPI_INT, source, 1,
69 NULL, 0, MPI_INT, source, 1, MPI_COMM_WORLD, &status);
71 MPI_Wait(&req, &status);
72 for (i = 0; i < NELM; i++) {
76 printf("buf[%d] = %d, expected %d\n", i, buf[i], i);
80 for (i = 0; i < NCOMM; i++) {
81 MPI_Comm_free(&tmpComm[i]);
85 else if (rank == source) {
86 buf = (int *) malloc(NELM * sizeof(int));
87 for (i = 0; i < NELM; i++)
90 for (i = 0; i < NCOMM; i++) {
91 MPI_Comm_split(MPI_COMM_WORLD, 0, size - rank, &tmpComm[i]);
93 /* Synchronize with the receiver */
94 MPI_Sendrecv(NULL, 0, MPI_INT, dest, 1, NULL, 0, MPI_INT, dest, 1, MPI_COMM_WORLD, &status);
95 MPI_Send(buf, NELM, MPI_INT, dest, 0, comm);
99 for (i = 0; i < NCOMM; i++) {
100 MPI_Comm_split(MPI_COMM_WORLD, 0, size - rank, &tmpComm[i]);
104 MPI_Barrier(MPI_COMM_WORLD);
107 /* Clean up the communicators */
108 for (i = 0; i < NCOMM; i++) {
109 MPI_Comm_free(&tmpComm[i]);
112 if (comm != MPI_COMM_NULL) {
113 MPI_Comm_free(&comm);
116 MTest_Finalize(errs);