Blame test/mpi/coll/red4.c

Packit 0848f5
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
Packit 0848f5
/*
Packit 0848f5
 *
Packit 0848f5
 *  (C) 2003 by Argonne National Laboratory.
Packit 0848f5
 *      See COPYRIGHT in top-level directory.
Packit 0848f5
 */
Packit 0848f5
#include "mpi.h"
Packit 0848f5
#include <stdio.h>
Packit 0848f5
#include <stdlib.h>
Packit 0848f5
#include "mpitest.h"
Packit 0848f5
Packit 0848f5
/*
Packit 0848f5
static char MTEST_Descrip[] = "Test MPI_Reduce with non-commutative user-define operations and arbitrary root";
Packit 0848f5
*/
Packit 0848f5
Packit 0848f5
/*
Packit 0848f5
 * This tests that the reduce operation respects the noncommutative flag.
Packit 0848f5
 * and that can distinguish between P_{root} P_{root+1}
Packit 0848f5
 * ... P_{root-1} and P_0 ... P_{size-1} .  The MPI standard clearly
Packit 0848f5
 * specifies that the result is P_0 ... P_{size-1}, independent of the root
Packit 0848f5
 * (see 4.9.4 in MPI-1)
Packit 0848f5
 */
Packit 0848f5
Packit 0848f5
/* This implements a simple matrix-matrix multiply.  This is an associative
Packit 0848f5
   but not commutative operation.  The matrix size is set in matSize;
Packit 0848f5
   the number of matrices is the count argument. The matrix is stored
Packit 0848f5
   in C order, so that
Packit 0848f5
     c(i,j) is cin[j+i*matSize]
Packit 0848f5
 */
