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

Packit 0848f5
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
Packit 0848f5
/*
Packit 0848f5
 *  (C) 2012 by Argonne National Laboratory.
Packit 0848f5
 *      See COPYRIGHT in top-level directory.
Packit 0848f5
 */
Packit 0848f5
Packit 0848f5
#include <stdio.h>
Packit 0848f5
#include <mpi.h>
Packit 0848f5
#include "mpitest.h"
Packit 0848f5
#include "mpithreadtest.h"
Packit 0848f5
Packit 0848f5
#define NUM_THREADS 4
Packit 0848f5
#define NUM_ITER    100
Packit 0848f5
Packit 0848f5
#define check(X_)       \
Packit 0848f5
    do {                \
Packit 0848f5
        if (!(X_)) {    \
Packit 0848f5
            printf("[%s:%d] -- Assertion failed: %s\n", __FILE__, __LINE__, #X_);\
Packit 0848f5
            MPI_Abort(MPI_COMM_WORLD, 1); \
Packit 0848f5
        }               \
Packit 0848f5
    } while (0)
Packit 0848f5
Packit 0848f5
MPI_Comm comms[NUM_THREADS];
Packit 0848f5
MTEST_THREAD_LOCK_TYPE comm_lock;
Packit 0848f5
int rank, size;
Packit 0848f5
int verbose = 0;
Packit 0848f5
Packit 0848f5
MTEST_THREAD_RETURN_TYPE test_comm_create_group(void *arg)
Packit 0848f5
{
Packit 0848f5
    int i;
Packit 0848f5
Packit 0848f5
    for (i = 0; i < NUM_ITER; i++) {
Packit 0848f5
        MPI_Group world_group;
Packit 0848f5
        MPI_Comm comm, self_dup;
Packit 0848f5
Packit 0848f5
        MPI_Comm_group(comms[*(int *) arg], &world_group);
Packit 0848f5
Packit 0848f5
        /* Every thread paticipates in a distinct MPI_Comm_create group,
Packit 0848f5
         * distinguished by its thread-id (used as the tag).  Threads on even
Packit 0848f5
         * ranks join an even comm and threads on odd ranks join the odd comm.
Packit 0848f5
         */
Packit 0848f5
Packit 0848f5
        if (verbose)
Packit 0848f5
            printf("%d: Thread %d - Comm_create_group %d start\n", rank, *(int *) arg, i);
Packit 0848f5
        MPI_Comm_create_group(comms[*(int *) arg], world_group, *(int *) arg /* tag */ , &comm);
Packit 0848f5
        MPI_Barrier(comm);
Packit 0848f5
        MPI_Comm_free(&comm);
Packit 0848f5
        if (verbose)
Packit 0848f5
            printf("%d: Thread %d - Comm_create_group %d finish\n", rank, *(int *) arg, i);
Packit 0848f5
Packit 0848f5
        MPI_Group_free(&world_group);
Packit 0848f5
Packit 0848f5
        /* Threads now call a collective MPI Comm routine.  This is done on a
Packit 0848f5
         * separate communicator so it can be concurrent with calls to
Packit 0848f5
         * Comm_create_group.  A lock is needed to preserve MPI's threads +
Packit 0848f5
         * collective semantics.
Packit 0848f5
         */
Packit 0848f5
        MTest_thread_lock(&comm_lock);
Packit 0848f5
        if (verbose)
Packit 0848f5
            printf("%d: Thread %d - Comm_dup %d start\n", rank, *(int *) arg, i);
Packit 0848f5
        MPI_Comm_dup(MPI_COMM_SELF, &self_dup);
Packit 0848f5
        if (verbose)
Packit 0848f5
            printf("%d: Thread %d - Comm_dup %d finish\n", rank, *(int *) arg, i);
Packit 0848f5
        MTest_thread_unlock(&comm_lock);
Packit 0848f5
Packit 0848f5
        MPI_Barrier(self_dup);
Packit 0848f5
        MPI_Comm_free(&self_dup);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    if (verbose)
Packit 0848f5
        printf("%d: Thread %d - Done.\n", rank, *(int *) arg);
Packit 0848f5
    return NULL;
Packit 0848f5
}
Packit 0848f5
Packit 0848f5
Packit 0848f5
int main(int argc, char **argv)
Packit 0848f5
{
Packit 0848f5
    int thread_args[NUM_THREADS];
Packit 0848f5
    int i, err, provided;
Packit 0848f5
Packit 0848f5
    MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided);
Packit 0848f5
Packit 0848f5
    check(provided == MPI_THREAD_MULTIPLE);
Packit 0848f5
Packit 0848f5
    MPI_Comm_rank(MPI_COMM_WORLD, &rank;;
Packit 0848f5
    MPI_Comm_size(MPI_COMM_WORLD, &size);
Packit 0848f5
Packit 0848f5
    MTest_thread_lock_create(&comm_lock);
Packit 0848f5
Packit 0848f5
    for (i = 0; i < NUM_THREADS; i++) {
Packit 0848f5
        MPI_Comm_dup(MPI_COMM_WORLD, &comms[i]);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    for (i = 0; i < NUM_THREADS; i++) {
Packit 0848f5
        thread_args[i] = i;
Packit 0848f5
        MTest_Start_thread(test_comm_create_group, (void *) &thread_args[i]);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    MTest_Join_threads();
Packit 0848f5
Packit 0848f5
    for (i = 0; i < NUM_THREADS; i++) {
Packit 0848f5
        MPI_Comm_free(&comms[i]);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    if (rank == 0)
Packit 0848f5
        printf(" No Errors\n");
Packit 0848f5
Packit 0848f5
    MPI_Finalize();
Packit 0848f5
Packit 0848f5
    return 0;
Packit 0848f5
}