File: pgroup_connect_test.c

package info (click to toggle)
mpich 4.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 423,384 kB
  • sloc: ansic: 1,088,434; cpp: 71,364; javascript: 40,763; f90: 22,829; sh: 17,463; perl: 14,773; xml: 14,418; python: 10,265; makefile: 9,246; fortran: 8,008; java: 4,355; asm: 324; ruby: 176; lisp: 19; php: 8; sed: 4
file content (151 lines) | stat: -rw-r--r-- 3,842 bytes parent folder | download | duplicates (3)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

/** PGROUP Creation (Connect/Accept Method)
  * James Dinan <dinan@mcs.anl.gov>
  * May, 2011
  *
  * In this test, processes create an intracommunicator and creation is
  * collective only on the members of the new communicator, not on the parent
  * communicator.  This is accomplished by building up and merging
  * intercommunicators using Connect/Accept to merge with a controller
  * process.
  */

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

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

const int verbose = 0;

void PGroup_create(int count, int members[], MPI_Comm * group);
void PGroup_create(int count, int members[], MPI_Comm * group)
{
    int i, me, nproc, is_member;
    char *port;
    MPI_Comm pgroup;

    MPI_Comm_rank(MPI_COMM_WORLD, &me);
    MPI_Comm_size(MPI_COMM_WORLD, &nproc);

    if (count == 0) {
        *group = MPI_COMM_NULL;
        return;
    } else if (count == 1 && members[0] == me) {
        *group = MPI_COMM_SELF;
        return;
    }

    for (i = 0, is_member = 0; i < count; i++) {
        if (members[i] == me) {
            is_member = 1;
            break;
        }
    }

    if (!is_member) {
        *group = MPI_COMM_NULL;
        return;
    }

    port = malloc(MPI_MAX_PORT_NAME);

    /* NOTE: Assume members list is identical and sorted on all processes */

    /* I am the leader */
    if (me == members[0]) {
        MPI_Comm pgroup_new;

        pgroup = MPI_COMM_SELF;

        MPI_Open_port(MPI_INFO_NULL, port);

        for (i = 1; i < count; i++) {
            MPI_Comm pgroup_old = pgroup;

            MPI_Send(port, MPI_MAX_PORT_NAME, MPI_CHAR, members[i], 0, MPI_COMM_WORLD);
            MPI_Comm_accept(port, MPI_INFO_NULL, 0, pgroup, &pgroup_new);
            MPI_Intercomm_merge(pgroup_new, 0 /* LOW */ , &pgroup);

            MPI_Comm_free(&pgroup_new);
            if (pgroup_old != MPI_COMM_SELF)
                MPI_Comm_free(&pgroup_old);
        }

        MPI_Close_port(port);
    }

    /* I am not the leader */
    else {
        int merged = 0;
        MPI_Comm pgroup_new;

        for (i = 0; i < count; i++) {
            if (members[i] == me) {
                MPI_Recv(port, MPI_MAX_PORT_NAME, MPI_CHAR, members[0], 0, MPI_COMM_WORLD,
                         MPI_STATUS_IGNORE);
                MPI_Comm_connect(port, MPI_INFO_NULL, 0, MPI_COMM_SELF, &pgroup_new);
                MPI_Intercomm_merge(pgroup_new, 1 /* HIGH */ , &pgroup);
                MPI_Comm_free(&pgroup_new);
                merged = 1;
            } else if (merged) {
                MPI_Comm pgroup_old = pgroup;

                MPI_Comm_connect(port, MPI_INFO_NULL, 0, pgroup, &pgroup_new);
                MPI_Intercomm_merge(pgroup_new, 0 /* HIGH */ , &pgroup);

                MPI_Comm_free(&pgroup_new);
                MPI_Comm_free(&pgroup_old);
            }
            /* else => !merged */
        }
    }

    free(port);
    *group = pgroup;
}


int main(int argc, char **argv)
{
    int me, nproc, i;
    int gsize, *glist;
    MPI_Comm group;

    MTest_Init(&argc, &argv);

    MPI_Comm_rank(MPI_COMM_WORLD, &me);
    MPI_Comm_size(MPI_COMM_WORLD, &nproc);

    gsize = nproc / 2 + (nproc % 2);
    glist = malloc(gsize * sizeof(int));

    for (i = 0; i < nproc; i += 2)
        glist[i / 2] = i;

    if (me % 2 == 0) {
        int gme, gnproc;

        PGroup_create(gsize, glist, &group);
        MPI_Barrier(group);

        MPI_Comm_rank(group, &gme);
        MPI_Comm_size(group, &gnproc);
        if (verbose)
            printf("[%d] Group rank = %d, size = %d\n", me, gme, gnproc);

        if (group != MPI_COMM_SELF)
            MPI_Comm_free(&group);
    }

    free(glist);

    MTest_Finalize(0);

    return MTestReturnValue(0);
}