File: reduce.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 (129 lines) | stat: -rw-r--r-- 4,009 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
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

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

/*
static char MTEST_Descrip[] = "A simple test of Reduce with all choices of root process";
*/

static void set_send_buf(int count, int *buf)
{
    int i;
    for (i = 0; i < count; i++) {
        buf[i] = i;
    }
}

static void set_recv_buf(int count, int *buf)
{
    int i;
    for (i = 0; i < count; i++) {
        buf[i] = -1;
    }
}

static void check_buf(int rank, int size, int count, int *errs, int *buf)
{
    int i;
    for (i = 0; i < count; i++) {
        int result = i * size;
        if (buf[i] != result) {
            (*errs)++;
            if ((*errs) < 10) {
                fprintf(stderr, "buf[%d] = %d expected %d\n", i, buf[i], result);
            }
        }
    }
}

static int test_reduce(mtest_mem_type_e oddmem, mtest_mem_type_e evenmem)
{
    int errs = 0;
    int wrank, rank, size, root;
    void *sendbuf, *recvbuf, *sendbuf_h, *recvbuf_h;
    int minsize = 2, count;
    MPI_Comm comm;
    mtest_mem_type_e memtype;

    MPI_Comm_rank(MPI_COMM_WORLD, &wrank);
    if (wrank % 2 == 0)
        memtype = evenmem;
    else
        memtype = oddmem;

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

    while (MTestGetIntracommGeneral(&comm, minsize, 1)) {
        if (comm == MPI_COMM_NULL)
            continue;
        /* Determine the sender and receiver */
        MPI_Comm_rank(comm, &rank);
        MPI_Comm_size(comm, &size);

        for (count = 1; count < 130000; count = count * 2) {
            /* we use wrank here to avoid switching devices midway thru the test */
            MTestMalloc(count * sizeof(int), memtype, &recvbuf_h, &recvbuf, wrank);
            MTestMalloc(count * sizeof(int), memtype, &sendbuf_h, &sendbuf, wrank);

            for (root = 0; root < size; root++) {
                set_send_buf(count, sendbuf_h);
                set_recv_buf(count, recvbuf_h);
                MTestCopyContent(sendbuf_h, sendbuf, count * sizeof(int), memtype);
                MTestCopyContent(recvbuf_h, recvbuf, count * sizeof(int), memtype);

                MPI_Reduce(sendbuf, recvbuf, count, MPI_INT, MPI_SUM, root, comm);

                if (rank == root) {
                    MTestCopyContent(recvbuf, recvbuf_h, count * sizeof(int), memtype);
                    check_buf(rank, size, count, &errs, recvbuf_h);
                }

                /* test again using MPI_IN_PLACE */
                if (rank == root) {
                    set_send_buf(count, recvbuf_h);
                    MTestCopyContent(recvbuf_h, recvbuf, count * sizeof(int), memtype);
                    MPI_Reduce(MPI_IN_PLACE, recvbuf, count, MPI_INT, MPI_SUM, root, comm);
                } else {
                    set_send_buf(count, sendbuf_h);
                    MTestCopyContent(sendbuf_h, sendbuf, count * sizeof(int), memtype);
                    MPI_Reduce(sendbuf, NULL, count, MPI_INT, MPI_SUM, root, comm);
                }

                if (rank == root) {
                    MTestCopyContent(recvbuf, recvbuf_h, count * sizeof(int), memtype);
                    check_buf(rank, size, count, &errs, recvbuf_h);
                }
            }
            MTestFree(memtype, sendbuf_h, sendbuf);
            MTestFree(memtype, recvbuf_h, recvbuf);
        }
        MTestFreeComm(&comm);
    }
    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_NOCOUNT, argc, argv);
    while (dtp_args_get_next(&dtp_args)) {
        errs += test_reduce(dtp_args.u.coll.evenmem, dtp_args.u.coll.oddmem);
    }
    dtp_args_finalize(&dtp_args);

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