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

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

/*
static char MTEST_Descrip[] = "Test MPI_Reduce with non-commutative user-define operations";
*/
/*
 * This tests that the reduce operation respects the noncommutative flag.
 * See red4.c for a version that can distinguish between P_{root} P_{root+1}
 * ... P_{root-1} and P_0 ... P_{size-1} .  The MPI standard clearly
 * specifies that the result is P_0 ... P_{size-1}, independent of the root
 * (see 4.9.4 in MPI-1)
 */

/* This implements a simple matrix-matrix multiply.  This is an associative
   but not commutative operation.  The matrix size is set in matSize;
   the number of matrices is the count argument. The matrix is stored
   in C order, so that
     c(i,j) is cin[j+i*matSize]
 */
#define MAXCOL 256
static int matSize = 0;         /* Must be < MAXCOL */
void uop(void *cinPtr, void *coutPtr, int *count, MPI_Datatype * dtype);
void uop(void *cinPtr, void *coutPtr, int *count, MPI_Datatype * dtype)
{
    const int *cin = (const int *) cinPtr;
    int *cout = (int *) coutPtr;
    int i, j, k, nmat;
    int tempCol[MAXCOL];

    for (nmat = 0; nmat < *count; nmat++) {
        for (j = 0; j < matSize; j++) {
            for (i = 0; i < matSize; i++) {
                tempCol[i] = 0;
                for (k = 0; k < matSize; k++) {
                    /* col[i] += cin(i,k) * cout(k,j) */
                    tempCol[i] += cin[k + i * matSize] * cout[j + k * matSize];
                }
            }
            for (i = 0; i < matSize; i++) {
                cout[j + i * matSize] = tempCol[i];
            }
        }
    }
}

/* Initialize the integer matrix as a permutation of rank with rank+1.
   If we call this matrix P_r, we know that product of P_0 P_1 ... P_{size-2}
   is a left shift by 1.
*/

static void initMat(MPI_Comm comm, int mat[])
{
    int i, size, rank;

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

    for (i = 0; i < size * size; i++)
        mat[i] = 0;

    /* For each row */
    for (i = 0; i < size; i++) {
        if (rank != size - 1) {
            if (i == rank)
                mat[((i + 1) % size) + i * size] = 1;
            else if (i == ((rank + 1) % size))
                mat[((i + size - 1) % size) + i * size] = 1;
            else
                mat[i + i * size] = 1;
        } else {
            mat[i + i * size] = 1;
        }
    }
}

#ifdef FOO
/* Compare a matrix with the identity matrix */
static int isIdentity(MPI_Comm comm, int mat[])
{
    int i, j, size, rank, errs = 0;

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

    for (i = 0; i < size; i++) {
        for (j = 0; j < size; j++) {
            if (i == j) {
                if (mat[j + i * size] != 1) {
                    errs++;
                }
            } else {
                if (mat[j + i * size] != 0) {
                    errs++;
                }
            }
        }
    }
    return errs;
}
#endif

/* Compare a matrix with the identity matrix */
static int isShiftLeft(MPI_Comm comm, int mat[])
{
    int i, j, size, rank, errs = 0;

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

    for (i = 0; i < size; i++) {
        for (j = 0; j < size; j++) {
            if (i == ((j + 1) % size)) {
                if (mat[j + i * size] != 1) {
                    errs++;
                }
            } else {
                if (mat[j + i * size] != 0) {
                    errs++;
                }
            }
        }
    }
    return errs;
}

int main(int argc, char *argv[])
{
    int errs = 0;
    int rank, size, root;
    int minsize = 2, count;
    MPI_Comm comm;
    int *buf, *bufout;
    MPI_Op op;
    MPI_Datatype mattype;

    MTest_Init(&argc, &argv);

    MPI_Op_create(uop, 0, &op);

    while (MTestGetIntracommGeneral(&comm, minsize, 1)) {
        if (comm == MPI_COMM_NULL)
            continue;

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

        matSize = size; /* used by the user-defined operation */
        /* Only one matrix for now */
        count = 1;

        /* A single matrix, the size of the communicator */
        MPI_Type_contiguous(size * size, MPI_INT, &mattype);
        MPI_Type_commit(&mattype);

        buf = (int *) malloc(count * size * size * sizeof(int));
        if (!buf)
            MPI_Abort(MPI_COMM_WORLD, 1);
        bufout = (int *) malloc(count * size * size * sizeof(int));
        if (!bufout)
            MPI_Abort(MPI_COMM_WORLD, 1);

        for (root = 0; root < size; root++) {
            initMat(comm, buf);
            MPI_Reduce(buf, bufout, count, mattype, op, root, comm);
            if (rank == root) {
                errs += isShiftLeft(comm, bufout);
            }

            /* Try the same test, but using MPI_IN_PLACE */
            initMat(comm, bufout);
            if (rank == root) {
                MPI_Reduce(MPI_IN_PLACE, bufout, count, mattype, op, root, comm);
            } else {
                MPI_Reduce(bufout, NULL, count, mattype, op, root, comm);
            }
            if (rank == root) {
                errs += isShiftLeft(comm, bufout);
            }
        }

        free(buf);
        free(bufout);

        MPI_Type_free(&mattype);

        MTestFreeComm(&comm);
    }

    MPI_Op_free(&op);

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