HyPar  1.0
Finite-Difference Hyperbolic-Parabolic PDE Solver on Cartesian Grids
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
MPIGetArrayDatanD.c File Reference

Send a part of a local array to another MPI rank. More...

#include <stdio.h>
#include <stdlib.h>
#include <basic.h>
#include <arrayfunctions.h>
#include <mpivars.h>

Go to the source code of this file.

Functions

int MPIGetArrayDatanD (double *xbuf, double *x, int *source, int *dest, int *limits, int *dim, int ghosts, int ndims, int nvars, void *m)
 

Detailed Description

Send a part of a local array to another MPI rank.

Author
Debojyoti Ghosh

Definition in file MPIGetArrayDatanD.c.

Function Documentation

int MPIGetArrayDatanD ( double *  xbuf,
double *  x,
int *  source,
int *  dest,
int *  limits,
int *  dim,
int  ghosts,
int  ndims,
int  nvars,
void *  m 
)

This function lets one rank get a portion of a local n-dimensional array on another rank. The n-dimensional array must be stored in the memory as a single-index array as described in the documentation of MPIExchangeBoundariesnD(). The source rank sends to the dest rank a logically rectangular n-dimensional portion of its local copy of an array x. The extent of this logically rectangular portion is defined by limits.

  • limits is an array of size 2x the number of spatial dimensions, with elements as: [ is[0], ie[0], is[1], ie[1], ..., is[ndims-1], ie[ndims-1] ], where is[n] is the starting index along spatial dimension n, and ie[n] is the end index (+1) along spatial dimension n.
Parameters
xbufpreallocated memory on destination rank to hold the received data
xlocal array of which a part is needed
sourceMPI rank of the source
destMPI rank of the destination
limitsInteger array (of size 2*ndims) with the start and end indices along each spatial dimension of the desired portion of the array
dimInteger array whose elements are the local size of x in each spatial dimension
ghostsNumber of ghost points
ndimsNumber of spatial dimensions
nvarsNumber of variables (vector components)
mMPI object of type MPIVariables

Definition at line 21 of file MPIGetArrayDatanD.c.

34 {
35  MPIVariables *mpi = (MPIVariables*) m;
36  int d;
37 
38  int source_rank = MPIRank1D(ndims,mpi->iproc,source);
39  int dest_rank = MPIRank1D(ndims,mpi->iproc,dest );
40 
41  int is[ndims], ie[ndims], index[ndims], bounds[ndims], size;
42  size = 1;
43  for (d=0; d<ndims; d++) {
44  is[d] = limits[2*d ];
45  ie[d] = limits[2*d+1];
46  size *= (ie[d] - is[d]);
47  }
48  _ArraySubtract1D_(bounds,ie,is,ndims);
49 
50  if (source_rank == dest_rank) {
51  /* source and dest are the same process */
52  int done = 0; _ArraySetValue_(index,ndims,0);
53  while (!done) {
54  int p1; _ArrayIndex1D_(ndims,bounds,index,0,p1);
55  int p2; _ArrayIndex1DWO_(ndims,dim,index,is,ghosts,p2);
56  _ArrayCopy1D_((x+nvars*p2),(xbuf+nvars*p1),nvars);
57  _ArrayIncrementIndex_(ndims,bounds,index,done);
58  }
59  } else {
60 #ifdef serial
61  fprintf(stderr,"Error in MPIGetArrayDatanD(): This is a serial run. Source and ");
62  fprintf(stderr,"destination ranks must be equal. Instead they are %d and %d.\n",
63  source_rank,dest_rank);
64  return(1);
65 #else
66  if (mpi->rank == source_rank) {
67  double *buf = (double*) calloc (size*nvars, sizeof(double));
68  int done = 0; _ArraySetValue_(index,ndims,0);
69  while (!done) {
70  int p1; _ArrayIndex1D_(ndims,bounds,index,0,p1);
71  int p2; _ArrayIndex1DWO_(ndims,dim,index,is,ghosts,p2);
72  _ArrayCopy1D_((x+nvars*p2),(buf+nvars*p1),nvars);
73  _ArrayIncrementIndex_(ndims,bounds,index,done);
74  }
75  MPI_Send(buf,size*nvars,MPI_DOUBLE,dest_rank,2211,mpi->world);
76  free(buf);
77  } else if (mpi->rank == dest_rank) {
78  MPI_Status status;
79  MPI_Recv(xbuf,size*nvars,MPI_DOUBLE,source_rank,2211,mpi->world,&status);
80  } else {
81  fprintf(stderr,"Error in MPIGetArrayData3D(): Process %d shouldn't have entered this function.\n",
82  mpi->rank);
83  return(1);
84  }
85 #endif
86  }
87  return(0);
88 }
int MPIRank1D(int, int *, int *)
Definition: MPIRank1D.c:26
#define _ArraySetValue_(x, size, value)
#define _ArrayIncrementIndex_(N, imax, i, done)
#define _ArraySubtract1D_(x, a, b, size)
#define _ArrayIndex1D_(N, imax, i, ghost, index)
MPI_Comm world
#define _ArrayIndex1DWO_(N, imax, i, offset, ghost, index)
#define _ArrayCopy1D_(x, y, size)
Structure of MPI-related variables.