COMBINATORIAL_BLAS 1.6
 
Loading...
Searching...
No Matches
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
100 }
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>
154 template <class NT1, typename _BinaryOperationIdx>
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>
173
174 FullyDistSpVec<IT,NT> Find(NT val) const;
175
176 template <typename _Predicate>
178
179 template <typename _Predicate>
180 IT Count(_Predicate pred) const;
181
182 template <typename _UnaryOperation>
184 {
185 std::transform(arr.begin(), arr.end(), arr.begin(), __unary_op);
186 }
187
188 template <typename _BinaryOperation>
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>
201
202 // extended callback versions
203 template <typename _BinaryOperation, typename _BinaryPredicate, class NT2>
205 template <typename _BinaryOperation, typename _BinaryPredicate, class NT2>
207
208 // plain fallback versions
209 template <typename _BinaryOperation, typename _BinaryPredicate, class NT2>
217 template <typename _BinaryOperation, typename _BinaryPredicate, class NT2>
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>
241 template <typename _BinaryOperation, class NT2>
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>
265
266 template <typename OUT, typename _BinaryOperation, typename _UnaryOperation>
268
269 void SelectCandidates(double nver);
270
271 template <typename _BinaryOperation, typename OUT = typename std::result_of<_BinaryOperation&(NT,NT)>::type>
273
276
277private:
278 std::vector< NT > arr;
279
280 template <typename _BinaryOperation>
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>
301
302 template <typename IU, typename NU1, typename NU2>
305
306 template <typename IU, typename NU1, typename NU2, typename _BinaryOperation>
309
310 template <typename RET, typename IU, typename NU1, typename NU2, typename _BinaryOperation, typename _BinaryPredicate>
313
314 template <typename RET, typename IU, typename NU1, typename NU2, typename _BinaryOperation, typename _BinaryPredicate>
317
318 template <typename IU>
319 friend void RenameVertices(DistEdgeList<IU> & DEL);
320
321 template <typename IU, typename NU>
323
324 template <typename IU, typename NU>
327 template <class IU, class DER>
329
330
332};
333
334}
335
336#include "FullyDistVec.cpp"
337
338#endif
339
int64_t IT
double NT
void save(std::basic_ostream< c, t > &os, const NT &v, IT index)
NT read(std::basic_istream< c, t > &is, IT index)
bool operator()(const T1 &x, const T2 &y)
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)
void ParallelRead(const std::string &filename, bool onebased, _BinaryOperation BinOp)
friend FullyDistVec< IU, typename promote_trait< NUM, NUV >::T_promote > SpMV(const SpParMat< IU, NUM, UDER > &A, const FullyDistVec< IU, NUV > &x)
void ParallelWrite(const std::string &filename, bool onebased, bool includeindices=true)
void EWiseApply(const FullyDistSpVec< IT, NT2 > &other, _BinaryOperation __binary_op, bool applyNulls, NT2 nullValue)
std::ifstream & ReadDistribute(std::ifstream &infile, int master, HANDLER handler)
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)
void EWiseApply(const FullyDistVec< IT, NT2 > &other, _BinaryOperation __binary_op, _BinaryPredicate _do_op)
void ParallelWrite(const std::string &filename, bool onebased, HANDLER handler, bool includeindices=true)
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)
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
friend void RenameVertices(DistEdgeList< IU > &DEL)
NT operator[](IT indx) const
void Set(const FullyDistSpVec< IT, NT > &rhs)
friend class FullyDistVec
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)
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)
void SelectCandidates(double nver)
ABAB: Put concept check, NT should be integer for this to make sense.
const std::vector< NT > & GetLocVec() const
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)
void EWiseOut(const FullyDistVec< IT, NT > &rhs, _BinaryOperation __binary_op, FullyDistVec< IT, OUT > &result)
void EWiseApply(const FullyDistVec< IT, NT2 > &other, _BinaryOperation __binary_op)
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)
void SaveGathered(std::ofstream &outfile, int master)
void SetLocalElement(IT index, NT value)
const NT * GetLocArr() const
void GSet(const FullyDistSpVec< IT, NT1 > &spVec, _BinaryOperationIdx __binopIdx, _BinaryOperationVal __binopVal, MPI_Win win)
std::ifstream & ReadDistribute(std::ifstream &infile, int master)
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
double A