COMBINATORIAL_BLAS  1.6
ReadMatDist.h
Go to the documentation of this file.
1 #ifndef _READ_MAT_DIST_H_
2 #define _READ_MAT_DIST_H_
3 
4 #include <mpi.h>
5 #include <sys/time.h>
6 #include <iostream>
7 #include <iomanip>
8 #include <functional>
9 #include <algorithm>
10 #include <std::vector>
11 #include <string>
12 #include <sstream>
13 
14 // These macros should be defined before stdint.h is included
15 #ifndef __STDC_CONSTANT_MACROS
16 #define __STDC_CONSTANT_MACROS
17 #endif
18 #ifndef __STDC_LIMIT_MACROS
19 #define __STDC_LIMIT_MACROS
20 #endif
21 #include <stdint.h>
22 
23 #include "CombBLAS/CombBLAS.h"
24 #include "Glue.h"
25 
26 namespace combblas {
27 
28 template <typename PARMAT>
29 void Symmetricize(PARMAT & A)
30 {
31  PARMAT AT = A;
32  AT.Transpose();
33  AT.RemoveLoops(); // needed for non-boolean matrix
34  A += AT;
35 }
36 
40 template <typename IT, typename NT>
41 void Reader(string filename, CCGrid & CMG, SpDCCols<IT,NT> & splitmat, bool trans, bool permute, FullyDistVec<IT, IT>& p)
42 {
43  std::vector<IT> vecEss; // at layer_grid=0, this will have [CMG.GridLayers * SpDCCols<IT,NT>::esscount] entries
44  std::vector< SpDCCols<IT, NT> > partsmat; // only valid at layer_grid=0
45  int nparts = CMG.GridLayers;
46  if(CMG.layer_grid == 0)
47  {
48  //SpDCCols<IT, NT> * localmat = GenRMat<IT,NT>(scale, EDGEFACTOR, initiator, CMG.layerWorld);
49  shared_ptr<CommGrid> layerGrid;
50  layerGrid.reset( new CommGrid(CMG.layerWorld, 0, 0) );
52  //A->ReadDistribute(filename, 0, false);
53  A->ParallelReadMM(filename);
54 
55  // random permutations for load balance
56  if(permute)
57  {
58  if(A->getnrow() == A->getncol())
59  {
60  if(p.TotalLength()!=A->getnrow())
61  {
62  SpParHelper::Print("Generating random permutation vector.\n");
63  p.iota(A->getnrow(), 0);
64  p.RandPerm();
65  }
66  (*A)(p,p,true);// in-place permute to save memory
67  }
68  else
69  {
70  SpParHelper::Print("nrow != ncol. Can not apply symmetric permutation.\n");
71  }
72  }
73 
74 
75  SpDCCols<IT, NT> * localmat = A->seqptr();
76  double trans_beg = MPI_Wtime();
77  if(trans) localmat->Transpose(); // locally transpose
78  comp_trans += (MPI_Wtime() - trans_beg);
79 
80 
81  double split_beg = MPI_Wtime();
82  localmat->ColSplit(nparts, partsmat); // split matrices are emplaced-back into partsmat vector, localmat destroyed
83 
84  for(int i=0; i< nparts; ++i)
85  {
86  std::vector<IT> ess = partsmat[i].GetEssentials();
87  for(auto itr = ess.begin(); itr != ess.end(); ++itr)
88  {
89  vecEss.push_back(*itr);
90  }
91  }
92  comp_split += (MPI_Wtime() - split_beg);
93  }
94 
95  double scatter_beg = MPI_Wtime(); // timer on
96  int esscnt = SpDCCols<IT,NT>::esscount; // necessary cast for MPI
97 
98  std::vector<IT> myess(esscnt);
99  MPI_Scatter(vecEss.data(), esscnt, MPIType<IT>(), myess.data(), esscnt, MPIType<IT>(), 0, CMG.fiberWorld);
100 
101  if(CMG.layer_grid == 0) // senders
102  {
103  splitmat = partsmat[0]; // just copy the local split
104  for(int recipient=1; recipient< nparts; ++recipient) // scatter the others
105  {
106  int tag = 0;
107  Arr<IT,NT> arrinfo = partsmat[recipient].GetArrays();
108  for(unsigned int i=0; i< arrinfo.indarrs.size(); ++i) // get index arrays
109  {
110  // MPI_Send(const void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm)
111  MPI_Send(arrinfo.indarrs[i].addr, arrinfo.indarrs[i].count, MPIType<IT>(), recipient, tag++, CMG.fiberWorld);
112  }
113  for(unsigned int i=0; i< arrinfo.numarrs.size(); ++i) // get numerical arrays
114  {
115  MPI_Send(arrinfo.numarrs[i].addr, arrinfo.numarrs[i].count, MPIType<NT>(), recipient, tag++, CMG.fiberWorld);
116  }
117  }
118  }
119  else // receivers
120  {
121  splitmat.Create(myess); // allocate memory for arrays
122  Arr<IT,NT> arrinfo = splitmat.GetArrays();
123 
124  int tag = 0;
125  for(unsigned int i=0; i< arrinfo.indarrs.size(); ++i) // get index arrays
126  {
127  MPI_Recv(arrinfo.indarrs[i].addr, arrinfo.indarrs[i].count, MPIType<IT>(), 0, tag++, CMG.fiberWorld, MPI_STATUS_IGNORE);
128  }
129  for(unsigned int i=0; i< arrinfo.numarrs.size(); ++i) // get numerical arrays
130  {
131  MPI_Recv(arrinfo.numarrs[i].addr, arrinfo.numarrs[i].count, MPIType<NT>(), 0, tag++, CMG.fiberWorld, MPI_STATUS_IGNORE);
132  }
133  }
134  comm_split += (MPI_Wtime() - scatter_beg);
135 }
136 
137 }
138 
139 #endif
int layer_grid
Definition: CCGrid.h:40
std::vector< LocArr< NT, IT > > numarrs
Definition: LocArr.h:55
std::vector< LocArr< IT, IT > > indarrs
Definition: LocArr.h:54
double comp_split
Definition: mpipspgemm.cpp:28
MPI_Comm layerWorld
Definition: CCGrid.h:41
void ColSplit(int parts, std::vector< SpDCCols< IT, NT > > &matrices)
Definition: SpDCCols.cpp:897
void Create(const std::vector< IT > &essentials)
Definition: SpMat.h:61
double A
static void Print(const std::string &s)
void iota(IT globalsize, NT first)
Arr< IT, NT > GetArrays() const
Definition: SpDCCols.cpp:787
double comp_trans
Definition: mpipspgemm.cpp:29
void Reader(string filename, CCGrid &CMG, SpDCCols< IT, NT > &splitmat, bool trans, bool permute, FullyDistVec< IT, IT > &p)
Definition: ReadMatDist.h:41
double comm_split
Definition: mpipspgemm.cpp:30
IT getncol() const
Definition: SpParMat.cpp:694
Definition: CCGrid.h:4
MPI_Comm fiberWorld
Definition: CCGrid.h:42
void Symmetricize(PARMAT &A)
Definition: ReadMatDist.h:29
void Transpose()
Mutator version, replaces the calling object.
Definition: SpDCCols.cpp:815
int GridLayers
Definition: CCGrid.h:36
void ParallelReadMM(const std::string &filename, bool onebased, _BinaryOperation BinOp)
Definition: SpParMat.cpp:3417
IT getnrow() const
Definition: SpParMat.cpp:685