File: allred.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 (488 lines) | stat: -rw-r--r-- 20,829 bytes parent folder | download
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

/*      Warning - this test will fail for MPI_PROD & maybe MPI_SUM
 *        if more than 10 MPI processes are used.  Loss of precision
 *        will occur as the number of processors is increased.
 */

#include "mpitest.h"
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif

#include "mtest_dtp.h"

struct int_test {
    int a;
    int b;
};
struct long_test {
    long a;
    int b;
};
struct short_test {
    short a;
    int b;
};
struct float_test {
    float a;
    int b;
};
struct double_test {
    double a;
    int b;
};

#define mpi_op2str(op)                          \
    ((op == MPI_SUM) ? "MPI_SUM" :              \
     (op == MPI_PROD) ? "MPI_PROD" :            \
     (op == MPI_MAX) ? "MPI_MAX" :              \
     (op == MPI_MIN) ? "MPI_MIN" :              \
     (op == MPI_LOR) ? "MPI_LOR" :              \
     (op == MPI_LXOR) ? "MPI_LXOR" :            \
     (op == MPI_LAND) ? "MPI_LAND" :            \
     (op == MPI_BOR) ? "MPI_BOR" :              \
     (op == MPI_BAND) ? "MPI_BAND" :            \
     (op == MPI_BXOR) ? "MPI_BXOR" :            \
     (op == MPI_MAXLOC) ? "MPI_MAXLOC" :        \
     (op == MPI_MINLOC) ? "MPI_MINLOC" :        \
     "MPI_NO_OP")

/* calloc to avoid spurious valgrind warnings when "type" has padding bytes */
#define DECL_MALLOC_IN_OUT_SOL(type)            \
    type *in, *out, *sol; /*allocated on host*/ \
    void *in_d, *out_d; /*allocated on device*/ \
    MTestCalloc(count * sizeof(type), memtype, ((void **)&in), &in_d, rank);  \
    MTestCalloc(count * sizeof(type), memtype, ((void **)&out), &out_d, rank);\
    sol = (type *) calloc(count, sizeof(type));

#define SET_INDEX_CONST(arr, val)               \
    {                                           \
        int i;                                  \
        for (i = 0; i < count; i++)             \
            arr[i] = val;                       \
    }

#define SET_INDEX_SUM(arr, val)                 \
    {                                           \
        int i;                                  \
        for (i = 0; i < count; i++)             \
            arr[i] = i + val;                   \
    }

#define SET_INDEX_FACTOR(arr, val)              \
    {                                           \
        int i;                                  \
        for (i = 0; i < count; i++)             \
            arr[i] = i * (val);                 \
    }

#define SET_INDEX_POWER(arr, val)               \
    {                                           \
        int i, j;                               \
        for (i = 0; i < count; i++) {           \
            (arr)[i] = 1;                       \
            for (j = 0; j < (val); j++)         \
                arr[i] *= i;                    \
        }                                       \
    }

#define ERROR_CHECK_AND_FREE(lerrcnt, mpi_type, mpi_op)                 \
    do {                                                                \
        char name[MPI_MAX_OBJECT_NAME] = {0};                           \
        int len = 0;                                                    \
        if (lerrcnt) {                                                  \
            MPI_Type_get_name(mpi_type, name, &len);                    \
            fprintf(stderr, "(%d) Error for type %s and op %s\n",       \
                    rank, name, mpi_op2str(mpi_op));                    \
        }                                                               \
        MTestFree(memtype, in, in_d);                                   \
        MTestFree(memtype, out, out_d);                                 \
        free(sol);                                                      \
    } while (0)

