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 and arbitrary root";
17 * This tests that the reduce operation respects the noncommutative flag.
18 * and that can distinguish between P_{root} P_{root+1}
19 * ... P_{root-1} and P_0 ... P_{size-1} . The MPI standard clearly
20 * specifies that the result is P_0 ... P_{size-1}, independent of the root
21 * (see 4.9.4 in MPI-1)
24 /* This implements a simple matrix-matrix multiply. This is an associative
25 but not commutative operation. The matrix size is set in matSize;
26 the number of matrices is the count argument. The matrix is stored
28 c(i,j) is cin[j+i*matSize]
31 static int matSize = 0; /* Must be < MAXCOL */
33 void uop(void *cinPtr, void *coutPtr, int *count, MPI_Datatype * dtype);
34 void uop(void *cinPtr, void *coutPtr, int *count, MPI_Datatype * dtype)
43 for (nmat = 0; nmat < *count; nmat++) {
44 cin = (const int *) cinPtr;
45 cout = (int *) coutPtr;
46 for (j = 0; j < matSize; j++) {
47 for (i = 0; i < matSize; i++) {
49 for (k = 0; k < matSize; k++) {
50 /* col[i] += cin(i,k) * cout(k,j) */
51 tempCol[i] += cin[k + i * matSize] * cout[j + k * matSize];
54 for (i = 0; i < matSize; i++) {
55 cout[j + i * matSize] = tempCol[i];
58 cinPtr = (int *) cinPtr + matSize * matSize;
59 coutPtr = (int *) coutPtr + matSize * matSize;
63 /* Initialize the integer matrix as a permutation of rank with rank+1.
64 If we call this matrix P_r, we know that product of P_0 P_1 ... P_{size-1}
65 is the matrix with rows ordered as
66 1,size,2,3,4,...,size-1
67 (The matrix is basically a circular shift right,
68 shifting right n-1 steps for an n x n dimensional matrix, with the last
69 step swapping rows 1 and size)
72 static void initMat(MPI_Comm comm, int mat[])
76 MPI_Comm_rank(comm, &rank);
77 MPI_Comm_size(comm, &size);
79 /* Remember the matrix size */
82 for (i = 0; i < matSize * matSize; i++)
85 for (i = 0; i < matSize; i++) {
87 mat[((i + 1) % matSize) + i * matSize] = 1;
88 else if (i == ((rank + 1) % matSize))
89 mat[((i + matSize - 1) % matSize) + i * matSize] = 1;
91 mat[i + i * matSize] = 1;
95 /* Compare a matrix with the identity matrix */
97 static int isIdentity(MPI_Comm comm, int mat[])
99 int i, j, size, rank, errs = 0;
101 MPI_Comm_rank(comm, &rank);
102 MPI_Comm_size(comm, &size);
104 for (i=0; i<size; i++) {
105 for (j=0; j<size; j++) {
107 if (mat[j+i*size] != 1) {
108 printf("mat(%d,%d) = %d, should = 1\n",
109 i, j, mat[j+i*size]);
114 if (mat[j+i*size] != 0) {
115 printf("mat(%d,%d) = %d, should = 0\n",
116 i, j, mat[j+i*size]);
126 /* Compare a matrix with the identity matrix with rows permuted to as rows
127 1,size,2,3,4,5,...,size-1 */
128 static int isPermutedIdentity(MPI_Comm comm, int mat[])
130 int i, j, size, rank, errs = 0;
132 MPI_Comm_rank(comm, &rank);
133 MPI_Comm_size(comm, &size);
135 /* Check the first two last rows */
137 for (j = 0; j < size; j++) {
140 printf("mat(%d,%d) = %d, should = 1\n", i, j, mat[j]);
146 printf("mat(%d,%d) = %d, should = 0\n", i, j, mat[j]);
152 for (j = 0; j < size; j++) {
154 if (mat[j + i * size] != 1) {
155 printf("mat(%d,%d) = %d, should = 1\n", i, j, mat[j + i * size]);
160 if (mat[j + i * size] != 0) {
161 printf("mat(%d,%d) = %d, should = 0\n", i, j, mat[j + i * size]);
166 /* The remaint rows are shifted down by one */
167 for (i = 2; i < size; i++) {
168 for (j = 0; j < size; j++) {
170 if (mat[j + i * size] != 1) {
171 printf("mat(%d,%d) = %d, should = 1\n", i, j, mat[j + i * size]);
176 if (mat[j + i * size] != 0) {
177 printf("mat(%d,%d) = %d, should = 0\n", i, j, mat[j + i * size]);
186 int main(int argc, char *argv[])
189 int rank, size, root;
190 int minsize = 2, count;
194 MPI_Datatype mattype;
196 MTest_Init(&argc, &argv);
198 MPI_Op_create(uop, 0, &op);
200 while (MTestGetIntracommGeneral(&comm, minsize, 1)) {
201 if (comm == MPI_COMM_NULL)
203 MPI_Comm_size(comm, &size);
204 MPI_Comm_rank(comm, &rank);
207 /* Skip because there are too many processes */
208 MTestFreeComm(&comm);
212 /* Only one matrix for now */
215 /* A single matrix, the size of the communicator */
216 MPI_Type_contiguous(size * size, MPI_INT, &mattype);
217 MPI_Type_commit(&mattype);
219 buf = (int *) malloc(count * size * size * sizeof(int));
221 MPI_Abort(MPI_COMM_WORLD, 1);
222 bufout = (int *) malloc(count * size * size * sizeof(int));
224 MPI_Abort(MPI_COMM_WORLD, 1);
226 for (root = 0; root < size; root++) {
228 MPI_Reduce(buf, bufout, count, mattype, op, root, comm);
230 errs += isPermutedIdentity(comm, bufout);
233 /* Try the same test, but using MPI_IN_PLACE */
234 initMat(comm, bufout);
236 MPI_Reduce(MPI_IN_PLACE, bufout, count, mattype, op, root, comm);
239 MPI_Reduce(bufout, NULL, count, mattype, op, root, comm);
242 errs += isPermutedIdentity(comm, bufout);
245 MPI_Type_free(&mattype);
250 MTestFreeComm(&comm);
255 MTest_Finalize(errs);