File: multipart.c

package info (click to toggle)
mpich 4.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites:
  • 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 (292 lines) | stat: -rw-r--r-- 8,952 bytes parent folder | download | duplicates (4)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

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

/* Checks message matching with multiple partitioned calls.
 *
 * It checks three types of matching scenarios:
 * - same comm same tag: matches in program order of initialization calls
 * - same comm different tags: matches based on tags
 * - different comms: matches based on comms */

#define PARTITIONS 8
#define COUNT 64
#define TOT_COUNT (PARTITIONS * COUNT)

/* Internal variables */
static int bufs[3][TOT_COUNT];
static int rank, size;

/* Internal utility routines */
static void fill_data(int seed, int *buf)
{
    for (int i = 0; i < TOT_COUNT; i++)
        buf[i] = seed;
}

static int check_recv_buf(int seed, int *buf)
{
    int errs = 0;
    for (int i = 0; i < TOT_COUNT; i++) {
        if (buf[i] != seed) {
            if (errs < 10) {
                fprintf(stderr, "expected %d but received %d at buf[%d]\n", seed, buf[i], i);
                fflush(stderr);
            }
            errs++;
        }
    }
    return errs;
}

static int check_recv_stat(int source, int tag, MPI_Status stat)
{
    int errs = 0;

    if (stat.MPI_SOURCE != source) {
        fprintf(stderr, "expected status.MPI_SOURCE %d, but received %d\n",
                source, stat.MPI_SOURCE);
        fflush(stderr);
        errs++;
    }
    if (stat.MPI_TAG != tag) {
        fprintf(stderr, "expected status.MPI_TAG %d, but received %d\n", tag, stat.MPI_TAG);
        fflush(stderr);
        errs++;
    }
    if (stat.MPI_ERROR != MPI_SUCCESS) {
        fprintf(stderr, "expected status.MPI_ERROR %d, but received %d\n",
                MPI_SUCCESS, stat.MPI_ERROR);
        fflush(stderr);
        errs++;
    }
    return errs;
}

static void start_sends(MPI_Request * reqs, MPI_Status * stats)
{
    MPI_Start(&reqs[0]);
    fill_data(1, bufs[0]);
    MPI_Pready_range(0, PARTITIONS - 1, reqs[0]);

    MPI_Start(&reqs[1]);
    fill_data(2, bufs[1]);
    MPI_Pready_range(0, PARTITIONS - 1, reqs[1]);

    MPI_Start(&reqs[2]);
    fill_data(3, bufs[2]);
    MPI_Pready_range(0, PARTITIONS - 1, reqs[2]);

    MPI_Waitall(3, reqs, stats);
}

static void start_recvs(MPI_Request * reqs, MPI_Status * stats)
{
    fill_data(-1, bufs[0]);
    MPI_Start(&reqs[0]);

    fill_data(-1, bufs[1]);
    MPI_Start(&reqs[1]);

    fill_data(-1, bufs[2]);
    MPI_Start(&reqs[2]);
    MPI_Waitall(3, reqs, stats);
}

static int check_recv_results(MPI_Status * stats, int *exp_sources, int *exp_tags)
{
    int errs = 0;

    errs += check_recv_buf(1, bufs[0]);
    errs += check_recv_buf(2, bufs[1]);
    errs += check_recv_buf(3, bufs[2]);

    errs += check_recv_stat(exp_sources[0], exp_tags[0], stats[0]);
    errs += check_recv_stat(exp_sources[1], exp_tags[1], stats[1]);
    errs += check_recv_stat(exp_sources[2], exp_tags[2], stats[2]);

    return errs;
}

/* Test routines for different scenarios */
static int test_diff_comm(void)
{
    int errs = 0;
    MPI_Request reqs[3];
    MPI_Status stats[3];

    int source = 0, dest = size - 1;

    MPI_Comm comms[3];
    for (int i = 0; i < 3; i++) {
        MPI_Comm_dup(MPI_COMM_WORLD, &comms[i]);
    }

    if (rank == source) {
        MTestPrintfMsg(1, "Testing partitioned message matching with different comm same tag\n");

        /* Perform initialization with different tags.
         * Expect message should match following tags. */
        MPI_Psend_init(bufs[2], PARTITIONS, COUNT, MPI_INT, dest, 0,
                       comms[2], MPI_INFO_NULL, &reqs[2]);
        MPI_Psend_init(bufs[1], PARTITIONS, COUNT, MPI_INT, dest, 0,
                       comms[1], MPI_INFO_NULL, &reqs[1]);
        MPI_Psend_init(bufs[0], PARTITIONS, COUNT, MPI_INT, dest, 0,
                       comms[0], MPI_INFO_NULL, &reqs[0]);

        start_sends(reqs, stats);       /* start sends in the order of 0,1,2 */

    } else if (rank == dest) {
        MPI_Precv_init(bufs[0], PARTITIONS, COUNT, MPI_INT, source, 0,
                       comms[0], MPI_INFO_NULL, &reqs[0]);
        MPI_Precv_init(bufs[1], PARTITIONS, COUNT, MPI_INT, source, 0,
                       comms[1], MPI_INFO_NULL, &reqs[1]);
        MPI_Precv_init(bufs[2], PARTITIONS, COUNT, MPI_INT, source, 0,
                       comms[2], MPI_INFO_NULL, &reqs[2]);

        start_recvs(reqs, stats);       /* start receives in the order of 0,1,2 */

        int exp_sources[3] = { source, source, source };
        int exp_tags[3] = { 0, 0, 0 };
        errs = check_recv_results(stats, exp_sources, exp_tags);
    }

    if (rank == source || rank == dest) {
        for (int i = 0; i < 3; i++) {
            MPI_Request_free(&reqs[i]);
        }
    }

    for (int i = 0; i < 3; i++) {
        MPI_Comm_free(&comms[i]);
    }

    return errs;
}