/* The logic on the error check on MPI_Allreduce assumes that all
   MPI_Allreduce routines return a failure if any do - this is sufficient
   for MPI implementations that reject some of the valid op/datatype pairs
   (and motivated this addition, as some versions of the IBM MPI
   failed in just this way).
*/
#define ALLREDUCE_AND_FREE(type, mpi_type, mpi_op, in, out, sol)        \
    {                                                                   \
        int i, rc, lerrcnt = 0;						\
        MTestCopyContent(in, in_d, count * sizeof(type),  memtype);     \
        rc = MPI_Allreduce(in_d, out_d, count, mpi_type, mpi_op, MPI_COMM_WORLD); \
        MTestCopyContent(out_d, out, count * sizeof(type), memtype);    \
        if (rc) { lerrcnt++; errs++; MTestPrintError(rc); }             \
        else {                                                          \
            for (i = 0; i < count; i++) {                               \
                if (out[i] != sol[i]) {                                 \
                    errs++;                                             \
                    lerrcnt++;                                          \
                }                                                       \
            }                                                           \
        }                                                               \
        ERROR_CHECK_AND_FREE(lerrcnt, mpi_type, mpi_op);                \
    }

#define STRUCT_ALLREDUCE_AND_FREE(type, mpi_type, mpi_op, in, out, sol) \
    {                                                                   \
        int i, rc, lerrcnt = 0;						\
        MTestCopyContent(in, in_d, count * sizeof(type),  memtype);     \
        rc = MPI_Allreduce(in_d, out_d, count, mpi_type, mpi_op, MPI_COMM_WORLD); \
        MTestCopyContent(out_d, out, count * sizeof(type), memtype);    \
        if (rc) { lerrcnt++; errs++; MTestPrintError(rc); }             \
        else {                                                          \
            for (i = 0; i < count; i++) {                               \
                if ((out[i].a != sol[i].a) || (out[i].b != sol[i].b)) { \
                    errs++;                                             \
                    lerrcnt++;                                          \
                }                                                       \
            }                                                           \
        }                                                               \
        ERROR_CHECK_AND_FREE(lerrcnt, mpi_type, mpi_op);                \
    }

#define SET_INDEX_STRUCT_CONST(arr, val, el)    \
    {                                           \
        int i;                                  \
        for (i = 0; i < count; i++)             \
            arr[i].el = val;                    \
    }

#define SET_INDEX_STRUCT_SUM(arr, val, el)      \
    {                                           \
        int i;                                  \
        for (i = 0; i < count; i++)             \
            arr[i].el = i + (val);              \
    }

#define sum_test1(type, mpi_type)                               \
    {                                                           \
        DECL_MALLOC_IN_OUT_SOL(type);                           \
        SET_INDEX_SUM(in, 0);                                   \
        SET_INDEX_FACTOR(sol, size);                            \
        SET_INDEX_CONST(out, 0);                                \
        ALLREDUCE_AND_FREE(type, mpi_type, MPI_SUM, in, out, sol);    \
    }

#define prod_test1(type, mpi_type)                              \
    {                                                           \
        DECL_MALLOC_IN_OUT_SOL(type);                           \
        SET_INDEX_SUM(in, 0);                                   \
        SET_INDEX_POWER(sol, size);                             \
        SET_INDEX_CONST(out, 0);                                \
        ALLREDUCE_AND_FREE(type, mpi_type, MPI_PROD, in, out, sol);   \
    }

#define max_test1(type, mpi_type)                               \
    {                                                           \
        DECL_MALLOC_IN_OUT_SOL(type);                           \
        SET_INDEX_SUM(in, rank);                                \
        SET_INDEX_SUM(sol, size - 1);                           \
        SET_INDEX_CONST(out, 0);                                \
        ALLREDUCE_AND_FREE(type, mpi_type, MPI_MAX, in, out, sol);    \
    }

#define min_test1(type, mpi_type)                               \
    {                                                           \
        DECL_MALLOC_IN_OUT_SOL(type);                           \
        SET_INDEX_SUM(in, rank);                                \
        SET_INDEX_SUM(sol, 0);                                  \
        SET_INDEX_CONST(out, 0);                                \
        ALLREDUCE_AND_FREE(type, mpi_type, MPI_MIN, in, out, sol);    \
    }

