Blame test/mpi/threads/comm/comm_create_group_threads2.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
/* This test test MPI_Comm_create_group tests calling by multiple threads
Packit 0848f5
   in parallel, using the same communicator but different tags */
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_Group global_group;
Packit 0848f5
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
    MPI_Group half_group, full_group;
Packit 0848f5
    int range[1][3];
Packit 0848f5
    MPI_Comm comm;
Packit 0848f5
Packit 0848f5
    range[0][0] = 0;
Packit 0848f5
    range[0][1] = size / 2;
Packit 0848f5
    range[0][2] = 1;
Packit 0848f5
Packit 0848f5
    for (i = 0; i < NUM_ITER; i++) {
Packit 0848f5
Packit 0848f5
        MPI_Comm_group(MPI_COMM_WORLD, &full_group);
Packit 0848f5
        MPI_Group_range_incl(full_group, 1, range, &half_group);
Packit 0848f5
        /* Every thread paticipates in a distinct MPI_Comm_create_group,
Packit 0848f5
         * distinguished by its thread-id (used as the tag).
Packit 0848f5
         */
Packit 0848f5
        if (verbose)
Packit 0848f5
            printf("%d: Thread %d - Comm_create_group %d start\n", rank, *(int *) arg, i);
Packit 0848f5
        if(rank <= size / 2){
Packit 0848f5
            MPI_Comm_create_group(MPI_COMM_WORLD, half_group, *(int *) arg /* tag */ , &comm);
Packit 0848f5
            MPI_Barrier(comm);
Packit 0848f5
            MPI_Comm_free(&comm);
Packit 0848f5
        }
Packit 0848f5
        if (verbose)
Packit 0848f5
            printf("%d: Thread %d - Comm_create_group %d finish\n", rank, *(int *) arg, i);
Packit 0848f5
        MPI_Group_free(&half_group);
Packit 0848f5
        MPI_Group_free(&full_group);
Packit 0848f5
Packit 0848f5
        /* Repeat the test, using the same group */
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(MPI_COMM_WORLD, global_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
    }
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
    MPI_Comm_group(MPI_COMM_WORLD, &global_group);
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
    err = MTest_Join_threads();
Packit 0848f5
Packit 0848f5
    MPI_Group_free(&global_group);
Packit 0848f5
Packit 0848f5
    MTest_Finalize(err);
Packit 0848f5
Packit 0848f5
    MPI_Finalize();
Packit 0848f5
Packit 0848f5
    return 0;
Packit 0848f5
}