|
Packit |
0848f5 |
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
|
|
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 |
#include <assert.h>
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
/*
|
|
Packit |
0848f5 |
static char MTEST_Descrip[] = "Test MPI_Allreduce with non-commutative user-defined operations";
|
|
Packit |
0848f5 |
*/
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
/* We make the error count global so that we can easily control the output
|
|
Packit |
0848f5 |
of error information (in particular, limiting it after the first 10
|
|
Packit |
0848f5 |
errors */
|
|
Packit |
0848f5 |
int errs = 0;
|
|
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 |
static int max_offset = 0;
|
|
Packit |
0848f5 |
void uop(void *, void *, int *, MPI_Datatype *);
|
|
Packit |
0848f5 |
void uop(void *cinPtr, void *coutPtr, int *count, MPI_Datatype * dtype)
|
|
Packit |
0848f5 |
{
|
|
Packit |
0848f5 |
const int *cin = (const int *) cinPtr;
|
|
Packit |
0848f5 |
int *cout = (int *) coutPtr;
|
|
Packit |
0848f5 |
int i, j, k, nmat;
|
|
Packit |
0848f5 |
int tempcol[MAXCOL];
|
|
Packit |
0848f5 |
int offset1, offset2;
|
|
Packit |
0848f5 |
int matsize2 = matSize * matSize;
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
for (nmat = 0; nmat < *count; nmat++) {
|
|
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 |
offset1 = k + i * matSize;
|
|
Packit |
0848f5 |
offset2 = j + k * matSize;
|
|
Packit |
0848f5 |
assert(offset1 < max_offset);
|
|
Packit |
0848f5 |
assert(offset2 < max_offset);
|
|
Packit |
0848f5 |
tempcol[i] += cin[offset1] * cout[offset2];
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
for (i = 0; i < matSize; i++) {
|
|
Packit |
0848f5 |
offset1 = j + i * matSize;
|
|
Packit |
0848f5 |
assert(offset1 < max_offset);
|
|
Packit |
0848f5 |
cout[offset1] = tempcol[i];
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
cin += matsize2;
|
|
Packit |
0848f5 |
cout += matsize2;
|
|
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-2}
|
|
Packit |
0848f5 |
is the the matrix representing the permutation that shifts left by one.
|
|
Packit |
0848f5 |
As the final matrix (in the size-1 position), we use the matrix that
|
|
Packit |
0848f5 |
shifts RIGHT by one
|
|
Packit |
0848f5 |
*/
|
|
Packit |
0848f5 |
static void initMat(MPI_Comm comm, int mat[])
|
|
Packit |
0848f5 |
{
|
|
Packit |
0848f5 |
int i, j, size, rank;
|
|
Packit |
0848f5 |
int offset;
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
MPI_Comm_rank(comm, &rank;;
|
|
Packit |
0848f5 |
MPI_Comm_size(comm, &size);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
for (i = 0; i < size * size; i++) {
|
|
Packit |
0848f5 |
assert(i < max_offset);
|
|
Packit |
0848f5 |
mat[i] = 0;
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
if (rank < size - 1) {
|
|
Packit |
0848f5 |
/* Create the permutation matrix that exchanges r with r+1 */
|
|
Packit |
0848f5 |
for (i = 0; i < size; i++) {
|
|
Packit |
0848f5 |
if (i == rank) {
|
|
Packit |
0848f5 |
offset = ((i + 1) % size) + i * size;
|
|
Packit |
0848f5 |
assert(offset < max_offset);
|
|
Packit |
0848f5 |
mat[offset] = 1;
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
else if (i == ((rank + 1) % size)) {
|
|
Packit |
0848f5 |
offset = ((i + size - 1) % size) + i * size;
|
|
Packit |
0848f5 |
assert(offset < max_offset);
|
|
Packit |
0848f5 |
mat[offset] = 1;
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
else {
|
|
Packit |
0848f5 |
offset = i + i * size;
|
|
Packit |
0848f5 |
assert(offset < max_offset);
|
|
Packit |
0848f5 |
mat[offset] = 1;
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
else {
|
|
Packit |
0848f5 |
/* Create the permutation matrix that shifts right by one */
|
|
Packit |
0848f5 |
for (i = 0; i < size; i++) {
|
|
Packit |
0848f5 |
for (j = 0; j < size; j++) {
|
|
Packit |
0848f5 |
offset = j + i * size; /* location of c(i,j) */
|
|
Packit |
0848f5 |
mat[offset] = 0;
|
|
Packit |
0848f5 |
if (((j - i + size) % size) == 1)
|
|
Packit |
0848f5 |
mat[offset] = 1;
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
/* Compare a matrix with the identity matrix */
|
|
Packit |
0848f5 |
static int isIdentity(MPI_Comm comm, int mat[])
|
|
Packit |
0848f5 |
{
|
|
Packit |
0848f5 |
int i, j, size, rank, lerrs = 0;
|
|
Packit |
0848f5 |
int offset;
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
MPI_Comm_rank(comm, &rank;;
|
|
Packit |
0848f5 |
MPI_Comm_size(comm, &size);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
for (i = 0; i < size; i++) {
|
|
Packit |
0848f5 |
for (j = 0; j < size; j++) {
|
|
Packit |
0848f5 |
if (i == j) {
|
|
Packit |
0848f5 |
offset = j + i * size;
|
|
Packit |
0848f5 |
assert(offset < max_offset);
|
|
Packit |
0848f5 |
if (mat[offset] != 1) {
|
|
Packit |
0848f5 |
lerrs++;
|
|
Packit |
0848f5 |
if (errs + lerrs < 10) {
|
|
Packit |
0848f5 |
printf("[%d] mat[%d,%d] = %d, expected 1 for comm %s\n",
|
|
Packit |
0848f5 |
rank, i, j, mat[offset], MTestGetIntracommName());
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
else {
|
|
Packit |
0848f5 |
offset = j + i * size;
|
|
Packit |
0848f5 |
assert(offset < max_offset);
|
|
Packit |
0848f5 |
if (mat[offset] != 0) {
|
|
Packit |
0848f5 |
lerrs++;
|
|
Packit |
0848f5 |
if (errs + lerrs < 10) {
|
|
Packit |
0848f5 |
printf("[%d] mat[%d,%d] = %d, expected 0 for comm %s\n",
|
|
Packit |
0848f5 |
rank, i, j, mat[offset], MTestGetIntracommName());
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
return lerrs;
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
int main(int argc, char *argv[])
|
|
Packit |
0848f5 |
{
|
|
Packit |
0848f5 |
int size;
|
|
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 |
}
|
|
Packit |
0848f5 |
MPI_Comm_size(comm, &size);
|
|
Packit |
0848f5 |
matSize = size;
|
|
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 |
max_offset = count * size * size;
|
|
Packit |
0848f5 |
buf = (int *) malloc(max_offset * sizeof(int));
|
|
Packit |
0848f5 |
if (!buf) {
|
|
Packit |
0848f5 |
MPI_Abort(MPI_COMM_WORLD, 1);
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
bufout = (int *) malloc(max_offset * sizeof(int));
|
|
Packit |
0848f5 |
if (!bufout) {
|
|
Packit |
0848f5 |
MPI_Abort(MPI_COMM_WORLD, 1);
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
initMat(comm, buf);
|
|
Packit |
0848f5 |
MPI_Allreduce(buf, bufout, count, mattype, op, comm);
|
|
Packit |
0848f5 |
errs += isIdentity(comm, bufout);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
/* Try the same test, but using MPI_IN_PLACE */
|
|
Packit |
0848f5 |
initMat(comm, bufout);
|
|
Packit |
0848f5 |
MPI_Allreduce(MPI_IN_PLACE, bufout, count, mattype, op, comm);
|
|
Packit |
0848f5 |
errs += isIdentity(comm, bufout);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
free(buf);
|
|
Packit |
0848f5 |
free(bufout);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
MPI_Type_free(&mattype);
|
|
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 |
}
|