File: op_coll.c

package info (click to toggle)
mpich 4.3.0%2Breally4.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 419,120 kB
  • sloc: ansic: 1,215,557; cpp: 74,755; javascript: 40,763; f90: 20,649; sh: 18,463; xml: 14,418; python: 14,397; perl: 13,772; makefile: 9,279; fortran: 8,063; java: 4,553; asm: 324; ruby: 176; lisp: 19; php: 8; sed: 4
file content (297 lines) | stat: -rw-r--r-- 11,288 bytes parent folder | download | duplicates (2)
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
293
294
295
296
297
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

/* A test of all op-based collectives with support for GPU buffers */

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

#define COUNT (10)

#define my_assert(cond_)                                                  \
    do {                                                                  \
        if (!(cond_)) {                                                   \
            printf("assertion (%s) failed at line %d, aborting\n", #cond_, __LINE__); \
            MPI_Abort(MPI_COMM_WORLD, 1);                                 \
        }                                                                 \
    } while (0)

int *buf;
int *buf_h;
int *recvbuf;
int *recvbuf_h;
int *recvcounts;
MPI_Request req;
mtest_mem_type_e memtype;

void test_op_coll_with_root(int rank, int size, int root);

static int run_test(mtest_mem_type_e oddmem, mtest_mem_type_e evenmem)
{
    int rank, size;

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

    if (rank == 0) {
        MTestPrintfMsg(1, "./op_coll -evenmemtype=%s -oddmemtype=%s\n",
                       MTest_memtype_name(evenmem), MTest_memtype_name(oddmem));
    }

    if (rank % 2 == 0)
        memtype = evenmem;
    else
        memtype = oddmem;

    MTestMalloc(COUNT * size * sizeof(int), memtype, (void **) &buf_h, (void **) &buf, rank);
    MTestMalloc(COUNT * size * sizeof(int), memtype, (void **) &recvbuf_h, (void **) &recvbuf,
                rank);
    recvcounts = malloc(size * sizeof(int));
    test_op_coll_with_root(rank, size, 0);
    test_op_coll_with_root(rank, size, size - 1);

    MTestFree(memtype, buf_h, buf);
    MTestFree(memtype, recvbuf_h, recvbuf);
    free(recvcounts);
    return 0;
}

int main(int argc, char **argv)
{
    int errs = 0;
    MTest_Init(&argc, &argv);

    struct dtp_args dtp_args;
    dtp_args_init(&dtp_args, MTEST_COLL_NOCOUNT, argc, argv);
    while (dtp_args_get_next(&dtp_args)) {
        errs += run_test(dtp_args.u.coll.evenmem, dtp_args.u.coll.oddmem);
    }
    dtp_args_finalize(&dtp_args);

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

void test_op_coll_with_root(int rank, int size, int root)
{
    int i, j;

    /* MPI_Reduce */
    for (i = 0; i < COUNT; ++i) {
        buf_h[i] = rank + i;
        recvbuf_h[i] = 0xdeadbeef;
    }
    MTestCopyContent(buf_h, buf, COUNT * sizeof(int), memtype);
    MTestCopyContent(recvbuf_h, recvbuf, COUNT * sizeof(int), memtype);
    MPI_Reduce(buf, recvbuf, COUNT, MPI_INT, MPI_SUM, root, MPI_COMM_WORLD);
    if (rank == root) {
        MTestCopyContent(recvbuf, recvbuf_h, COUNT * sizeof(int), memtype);
        for (i = 0; i < COUNT; ++i) {
            if (recvbuf_h[i] != ((size * (size - 1) / 2) + (i * size)))
                printf("got recvbuf[%d]=%d, expected %d\n", i, recvbuf_h[i],
                       ((size * (size - 1) / 2) + (i * size)));
            my_assert(recvbuf_h[i] == ((size * (size - 1) / 2) + (i * size)));
        }
    }

    /* MPI_Ireduce */
    for (i = 0; i < COUNT; ++i) {
        buf_h[i] = rank + i;
        recvbuf_h[i] = 0xdeadbeef;
    }
    MTestCopyContent(buf_h, buf, COUNT * sizeof(int), memtype);
    MTestCopyContent(recvbuf_h, recvbuf, COUNT * sizeof(int), memtype);
    MPI_Ireduce(buf, recvbuf, COUNT, MPI_INT, MPI_SUM, root, MPI_COMM_WORLD, &req);
    MPI_Wait(&req, MPI_STATUS_IGNORE);
    if (rank == root) {
        MTestCopyContent(recvbuf, recvbuf_h, COUNT * sizeof(int), memtype);
        for (i = 0; i < COUNT; ++i) {
            if (recvbuf_h[i] != ((size * (size - 1) / 2) + (i * size)))
                printf("got recvbuf_h[%d]=%d, expected %d\n", i, recvbuf_h[i],
                       ((size * (size - 1) / 2) + (i * size)));
            my_assert(recvbuf_h[i] == ((size * (size - 1) / 2) + (i * size)));
        }
    }

    /* MPI_Allreduce */
    for (i = 0; i < COUNT; ++i) {
        buf_h[i] = rank + i;
        recvbuf_h[i] = 0xdeadbeef;
    }
    MTestCopyContent(buf_h, buf, COUNT * sizeof(int), memtype);
    MTestCopyContent(recvbuf_h, recvbuf, COUNT * sizeof(int), memtype);
    MPI_Allreduce(buf, recvbuf, COUNT, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
    MTestCopyContent(recvbuf, recvbuf_h, COUNT * sizeof(int), memtype);
    for (i = 0; i < COUNT; ++i) {
        if (recvbuf_h[i] != ((size * (size - 1) / 2) + (i * size)))
            printf("got recvbuf_h[%d]=%d, expected %d\n", i, recvbuf_h[i],
                   ((size * (size - 1) / 2) + (i * size)));
        my_assert(recvbuf_h[i] == ((size * (size - 1) / 2) + (i * size)));
    }

    /* MPI_Iallreduce */
    for (i = 0; i < COUNT; ++i) {
        buf_h[i] = rank + i;
        recvbuf_h[i] = 0xdeadbeef;
    }
    MTestCopyContent(buf_h, buf, COUNT * sizeof(int), memtype);
    MTestCopyContent(recvbuf_h, recvbuf, COUNT * sizeof(int), memtype);
    MPI_Iallreduce(buf, recvbuf, COUNT, MPI_INT, MPI_SUM, MPI_COMM_WORLD, &req);
    MPI_Wait(&req, MPI_STATUS_IGNORE);
    MTestCopyContent(recvbuf, recvbuf_h, COUNT * sizeof(int), memtype);
    for (i = 0; i < COUNT; ++i) {
        if (recvbuf_h[i] != ((size * (size - 1) / 2) + (i * size)))
            printf("got recvbuf_h[%d]=%d, expected %d\n", i, recvbuf_h[i],
                   ((size * (size - 1) / 2) + (i * size)));
        my_assert(recvbuf_h[i] == ((size * (size - 1) / 2) + (i * size)));
    }

    /* MPI_Reduce_scatter */
    for (i = 0; i < size; ++i) {
        recvcounts[i] = COUNT;
        for (j = 0; j < COUNT; ++j) {
            buf_h[i * COUNT + j] = rank + i;
            recvbuf_h[i * COUNT + j] = 0xdeadbeef;
        }
    }
    MTestCopyContent(buf_h, buf, size * COUNT * sizeof(int), memtype);
    MTestCopyContent(recvbuf_h, recvbuf, COUNT * sizeof(int), memtype);
    MPI_Reduce_scatter(buf, recvbuf, recvcounts, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
    MTestCopyContent(recvbuf, recvbuf_h, COUNT * sizeof(int), memtype);
    for (j = 0; j < COUNT; ++j) {
        my_assert(recvbuf_h[j] == (size * rank + ((size - 1) * size) / 2));
    }
    for (i = 1; i < size; ++i) {
        for (j = 0; j < COUNT; ++j) {
            /* check we didn't corrupt the rest of the recvbuf_h */
            my_assert(recvbuf_h[i * COUNT + j] == 0xdeadbeef);
        }
    }

    /* MPI_Ireduce_scatter */
    for (i = 0; i < size; ++i) {
        recvcounts[i] = COUNT;
        for (j = 0; j < COUNT; ++j) {
            buf_h[i * COUNT + j] = rank + i;
            recvbuf_h[i * COUNT + j] = 0xdeadbeef;
        }
    }
    MTestCopyContent(buf_h, buf, size * COUNT * sizeof(int), memtype);
    MTestCopyContent(recvbuf_h, recvbuf, COUNT * sizeof(int), memtype);
    MPI_Ireduce_scatter(buf, recvbuf, recvcounts, MPI_INT, MPI_SUM, MPI_COMM_WORLD, &req);
    MPI_Wait(&req, MPI_STATUS_IGNORE);
    MTestCopyContent(recvbuf, recvbuf_h, COUNT * sizeof(int), memtype);
    for (j = 0; j < COUNT; ++j) {
        my_assert(recvbuf_h[j] == (size * rank + ((size - 1) * size) / 2));
    }
    for (i = 1; i < size; ++i) {
        for (j = 0; j < COUNT; ++j) {
            /* check we didn't corrupt the rest of the recvbuf_h */
            my_assert(recvbuf_h[i * COUNT + j] == 0xdeadbeef);
        }
    }

    /* MPI_Reduce_scatter_block */
    for (i = 0; i < size; ++i) {
        for (j = 0; j < COUNT; ++j) {
            buf_h[i * COUNT + j] = rank + i;
            recvbuf_h[i * COUNT + j] = 0xdeadbeef;
        }
    }
    MTestCopyContent(buf_h, buf, size * COUNT * sizeof(int), memtype);
    MTestCopyContent(recvbuf_h, recvbuf, COUNT * sizeof(int), memtype);
    MPI_Reduce_scatter_block(buf, recvbuf, COUNT, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
    MTestCopyContent(recvbuf, recvbuf_h, COUNT * sizeof(int), memtype);
    for (j = 0; j < COUNT; ++j) {
        my_assert(recvbuf_h[j] == (size * rank + ((size - 1) * size) / 2));
    }
    for (i = 1; i < size; ++i) {
        for (j = 0; j < COUNT; ++j) {
            /* check we didn't corrupt the rest of the recvbuf_h */
            my_assert(recvbuf_h[i * COUNT + j] == 0xdeadbeef);
        }
    }

    /* MPI_Ireduce_scatter_block */
    for (i = 0; i < size; ++i) {
        for (j = 0; j < COUNT; ++j) {
            buf_h[i * COUNT + j] = rank + i;
            recvbuf_h[i * COUNT + j] = 0xdeadbeef;
        }
    }
    MTestCopyContent(buf_h, buf, size * COUNT * sizeof(int), memtype);
    MTestCopyContent(recvbuf_h, recvbuf, COUNT * sizeof(int), memtype);
    MPI_Ireduce_scatter_block(buf, recvbuf, COUNT, MPI_INT, MPI_SUM, MPI_COMM_WORLD, &req);
    MPI_Wait(&req, MPI_STATUS_IGNORE);
    MTestCopyContent(recvbuf, recvbuf_h, COUNT * sizeof(int), memtype);
    for (j = 0; j < COUNT; ++j) {
        my_assert(recvbuf_h[j] == (size * rank + ((size - 1) * size) / 2));
    }
    for (i = 1; i < size; ++i) {
        for (j = 0; j < COUNT; ++j) {
            /* check we didn't corrupt the rest of the recvbuf_h */
            my_assert(recvbuf_h[i * COUNT + j] == 0xdeadbeef);
        }
    }

    /* MPI_Scan */
    for (i = 0; i < COUNT; ++i) {
        buf_h[i] = rank + i;
        recvbuf_h[i] = 0xdeadbeef;
    }
    MTestCopyContent(buf_h, buf, COUNT * sizeof(int), memtype);
    MTestCopyContent(recvbuf_h, recvbuf, COUNT * sizeof(int), memtype);
    MPI_Scan(buf, recvbuf, COUNT, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
    MTestCopyContent(recvbuf, recvbuf_h, COUNT * sizeof(int), memtype);
    for (i = 0; i < COUNT; ++i) {
        my_assert(recvbuf_h[i] == ((rank * (rank + 1) / 2) + (i * (rank + 1))));
    }

    /* MPI_Iscan */
    for (i = 0; i < COUNT; ++i) {
        buf_h[i] = rank + i;
        recvbuf_h[i] = 0xdeadbeef;
    }
    MTestCopyContent(buf_h, buf, COUNT * sizeof(int), memtype);
    MTestCopyContent(recvbuf_h, recvbuf, COUNT * sizeof(int), memtype);
    MPI_Iscan(buf, recvbuf, COUNT, MPI_INT, MPI_SUM, MPI_COMM_WORLD, &req);
    MPI_Wait(&req, MPI_STATUS_IGNORE);
    MTestCopyContent(recvbuf, recvbuf_h, COUNT * sizeof(int), memtype);
    for (i = 0; i < COUNT; ++i) {
        my_assert(recvbuf_h[i] == ((rank * (rank + 1) / 2) + (i * (rank + 1))));
    }

    /* MPI_Exscan */
    for (i = 0; i < COUNT; ++i) {
        buf_h[i] = rank + i;
        recvbuf_h[i] = 0xdeadbeef;
    }
    MTestCopyContent(buf_h, buf, COUNT * sizeof(int), memtype);
    MTestCopyContent(recvbuf_h, recvbuf, COUNT * sizeof(int), memtype);
    MPI_Exscan(buf, recvbuf, COUNT, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
    MTestCopyContent(recvbuf, recvbuf_h, COUNT * sizeof(int), memtype);
    for (i = 0; i < COUNT; ++i) {
        if (rank != 0)
            my_assert(recvbuf_h[i] == ((rank * (rank + 1) / 2) + (i * (rank + 1)) - (rank + i)));
    }

    /* MPI_Iexscan */
    for (i = 0; i < COUNT; ++i) {
        buf_h[i] = rank + i;
        recvbuf_h[i] = 0xdeadbeef;
    }
    MTestCopyContent(buf_h, buf, COUNT * sizeof(int), memtype);
    MTestCopyContent(recvbuf_h, recvbuf, COUNT * sizeof(int), memtype);
    MPI_Iexscan(buf, recvbuf, COUNT, MPI_INT, MPI_SUM, MPI_COMM_WORLD, &req);
    MPI_Wait(&req, MPI_STATUS_IGNORE);
    MTestCopyContent(recvbuf, recvbuf_h, COUNT * sizeof(int), memtype);
    for (i = 0; i < COUNT; ++i) {
        if (rank != 0)
            my_assert(recvbuf_h[i] == ((rank * (rank + 1) / 2) + (i * (rank + 1)) - (rank + i)));
    }
}