#define const_test(type, mpi_type, mpi_op, val1, val2, val3)    \
    {                                                           \
        DECL_MALLOC_IN_OUT_SOL(type);                           \
        SET_INDEX_CONST(in, (val1));                            \
        SET_INDEX_CONST(sol, (val2));                           \
        SET_INDEX_CONST(out, (val3));                           \
        ALLREDUCE_AND_FREE(type, mpi_type, mpi_op, in, out, sol);     \
    }

#define lor_test1(type, mpi_type)                                       \
    const_test(type, mpi_type, MPI_LOR, (rank & 0x1), (size > 1), 0)
#define lor_test2(type, mpi_type)                       \
    const_test(type, mpi_type, MPI_LOR, 0, 0, 0)
#define lxor_test1(type, mpi_type)                                      \
    const_test(type, mpi_type, MPI_LXOR, (rank == 1), (size > 1), 0)
#define lxor_test2(type, mpi_type)                      \
    const_test(type, mpi_type, MPI_LXOR, 0, 0, 0)
#define lxor_test3(type, mpi_type)                              \
    const_test(type, mpi_type, MPI_LXOR, 1, (size & 0x1), 0)
#define land_test1(type, mpi_type)                              \
    const_test(type, mpi_type, MPI_LAND, (rank & 0x1), 0, 0)
#define land_test2(type, mpi_type)                      \
    const_test(type, mpi_type, MPI_LAND, 1, 1, 0)
#define bor_test1(type, mpi_type)                                       \
    const_test(type, mpi_type, MPI_BOR, (rank & 0x3), ((size < 3) ? size - 1 : 0x3), 0)
#define bxor_test1(type, mpi_type)                                      \
    const_test(type, mpi_type, MPI_BXOR, (rank == 1) * 0xf0, (size > 1) * 0xf0, 0)
#define bxor_test2(type, mpi_type)                      \
    const_test(type, mpi_type, MPI_BXOR, 0, 0, 0)
#define bxor_test3(type, mpi_type)                                      \
    const_test(type, mpi_type, MPI_BXOR, ~0, (size &0x1) ? ~0 : 0, 0)

#define band_test1(type, mpi_type)                              \
    {                                                           \
        DECL_MALLOC_IN_OUT_SOL(type);                           \
        if (rank == size-1) {                                   \
            SET_INDEX_SUM(in, 0);                               \
        }                                                       \
        else {                                                  \
            SET_INDEX_CONST(in, ~0);                            \
        }                                                       \
        SET_INDEX_SUM(sol, 0);                                  \
        SET_INDEX_CONST(out, 0);                                \
        ALLREDUCE_AND_FREE(type, mpi_type, MPI_BAND, in, out, sol);   \
    }

#define band_test2(type, mpi_type)                              \
    {                                                           \
        DECL_MALLOC_IN_OUT_SOL(type);                           \
        if (rank == size-1) {                                   \
            SET_INDEX_SUM(in, 0);                               \
        }                                                       \
        else {                                                  \
            SET_INDEX_CONST(in, 0);                             \
        }                                                       \
        SET_INDEX_CONST(sol, 0);                                \
        SET_INDEX_CONST(out, 0);                                \
        ALLREDUCE_AND_FREE(type, mpi_type, MPI_BAND, in, out, sol);   \
    }

#define maxloc_test(type, mpi_type)                                     \
    {                                                                   \
        DECL_MALLOC_IN_OUT_SOL(type);                                   \
        SET_INDEX_STRUCT_SUM(in, rank, a);                              \
        SET_INDEX_STRUCT_CONST(in, rank, b);                            \
        SET_INDEX_STRUCT_SUM(sol, size - 1, a);                         \
        SET_INDEX_STRUCT_CONST(sol, size - 1, b);                       \
        SET_INDEX_STRUCT_CONST(out, 0, a);                              \
        SET_INDEX_STRUCT_CONST(out, -1, b);                             \
        STRUCT_ALLREDUCE_AND_FREE(type, mpi_type, MPI_MAXLOC, in, out, sol);  \
    }