Packit 0848f5
#define MAXCOL 256
Packit 0848f5
static int matSize = 0;         /* Must be < MAXCOL */
Packit 0848f5
Packit 0848f5
void uop(void *cinPtr, void *coutPtr, int *count, MPI_Datatype * dtype);
Packit 0848f5
void uop(void *cinPtr, void *coutPtr, int *count, MPI_Datatype * dtype)
Packit 0848f5
{
Packit 0848f5
    const int *cin;
Packit 0848f5
    int *cout;
Packit 0848f5
    int i, j, k, nmat;
Packit 0848f5
    int tempCol[MAXCOL];
Packit 0848f5
Packit 0848f5
    if (*count != 1)
Packit 0848f5
        printf("Panic!\n");
Packit 0848f5
    for (nmat = 0; nmat < *count; nmat++) {
Packit 0848f5
        cin = (const int *) cinPtr;
Packit 0848f5
        cout = (int *) coutPtr;
Packit 0848f5
        for (j = 0; j < matSize; j++) {
Packit 0848f5
            for (i = 0; i < matSize; i++) {
Packit 0848f5
                tempCol[i] = 0;
Packit 0848f5
                for (k = 0; k < matSize; k++) {
Packit 0848f5
                    /* col[i] += cin(i,k) * cout(k,j) */
Packit 0848f5
                    tempCol[i] += cin[k + i * matSize] * cout[j + k * matSize];
Packit 0848f5
                }
Packit 0848f5
            }
Packit 0848f5
            for (i = 0; i < matSize; i++) {
Packit 0848f5
                cout[j + i * matSize] = tempCol[i];
Packit 0848f5
            }
Packit 0848f5
        }
Packit 0848f5
        cinPtr = (int *) cinPtr + matSize * matSize;
Packit 0848f5
        coutPtr = (int *) coutPtr + matSize * matSize;
Packit 0848f5
    }
Packit 0848f5
}
Packit 0848f5
Packit 0848f5
/* Initialize the integer matrix as a permutation of rank with rank+1.
Packit 0848f5
   If we call this matrix P_r, we know that product of P_0 P_1 ... P_{size-1}
Packit 0848f5
   is the matrix with rows ordered as
Packit 0848f5
   1,size,2,3,4,...,size-1
Packit 0848f5
   (The matrix is basically a circular shift right,
Packit 0848f5
   shifting right n-1 steps for an n x n dimensional matrix, with the last
Packit 0848f5
   step swapping rows 1 and size)
Packit 0848f5
*/
Packit 0848f5
Packit 0848f5
static void initMat(MPI_Comm comm, int mat[])
Packit 0848f5
{
Packit 0848f5
    int i, size, rank;
Packit 0848f5
Packit 0848f5
    MPI_Comm_rank(comm, &rank;;
Packit 0848f5
    MPI_Comm_size(comm, &size);
Packit 0848f5
Packit 0848f5
    /* Remember the matrix size */
Packit 0848f5
    matSize = size;
Packit 0848f5
Packit 0848f5
    for (i = 0; i < matSize * matSize; i++)
Packit 0848f5
        mat[i] = 0;
Packit 0848f5
Packit 0848f5
    for (i = 0; i < matSize; i++) {
Packit 0848f5
        if (i == rank)
Packit 0848f5
            mat[((i + 1) % matSize) + i * matSize] = 1;
Packit 0848f5
        else if (i == ((rank + 1) % matSize))
Packit 0848f5
            mat[((i + matSize - 1) % matSize) + i * matSize] = 1;
Packit 0848f5
        else
Packit 0848f5
            mat[i + i * matSize] = 1;
Packit 0848f5
    }
Packit 0848f5
}
Packit 0848f5
Packit 0848f5
/* Compare a matrix with the identity matrix */
Packit 0848f5
/*
Packit 0848f5
static int isIdentity(MPI_Comm comm, int mat[])
Packit 0848f5
{
Packit 0848f5
    int i, j, size, rank, errs = 0;
Packit 0848f5
Packit 0848f5
    MPI_Comm_rank(comm, &rank;;
Packit 0848f5
    MPI_Comm_size(comm, &size);
Packit 0848f5
Packit 0848f5
    for (i=0; i
Packit 0848f5
	for (j=0; j
Packit 0848f5
	    if (j == i) {
Packit 0848f5
		if (mat[j+i*size] != 1) {
Packit 0848f5
		    printf("mat(%d,%d) = %d, should = 1\n",
Packit 0848f5
			    i, j, mat[j+i*size]);
Packit 0848f5
		    errs++;
Packit 0848f5
		}
Packit 0848f5
	    }
Packit 0848f5
	    else {
Packit 0848f5
		if (mat[j+i*size] != 0) {
Packit 0848f5
		    printf("mat(%d,%d) = %d, should = 0\n",
Packit 0848f5
			    i, j, mat[j+i*size]);
Packit 0848f5
		    errs++;
Packit 0848f5
		}
Packit 0848f5
	    }
Packit 0848f5
	}
Packit 0848f5
    }
Packit 0848f5
    return errs;
Packit 0848f5
}
Packit 0848f5
*/
Packit 0848f5
Packit 0848f5
/* Compare a matrix with the identity matrix with rows permuted to as rows
Packit 0848f5
   1,size,2,3,4,5,...,size-1 */
Packit 0848f5
static int isPermutedIdentity(MPI_Comm comm, int mat[])
Packit 0848f5
{
Packit 0848f5
    int i, j, size, rank, errs = 0;
Packit 0848f5
Packit 0848f5
    MPI_Comm_rank(comm, &rank;;
Packit 0848f5
    MPI_Comm_size(comm, &size);
Packit 0848f5
Packit 0848f5
    /* Check the first two last rows */
Packit 0848f5
    i = 0;
Packit 0848f5
    for (j = 0; j < size; j++) {
Packit 0848f5
        if (j == 0) {
Packit 0848f5
            if (mat[j] != 1) {
Packit 0848f5
                printf("mat(%d,%d) = %d, should = 1\n", i, j, mat[j]);
Packit 0848f5
                errs++;
Packit 0848f5
            }
Packit 0848f5
        }
Packit 0848f5
        else {
Packit 0848f5
            if (mat[j] != 0) {
Packit 0848f5
                printf("mat(%d,%d) = %d, should = 0\n", i, j, mat[j]);
Packit 0848f5
                errs++;
Packit 0848f5
            }
Packit 0848f5
        }
Packit 0848f5
    }
Packit 0848f5
    i = 1;
Packit 0848f5
    for (j = 0; j < size; j++) {
Packit 0848f5
        if (j == size - 1) {
Packit 0848f5
            if (mat[j + i * size] != 1) {
Packit 0848f5
                printf("mat(%d,%d) = %d, should = 1\n", i, j, mat[j + i * size]);
Packit 0848f5
                errs++;
Packit 0848f5
            }
Packit 0848f5
        }
Packit 0848f5
        else {
Packit 0848f5
            if (mat[j + i * size] != 0) {
Packit 0848f5
                printf("mat(%d,%d) = %d, should = 0\n", i, j, mat[j + i * size]);
Packit 0848f5
                errs++;
Packit 0848f5
            }
Packit 0848f5
        }
Packit 0848f5
    }
Packit 0848f5
    /* The remaint rows are shifted down by one */
Packit 0848f5
    for (i = 2; i < size; i++) {
Packit 0848f5
        for (j = 0; j < size; j++) {
Packit 0848f5
            if (j == i - 1) {
Packit 0848f5
                if (mat[j + i * size] != 1) {
Packit 0848f5
                    printf("mat(%d,%d) = %d, should = 1\n", i, j, mat[j + i * size]);
Packit 0848f5
                    errs++;
Packit 0848f5
                }
Packit 0848f5
            }
Packit 0848f5
            else {
Packit 0848f5
                if (mat[j + i * size] != 0) {
Packit 0848f5
                    printf("mat(%d,%d) = %d, should = 0\n", i, j, mat[j + i * size]);
Packit 0848f5
                    errs++;
Packit 0848f5
                }
Packit 0848f5
            }
Packit 0848f5
        }
Packit 0848f5
    }
Packit 0848f5
    return errs;
Packit 0848f5
}
Packit 0848f5
Packit 0848f5
int main(int argc, char *argv[])
Packit 0848f5
{
Packit 0848f5
    int errs = 0;
Packit 0848f5
    int rank, size, root;
Packit 0848f5
    int minsize = 2, count;
Packit 0848f5
    MPI_Comm comm;
Packit 0848f5
    int *buf, *bufout;
Packit 0848f5
    MPI_Op op;
Packit 0848f5
    MPI_Datatype mattype;
Packit 0848f5
Packit 0848f5
    MTest_Init(&argc, &argv);
Packit 0848f5
Packit 0848f5
    MPI_Op_create(uop, 0, &op);
Packit 0848f5
Packit 0848f5
    while (MTestGetIntracommGeneral(&comm, minsize, 1)) {
Packit 0848f5
        if (comm == MPI_COMM_NULL)
Packit 0848f5
            continue;
Packit 0848f5
        MPI_Comm_size(comm, &size);
Packit 0848f5
        MPI_Comm_rank(comm, &rank;;
Packit 0848f5
Packit 0848f5
        if (size > MAXCOL) {
Packit 0848f5
            /* Skip because there are too many processes */
Packit 0848f5
            MTestFreeComm(&comm);
Packit 0848f5
            continue;
Packit 0848f5
        }
Packit 0848f5
Packit 0848f5
        /* Only one matrix for now */
Packit 0848f5
        count = 1;
Packit 0848f5
Packit 0848f5
        /* A single matrix, the size of the communicator */
Packit 0848f5
        MPI_Type_contiguous(size * size, MPI_INT, &mattype);
Packit 0848f5
        MPI_Type_commit(&mattype);
Packit 0848f5
Packit 0848f5
        buf = (int *) malloc(count * size * size * sizeof(int));
Packit 0848f5
        if (!buf)
Packit 0848f5
            MPI_Abort(MPI_COMM_WORLD, 1);
Packit 0848f5
        bufout = (int *) malloc(count * size * size * sizeof(int));
Packit 0848f5
        if (!bufout)
Packit 0848f5
            MPI_Abort(MPI_COMM_WORLD, 1);
Packit 0848f5
Packit 0848f5
        for (root = 0; root < size; root++) {
Packit 0848f5
            initMat(comm, buf);
Packit 0848f5
            MPI_Reduce(buf, bufout, count, mattype, op, root, comm);
Packit 0848f5
            if (rank == root) {
Packit 0848f5
                errs += isPermutedIdentity(comm, bufout);
Packit 0848f5
            }
Packit 0848f5
Packit 0848f5
            /* Try the same test, but using MPI_IN_PLACE */
Packit 0848f5
            initMat(comm, bufout);
Packit 0848f5
            if (rank == root) {
Packit 0848f5
                MPI_Reduce(MPI_IN_PLACE, bufout, count, mattype, op, root, comm);
Packit 0848f5
            }
Packit 0848f5
            else {
Packit 0848f5
                MPI_Reduce(bufout, NULL, count, mattype, op, root, comm);
Packit 0848f5
            }
Packit 0848f5
            if (rank == root) {
Packit 0848f5
                errs += isPermutedIdentity(comm, bufout);
Packit 0848f5
            }
Packit 0848f5
        }
Packit 0848f5
        MPI_Type_free(&mattype);
Packit 0848f5
Packit 0848f5
        free(buf);
Packit 0848f5
        free(bufout);
Packit 0848f5
Packit 0848f5
        MTestFreeComm(&comm);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    MPI_Op_free(&op);
Packit 0848f5
Packit 0848f5
    MTest_Finalize(errs);
Packit 0848f5
    MPI_Finalize();
Packit 0848f5
    return 0;
Packit 0848f5
}