COMBINATORIAL_BLAS 1.6
 
Loading...
Searching...
No Matches
FullyDistSpVec.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_SP_VEC_H_
31#define _FULLY_DIST_SP_VEC_H_
32
33#include <iostream>
34#include <vector>
35#include <utility>
36#include "CommGrid.h"
37#include "promote.h"
38#include "SpParMat.h"
39#include "FullyDist.h"
40#include "Exception.h"
41#include "OptBuf.h"
42#include "CombBLAS.h"
43
44namespace combblas {
45
46template <class IT, class NT, class DER>
47class SpParMat;
48
49template <class IT>
50class DistEdgeList;
51
52template <class IU, class NU>
53class FullyDistVec;
54
55template <class IU, class NU>
56class SparseVectorLocalIterator;
57
72template <class IT, class NT>
73class FullyDistSpVec: public FullyDist<IT,NT,typename combblas::disable_if< combblas::is_boolean<NT>::value, NT >::type>
74{
75public:
77 explicit FullyDistSpVec ( IT glen );
78 FullyDistSpVec ( std::shared_ptr<CommGrid> grid);
79 FullyDistSpVec ( std::shared_ptr<CommGrid> grid, IT glen);
80
81 template <typename _UnaryOperation>
83 FullyDistSpVec (const FullyDistVec<IT,NT> & rhs); // Conversion copy-constructor
85 FullyDistSpVec (std::shared_ptr<CommGrid> grid, IT globallen, const std::vector<IT>& indvec, const std::vector<NT> & numvec, bool SumDuplicates = false, bool sorted=false);
86
87 IT NnzUntil() const;
88
90 template <typename _BinaryOperationIdx, typename _BinaryOperationVal, typename _BinaryOperationDuplicate>
92 template <typename _BinaryOperationIdx, typename _BinaryOperationVal>
94
95
96 template <typename NT1, typename _UnaryOperation>
98 template <typename _UnaryOperation>
100 template <typename NT1>
101 void Setminus (const FullyDistSpVec<IT,NT1> & other);
102
103 //template <typename NT1, typename _UnaryOperation>
104 //void Set (FullyDistSpVec<IT,NT1> Selector, _UnaryOperation __unop);
105
106 template <typename NT1, typename _UnaryOperation, typename _BinaryOperation>
108
109
110
115 FullyDistSpVec<IT,NT> & operator=(const FullyDistVec< IT,NT > & rhs); // convert from dense
117 {
118#ifdef _OPENMP
119#pragma omp parallel for
120#endif
121 for(size_t i=0; i < ind.size(); ++i)
122 num[i] = fixedval;
123 return *this;
124 }
127
129 {
130 public:
131 NT getNoNum(IT index) { return static_cast<NT>(1); }
132
133 template <typename c, typename t>
134 NT read(std::basic_istream<c,t>& is, IT index)
135 {
136 NT v;
137 is >> v;
138 return v;
139 }
140
141 template <typename c, typename t>
142 void save(std::basic_ostream<c,t>& os, const NT& v, IT index)
143 {
144 os << v;
145 }
146 };
147
148 template <class HANDLER>
149 void ParallelWrite(const std::string & filename, bool onebased, HANDLER handler, bool includeindices = true, bool includeheader = false);
151
152
153 template <typename _BinaryOperation>
154 void ParallelRead (const std::string & filename, bool onebased, _BinaryOperation BinOp);
155
156
158 template <class HANDLER>
159 std::ifstream& ReadDistribute (std::ifstream& infile, int master, HANDLER handler);
160 std::ifstream& ReadDistribute (std::ifstream& infile, int master) { return ReadDistribute(infile, master, ScalarReadSaveHandler()); }
161
162 template <class HANDLER>
163 void SaveGathered(std::ofstream& outfile, int master, HANDLER handler, bool printProcSplits = false);
165
166
167 template <typename NNT> operator FullyDistSpVec< IT,NNT > () const
168 {
169 FullyDistSpVec<IT,NNT> CVT(commGrid);
170 CVT.ind = std::vector<IT>(ind.begin(), ind.end());
171 CVT.num = std::vector<NNT>(num.begin(), num.end());
172 CVT.glen = glen;
173 return CVT;
174 }
175
177 {
178 FullyDistVec<IT,NT> v = *this;
180 return (v == w);
181 }
182
183 void PrintInfo(std::string vecname) const;
184 void iota(IT globalsize, NT first);
185 void nziota(NT first);
187 void SetElement (IT indx, NT numx); // element-wise assignment
188 void DelElement (IT indx); // element-wise deletion
190 bool WasFound() const { return wasFound; }
191
194
195#if __cplusplus > 199711L
196 template <typename _BinaryOperation = minimum<NT> >
198#else
199 template <typename _BinaryOperation >
201#endif
202
203 // Aydin TODO: parallelize with OpenMP
204 template <typename _UnaryOperation>
205 FullyDistSpVec<IT,NT> Prune(_UnaryOperation __unary_op, bool inPlace = true) //<! Prune any nonzero entries for which the __unary_op evaluates to true (solely based on value)
206 {
207 FullyDistSpVec<IT,NT> temp(commGrid);
208 IT spsize = ind.size();
209 for(IT i=0; i< spsize; ++i)
210 {
211 if(!(__unary_op(num[i]))) // keep this nonzero
212 {
213 temp.ind.push_back(ind[i]);
214 temp.num.push_back(num[i]);
215 }
216 }
217
218 if (inPlace)
219 {
220 ind.swap(temp.ind);
221 ind.swap(temp.num);
222
223 return FullyDistSpVec<IT,NT>(commGrid); // return blank to match signature
224 }
225 else
226 {
227 return temp;
228 }
229 }
230
231
232 IT getlocnnz() const
233 {
234 return ind.size();
235 }
236 IT getnnz() const
237 {
238 IT totnnz = 0;
239 IT locnnz = ind.size();
240 MPI_Allreduce( &locnnz, &totnnz, 1, MPIType<IT>(), MPI_SUM, commGrid->GetWorld());
241 return totnnz;
242 }
243 using FullyDist<IT,NT,typename combblas::disable_if< combblas::is_boolean<NT>::value, NT >::type>::LengthUntil;
244 using FullyDist<IT,NT,typename combblas::disable_if< combblas::is_boolean<NT>::value, NT >::type>::MyLocLength;
245 using FullyDist<IT,NT,typename combblas::disable_if< combblas::is_boolean<NT>::value, NT >::type>::MyRowLength;
246 using FullyDist<IT,NT,typename combblas::disable_if< combblas::is_boolean<NT>::value, NT >::type>::TotalLength;
248 using FullyDist<IT,NT,typename combblas::disable_if< combblas::is_boolean<NT>::value, NT >::type>::RowLenUntil;
249
251 {
252 IT offset = LengthUntil();
253 IT spsize = ind.size();
254 #ifdef _OPENMP
255 #pragma omp parallel for
256 #endif
257 for(IT i=0; i< spsize; ++i)
258 num[i] = ind[i] + offset;
259 }
260
261 template <typename _Predicate>
262 IT Count(_Predicate pred) const;
263
264 template <typename _UnaryOperation>
266 {
267 //transform(num.begin(), num.end(), num.begin(), __unary_op);
268 IT spsize = num.size();
269#ifdef _OPENMP
270#pragma omp parallel for
271#endif
272 for(IT i=0; i < spsize; ++i)
273 num[i] = __unary_op(num[i]);
274 }
275
276 template <typename _BinaryOperation>
278 {
279 IT offset = LengthUntil();
280 IT spsize = ind.size();
281 #ifdef _OPENMP
282 #pragma omp parallel for
283 #endif
284 for(IT i=0; i < spsize; ++i)
285 num[i] = __binary_op(num[i], ind[i] + offset);
286 }
287
288
289
290 template <typename _BinaryOperation>
292
293 template <typename OUT, typename _BinaryOperation, typename _UnaryOperation>
295
296 void DebugPrint();
297 std::shared_ptr<CommGrid> getcommgrid() const { return commGrid; }
298
299 void Reset();
301 void BulkSet(IT inds[], int count);
302 std::vector<IT> GetLocalInd (){std::vector<IT> rind = ind; return rind;};
303 std::vector<NT> GetLocalNum (){std::vector<NT> rnum = num; return rnum;};
304
305 template <typename _Predicate>
307 template <typename _Predicate>
309
310
311protected:
314
315private:
316 std::vector< IT > ind; // ind.size() give the number of nonzeros
317 std::vector< NT > num;
318 bool wasFound; // true if the last GetElement operation returned an actual value
319
320
321 template <typename _BinaryOperation>
322 void SparseCommon(std::vector< std::vector < std::pair<IT,NT> > > & data, _BinaryOperation BinOp);
323
324
325#if __cplusplus > 199711L
326 template <typename _BinaryOperation = minimum<NT> >
328#else
329 template <typename _BinaryOperation >
331#endif
332
333
334 template <class IU, class NU>
335 friend class FullyDistSpVec;
336
337 template <class IU, class NU>
338 friend class FullyDistVec;
339
340 template <class IU, class NU, class UDER>
341 friend class SpParMat;
342
343 template <class IU, class NU>
345
346 template <typename SR, typename IU, typename NUM, typename NUV, typename UDER>
349
350 template <typename SR, typename IU, typename NUM, typename UDER>
353
354 template <typename VT, typename IU, typename UDER> // NoSR version (in BFSFriends.h)
356
357 template <typename SR, typename IVT, typename OVT, typename IU, typename NUM, typename UDER>
359
360 template <typename SR, typename IVT, typename OVT, typename IU, typename NUM, typename UDER>
362
363 template <typename IU, typename NU1, typename NU2>
366
367 template <typename RET, typename IU, typename NU1, typename NU2, typename _BinaryOperation, typename _BinaryPredicate>
370
371 template <typename RET, typename IU, typename NU1, typename NU2, typename _BinaryOperation, typename _BinaryPredicate>
374
375
376 template <typename RET, typename IU, typename NU1, typename NU2, typename _BinaryOperation, typename _BinaryPredicate>
379
380 template <typename IU>
381 friend void RandPerm(FullyDistSpVec<IU,IU> & V); // called on an existing object, randomly permutes it
382
383 template <typename IU>
384 friend void RenameVertices(DistEdgeList<IU> & DEL);
385
387 // Ariful: I made this an internal function in ParFriends.h
388 //template <typename SR, typename IU, typename OVT>
389 //friend void MergeContributions(FullyDistSpVec<IU,OVT> & y, int * & recvcnt, int * & rdispls, int32_t * & recvindbuf, OVT * & recvnumbuf, int rowneighs);
390
391 template <typename IU, typename VT>
392 friend void MergeContributions(FullyDistSpVec<IU,VT> & y, int * & recvcnt, int * & rdispls, int32_t * & recvindbuf, VT * & recvnumbuf, int rowneighs);
393
394 template<typename IU, typename NV>
396
397 template <class IU, class NU, class DER, typename _UnaryOperation>
399};
400
401}
402
403#include "FullyDistSpVec.cpp"
404
405#endif
int64_t IT
double NT
int init
Definition auction.cpp:16
NT read(std::basic_istream< c, t > &is, IT index)
void save(std::basic_ostream< c, t > &os, const NT &v, IT index)
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)
friend FullyDistSpVec< IU, VT > SpMV(const SpParMat< IU, bool, UDER > &A, const FullyDistSpVec< IU, VT > &x, OptBuf< int32_t, VT > &optbuf)
FullyDistSpVec< IT, NT > & operator=(const FullyDistSpVec< IT, NT > &rhs)
void Select(const FullyDistVec< IT, NT1 > &denseVec, _UnaryOperation unop)
FullyDistSpVec< IT, IT > sort()
sort the vector itself, return the permutation vector (0-based)
std::ifstream & ReadDistribute(std::ifstream &infile, int master, HANDLER handler)
Totally obsolete version that only accepts an ifstream object and ascii files.
FullyDistSpVec< IT, NT > & operator-=(const FullyDistSpVec< IT, NT > &rhs)
NT Reduce(_BinaryOperation __binary_op, NT init) const
FullyDistSpVec< IT, NT > Uniq(_BinaryOperation __binary_op, MPI_Op mympiop)
FullyDistSpVec< IT, NT > Prune(_UnaryOperation __unary_op, bool inPlace=true)
friend FullyDistSpVec< IU, typename promote_trait< NUM, NUV >::T_promote > SpMV(const SpParMat< IU, NUM, UDER > &A, const FullyDistSpVec< IU, NUV > &x)
std::vector< NT > GetLocalNum()
void Apply(_UnaryOperation __unary_op)
FullyDistVec< IT, NT > FindVals(_Predicate pred) const
friend void RandPerm(FullyDistSpVec< IU, IU > &V)
void nziota(NT first)
iota over existing nonzero entries
void ParallelWrite(const std::string &filename, bool onebased, HANDLER handler, bool includeindices=true, bool includeheader=false)
void FilterByVal(FullyDistSpVec< IT, IT > Selector, _UnaryOperation __unop, bool filterByIndex)
FullyDistSpVec< IT, NT > Invert(IT globallen)
std::shared_ptr< CommGrid > getcommgrid() const
void SelectApply(const FullyDistVec< IT, NT1 > &denseVec, _UnaryOperation __unop, _BinaryOperation __binop)
void ParallelWrite(const std::string &filename, bool onebased, bool includeindices=true)
IT Count(_Predicate pred) const
Return the number of elements for which pred is true.
FullyDistVec< IT, NT > operator()(const FullyDistVec< IT, IT > &ri) const
SpRef (expects ri to be 0-based)
void SaveGathered(std::ofstream &outfile, int master)
FullyDistSpVec< IT, NT > & operator=(NT fixedval)
void ParallelRead(const std::string &filename, bool onebased, _BinaryOperation BinOp)
bool operator==(const FullyDistSpVec< IT, NT > &rhs) const
std::ifstream & ReadDistribute(std::ifstream &infile, int master)
friend void RenameVertices(DistEdgeList< IU > &DEL)
FullyDistVec< IT, IT > FindInds(_Predicate pred) const
void SaveGathered(std::ofstream &outfile, int master, HANDLER handler, bool printProcSplits=false)
friend FullyDistSpVec< IU, RET > EWiseApply(const FullyDistSpVec< IU, NU1 > &V, const FullyDistVec< IU, NU2 > &W, _BinaryOperation _binary_op, _BinaryPredicate _doOp, bool allowVNulls, NU1 Vzero, const bool useExtendedBinOp)
IT NnzUntil() const
Returns the number of nonzeros until this processor.
void BulkSet(IT inds[], int count)
friend void TransposeVector(MPI_Comm &World, const FullyDistSpVec< IU, NV > &x, int32_t &trxlocnz, IU &lenuntil, int32_t *&trxinds, NV *&trxnums, bool indexisvalue)
void ApplyInd(_BinaryOperation __binary_op)
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)
FullyDistSpVec< IT, NT > & operator+=(const FullyDistSpVec< IT, NT > &rhs)
std::vector< IT > GetLocalInd()
void PrintInfo(std::string vecname) const
void Setminus(const FullyDistSpVec< IT, NT1 > &other)
friend SpParMat< IU, bool, DER > PermMat1(const FullyDistSpVec< IU, NU > &ri, const IU ncol, _UnaryOperation __unop)
void iota(IT globalsize, NT first)
void SetElement(IT indx, NT numx)
Indexing is performed 0-based.
friend void MergeContributions(FullyDistSpVec< IU, VT > &y, int *&recvcnt, int *&rdispls, int32_t *&recvindbuf, VT *&recvnumbuf, int rowneighs)
Helper functions for sparse matrix X sparse vector.
Definition BFSFriends.h:224
void stealFrom(FullyDistSpVec< IT, NT > &victim)
FullyDistSpVec< IT, NT > InvertRMA(IT globallen, _BinaryOperationIdx __binopIdx, _BinaryOperationVal __binopVal)
double A
signed int int32_t
Definition stdint.h:77