HyPar  1.0
Finite-Difference Hyperbolic-Parabolic PDE Solver on Cartesian Grids
MPICommunicators.c File Reference

Functions to create and destroy MPI subcommunicators. More...

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

Go to the source code of this file.

Functions

int MPICreateCommunicators (int ndims, void *m)
 
int MPIFreeCommunicators (int ndims, void *m)
 

Detailed Description

Functions to create and destroy MPI subcommunicators.

Author
Debojyoti Ghosh

Definition in file MPICommunicators.c.

Function Documentation

◆ MPICreateCommunicators()

int MPICreateCommunicators ( int  ndims,
void *  m 
)

Create subcommunicators from MPI_WORLD, where each subcommunicator contains MPI ranks along a spatial dimension. Consider a two-dimensional problem, partitioned on 21 MPI ranks as follows:

mpi_ranks.png

This function will create 10 subcommunicators with the following ranks:

  • 0,1,2,3,4,5,6
  • 7,8,9,10,11,12,13
  • 14,15,16,17,18,19,20
  • 0,7,14
  • 1,8,15
  • 2,9,16
  • 3,10,17
  • 4,11,18
  • 5,12,19
  • 6,13,20

These subcommunicators are useful for parallel computations along grid lines. For example, a compact finite-difference scheme solves implicit systems along grid lines in every spatial dimension. Thus, the subcommunicator may be passed on to the parallel systems solver instead of MPI_WORLD.

Parameters
ndimsNumber of spatial dimensions
mMPI object of type MPIVariables

Definition at line 35 of file MPICommunicators.c.

39 {
40  MPIVariables *mpi = (MPIVariables*) m;
41 #ifdef serial
42  mpi->comm = NULL;
43 #else
44  int i,n,color,key;
45  int *ip,*iproc;
46 
47  mpi->comm = (MPI_Comm*) calloc (ndims, sizeof(MPI_Comm));
48  if (ndims == 1) MPI_Comm_dup(mpi->world,mpi->comm);
49  else {
50  ip = (int*) calloc (ndims-1,sizeof(int));
51  iproc = (int*) calloc (ndims-1,sizeof(int));
52  for (n=0; n<ndims; n++) {
53  int tick=0;
54  for (i=0; i<ndims; i++) {
55  if (i != n) {
56  ip[tick] = mpi->ip[i];
57  iproc[tick] = mpi->iproc[i];
58  tick++;
59  }
60  }
61  _ArrayIndex1D_(ndims-1,iproc,ip,0,color);
62  key = mpi->ip[n];
63  MPI_Comm_split(mpi->world,color,key,&mpi->comm[n]);
64  }
65  free(ip);
66  free(iproc);
67  }
68 #endif
69  return(0);
70 }
MPI_Comm world
MPI_Comm * comm
#define _ArrayIndex1D_(N, imax, i, ghost, index)
Structure of MPI-related variables.

◆ MPIFreeCommunicators()

int MPIFreeCommunicators ( int  ndims,
void *  m 
)

Free the subcommunicators created in MPICreateCommunicators().

Parameters
ndimsNumber of spatial dimensions
mMPI object of type MPIVariables

Definition at line 75 of file MPICommunicators.c.

79 {
80 #ifndef serial
81  MPIVariables *mpi = (MPIVariables*) m;
82  int n;
83  for (n=0; n<ndims; n++) MPI_Comm_free(&mpi->comm[n]);
84  free(mpi->comm);
85  if (mpi->IOParticipant) MPI_Comm_free(&mpi->IOWorld);
86  MPI_Comm_free(&mpi->world);
87 #endif
88  return(0);
89 }
MPI_Comm world
MPI_Comm * comm
MPI_Comm IOWorld
Structure of MPI-related variables.