planc
Parallel Lowrank Approximation with Non-negativity Constraints
mpicomm.hpp
Go to the documentation of this file.
1 /* Copyright 2016 Ramakrishnan Kannan */
2 
3 #ifndef DISTNMF_MPICOMM_HPP_
4 #define DISTNMF_MPICOMM_HPP_
5 
6 #include <mpi.h>
7 #include <vector>
8 #include "common/distutils.hpp"
9 
10 #ifdef USE_PACOSS
11 #include "pacoss/pacoss.h"
12 #endif
13 
19 namespace planc {
20 
22  private:
23  int m_rank;
24  int m_numProcs;
25  int m_row_rank;
26  int m_row_size;
27  int m_col_rank;
28  int m_col_size;
29  int m_pr, m_pc;
30 
31  // for 2D communicators
32  // MPI Related stuffs
33  MPI_Comm *m_commSubs;
34  void printConfig() {
35  if (rank() == 0) {
36  INFO << "successfully setup MPI communicators" << std::endl;
37  INFO << "size=" << size() << std::endl;
38  INFO << "rowsize=" << m_row_size << ":pr=" << m_pr << std::endl;
39  INFO << "colsize=" << m_col_size << ":pc=" << m_pc << std::endl;
40  }
41  MPI_Barrier(MPI_COMM_WORLD);
42  INFO << ":rank=" << rank() << ":row_rank=" << row_rank() << ":colrank"
43  << col_rank() << std::endl;
44  }
45 
46  public:
47  // Violating the cpp guidlines. Other functions need
48  // non const pointers.
49  MPICommunicator(int argc, char *argv[]) {
50 #ifdef USE_PACOSS
51  TMPI_Init(&argc, &argv);
52 #else
53  MPI_Init(&argc, &argv);
54 #endif
55  MPI_Comm_rank(MPI_COMM_WORLD, &m_rank);
56  MPI_Comm_size(MPI_COMM_WORLD, &m_numProcs);
57  }
59  MPI_Barrier(MPI_COMM_WORLD);
60 #ifdef USE_PACOSS
61  TMPI_Finalize();
62 #else
63  MPI_Finalize();
64 #endif
65  }
66  MPICommunicator(int argc, char *argv[], int pr, int pc) {
67 #ifdef USE_PACOSS
68  TMPI_Init(&argc, &argv);
69 #else
70  MPI_Init(&argc, &argv);
71 #endif
72  MPI_Comm_rank(MPI_COMM_WORLD, &m_rank);
73  MPI_Comm_size(MPI_COMM_WORLD, &m_numProcs);
74  int reorder = 0;
75  std::vector<int> dimSizes;
76  std::vector<int> periods;
77  int nd = 2;
78  dimSizes.resize(nd);
79  this->m_pr = pr;
80  this->m_pc = pc;
81  dimSizes[0] = pr;
82  dimSizes[1] = pc;
83  periods.resize(nd);
84  MPI_Comm gridComm;
85  std::vector<int> gridCoords;
86  fillVector<int>(1, &periods);
87  // int MPI_Cart_create(MPI_Comm comm_old, int ndims, const int dims[],
88  // const int periods[], int reorder, MPI_Comm *comm_cart)
89  if (dimSizes[0] * dimSizes[1] != m_numProcs) {
90  if (m_rank == 0) {
91  std::cerr << "Processor grid dimensions do not"
92  << "multiply to MPI_SIZE::" << dimSizes[0] << 'x'
93  << dimSizes[1] << "::m_numProcs::" << m_numProcs << std::endl;
94  }
95  MPI_Barrier(MPI_COMM_WORLD);
96  MPI_Abort(MPI_COMM_WORLD, 1);
97  }
98  MPI_Cart_create(MPI_COMM_WORLD, nd, &dimSizes[0], &periods[0], reorder,
99  &gridComm);
100  gridCoords.resize(nd);
101  MPI_Cart_get(gridComm, nd, &dimSizes[0], &periods[0], &(gridCoords[0]));
102  this->m_commSubs = new MPI_Comm[nd];
103  int *keepCols = new int[nd];
104  for (int i = 0; i < nd; i++) {
105  std::fill_n(keepCols, nd, 0);
106  keepCols[i] = 1;
107  MPI_Cart_sub(gridComm, keepCols, &(this->m_commSubs[i]));
108  }
109  MPI_Comm_size(m_commSubs[0], &m_row_size);
110  MPI_Comm_size(m_commSubs[1], &m_col_size);
111  MPI_Comm_rank(m_commSubs[0], &m_row_rank);
112  MPI_Comm_rank(m_commSubs[1], &m_col_rank);
113 #ifdef MPI_VERBOSE
114  printConfig();
115 #endif
116  }
118  const int rank() const { return m_rank; }
120  const int size() const { return m_numProcs; }
122  const int row_rank() const { return m_row_rank; }
124  const int col_rank() const { return m_col_rank; }
126  const int pr() const { return m_pr; }
128  const int pc() const { return m_pc; }
129  const MPI_Comm *commSubs() const { return m_commSubs; }
130 };
131 
132 } // namespace planc
133 
134 #endif // DISTNMF_MPICOMM_HPP_
MPICommunicator(int argc, char *argv[])
Definition: mpicomm.hpp:49
const int pc() const
Total number of column processor.
Definition: mpicomm.hpp:128
const int pr() const
Total number of row processors.
Definition: mpicomm.hpp:126
#define INFO
Definition: utils.h:36
const int row_rank() const
returns its rank in the row processor grid
Definition: mpicomm.hpp:122
MPICommunicator(int argc, char *argv[], int pr, int pc)
Definition: mpicomm.hpp:66
const int size() const
returns the total number of mpi processes
Definition: mpicomm.hpp:120
const int col_rank() const
returns the rank in the column processor grid
Definition: mpicomm.hpp:124
const int rank() const
returns the global rank
Definition: mpicomm.hpp:118
ncp_factors contains the factors of the ncp every ith factor is of size n_i * k number of factors is ...
Definition: ncpfactors.hpp:20
const MPI_Comm * commSubs() const
Definition: mpicomm.hpp:129