CoMD
A Mini-app for Co-Design of Classical Molecular Dynamics.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
decomposition.c
Go to the documentation of this file.
1 /// \file
2 /// Parallel domain decomposition. This version of CoMD uses a simple
3 /// spatial Cartesian domain decomposition. The simulation box is
4 /// divided into equal size bricks by a grid that is xproc by yproc by
5 /// zproc in size.
6 
7 #include "decomposition.h"
8 
9 #include <assert.h>
10 
11 #include "memUtils.h"
12 #include "parallel.h"
13 
14 /// \param [in] xproc x-size of domain decomposition grid.
15 /// \param [in] yproc y-size of domain decomposition grid.
16 /// \param [in] zproc z-size of domain decomposition grid.
17 /// \param [in] globalExtent Size of the simulation domain (in Angstroms).
18 Domain* initDecomposition(int xproc, int yproc, int zproc, real3 globalExtent)
19 {
20  assert( xproc * yproc * zproc == getNRanks());
21 
22  Domain* dd = comdMalloc(sizeof(Domain));
23  dd->procGrid[0] = xproc;
24  dd->procGrid[1] = yproc;
25  dd->procGrid[2] = zproc;
26  // calculate grid coordinates i,j,k for this processor
27  int myRank = getMyRank();
28  dd->procCoord[0] = myRank % dd->procGrid[0];
29  myRank /= dd->procGrid[0];
30  dd->procCoord[1] = myRank % dd->procGrid[1];
31  dd->procCoord[2] = myRank / dd->procGrid[1];
32 
33  // initialialize global bounds
34  for (int i = 0; i < 3; i++)
35  {
36  dd->globalMin[i] = 0;
37  dd->globalMax[i] = globalExtent[i];
38  dd->globalExtent[i] = dd->globalMax[i] - dd->globalMin[i];
39  }
40 
41  // initialize local bounds on this processor
42  for (int i = 0; i < 3; i++)
43  {
44  dd->localExtent[i] = dd->globalExtent[i] / dd->procGrid[i];
45  dd->localMin[i] = dd->globalMin[i] + dd->procCoord[i] * dd->localExtent[i];
46  dd->localMax[i] = dd->globalMin[i] + (dd->procCoord[i]+1) * dd->localExtent[i];
47  }
48 
49  return dd;
50 }
51 
52 /// \details
53 /// Calculates the rank of the processor with grid coordinates
54 /// (ix+dix, iy+diy, iz+diz) where (ix, iy, iz) are the grid coordinates
55 /// of the local rank. Assumes periodic boundary conditions. The
56 /// deltas cannot be smaller than -procGrid[ii].
57 int processorNum(Domain* domain, int dix, int diy, int diz)
58 {
59  const int* procCoord = domain->procCoord; // alias
60  const int* procGrid = domain->procGrid; // alias
61  int ix = (procCoord[0] + dix + procGrid[0]) % procGrid[0];
62  int iy = (procCoord[1] + diy + procGrid[1]) % procGrid[1];
63  int iz = (procCoord[2] + diz + procGrid[2]) % procGrid[2];
64 
65  return ix + procGrid[0] *(iy + procGrid[1]*iz);
66 }