#define minloc_test(type, mpi_type)                                     \
    {                                                                   \
        DECL_MALLOC_IN_OUT_SOL(type);                                   \
        SET_INDEX_STRUCT_SUM(in, rank, a);                              \
        SET_INDEX_STRUCT_CONST(in, rank, b);                            \
        SET_INDEX_STRUCT_SUM(sol, 0, a);                                \
        SET_INDEX_STRUCT_CONST(sol, 0, b);                              \
        SET_INDEX_STRUCT_CONST(out, 0, a);                              \
        SET_INDEX_STRUCT_CONST(out, -1, b);                             \
        STRUCT_ALLREDUCE_AND_FREE(type, mpi_type, MPI_MINLOC, in, out, sol);  \
    }

#if MTEST_HAVE_MIN_MPI_VERSION(2,2)
#define test_types_set_mpi_2_2_integer(op,post) do {    \
        op##_test##post(int8_t, MPI_INT8_T);            \
        op##_test##post(int16_t, MPI_INT16_T);          \
        op##_test##post(int32_t, MPI_INT32_T);          \
        op##_test##post(int64_t, MPI_INT64_T);          \
        op##_test##post(uint8_t, MPI_UINT8_T);          \
        op##_test##post(uint16_t, MPI_UINT16_T);        \
        op##_test##post(uint32_t, MPI_UINT32_T);        \
        op##_test##post(uint64_t, MPI_UINT64_T);        \
        op##_test##post(MPI_Aint, MPI_AINT);            \
        op##_test##post(MPI_Offset, MPI_OFFSET);        \
    } while (0)
#else
#define test_types_set_mpi_2_2_integer(op,post) do { } while (0)
#endif

#if MTEST_HAVE_MIN_MPI_VERSION(3,0)
#define test_types_set_mpi_3_0_integer(op,post) do {    \
        op##_test##post(MPI_Count, MPI_COUNT);          \
    } while (0)
#else
#define test_types_set_mpi_3_0_integer(op,post) do { } while (0)
#endif

#define test_types_set1(op, post)                               \
    {                                                           \
        op##_test##post(int, MPI_INT);                          \
        op##_test##post(long, MPI_LONG);                        \
        op##_test##post(short, MPI_SHORT);                      \
        op##_test##post(unsigned short, MPI_UNSIGNED_SHORT);    \
        op##_test##post(unsigned, MPI_UNSIGNED);                \
        op##_test##post(unsigned long, MPI_UNSIGNED_LONG);      \
        op##_test##post(unsigned char, MPI_UNSIGNED_CHAR);      \
        test_types_set_mpi_2_2_integer(op,post);                \
        test_types_set_mpi_3_0_integer(op,post);                \
    }

#define test_types_set2(op, post)               \
    {                                           \
        test_types_set1(op, post);              \
        op##_test##post(float, MPI_FLOAT);      \
        op##_test##post(double, MPI_DOUBLE);    \
    }

#define test_types_set3(op, post)                       \
    {                                                   \
        op##_test##post(unsigned char, MPI_BYTE);       \
    }

/* Make sure that we test complex and double complex, even if long
   double complex is not available */
#if defined(USE_LONG_DOUBLE_COMPLEX)

#if MTEST_HAVE_MIN_MPI_VERSION(2,2) && defined(HAVE_FLOAT__COMPLEX)     \
    && defined(HAVE_DOUBLE__COMPLEX)                                    \
    && defined(HAVE_LONG_DOUBLE__COMPLEX)
#define test_types_set4(op, post)                                       \
    do {                                                                \
        op##_test##post(float _Complex, MPI_C_FLOAT_COMPLEX);           \
        op##_test##post(double _Complex, MPI_C_DOUBLE_COMPLEX);         \
        if (MPI_C_LONG_DOUBLE_COMPLEX != MPI_DATATYPE_NULL) {           \
            op##_test##post(long double _Complex, MPI_C_LONG_DOUBLE_COMPLEX); \
        }                                                               \
    } while (0)

