startup.c
#include "array.h"
#include "poisson.h"
int
startup(int argc, char *argv[], Decomposition *oned, Grid *grid)
{
/* get arguments, initialize topology and decomposition */
int nproc; /* Number of processors */
int myid;
int dims[1];
int periods[1];
int args[3]; /* size of the global array */
int rest;
MPI_Init(&argc, &argv); /* enroll in MPI */
MPI_Comm_size(MPI_COMM_WORLD, &nproc);
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
/* get size of global array from command line arguments and broadcast it */
if (myid == 0)
{
if ((argc != 3) && (argc != 4))
{
fprintf(stderr,"Usage: %s <nx> <ny> [<no_it>]\n", argv[0]);
MPI_Abort(MPI_COMM_WORLD, -1);
exit(1);
}
args[0] = atoi(argv[1]); /* nx */
args[1] = atoi(argv[2]); /* ny */
if (argc == 4)
{
args[2] = atoi(argv[3]); /* no_it */
}
else
{
args[2] = -1; /* take maximum number of iterations */
}
}
MPI_Bcast(args, 3, MPI_INT, 0, MPI_COMM_WORLD);
/* get a new communicator for the domain decomposition */
dims[0] = nproc;
periods[0] = 0;
MPI_Cart_create(MPI_COMM_WORLD, 1, dims, periods, TRUE, &grid->comm);
MPI_Comm_rank(grid->comm, &grid->me);
MPI_Cart_shift(grid->comm, 0, 1, &grid->down, &grid->up);
MPI_Cart_coords(grid->comm, grid->me, 1, &grid->my_x);
/* compute the array decomposition */
oned->gx = args[0];
oned->gy = args[1];
rest = oned->gx % nproc;
if (grid->my_x < rest)
{
oned->lx = oned->gx/nproc + 1;
oned->ly = oned->gy;
oned->llc_x = grid->my_x * oned->lx + 1;
oned->llc_y = 1;
}
else
{
oned->lx = oned->gx/nproc;
oned->ly = oned->gy;
oned->llc_x = grid->my_x * oned->lx + rest + 1;
oned->llc_y = 1;
}
return(args[2]);
}

Peter Junglas 11.5.2000