PetaVision  Alpha
Communicator.hpp
1 /*
2  * Communicator.hpp
3  */
4 
5 #ifndef COMMUNICATOR_HPP_
6 #define COMMUNICATOR_HPP_
7 
8 #include "Arguments.hpp"
9 #include "include/pv_arch.h"
10 #include "include/pv_types.h"
11 #include "structures/MPIBlock.hpp"
12 #include <cstdio>
13 #include <vector>
14 
15 #include "arch/mpi/mpi.h"
16 
17 #define COMMNAME_MAXLENGTH 16
18 
19 namespace PV {
20 
21 class Communicator {
22  public:
23  // The following Communicator methods related to border exchange were moved to
24  // the BorderExchange class in utils/BorderExchange.{c,h}pp Feb 6, 2017.
25  // newDatatypes
26  // freeDatatypes
27  // exchange
28  // wait
29  // recvOffset
30  // sendOffset
31 
32  Communicator(Arguments *argumentList);
33  virtual ~Communicator();
34 
35  // Previous names of MPI getter functions now default to local ranks and sizes
36  int commRank() { return localRank; }
37  int globalCommRank() { return globalRank; }
38  int commSize() { return localSize; }
39  int globalCommSize() { return globalSize; }
40 
41  MPI_Comm communicator() const { return localMPIBlock->getComm(); }
42  MPI_Comm batchCommunicator() const { return batchMPIBlock->getComm(); }
43  MPI_Comm globalCommunicator() const { return globalMPIBlock->getComm(); }
44 
45  MPIBlock const *getLocalMPIBlock() const { return localMPIBlock; }
46  MPIBlock const *getBatchMPIBlock() const { return batchMPIBlock; }
47  MPIBlock const *getGlobalMPIBlock() const { return globalMPIBlock; }
48 
49  int numberOfNeighbors(); // includes interior (self) as a neighbor
50 
51  bool hasNeighbor(int neighborId);
52  int neighborIndex(int commId, int index);
53  int reverseDirection(int commId, int direction);
54 
55  int commRow() { return commRow(localRank); }
56  int commColumn() { return commColumn(localRank); }
57  int commBatch() { return commBatch(globalRank); }
58  int numCommRows() { return numRows; }
59  int numCommColumns() { return numCols; }
60  int numCommBatches() { return batchWidth; }
61 
62  int getTag(int neighbor) { return tags[neighbor]; }
63  int getReverseTag(int neighbor) { return tags[reverseDirection(localRank, neighbor)]; }
64 
65  bool isExtraProc() { return isExtra; }
66 
67  static const int LOCAL = 0;
68  static const int NORTHWEST = 1;
69  static const int NORTH = 2;
70  static const int NORTHEAST = 3;
71  static const int WEST = 4;
72  static const int EAST = 5;
73  static const int SOUTHWEST = 6;
74  static const int SOUTH = 7;
75  static const int SOUTHEAST = 8;
76 
77  protected:
78  int commRow(int commId);
79  int commColumn(int commId);
80  int commBatch(int commId);
81  int commIdFromRowColumn(int commRow, int commColumn);
82 
83  int numNeighbors; // # of remote neighbors plus local. NOT the size of the
84  // neighbors array,
85  // which uses negative values to mark directions where there is no remote
86  // neighbor.
87 
88  bool isExtra; // Defines if the process is an extra process
89 
90  int neighbors[NUM_NEIGHBORHOOD]; // [0] is interior (local)
91  int tags[NUM_NEIGHBORHOOD]; // diagonal communication needs a different tag
92  int exchangeCounter = 1024;
93  // from left/right or
94  // up/down communication.
95 
96  private:
97  int gcd(int a, int b);
98 
99  int localRank;
100  int localSize;
101  int globalRank;
102  int globalSize;
103  int batchRank;
104  int numRows;
105  int numCols;
106  int batchWidth;
107 
108  MPIBlock *localMPIBlock = nullptr;
109  MPIBlock *batchMPIBlock = nullptr;
110  MPIBlock *globalMPIBlock = nullptr;
111 
112  // These methods are private for now, move to public as needed
113 
114  int neighborInit();
115 
116  bool hasNorthwesternNeighbor(int commRow, int commColumn);
117  bool hasNorthernNeighbor(int commRow, int commColumn);
118  bool hasNortheasternNeighbor(int commRow, int commColumn);
119  bool hasWesternNeighbor(int commRow, int commColumn);
120  bool hasEasternNeighbor(int commRow, int commColumn);
121  bool hasSouthwesternNeighbor(int commRow, int commColumn);
122  bool hasSouthernNeighbor(int commRow, int commColumn);
123  bool hasSoutheasternNeighbor(int commRow, int commColumn);
124 
125  int northwest(int commRow, int commColumn);
126  int north(int commRow, int commColumn);
127  int northeast(int commRow, int commColumn);
128  int west(int commRow, int commColumn);
129  int east(int commRow, int commColumn);
130  int southwest(int commRow, int commColumn);
131  int south(int commRow, int commColumn);
132  int southeast(int commRow, int commColumn);
133 };
134 
135 } // namespace PV
136 
137 #endif /* COMMUNICATOR_HPP_ */
bool hasWesternNeighbor(int commRow, int commColumn)
bool hasSoutheasternNeighbor(int commRow, int commColumn)
bool hasNortheasternNeighbor(int commRow, int commColumn)
int west(int commRow, int commColumn)
int northeast(int commRow, int commColumn)
bool hasSouthwesternNeighbor(int commRow, int commColumn)
bool hasSouthernNeighbor(int commRow, int commColumn)
int east(int commRow, int commColumn)
int north(int commRow, int commColumn)
MPI_Comm getComm() const
Definition: MPIBlock.hpp:90
int southwest(int commRow, int commColumn)
int northwest(int commRow, int commColumn)
int south(int commRow, int commColumn)
bool hasNeighbor(int neighborId)
bool hasNorthernNeighbor(int commRow, int commColumn)
int neighborIndex(int commId, int index)
bool hasEasternNeighbor(int commRow, int commColumn)
int southeast(int commRow, int commColumn)
bool hasNorthwesternNeighbor(int commRow, int commColumn)
int commIdFromRowColumn(int commRow, int commColumn)