#else
#define test_types_set4(op, post) do { } while (0)
#endif
#else

#if MTEST_HAVE_MIN_MPI_VERSION(2,2) && defined(HAVE_FLOAT__COMPLEX)     \
    && defined(HAVE_DOUBLE__COMPLEX)
#define test_types_set4(op, post)                               \
    do {                                                        \
        op##_test##post(float _Complex, MPI_C_FLOAT_COMPLEX);   \
        op##_test##post(double _Complex, MPI_C_DOUBLE_COMPLEX); \
    } while (0)

#else
#define test_types_set4(op, post) do { } while (0)
#endif

#endif /* defined(USE_LONG_DOUBLE_COMPLEX) */

#if MTEST_HAVE_MIN_MPI_VERSION(2,2) && defined(HAVE__BOOL)
#define test_types_set5(op, post)               \
    do {                                        \
        op##_test##post(_Bool, MPI_C_BOOL);     \
    } while (0)

#else
#define test_types_set5(op, post) do { } while (0)
#endif

static int test_allred(int count, mtest_mem_type_e evenmem, mtest_mem_type_e oddmem)
{
    int errs = 0;
    int size, rank;
    mtest_mem_type_e memtype;

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

    if (size < 2) {
        fprintf(stderr, "At least 2 processes required\n");
        MPI_Abort(MPI_COMM_WORLD, 1);
    }

    /* Set errors return so that we can provide better information
     * should a routine reject one of the operand/datatype pairs */
    MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);

    if (count <= 0) {
        fprintf(stderr, "Invalid count argument %d\n", count);
        MPI_Abort(MPI_COMM_WORLD, 1);
    }

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

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

    test_types_set2(sum, 1);
    test_types_set2(prod, 1);
    test_types_set2(max, 1);
    test_types_set2(min, 1);

    test_types_set1(lor, 1);
    test_types_set1(lor, 2);

    test_types_set1(lxor, 1);
    test_types_set1(lxor, 2);
    test_types_set1(lxor, 3);

    test_types_set1(land, 1);
    test_types_set1(land, 2);

    test_types_set1(bor, 1);
    test_types_set1(band, 1);
    test_types_set1(band, 2);

    test_types_set1(bxor, 1);
    test_types_set1(bxor, 2);
    test_types_set1(bxor, 3);

    test_types_set3(bor, 1);
    test_types_set3(band, 1);
    test_types_set3(band, 2);

    test_types_set3(bxor, 1);
    test_types_set3(bxor, 2);
    test_types_set3(bxor, 3);

    test_types_set4(sum, 1);
    test_types_set4(prod, 1);

    test_types_set5(lor, 1);
    test_types_set5(lor, 2);
    test_types_set5(lxor, 1);
    test_types_set5(lxor, 2);
    test_types_set5(lxor, 3);
    test_types_set5(land, 1);
    test_types_set5(land, 2);

    maxloc_test(struct int_test, MPI_2INT);
    maxloc_test(struct long_test, MPI_LONG_INT);
    maxloc_test(struct short_test, MPI_SHORT_INT);
    maxloc_test(struct float_test, MPI_FLOAT_INT);
    maxloc_test(struct double_test, MPI_DOUBLE_INT);

    minloc_test(struct int_test, MPI_2INT);
    minloc_test(struct long_test, MPI_LONG_INT);
    minloc_test(struct short_test, MPI_SHORT_INT);
    minloc_test(struct float_test, MPI_FLOAT_INT);
    minloc_test(struct double_test, MPI_DOUBLE_INT);

    MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_ARE_FATAL);
    return errs;
}

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_COUNT, argc, argv);
    while (dtp_args_get_next(&dtp_args)) {
        errs += test_allred(dtp_args.count, dtp_args.u.coll.evenmem, dtp_args.u.coll.oddmem);
    }
    dtp_args_finalize(&dtp_args);

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