File: red4.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 (252 lines) | stat: -rw-r--r-- 7,065 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
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
/*
 * 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 and arbitrary root";
*/

/*
 * This tests that the reduce operation respects the noncommutative flag.
 * and 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;
    int *cout;
    int i, j, k, nmat;
    int tempCol[MAXCOL];

    if (*count != 1)
        printf("Panic!\n");
    for (nmat = 0; nmat < *count; nmat++) {
        cin = (const int *) cinPtr;
        cout = (int *) coutPtr;
        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];
            }
        }
        cinPtr = (int *) cinPtr + matSize * matSize;
        coutPtr = (int *) coutPtr + matSize * matSize;
    }
}

/* 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-1}
   is the matrix with rows ordered as
   1,size,2,3,4,...,size-1
   (The matrix is basically a circular shift right,
   shifting right n-1 steps for an n x n dimensional matrix, with the last
   step swapping rows 1 and size)
*/

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

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

    /* Remember the matrix size */
    matSize = size;

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

    for (i = 0; i < matSize; i++) {
        if (i == rank)
            mat[((i + 1) % matSize) + i * matSize] = 1;
        else if (i == ((rank + 1) % matSize))
            mat[((i + matSize - 1) % matSize) + i * matSize] = 1;
        else
            mat[i + i * matSize] = 1;
    }
}

/* 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 (j == i) {
		if (mat[j+i*size] != 1) {
		    printf("mat(%d,%d) = %d, should = 1\n",
			    i, j, mat[j+i*size]);
		    errs++;
		}
	    }
	    else {
		if (mat[j+i*size] != 0) {
		    printf("mat(%d,%d) = %d, should = 0\n",
			    i, j, mat[j+i*size]);
		    errs++;
		}
	    }
	}
    }
    return errs;
}
*/

/* Compare a matrix with the identity matrix with rows permuted to as rows
   1,size,2,3,4,5,...,size-1 */
static int isPermutedIdentity(MPI_Comm comm, int mat[])
{
    int i, j, size, rank, errs = 0;

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

    /* Check the first two last rows */
    i = 0;
    for (j = 0; j < size; j++) {
        if (j == 0) {
            if (mat[j] != 1) {
                printf("mat(%d,%d) = %d, should = 1\n", i, j, mat[j]);
                errs++;
            }
        } else {
            if (mat[j] != 0) {
                printf("mat(%d,%d) = %d, should = 0\n", i, j, mat[j]);
                errs++;
            }
        }
    }
    i = 1;
    for (j = 0; j < size; j++) {
        if (j == size - 1) {
            if (mat[j + i * size] != 1) {
                printf("mat(%d,%d) = %d, should = 1\n", i, j, mat[j + i * size]);
                errs++;
            }
        } else {
            if (mat[j + i * size] != 0) {
                printf("mat(%d,%d) = %d, should = 0\n", i, j, mat[j + i * size]);
                errs++;
            }
        }
    }
    /* The remaint rows are shifted down by one */
    for (i = 2; i < size; i++) {
        for (j = 0; j < size; j++) {
            if (j == i - 1) {
                if (mat[j + i * size] != 1) {
                    printf("mat(%d,%d) = %d, should = 1\n", i, j, mat[j + i * size]);
                    errs++;
                }
            } else {
                if (mat[j + i * size] != 0) {
                    printf("mat(%d,%d) = %d, should = 0\n", i, j, mat[j + i * size]);
                    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);

        if (size > MAXCOL) {
            /* Skip because there are too many processes */
            MTestFreeComm(&comm);
            continue;
        }

        /* 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 += isPermutedIdentity(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 += isPermutedIdentity(comm, bufout);
            }
        }
        MPI_Type_free(&mattype);

        free(buf);
        free(bufout);

        MTestFreeComm(&comm);
    }

    MPI_Op_free(&op);

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