COMBINATORIAL_BLAS  1.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SpParVec.h
Go to the documentation of this file.
1 #ifndef _SP_PAR_VEC_H_
2 #define _SP_PAR_VEC_H_
3 
4 #include <iostream>
5 #include <vector>
6 #include <utility>
7 #include "CombBLAS.h"
8 #include "CommGrid.h"
9 #include "promote.h"
10 #include "SpParMat.h"
11 #include "Operations.h"
12 
13 template <class IT, class NT, class DER>
14 class SpParMat;
15 
16 template <class IT>
17 class DistEdgeList;
18 
19 
37 template <class IT, class NT>
38 class SpParVec
39 {
40 public:
41  SpParVec ( );
42  SpParVec ( IT loclength );
43  SpParVec ( shared_ptr<CommGrid> grid);
44  SpParVec ( shared_ptr<CommGrid> grid, IT loclength);
45 
48  void stealFrom(SpParVec<IT,NT> & victim);
51  ifstream& ReadDistribute (ifstream& infile, int master);
52 
53  template <typename NNT> operator SpParVec< IT,NNT > () const
54  {
55  SpParVec<IT,NNT> CVT(commGrid);
56  CVT.ind = vector<IT>(ind.begin(), ind.end());
57  CVT.num = vector<NNT>(num.begin(), num.end());
58  CVT.length = length;
59  }
60 
61  void PrintInfo(string vecname) const;
62  void iota(IT size, NT first);
63  SpParVec<IT,NT> operator() (const SpParVec<IT,IT> & ri) const;
64  void SetElement (IT indx, NT numx); // element-wise assignment
65  NT operator[](IT indx) const;
66 
67  // sort the vector itself
68  // return the permutation vector (0-based)
70 
71  IT getlocnnz() const
72  {
73  return ind.size();
74  }
75 
76  IT getnnz() const
77  {
78  IT totnnz = 0;
79  IT locnnz = ind.size();
80  MPI_Allreduce( &locnnz, & totnnz, 1, MPIType<IT>(), MPI_SUM, commGrid->GetWorld());
81  return totnnz;
82  }
83 
84  IT getTypicalLocLength() const;
85  IT getTotalLength(MPI_Comm & comm) const;
86  IT getTotalLength() const { return getTotalLength(commGrid->GetWorld()); }
87 
88  void setNumToInd()
89  {
90  MPI_Comm DiagWorld = commGrid->GetDiagWorld();
91  if(DiagWorld != MPI_COMM_NULL) // Diagonal processors only
92  {
93  int rank;
94  MPI_Comm_rank(DiagWorld,&rank);
95 
96  IT n_perproc = getTypicalLocLength();
97  IT offset = static_cast<IT>(rank) * n_perproc;
98 
99  transform(ind.begin(), ind.end(), num.begin(), bind2nd(plus<IT>(), offset));
100  }
101  }
102 
103  template <typename _UnaryOperation>
104  void Apply(_UnaryOperation __unary_op)
105  {
106  transform(num.begin(), num.end(), num.begin(), __unary_op);
107  }
108 
109  template <typename _BinaryOperation>
110  NT Reduce(_BinaryOperation __binary_op, NT init);
111 
112  void DebugPrint();
113  shared_ptr<CommGrid> getcommgrid() { return commGrid; }
115 private:
116  shared_ptr<CommGrid> commGrid;
117  vector< IT > ind; // ind.size() give the number of nonzeros
118  vector< NT > num;
119  IT length; // actual local length of the vector (including zeros)
120  bool diagonal;
121 
122  template <class IU, class NU>
123  friend class SpParVec;
124 
125  template <class IU, class NU>
126  friend class DenseParVec;
127 
128  template <class IU, class NU, class UDER>
129  friend class SpParMat;
130 
131  template <typename SR, typename IU, typename NUM, typename NUV, typename UDER>
133  SpMV (const SpParMat<IU,NUM,UDER> & A, const SpParVec<IU,NUV> & x );
134 
135  template <typename IU, typename NU1, typename NU2>
137  EWiseMult (const SpParVec<IU,NU1> & V, const DenseParVec<IU,NU2> & W , bool exclude, NU2 zero);
138 
139  template <typename IU>
140  friend void RandPerm(SpParVec<IU,IU> & V); // called on an existing object, randomly permutes it
141 
142  template <typename IU>
143  friend void RenameVertices(DistEdgeList<IU> & DEL);
144 };
145 
146 #include "SpParVec.cpp"
147 #endif
148