COMBINATORIAL_BLAS 1.6
FullyDistVec.h
Go to the documentation of this file.
1/****************************************************************/
2/* Parallel Combinatorial BLAS Library (for Graph Computations) */
3/* version 1.6 -------------------------------------------------*/
4/* date: 6/15/2017 ---------------------------------------------*/
5/* authors: Ariful Azad, Aydin Buluc --------------------------*/
6/****************************************************************/
7/*
8 Copyright (c) 2010-2017, The Regents of the University of California
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
30#ifndef _FULLY_DIST_VEC_H_
31#define _FULLY_DIST_VEC_H_
32
33#include <iostream>
34#include <fstream>
35#include <vector>
36#include <utility>
37#include <iterator>
38#include <random>
39#include "CombBLAS.h"
40#include "CommGrid.h"
41#include "FullyDist.h"
42#include "Exception.h"
43
44namespace combblas {
45
46template <class IT, class NT>
47class FullyDistSpVec;
48
49template <class IT, class NT, class DER>
50class SpParMat;
51
52template <class IT>
53class DistEdgeList;
54
55template <class IU, class NU>
56class DenseVectorLocalIterator;
57
58// ABAB: As opposed to SpParMat, IT here is used to encode global size and global indices;
59// therefore it can not be 32-bits, in general.
60template <class IT, class NT>
61class FullyDistVec: public FullyDist<IT,NT, typename combblas::disable_if< combblas::is_boolean<NT>::value, NT >::type >
62{
63public:
64 FullyDistVec ( );
65 FullyDistVec ( IT globallen, NT initval);
66 FullyDistVec ( std::shared_ptr<CommGrid> grid);
67 FullyDistVec ( std::shared_ptr<CommGrid> grid, IT globallen, NT initval);
68 FullyDistVec ( const FullyDistSpVec<IT, NT> & rhs ); // Sparse -> Dense conversion constructor
69 FullyDistVec ( const std::vector<NT> & fillarr, std::shared_ptr<CommGrid> grid ); // initialize a FullyDistVec with a vector of length n/p from each processor
70
71
72 template <class ITRHS, class NTRHS>
73 FullyDistVec ( const FullyDistVec<ITRHS, NTRHS>& rhs ); // type converter constructor
74
76 {
77 public:
78 NT getNoNum(IT index) { return static_cast<NT>(1); }
79
80 template <typename c, typename t>
81 NT read(std::basic_istream<c,t>& is, IT index)
82 {
83 NT v;
84 is >> v;
85 return v;
86 }
87
88 template <typename c, typename t>
89 void save(std::basic_ostream<c,t>& os, const NT& v, IT index)
90 {
91 os << v;
92 }
93 };
94
95 template <class HANDLER>
96 void ParallelWrite(const std::string & filename, bool onebased, HANDLER handler, bool includeindices = true)
97 {
98 FullyDistSpVec<IT,NT> tmpSpVec = *this; // delegate
99 tmpSpVec.ParallelWrite(filename, onebased, handler, includeindices);
100 }
101 void ParallelWrite(const std::string & filename, bool onebased, bool includeindices = true) { ParallelWrite(filename, onebased, ScalarReadSaveHandler(), includeindices); };
102
103
104 template <typename _BinaryOperation>
105 void ParallelRead (const std::string & filename, bool onebased, _BinaryOperation BinOp)
106 {
107 FullyDistSpVec<IT,NT> tmpSpVec = *this; // delegate
108 tmpSpVec.ParallelRead(filename, onebased, BinOp);
109 *this = tmpSpVec; // sparse -> dense conversion
110 }
111
112 template <class HANDLER>
113 std::ifstream& ReadDistribute (std::ifstream& infile, int master, HANDLER handler);
114 std::ifstream& ReadDistribute (std::ifstream& infile, int master) { return ReadDistribute(infile, master, ScalarReadSaveHandler()); }
115
116 template <class HANDLER>
117 void SaveGathered(std::ofstream& outfile, int master, HANDLER handler, bool printProcSplits = false);
118 void SaveGathered(std::ofstream& outfile, int master) { SaveGathered(outfile, master, ScalarReadSaveHandler(), false); }
119
120
121 template <class ITRHS, class NTRHS>
122 FullyDistVec<IT,NT> & operator=(const FullyDistVec< ITRHS,NTRHS > & rhs); // assignment with type conversion
125
126 FullyDistVec<IT,NT> & operator=(NT fixedval) // assign fixed value
127 {
128#ifdef _OPENMP
129#pragma omp parallel for
130#endif
131 for(IT i=0; i < arr.size(); ++i)
132 arr[i] = fixedval;
133 return *this;
134 }
135 FullyDistVec<IT,NT> operator() (const FullyDistVec<IT,IT> & ri) const; //<! subsref
136
141 bool operator==(const FullyDistVec<IT,NT> & rhs) const;
142
143 void SetElement (IT indx, NT numx); // element-wise assignment
144 void SetLocalElement(IT index, NT value) { arr[index] = value; }; // no checks, local index
145 NT GetElement (IT indx) const; // element-wise fetch
146 NT operator[](IT indx) const // more c++ like API
147 {
148 return GetElement(indx);
149 }
150
151 void Set(const FullyDistSpVec< IT,NT > & rhs);
152 template <class NT1, typename _BinaryOperationIdx, typename _BinaryOperationVal>
153 void GSet (const FullyDistSpVec<IT,NT1> & spVec, _BinaryOperationIdx __binopIdx, _BinaryOperationVal __binopVal, MPI_Win win);
154 template <class NT1, typename _BinaryOperationIdx>
155 FullyDistSpVec<IT,NT> GGet (const FullyDistSpVec<IT,NT1> & spVec, _BinaryOperationIdx __binopIdx, NT nullValue);
156
157 void iota(IT globalsize, NT first);
158 void RandPerm(); // randomly permute the vector
159 FullyDistVec<IT,IT> sort(); // sort and return the permutation
160
161 using FullyDist<IT,NT,typename combblas::disable_if< combblas::is_boolean<NT>::value, NT >::type>::LengthUntil;
162 using FullyDist<IT,NT,typename combblas::disable_if< combblas::is_boolean<NT>::value, NT >::type>::TotalLength;
164 using FullyDist<IT,NT,typename combblas::disable_if< combblas::is_boolean<NT>::value, NT >::type>::MyLocLength;
165 IT LocArrSize() const { return arr.size(); } // = MyLocLength() once arr is resized
166 //TODO: we should change this function and return the vector directly
167 const NT * GetLocArr() const { return arr.data(); } // = MyLocLength() once arr is resized
168
169 const std::vector<NT>& GetLocVec() const { return arr; }
170
171 template <typename _Predicate>
172 FullyDistSpVec<IT,NT> Find(_Predicate pred) const;
173
174 FullyDistSpVec<IT,NT> Find(NT val) const;
175
176 template <typename _Predicate>
177 FullyDistVec<IT,IT> FindInds(_Predicate pred) const;
178
179 template <typename _Predicate>
180 IT Count(_Predicate pred) const;
181
182 template <typename _UnaryOperation>
183 void Apply(_UnaryOperation __unary_op)
184 {
185 std::transform(arr.begin(), arr.end(), arr.begin(), __unary_op);
186 }
187
188 template <typename _BinaryOperation>
189 void ApplyInd(_BinaryOperation __binary_op)
190 {
191 IT offset = LengthUntil();
192 #ifdef _OPENMP
193 #pragma omp parallel for
194 #endif
195 for(size_t i=0; i < arr.size(); ++i)
196 arr[i] = __binary_op(arr[i], i + offset);
197 }
198
199 template <typename _UnaryOperation, typename IRRELEVANT_NT>
200 void Apply(_UnaryOperation __unary_op, const FullyDistSpVec<IT,IRRELEVANT_NT>& mask);
201
202 // extended callback versions
203 template <typename _BinaryOperation, typename _BinaryPredicate, class NT2>
204 void EWiseApply(const FullyDistVec<IT,NT2> & other, _BinaryOperation __binary_op, _BinaryPredicate _do_op, const bool useExtendedBinOp);
205 template <typename _BinaryOperation, typename _BinaryPredicate, class NT2>
206 void EWiseApply(const FullyDistSpVec<IT,NT2> & other, _BinaryOperation __binary_op, _BinaryPredicate _do_op, bool applyNulls, NT2 nullValue, const bool useExtendedBinOp);
207
208 // plain fallback versions
209 template <typename _BinaryOperation, typename _BinaryPredicate, class NT2>
210 void EWiseApply(const FullyDistVec<IT,NT2> & other, _BinaryOperation __binary_op, _BinaryPredicate _do_op)
211 {
212 EWiseApply(other,
215 true);
216 }
217 template <typename _BinaryOperation, typename _BinaryPredicate, class NT2>
218 void EWiseApply(const FullyDistSpVec<IT,NT2> & other, _BinaryOperation __binary_op, _BinaryPredicate _do_op, bool applyNulls, NT2 nullValue)
219 {
220 EWiseApply(other,
223 applyNulls, nullValue, true);
224 }
225
226
227 template <typename T1, typename T2>
228 class retTrue {
229 public:
230 bool operator()(const T1& x, const T2& y)
231 {
232 return true;
233 }
234 };
235
236 template <typename _BinaryOperation, class NT2>
237 void EWiseApply(const FullyDistVec<IT,NT2> & other, _BinaryOperation __binary_op)
238 {
239 this->EWiseApply(other, __binary_op, retTrue<NT, NT2>());
240 }
241 template <typename _BinaryOperation, class NT2>
242 void EWiseApply(const FullyDistSpVec<IT,NT2> & other, _BinaryOperation __binary_op, bool applyNulls, NT2 nullValue)
243 {
244 this->EWiseApply(other, __binary_op, retTrue<NT, NT2>(), applyNulls, nullValue);
245 }
246
247 void PrintToFile(std::string prefix)
248 {
249 std::ofstream output;
250 commGrid->OpenDebugFile(prefix, output);
251 std::copy(arr.begin(), arr.end(), std::ostream_iterator<NT> (output, " "));
252 output << std::endl;
253 output.close();
254 }
255
256 void PrintInfo(std::string vectorname) const;
257 void DebugPrint();
258 std::shared_ptr<CommGrid> getcommgrid() const { return commGrid; }
259
260 std::pair<IT, NT> MinElement() const; // returns <index, value> pair of global minimum
261
262
263 template <typename _BinaryOperation>
264 NT Reduce(_BinaryOperation __binary_op, NT identity) const;
265
266 template <typename OUT, typename _BinaryOperation, typename _UnaryOperation>
267 OUT Reduce(_BinaryOperation __binary_op, OUT default_val, _UnaryOperation __unary_op) const;
268
269 void SelectCandidates(double nver);
270
271 template <typename _BinaryOperation, typename OUT = typename std::result_of<_BinaryOperation&(NT,NT)>::type>
272 void EWiseOut(const FullyDistVec<IT,NT> & rhs, _BinaryOperation __binary_op, FullyDistVec<IT,OUT> & result);
273
276
277private:
278 std::vector< NT > arr;
279
280 template <typename _BinaryOperation>
281 void EWise(const FullyDistVec<IT,NT> & rhs, _BinaryOperation __binary_op);
282
283 template <class IU, class NU>
284 friend class DenseParMat;
285
286 template <class IU, class NU, class UDER>
287 friend class SpParMat;
288
289 template <class IU, class NU>
290 friend class FullyDistVec;
291
292 template <class IU, class NU>
293 friend class FullyDistSpVec;
294
295 template <class IU, class NU>
297
298 template <typename SR, typename IU, typename NUM, typename NUV, typename UDER>
300 SpMV (const SpParMat<IU,NUM,UDER> & A, const FullyDistVec<IU,NUV> & x );
301
302 template <typename IU, typename NU1, typename NU2>
304 EWiseMult (const FullyDistSpVec<IU,NU1> & V, const FullyDistVec<IU,NU2> & W , bool exclude, NU2 zero);
305
306 template <typename IU, typename NU1, typename NU2, typename _BinaryOperation>
308 EWiseApply (const FullyDistSpVec<IU,NU1> & V, const FullyDistVec<IU,NU2> & W , _BinaryOperation _binary_op, typename promote_trait<NU1,NU2>::T_promote zero);
309
310 template <typename RET, typename IU, typename NU1, typename NU2, typename _BinaryOperation, typename _BinaryPredicate>
312 EWiseApply (const FullyDistSpVec<IU,NU1> & V, const FullyDistVec<IU,NU2> & W , _BinaryOperation _binary_op, _BinaryPredicate _doOp, bool allowVNulls, NU1 Vzero, const bool useExtendedBinOp);
313
314 template <typename RET, typename IU, typename NU1, typename NU2, typename _BinaryOperation, typename _BinaryPredicate>
316 EWiseApply_threaded (const FullyDistSpVec<IU,NU1> & V, const FullyDistVec<IU,NU2> & W , _BinaryOperation _binary_op, _BinaryPredicate _doOp, bool allowVNulls, NU1 Vzero, const bool useExtendedBinOp);
317
318 template <typename IU>
319 friend void RenameVertices(DistEdgeList<IU> & DEL);
320
321 template <typename IU, typename NU>
322 friend FullyDistVec<IU,NU> Concatenate ( std::vector< FullyDistVec<IU,NU> > & vecs);
323
324 template <typename IU, typename NU>
327 template <class IU, class DER>
328 friend SpParMat<IU, bool, DER> PermMat (const FullyDistVec<IU,IU> & ri, const IU ncol);
329
330
332};
333
334}
335
336#include "FullyDistVec.cpp"
337
338#endif
339
int64_t IT
double NT
void ParallelWrite(const std::string &filename, bool onebased, HANDLER handler, bool includeindices=true, bool includeheader=false)
void ParallelRead(const std::string &filename, bool onebased, _BinaryOperation BinOp)
void save(std::basic_ostream< c, t > &os, const NT &v, IT index)
Definition: FullyDistVec.h:89
NT read(std::basic_istream< c, t > &is, IT index)
Definition: FullyDistVec.h:81
bool operator()(const T1 &x, const T2 &y)
Definition: FullyDistVec.h:230
FullyDistVec< IT, IT > FindInds(_Predicate pred) const
Return the indices where pred is true.
friend FullyDistSpVec< IU, RET > EWiseApply_threaded(const FullyDistSpVec< IU, NU1 > &V, const FullyDistVec< IU, NU2 > &W, _BinaryOperation _binary_op, _BinaryPredicate _doOp, bool allowVNulls, NU1 Vzero, const bool useExtendedBinOp)
Definition: ParFriends.h:2334
void ParallelRead(const std::string &filename, bool onebased, _BinaryOperation BinOp)
Definition: FullyDistVec.h:105
friend FullyDistVec< IU, typename promote_trait< NUM, NUV >::T_promote > SpMV(const SpParMat< IU, NUM, UDER > &A, const FullyDistVec< IU, NUV > &x)
Definition: ParFriends.h:1926
void ParallelWrite(const std::string &filename, bool onebased, bool includeindices=true)
Definition: FullyDistVec.h:101
void EWiseApply(const FullyDistSpVec< IT, NT2 > &other, _BinaryOperation __binary_op, bool applyNulls, NT2 nullValue)
Definition: FullyDistVec.h:242
std::ifstream & ReadDistribute(std::ifstream &infile, int master, HANDLER handler)
void EWiseApply(const FullyDistVec< IT, NT2 > &other, _BinaryOperation __binary_op, _BinaryPredicate _do_op, const bool useExtendedBinOp)
NT Reduce(_BinaryOperation __binary_op, NT identity) const
friend FullyDistSpVec< IU, typename promote_trait< NU1, NU2 >::T_promote > EWiseApply(const FullyDistSpVec< IU, NU1 > &V, const FullyDistVec< IU, NU2 > &W, _BinaryOperation _binary_op, typename promote_trait< NU1, NU2 >::T_promote zero)
FullyDistVec< IT, NT > operator()(const FullyDistVec< IT, IT > &ri) const
void EWiseApply(const FullyDistSpVec< IT, NT2 > &other, _BinaryOperation __binary_op, _BinaryPredicate _do_op, bool applyNulls, NT2 nullValue)
Definition: FullyDistVec.h:218
void EWiseApply(const FullyDistVec< IT, NT2 > &other, _BinaryOperation __binary_op, _BinaryPredicate _do_op)
Definition: FullyDistVec.h:210
void ParallelWrite(const std::string &filename, bool onebased, HANDLER handler, bool includeindices=true)
Definition: FullyDistVec.h:96
FullyDistSpVec< IT, NT > Find(_Predicate pred) const
Return the elements for which pred is true.
bool operator==(const FullyDistVec< IT, NT > &rhs) const
void PrintInfo(std::string vectorname) const
void SaveGathered(std::ofstream &outfile, int master, HANDLER handler, bool printProcSplits=false)
void ApplyInd(_BinaryOperation __binary_op)
Definition: FullyDistVec.h:189
FullyDistVec< IT, NT > & operator-=(const FullyDistSpVec< IT, NT > &rhs)
friend SpParMat< IU, bool, DER > PermMat(const FullyDistVec< IU, IU > &ri, const IU ncol)
void SetElement(IT indx, NT numx)
void iota(IT globalsize, NT first)
std::shared_ptr< CommGrid > getcommgrid() const
Definition: FullyDistVec.h:258
friend void RenameVertices(DistEdgeList< IU > &DEL)
NT operator[](IT indx) const
Definition: FullyDistVec.h:146
void Set(const FullyDistSpVec< IT, NT > &rhs)
friend class FullyDistVec
Definition: FullyDistVec.h:290
FullyDistSpVec< IT, NT > GGet(const FullyDistSpVec< IT, NT1 > &spVec, _BinaryOperationIdx __binopIdx, NT nullValue)
IT Count(_Predicate pred) const
Return the number of elements for which pred is true.
FullyDistVec< IT, NT > & operator+=(const FullyDistSpVec< IT, NT > &rhs)
FullyDistVec< IT, NT > & operator=(NT fixedval)
Definition: FullyDistVec.h:126
friend FullyDistSpVec< IU, typename promote_trait< NU1, NU2 >::T_promote > EWiseMult(const FullyDistSpVec< IU, NU1 > &V, const FullyDistVec< IU, NU2 > &W, bool exclude, NU2 zero)
Definition: ParFriends.h:2245
void SelectCandidates(double nver)
ABAB: Put concept check, NT should be integer for this to make sense.
const std::vector< NT > & GetLocVec() const
Definition: FullyDistVec.h:169
friend void Augment(FullyDistVec< int64_t, int64_t > &mateRow2Col, FullyDistVec< int64_t, int64_t > &mateCol2Row, FullyDistVec< int64_t, int64_t > &parentsRow, FullyDistVec< int64_t, int64_t > &leaves)
void Apply(_UnaryOperation __unary_op)
Definition: FullyDistVec.h:183
void EWiseOut(const FullyDistVec< IT, NT > &rhs, _BinaryOperation __binary_op, FullyDistVec< IT, OUT > &result)
void EWiseApply(const FullyDistVec< IT, NT2 > &other, _BinaryOperation __binary_op)
Definition: FullyDistVec.h:237
friend void maximumMatching(SpParMat< int64_t, bool, SpDCCols< int64_t, bool > > &A, FullyDistVec< int64_t, int64_t > &mateRow2Col, FullyDistVec< int64_t, int64_t > &mateCol2Row)
void PrintToFile(std::string prefix)
Definition: FullyDistVec.h:247
void SaveGathered(std::ofstream &outfile, int master)
Definition: FullyDistVec.h:118
void SetLocalElement(IT index, NT value)
Definition: FullyDistVec.h:144
const NT * GetLocArr() const
Definition: FullyDistVec.h:167
void GSet(const FullyDistSpVec< IT, NT1 > &spVec, _BinaryOperationIdx __binopIdx, _BinaryOperationVal __binopVal, MPI_Win win)
std::ifstream & ReadDistribute(std::ifstream &infile, int master)
Definition: FullyDistVec.h:114
NT GetElement(IT indx) const
friend FullyDistVec< IU, NU > Concatenate(std::vector< FullyDistVec< IU, NU > > &vecs)
FullyDistVec< IT, NT > & operator=(const FullyDistVec< ITRHS, NTRHS > &rhs)
std::pair< IT, NT > MinElement() const
FullyDistVec< IT, IT > sort()
bool output
Definition: comms.cpp:56
long int64_t
Definition: compat.h:21
Definition: CCGrid.h:4
double A