static int test_same_comm_diff_tag(void)
{
    int errs = 0;
    MPI_Request reqs[3];
    MPI_Status stats[3];

    int source = 0, dest = size - 1;

    if (rank == source) {
        MTestPrintfMsg(1, "Testing partitioned message matching with same comm different tag\n");

        /* Perform initialization with different tags.
         * Expect message should match following tags. */
        MPI_Psend_init(bufs[2], PARTITIONS, COUNT, MPI_INT, dest, 2,
                       MPI_COMM_WORLD, MPI_INFO_NULL, &reqs[2]);
        MPI_Psend_init(bufs[1], PARTITIONS, COUNT, MPI_INT, dest, 1,
                       MPI_COMM_WORLD, MPI_INFO_NULL, &reqs[1]);
        MPI_Psend_init(bufs[0], PARTITIONS, COUNT, MPI_INT, dest, 0,
                       MPI_COMM_WORLD, MPI_INFO_NULL, &reqs[0]);

        start_sends(reqs, stats);       /* start sends in the order of 0,1,2 */

    } else if (rank == dest) {
        MPI_Precv_init(bufs[0], PARTITIONS, COUNT, MPI_INT, source, 0,
                       MPI_COMM_WORLD, MPI_INFO_NULL, &reqs[0]);
        MPI_Precv_init(bufs[1], PARTITIONS, COUNT, MPI_INT, source, 1,
                       MPI_COMM_WORLD, MPI_INFO_NULL, &reqs[1]);
        MPI_Precv_init(bufs[2], PARTITIONS, COUNT, MPI_INT, source, 2,
                       MPI_COMM_WORLD, MPI_INFO_NULL, &reqs[2]);

        start_recvs(reqs, stats);       /* start receives in the order of 0,1,2 */

        int exp_sources[3] = { source, source, source };
        int exp_tags[3] = { 0, 1, 2 };
        errs = check_recv_results(stats, exp_sources, exp_tags);
    }

    if (rank == source || rank == dest) {
        for (int i = 0; i < 3; i++) {
            MPI_Request_free(&reqs[i]);
        }
    }

    return errs;
}

static int test_same_comm_same_tag(void)
{
    int errs = 0;
    MPI_Request reqs[3];
    MPI_Status stats[3];

    int source = 0, dest = size - 1;

    if (rank == source) {
        MTestPrintfMsg(1, "Testing partitioned message matching with same comm same tag\n");

        /* Perform initialization and start/pready in different order.
         * Expect message should match following the order of initialization calls. */
        MPI_Psend_init(bufs[0], PARTITIONS, COUNT, MPI_INT, dest, 0,
                       MPI_COMM_WORLD, MPI_INFO_NULL, &reqs[0]);
        MPI_Psend_init(bufs[1], PARTITIONS, COUNT, MPI_INT, dest, 0,
                       MPI_COMM_WORLD, MPI_INFO_NULL, &reqs[1]);
        MPI_Psend_init(bufs[2], PARTITIONS, COUNT, MPI_INT, dest, 0,
                       MPI_COMM_WORLD, MPI_INFO_NULL, &reqs[2]);

        MPI_Start(&reqs[2]);
        fill_data(3, bufs[2]);
        MPI_Pready_range(0, PARTITIONS - 1, reqs[2]);

        MPI_Start(&reqs[0]);
        fill_data(1, bufs[0]);
        MPI_Pready_range(0, PARTITIONS - 1, reqs[0]);

        MPI_Start(&reqs[1]);
        fill_data(2, bufs[1]);
        MPI_Pready_range(0, PARTITIONS - 1, reqs[1]);

        MPI_Waitall(3, reqs, stats);

    } else if (rank == dest) {
        MPI_Precv_init(bufs[0], PARTITIONS, COUNT, MPI_INT, source, 0,
                       MPI_COMM_WORLD, MPI_INFO_NULL, &reqs[0]);
        MPI_Precv_init(bufs[1], PARTITIONS, COUNT, MPI_INT, source, 0,
                       MPI_COMM_WORLD, MPI_INFO_NULL, &reqs[1]);
        MPI_Precv_init(bufs[2], PARTITIONS, COUNT, MPI_INT, source, 0,
                       MPI_COMM_WORLD, MPI_INFO_NULL, &reqs[2]);

        start_recvs(reqs, stats);       /* start receives in the order of 0,1,2 */

        int exp_sources[3] = { source, source, source };
        int exp_tags[3] = { 0, 0, 0 };
        errs = check_recv_results(stats, exp_sources, exp_tags);
    }

    if (rank == source || rank == dest) {
        for (int i = 0; i < 3; i++) {
            MPI_Request_free(&reqs[i]);
        }
    }

    return errs;
}

int main(int argc, char *argv[])
{
    int errs = 0;

    MTest_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    errs += test_same_comm_same_tag();

    errs += test_same_comm_diff_tag();

    errs += test_diff_comm();

    MTest_Finalize(errs);
    return MTestReturnValue(errs);
}