1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
4 * (C) 2003 by Argonne National Laboratory.
5 * See COPYRIGHT in top-level directory.
13 static char MTEST_Descrip[] = "Test MPI_Reduce with non-commutative user-define operations";
16 * This tests that the reduce operation respects the noncommutative flag.
17 * See red4.c for a version that can distinguish between P_{root} P_{root+1}
18 * ... P_{root-1} and P_0 ... P_{size-1} . The MPI standard clearly
19 * specifies that the result is P_0 ... P_{size-1}, independent of the root
20 * (see 4.9.4 in MPI-1)
23 /* This implements a simple matrix-matrix multiply. This is an associative
24 but not commutative operation. The matrix size is set in matSize;
25 the number of matrices is the count argument. The matrix is stored
27 c(i,j) is cin[j+i*matSize]
30 static int matSize = 0; /* Must be < MAXCOL */
31 void uop(void *cinPtr, void *coutPtr, int *count, MPI_Datatype * dtype);
32 void uop(void *cinPtr, void *coutPtr, int *count, MPI_Datatype * dtype)
34 const int *cin = (const int *) cinPtr;
35 int *cout = (int *) coutPtr;
39 for (nmat = 0; nmat < *count; nmat++) {
40 for (j = 0; j < matSize; j++) {
41 for (i = 0; i < matSize; i++) {
43 for (k = 0; k < matSize; k++) {
44 /* col[i] += cin(i,k) * cout(k,j) */
45 tempCol[i] += cin[k + i * matSize] * cout[j + k * matSize];
48 for (i = 0; i < matSize; i++) {
49 cout[j + i * matSize] = tempCol[i];
55 /* Initialize the integer matrix as a permutation of rank with rank+1.
56 If we call this matrix P_r, we know that product of P_0 P_1 ... P_{size-2}
60 static void initMat(MPI_Comm comm, int mat[])
64 MPI_Comm_rank(comm, &rank);
65 MPI_Comm_size(comm, &size);
67 for (i = 0; i < size * size; i++)
71 for (i = 0; i < size; i++) {
72 if (rank != size - 1) {
74 mat[((i + 1) % size) + i * size] = 1;
75 else if (i == ((rank + 1) % size))
76 mat[((i + size - 1) % size) + i * size] = 1;
78 mat[i + i * size] = 1;
81 mat[i + i * size] = 1;
87 /* Compare a matrix with the identity matrix */
88 static int isIdentity(MPI_Comm comm, int mat[])
90 int i, j, size, rank, errs = 0;
92 MPI_Comm_rank(comm, &rank);
93 MPI_Comm_size(comm, &size);
95 for (i = 0; i < size; i++) {
96 for (j = 0; j < size; j++) {
98 if (mat[j + i * size] != 1) {
103 if (mat[j + i * size] != 0) {
113 /* Compare a matrix with the identity matrix */
114 static int isShiftLeft(MPI_Comm comm, int mat[])
116 int i, j, size, rank, errs = 0;
118 MPI_Comm_rank(comm, &rank);
119 MPI_Comm_size(comm, &size);
121 for (i = 0; i < size; i++) {
122 for (j = 0; j < size; j++) {
123 if (i == ((j + 1) % size)) {
124 if (mat[j + i * size] != 1) {
129 if (mat[j + i * size] != 0) {
138 int main(int argc, char *argv[])
141 int rank, size, root;
142 int minsize = 2, count;
146 MPI_Datatype mattype;
148 MTest_Init(&argc, &argv);
150 MPI_Op_create(uop, 0, &op);
152 while (MTestGetIntracommGeneral(&comm, minsize, 1)) {
153 if (comm == MPI_COMM_NULL)
156 MPI_Comm_size(comm, &size);
157 MPI_Comm_rank(comm, &rank);
159 matSize = size; /* used by the user-defined operation */
160 /* Only one matrix for now */
163 /* A single matrix, the size of the communicator */
164 MPI_Type_contiguous(size * size, MPI_INT, &mattype);
165 MPI_Type_commit(&mattype);
167 buf = (int *) malloc(count * size * size * sizeof(int));
169 MPI_Abort(MPI_COMM_WORLD, 1);
170 bufout = (int *) malloc(count * size * size * sizeof(int));
172 MPI_Abort(MPI_COMM_WORLD, 1);
174 for (root = 0; root < size; root++) {
176 MPI_Reduce(buf, bufout, count, mattype, op, root, comm);
178 errs += isShiftLeft(comm, bufout);
181 /* Try the same test, but using MPI_IN_PLACE */
182 initMat(comm, bufout);
184 MPI_Reduce(MPI_IN_PLACE, bufout, count, mattype, op, root, comm);
187 MPI_Reduce(bufout, NULL, count, mattype, op, root, comm);
190 errs += isShiftLeft(comm, bufout);
197 MPI_Type_free(&mattype);
199 MTestFreeComm(&comm);
204 MTest_Finalize(errs);