File: comm_create_group_threads2.c

package info (click to toggle)
mpich 5.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 251,828 kB
  • sloc: ansic: 1,323,147; cpp: 82,869; f90: 72,420; javascript: 40,763; perl: 28,296; sh: 19,399; python: 16,191; xml: 14,418; makefile: 9,468; fortran: 8,046; java: 4,635; pascal: 352; asm: 324; ruby: 176; awk: 27; lisp: 19; php: 8; sed: 4
file content (103 lines) | stat: -rw-r--r-- 2,884 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

/* This test test MPI_Comm_create_group tests calling by multiple threads
   in parallel, using the same communicator but different tags */

#include <stdio.h>
#include <mpi.h>
#include "mpitest.h"
#include "mpithreadtest.h"

#define NUM_THREADS 4
#define NUM_ITER    100

#define check(X_)       \
    do {                \
        if (!(X_)) {    \
            printf("[%s:%d] -- Assertion failed: %s\n", __FILE__, __LINE__, #X_);\
            MPI_Abort(MPI_COMM_WORLD, 1); \
        }               \
    } while (0)

MPI_Group global_group;

int rank, size;
int verbose = 0;

static MTEST_THREAD_RETURN_TYPE test_comm_create_group(void *arg)
{
    int i;
    MPI_Group half_group, full_group;
    int range[1][3];
    MPI_Comm comm;

    range[0][0] = 0;
    range[0][1] = size / 2;
    range[0][2] = 1;

    for (i = 0; i < NUM_ITER; i++) {

        MPI_Comm_group(MPI_COMM_WORLD, &full_group);
        MPI_Group_range_incl(full_group, 1, range, &half_group);
        /* Every thread paticipates in a distinct MPI_Comm_create_group,
         * distinguished by its thread-id (used as the tag).
         */
        if (verbose)
            printf("%d: Thread %d - Comm_create_group %d start\n", rank, *(int *) arg, i);
        if (rank <= size / 2) {
            MPI_Comm_create_group(MPI_COMM_WORLD, half_group, *(int *) arg /* tag */ , &comm);
            MPI_Barrier(comm);
            MPI_Comm_free(&comm);
        }
        if (verbose)
            printf("%d: Thread %d - Comm_create_group %d finish\n", rank, *(int *) arg, i);
        MPI_Group_free(&half_group);
        MPI_Group_free(&full_group);

        /* Repeat the test, using the same group */
        if (verbose)
            printf("%d: Thread %d - Comm_create_group %d start\n", rank, *(int *) arg, i);
        MPI_Comm_create_group(MPI_COMM_WORLD, global_group, *(int *) arg /* tag */ , &comm);
        MPI_Barrier(comm);
        MPI_Comm_free(&comm);
        if (verbose)
            printf("%d: Thread %d - Comm_create_group %d finish\n", rank, *(int *) arg, i);
    }

    if (verbose)
        printf("%d: Thread %d - Done.\n", rank, *(int *) arg);
    return NULL;
}


int main(int argc, char **argv)
{
    int thread_args[NUM_THREADS];
    int i, errs, provided;

    MTest_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided);

    check(provided == MPI_THREAD_MULTIPLE);

    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    MPI_Comm_group(MPI_COMM_WORLD, &global_group);

    for (i = 0; i < NUM_THREADS; i++) {
        thread_args[i] = i;
        MTest_Start_thread(test_comm_create_group, (void *) &thread_args[i]);
    }

    errs = MTest_Join_threads();

    MPI_Group_free(&global_group);

    MTest_Finalize(errs);


    return MTestReturnValue(errs);
}