COMBINATORIAL_BLAS  1.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FullyDistVec.h
Go to the documentation of this file.
1 /****************************************************************/
2 /* Parallel Combinatorial BLAS Library (for Graph Computations) */
3 /* version 1.2 -------------------------------------------------*/
4 /* date: 10/06/2011 --------------------------------------------*/
5 /* authors: Aydin Buluc (abuluc@lbl.gov), Adam Lugowski --------*/
6 /****************************************************************/
7 /*
8 Copyright (c) 2011, Aydin Buluc
9 
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files (the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions:
16 
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 THE SOFTWARE.
27 */
28 
29 #ifndef _FULLY_DIST_VEC_H_
30 #define _FULLY_DIST_VEC_H_
31 
32 #include <iostream>
33 #include <fstream>
34 #include <vector>
35 #include <utility>
36 #include <iterator>
37 #include "CombBLAS.h"
38 #include "CommGrid.h"
39 #include "FullyDist.h"
40 #include "Exception.h"
41 
42 template <class IT, class NT>
43 class FullyDistSpVec;
44 
45 template <class IT, class NT, class DER>
46 class SpParMat;
47 
48 template <class IT>
49 class DistEdgeList;
50 
51 template <class IU, class NU>
53 
54 // ABAB: As opposed to SpParMat, IT here is used to encode global size and global indices;
55 // therefore it can not be 32-bits, in general.
56 template <class IT, class NT>
57 class FullyDistVec: public FullyDist<IT,NT, typename CombBLAS::disable_if< CombBLAS::is_boolean<NT>::value, NT >::type >
58 {
59 public:
60  FullyDistVec ( );
61  FullyDistVec ( IT globallen, NT initval);
62  FullyDistVec ( shared_ptr<CommGrid> grid);
63  FullyDistVec ( shared_ptr<CommGrid> grid, IT globallen, NT initval);
64  FullyDistVec ( const FullyDistSpVec<IT, NT> & rhs ); // Sparse -> Dense conversion constructor
65 
66  template <class ITRHS, class NTRHS>
67  FullyDistVec ( const FullyDistVec<ITRHS, NTRHS>& rhs ); // type converter constructor
68 
70  {
71  public:
72  NT getNoNum(IT index) { return static_cast<NT>(1); }
73 
74  template <typename c, typename t>
75  NT read(std::basic_istream<c,t>& is, IT index)
76  {
77  NT v;
78  is >> v;
79  return v;
80  }
81 
82  template <typename c, typename t>
83  void save(std::basic_ostream<c,t>& os, const NT& v, IT index)
84  {
85  os << v;
86  }
87  };
88 
89  template <class HANDLER>
90  ifstream& ReadDistribute (ifstream& infile, int master, HANDLER handler);
91  ifstream& ReadDistribute (ifstream& infile, int master) { return ReadDistribute(infile, master, ScalarReadSaveHandler()); }
92 
93  template <class HANDLER>
94  void SaveGathered(ofstream& outfile, int master, HANDLER handler, bool printProcSplits = false);
95  void SaveGathered(ofstream& outfile, int master) { SaveGathered(outfile, master, ScalarReadSaveHandler(), false); }
96 
97 
98  template <class ITRHS, class NTRHS>
99  FullyDistVec<IT,NT> & operator=(const FullyDistVec< ITRHS,NTRHS > & rhs); // assignment with type conversion
103  FullyDistVec<IT,NT> operator() (const FullyDistVec<IT,IT> & ri) const; //<! subsref
104 
112  bool operator==(const FullyDistVec<IT,NT> & rhs) const;
113 
114  void SetElement (IT indx, NT numx); // element-wise assignment
115  NT GetElement (IT indx) const; // element-wise fetch
116  NT operator[](IT indx) const // more c++ like API
117  {
118  return GetElement(indx);
119  }
120 
121  void Set(const FullyDistSpVec< IT,NT > & rhs);
122  void iota(IT globalsize, NT first);
123  void RandPerm(); // randomly permute the vector
124  FullyDistVec<IT,IT> sort(); // sort and return the permutation
125 
130  IT LocArrSize() const { return arr.size(); } // = MyLocLength() once arr is resized
131 
132  template <typename _Predicate>
133  FullyDistSpVec<IT,NT> Find(_Predicate pred) const;
134 
135  template <typename _Predicate>
136  FullyDistVec<IT,IT> FindInds(_Predicate pred) const;
137 
138  template <typename _Predicate>
139  IT Count(_Predicate pred) const;
140 
141  template <typename _UnaryOperation>
142  void Apply(_UnaryOperation __unary_op)
143  {
144  transform(arr.begin(), arr.end(), arr.begin(), __unary_op);
145  }
146 
147  template <typename _BinaryOperation>
148  void ApplyInd(_BinaryOperation __binary_op)
149  {
150  IT offset = LengthUntil();
151  #ifdef _OPENMP
152  #pragma omp parallel for
153  #endif
154  for(IT i=0; (unsigned)i < arr.size(); ++i)
155  arr[i] = __binary_op(arr[i], i + offset);
156  }
157 
158  template <typename _UnaryOperation, typename IRRELEVANT_NT>
159  void Apply(_UnaryOperation __unary_op, const FullyDistSpVec<IT,IRRELEVANT_NT>& mask);
160 
161  // extended callback versions
162  template <typename _BinaryOperation, typename _BinaryPredicate, class NT2>
163  void EWiseApply(const FullyDistVec<IT,NT2> & other, _BinaryOperation __binary_op, _BinaryPredicate _do_op, const bool useExtendedBinOp);
164  template <typename _BinaryOperation, typename _BinaryPredicate, class NT2>
165  void EWiseApply(const FullyDistSpVec<IT,NT2> & other, _BinaryOperation __binary_op, _BinaryPredicate _do_op, bool applyNulls, NT2 nullValue, const bool useExtendedBinOp);
166 
167  // plain fallback versions
168  template <typename _BinaryOperation, typename _BinaryPredicate, class NT2>
169  void EWiseApply(const FullyDistVec<IT,NT2> & other, _BinaryOperation __binary_op, _BinaryPredicate _do_op)
170  {
171  EWiseApply(other,
174  true);
175  }
176  template <typename _BinaryOperation, typename _BinaryPredicate, class NT2>
177  void EWiseApply(const FullyDistSpVec<IT,NT2> & other, _BinaryOperation __binary_op, _BinaryPredicate _do_op, bool applyNulls, NT2 nullValue)
178  {
179  EWiseApply(other,
182  applyNulls, nullValue, true);
183  }
184 
185 
186  template <typename T1, typename T2>
187  class retTrue {
188  public:
189  bool operator()(const T1& x, const T2& y)
190  {
191  return true;
192  }
193  };
194 
195  template <typename _BinaryOperation, class NT2>
196  void EWiseApply(const FullyDistVec<IT,NT2> & other, _BinaryOperation __binary_op)
197  {
198  this->EWiseApply(other, __binary_op, retTrue<NT, NT2>());
199  }
200  template <typename _BinaryOperation, class NT2>
201  void EWiseApply(const FullyDistSpVec<IT,NT2> & other, _BinaryOperation __binary_op, bool applyNulls, NT2 nullValue)
202  {
203  this->EWiseApply(other, __binary_op, retTrue<NT, NT2>(), applyNulls, nullValue);
204  }
205 
206  void PrintToFile(string prefix)
207  {
208  ofstream output;
209  commGrid->OpenDebugFile(prefix, output);
210  copy(arr.begin(), arr.end(), ostream_iterator<NT> (output, " "));
211  output << endl;
212  output.close();
213  }
214 
215  void PrintInfo(string vectorname) const;
216  void DebugPrint();
217  shared_ptr<CommGrid> getcommgrid() const { return commGrid; }
218 
219  template <typename _BinaryOperation>
220  NT Reduce(_BinaryOperation __binary_op, NT identity);
221 
222  template <typename OUT, typename _BinaryOperation, typename _UnaryOperation>
223  OUT Reduce(_BinaryOperation __binary_op, OUT default_val, _UnaryOperation __unary_op);
224 
225  void SelectCandidates(double nver, bool deterministic);
226 
229 
230 private:
231  vector< NT > arr;
232 
233  template <typename _BinaryOperation>
234  void EWise(const FullyDistVec<IT,NT> & rhs, _BinaryOperation __binary_op);
235 
236  template <class IU, class NU>
237  friend class DenseParMat;
238 
239  template <class IU, class NU, class UDER>
240  friend class SpParMat;
241 
242  template <class IU, class NU>
243  friend class FullyDistVec;
244 
245  template <class IU, class NU>
246  friend class FullyDistSpVec;
247 
248  template <class IU, class NU>
250 
251  template <typename SR, typename IU, typename NUM, typename NUV, typename UDER>
253  SpMV (const SpParMat<IU,NUM,UDER> & A, const FullyDistVec<IU,NUV> & x );
254 
255  template <typename IU, typename NU1, typename NU2>
257  EWiseMult (const FullyDistSpVec<IU,NU1> & V, const FullyDistVec<IU,NU2> & W , bool exclude, NU2 zero);
258 
259  template <typename IU, typename NU1, typename NU2, typename _BinaryOperation>
261  EWiseApply (const FullyDistSpVec<IU,NU1> & V, const FullyDistVec<IU,NU2> & W , _BinaryOperation _binary_op, typename promote_trait<NU1,NU2>::T_promote zero);
262 
263  template <typename RET, typename IU, typename NU1, typename NU2, typename _BinaryOperation, typename _BinaryPredicate>
264  friend FullyDistSpVec<IU,RET>
265  EWiseApply (const FullyDistSpVec<IU,NU1> & V, const FullyDistVec<IU,NU2> & W , _BinaryOperation _binary_op, _BinaryPredicate _doOp, bool allowVNulls, NU1 Vzero, const bool useExtendedBinOp);
266 
267  template <typename IU>
268  friend void RenameVertices(DistEdgeList<IU> & DEL);
269 
270  template <typename IU, typename NU>
271  friend FullyDistVec<IU,NU> Concatenate ( vector< FullyDistVec<IU,NU> > & vecs);
272 };
273 
274 #include "FullyDistVec.cpp"
275 #endif
276 
277