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

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

/*
  This program tests MPI_Alltoallw by having processor i send different
  amounts of data to each processor.  This is just the MPI_Alltoallv test,
  but with displacements in bytes rather than units of the datatype.

  Because there are separate send and receive types to alltoallw,
  there need to be tests to rearrange data on the fly.  Not done yet.

  The first test sends i items to processor i from all processors.

  Currently, the test uses only MPI_INT; this is adequate for testing systems
  that use point-to-point operations
 */

static int test_noinplace(MPI_Comm comm);
static int test_inplace(MPI_Comm comm, bool sametype);

int main(int argc, char **argv)
{
    int errs = 0;
    MPI_Comm comm;

    MTest_Init(&argc, &argv);
    while (MTestGetIntracommGeneral(&comm, 2, 1)) {
        if (comm == MPI_COMM_NULL)
            continue;

        errs += test_noinplace(comm);

#if MTEST_HAVE_MIN_MPI_VERSION(2,2)
        errs += test_inplace(comm, true);
        errs += test_inplace(comm, false);
#endif
        MTestFreeComm(&comm);
    }

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

static int test_noinplace(MPI_Comm comm)
{
    int errs = 0;
    int *sbuf, *rbuf;
    int *sendcounts, *recvcounts, *rdispls, *sdispls;
    MPI_Datatype *sendtypes, *recvtypes;

    int rank, size;
    MPI_Comm_size(comm, &size);
    MPI_Comm_rank(comm, &rank);

    /* Create the buffer */
    sbuf = (int *) malloc(size * size * sizeof(int));
    rbuf = (int *) malloc(size * size * sizeof(int));
    if (!sbuf || !rbuf) {
        fprintf(stderr, "Could not allocated buffers!\n");
        MPI_Abort(comm, 1);
    }

    /* Load up the buffers */
    for (int i = 0; i < size * size; i++) {
        sbuf[i] = i + 100 * rank;
        rbuf[i] = -i;
    }

    /* Create and load the arguments to alltoallv */
    sendcounts = (int *) malloc(size * sizeof(int));
    recvcounts = (int *) malloc(size * sizeof(int));
    rdispls = (int *) malloc(size * sizeof(int));
    sdispls = (int *) malloc(size * sizeof(int));
    sendtypes = (MPI_Datatype *) malloc(size * sizeof(MPI_Datatype));
    recvtypes = (MPI_Datatype *) malloc(size * sizeof(MPI_Datatype));
    if (!sendcounts || !recvcounts || !rdispls || !sdispls || !sendtypes || !recvtypes) {
        fprintf(stderr, "Could not allocate arg items!\n");
        MPI_Abort(comm, 1);
    }
    /* Note that process 0 sends no data (sendcounts[0] = 0) */
    for (int i = 0; i < size; i++) {
        sendcounts[i] = i;
        recvcounts[i] = rank;
        rdispls[i] = i * rank * sizeof(int);
        sdispls[i] = (((i + 1) * (i)) / 2) * sizeof(int);
        sendtypes[i] = recvtypes[i] = MPI_INT;
    }
    MPI_Alltoallw(sbuf, sendcounts, sdispls, sendtypes, rbuf, recvcounts, rdispls, recvtypes, comm);

    /* Check rbuf */
    for (int i = 0; i < size; i++) {
        int *p = rbuf + rdispls[i] / sizeof(int);
        for (int j = 0; j < rank; j++) {
            if (p[j] != i * 100 + (rank * (rank + 1)) / 2 + j) {
                fprintf(stderr, "[%d] got %d expected %d for %dth\n",
                        rank, p[j], (i * (i + 1)) / 2 + j, j);
                errs++;
            }
        }
    }

    free(sendtypes);
    free(sdispls);
    free(sendcounts);
    free(sbuf);
    free(recvtypes);
    free(rdispls);
    free(recvcounts);
    free(rbuf);

    return errs;
}

#if MTEST_HAVE_MIN_MPI_VERSION(2,2)
static int test_inplace(MPI_Comm comm, bool sametype)
{
    int errs = 0;
    int *rbuf;
    int *recvcounts, *rdispls;
    MPI_Datatype *recvtypes;

    int rank, size;
    MPI_Comm_size(comm, &size);
    MPI_Comm_rank(comm, &rank);

    /* Create the buffer */
    rbuf = (int *) malloc(size * (2 * size) * sizeof(int));
    if (!rbuf) {
        fprintf(stderr, "Could not reallocate rbuf!\n");
        MPI_Abort(comm, 1);
    }

    /* Load up the buffers */
    memset(rbuf, -1, size * (2 * size) * sizeof(int));
    for (int i = 0; i < size; i++) {
        int *p = rbuf + i * (2 * size);
        for (int j = 0; j < i + rank; ++j) {
            p[j] = 100 * rank + 10 * i + j;
        }
    }

    /* Create and load the arguments to alltoallv */
    recvcounts = (int *) malloc(size * sizeof(int));
    rdispls = (int *) malloc(size * sizeof(int));
    recvtypes = (MPI_Datatype *) malloc(size * sizeof(MPI_Datatype));
    if (!recvcounts || !rdispls || !recvtypes) {
        fprintf(stderr, "Could not allocate arg items!\n");
        MPI_Abort(comm, 1);
    }

    for (int i = 0; i < size; i++) {
        /* alltoallw displs are in bytes, not in type extents */
        rdispls[i] = i * (2 * size) * sizeof(int);
        if (sametype) {
            recvtypes[i] = MPI_INT;
            recvcounts[i] = i + rank;
        } else {
            MPI_Type_contiguous(i + rank, MPI_INT, &recvtypes[i]);
            MPI_Type_commit(&recvtypes[i]);
            recvcounts[i] = 1;
        }
    }

    MPI_Alltoallw(MPI_IN_PLACE, NULL, NULL, NULL, rbuf, recvcounts, rdispls, recvtypes, comm);

    if (!sametype) {
        for (int i = 0; i < size; i++) {
            MPI_Type_free(&recvtypes[i]);
        }
    }

    /* Check rbuf */
    for (int i = 0; i < size; i++) {
        int *p = rbuf + (rdispls[i] / sizeof(int));
        for (int j = 0; j < i + rank; j++) {
            int expected = 100 * i + 10 * rank + j;
            if (p[j] != expected) {
                fprintf(stderr, "[%d] got %d expected %d for block=%d, element=%dth\n",
                        rank, p[j], expected, i, j);
                ++errs;
            }
        }
    }

    free(recvtypes);
    free(rdispls);
    free(recvcounts);
    free(rbuf);

    return errs;
}
#endif