Blame test/mpi/threads/comm/ctxdup.c

Packit Service c5cf8c
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
Packit Service c5cf8c
/*
Packit Service c5cf8c
 *
Packit Service c5cf8c
 *  (C) 2006 by Argonne National Laboratory.
Packit Service c5cf8c
 *      See COPYRIGHT in top-level directory.
Packit Service c5cf8c
 */
Packit Service c5cf8c
#include "mpi.h"
Packit Service c5cf8c
#include <stdio.h>
Packit Service c5cf8c
#include <stdlib.h>
Packit Service c5cf8c
#include "mpitest.h"
Packit Service c5cf8c
#include "mpithreadtest.h"
Packit Service c5cf8c
Packit Service c5cf8c
/*
Packit Service c5cf8c
static char MTEST_Descrip[] = "Creating communications concurrently in different threads";
Packit Service c5cf8c
*/
Packit Service c5cf8c
Packit Service c5cf8c
MTEST_THREAD_RETURN_TYPE dup_thread(void *);
Packit Service c5cf8c
Packit Service c5cf8c
MTEST_THREAD_RETURN_TYPE dup_thread(void *p)
Packit Service c5cf8c
{
Packit Service c5cf8c
    int rank;
Packit Service c5cf8c
    int buffer[1];
Packit Service c5cf8c
    MPI_Comm *comm2_ptr = (MPI_Comm *) p;
Packit Service c5cf8c
    MPI_Comm comm3;
Packit Service c5cf8c
Packit Service c5cf8c
    MPI_Comm_rank(MPI_COMM_WORLD, &rank;;
Packit Service c5cf8c
Packit Service c5cf8c
    if (rank & 0x1) {
Packit Service c5cf8c
        /* If odd, wait for message */
Packit Service c5cf8c
        MPI_Recv(buffer, 0, MPI_INT, rank, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
Packit Service c5cf8c
    }
Packit Service c5cf8c
    MPI_Comm_dup(*comm2_ptr, &comm3;;
Packit Service c5cf8c
Packit Service c5cf8c
    MPI_Barrier(comm3);
Packit Service c5cf8c
    MPI_Recv(buffer, 0, MPI_INT, rank, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
Packit Service c5cf8c
Packit Service c5cf8c
    MPI_Comm_free(&comm3;;
Packit Service c5cf8c
    /* Tell the main thread that we're done */
Packit Service c5cf8c
    MPI_Send(buffer, 0, MPI_INT, rank, 2, MPI_COMM_WORLD);
Packit Service c5cf8c
Packit Service c5cf8c
    return (MTEST_THREAD_RETURN_TYPE) 0;
Packit Service c5cf8c
}
Packit Service c5cf8c
Packit Service c5cf8c
int main(int argc, char *argv[])
Packit Service c5cf8c
{
Packit Service c5cf8c
    int rank, size;
Packit Service c5cf8c
    int provided;
Packit Service c5cf8c
    int buffer[1];
Packit Service c5cf8c
    MPI_Comm comm1, comm2, comm4;
Packit Service c5cf8c
Packit Service c5cf8c
    MTest_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided);
Packit Service c5cf8c
Packit Service c5cf8c
    MPI_Comm_rank(MPI_COMM_WORLD, &rank;;
Packit Service c5cf8c
    MPI_Comm_size(MPI_COMM_WORLD, &size);
Packit Service c5cf8c
Packit Service c5cf8c
    /* Check that we're multi-threaded */
Packit Service c5cf8c
    if (provided != MPI_THREAD_MULTIPLE) {
Packit Service c5cf8c
        if (rank == 0) {
Packit Service c5cf8c
            printf
Packit Service c5cf8c
                ("MPI_Init_thread must return MPI_THREAD_MULTIPLE in order for this test to run.\n");
Packit Service c5cf8c
            fflush(stdout);
Packit Service c5cf8c
        }
Packit Service c5cf8c
        return 1;
Packit Service c5cf8c
    }
Packit Service c5cf8c
Packit Service c5cf8c
    /* The test is this:
Packit Service c5cf8c
     * The main thread on ODD processors tells the other thread to start
Packit Service c5cf8c
     * a comm dup(on comm2), then starts a comm dup(on comm1) after a delay.
Packit Service c5cf8c
     * The main thread on even processors starts a comm dup(on comm1)
Packit Service c5cf8c
     *
Packit Service c5cf8c
     * The second thread on ODD processors waits until it gets a message
Packit Service c5cf8c
     * (from the same process) before starting the comm dup on comm2.
Packit Service c5cf8c
     */
Packit Service c5cf8c
Packit Service c5cf8c
    /* Create two communicators */
Packit Service c5cf8c
    MPI_Comm_dup(MPI_COMM_WORLD, &comm1;;
Packit Service c5cf8c
    MPI_Comm_dup(MPI_COMM_WORLD, &comm2;;
Packit Service c5cf8c
Packit Service c5cf8c
    /* Start a thread that will perform a dup comm2 */
Packit Service c5cf8c
    MTest_Start_thread(dup_thread, (void *) &comm2;;
Packit Service c5cf8c
Packit Service c5cf8c
    /* If we're odd, send to our new thread and then delay */
Packit Service c5cf8c
    if (rank & 0x1) {
Packit Service c5cf8c
        MPI_Ssend(buffer, 0, MPI_INT, rank, 0, MPI_COMM_WORLD);
Packit Service c5cf8c
        MTestSleep(1);
Packit Service c5cf8c
    }
Packit Service c5cf8c
    MPI_Comm_dup(comm1, &comm4;;
Packit Service c5cf8c
Packit Service c5cf8c
    /* Tell the threads to exit after we've created our new comm */
Packit Service c5cf8c
    MPI_Barrier(comm4);
Packit Service c5cf8c
    MPI_Ssend(buffer, 0, MPI_INT, rank, 1, MPI_COMM_WORLD);
Packit Service c5cf8c
    MPI_Recv(buffer, 0, MPI_INT, rank, 2, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
Packit Service c5cf8c
Packit Service c5cf8c
    MTest_Join_threads();
Packit Service c5cf8c
Packit Service c5cf8c
    MPI_Comm_free(&comm4;;
Packit Service c5cf8c
    MPI_Comm_free(&comm1;;
Packit Service c5cf8c
    MPI_Comm_free(&comm2;;
Packit Service c5cf8c
Packit Service c5cf8c
    MTest_Finalize(0);
Packit Service c5cf8c
    return 0;
Packit Service c5cf8c
}