Blame test/mpi/comm/cmfree.c

Packit 0848f5
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
Packit 0848f5
/*
Packit 0848f5
 *
Packit 0848f5
 *  (C) 2003 by Argonne National Laboratory.
Packit 0848f5
 *      See COPYRIGHT in top-level directory.
Packit 0848f5
 */
Packit 0848f5
#include "mpi.h"
Packit 0848f5
#include <stdio.h>
Packit 0848f5
#include <stdlib.h>
Packit 0848f5
#include "mpitest.h"
Packit 0848f5
Packit 0848f5
/*
Packit 0848f5
static char MTEST_Descrip[] = "Test that communicators have reference count semantics";
Packit 0848f5
*/
Packit 0848f5
Packit 0848f5
#define NELM 128
Packit 0848f5
#define NCOMM 1020
Packit 0848f5
Packit 0848f5
int main(int argc, char *argv[])
Packit 0848f5
{
Packit 0848f5
    int errs = 0;
Packit 0848f5
    int rank, size, source, dest, i;
Packit 0848f5
    MPI_Comm comm;
Packit 0848f5
    MPI_Comm tmpComm[NCOMM];
Packit 0848f5
    MPI_Status status;
Packit 0848f5
    MPI_Request req;
Packit 0848f5
    int *buf = 0;
Packit 0848f5
Packit 0848f5
    MTest_Init(&argc, &argv);
Packit 0848f5
Packit 0848f5
    MPI_Comm_dup(MPI_COMM_WORLD, &comm);
Packit 0848f5
Packit 0848f5
    /* This is similar to the datatype test, except that we post
Packit 0848f5
     * an irecv on a simple data buffer but use a rank-reordered communicator.
Packit 0848f5
     * In this case, an error in handling the reference count will most
Packit 0848f5
     * likely cause the program to hang, so this should be run only
Packit 0848f5
     * if (a) you are confident that the code is correct or (b)
Packit 0848f5
     * a timeout is set for mpiexec
Packit 0848f5
     */
Packit 0848f5
Packit 0848f5
    MPI_Comm_rank(comm, &rank;;
Packit 0848f5
    MPI_Comm_size(comm, &size);
Packit 0848f5
Packit 0848f5
    if (size < 2) {
Packit 0848f5
        fprintf(stderr, "This test requires at least two processes.");
Packit 0848f5
        MPI_Abort(MPI_COMM_WORLD, 1);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    source = 0;
Packit 0848f5
    dest = size - 1;
Packit 0848f5
Packit 0848f5
    if (rank == dest) {
Packit 0848f5
        buf = (int *) malloc(NELM * sizeof(int));
Packit 0848f5
        for (i = 0; i < NELM; i++)
Packit 0848f5
            buf[i] = -i;
Packit 0848f5
        MPI_Irecv(buf, NELM, MPI_INT, source, 0, comm, &req;;
Packit 0848f5
        MPI_Comm_free(&comm);
Packit 0848f5
Packit 0848f5
        if (comm != MPI_COMM_NULL) {
Packit 0848f5
            errs++;
Packit 0848f5
            printf("Freed comm was not set to COMM_NULL\n");
Packit 0848f5
        }
Packit 0848f5
Packit 0848f5
        for (i = 0; i < NCOMM; i++) {
Packit 0848f5
            MPI_Comm_split(MPI_COMM_WORLD, 0, size - rank, &tmpComm[i]);
Packit 0848f5
        }
Packit 0848f5
Packit 0848f5
        MPI_Sendrecv(NULL, 0, MPI_INT, source, 1,
Packit 0848f5
                     NULL, 0, MPI_INT, source, 1, MPI_COMM_WORLD, &status);
Packit 0848f5
Packit 0848f5
        MPI_Wait(&req, &status);
Packit 0848f5
        for (i = 0; i < NELM; i++) {
Packit 0848f5
            if (buf[i] != i) {
Packit 0848f5
                errs++;
Packit 0848f5
                if (errs < 10) {
Packit 0848f5
                    printf("buf[%d] = %d, expected %d\n", i, buf[i], i);
Packit 0848f5
                }
Packit 0848f5
            }
Packit 0848f5
        }
Packit 0848f5
        for (i = 0; i < NCOMM; i++) {
Packit 0848f5
            MPI_Comm_free(&tmpComm[i]);
Packit 0848f5
        }
Packit 0848f5
        free(buf);
Packit 0848f5
    }
Packit 0848f5
    else if (rank == source) {
Packit 0848f5
        buf = (int *) malloc(NELM * sizeof(int));
Packit 0848f5
        for (i = 0; i < NELM; i++)
Packit 0848f5
            buf[i] = i;
Packit 0848f5
Packit 0848f5
        for (i = 0; i < NCOMM; i++) {
Packit 0848f5
            MPI_Comm_split(MPI_COMM_WORLD, 0, size - rank, &tmpComm[i]);
Packit 0848f5
        }
Packit 0848f5
        /* Synchronize with the receiver */
Packit 0848f5
        MPI_Sendrecv(NULL, 0, MPI_INT, dest, 1, NULL, 0, MPI_INT, dest, 1, MPI_COMM_WORLD, &status);
Packit 0848f5
        MPI_Send(buf, NELM, MPI_INT, dest, 0, comm);
Packit 0848f5
        free(buf);
Packit 0848f5
    }
Packit 0848f5
    else {
Packit 0848f5
        for (i = 0; i < NCOMM; i++) {
Packit 0848f5
            MPI_Comm_split(MPI_COMM_WORLD, 0, size - rank, &tmpComm[i]);
Packit 0848f5
        }
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    MPI_Barrier(MPI_COMM_WORLD);
Packit 0848f5
Packit 0848f5
    if (rank != dest) {
Packit 0848f5
        /* Clean up the communicators */
Packit 0848f5
        for (i = 0; i < NCOMM; i++) {
Packit 0848f5
            MPI_Comm_free(&tmpComm[i]);
Packit 0848f5
        }
Packit 0848f5
    }
Packit 0848f5
    if (comm != MPI_COMM_NULL) {
Packit 0848f5
        MPI_Comm_free(&comm);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    MTest_Finalize(errs);
Packit 0848f5
    MPI_Finalize();
Packit 0848f5
    return 0;
Packit 0848f5
}