COMBINATORIAL_BLAS 1.6
 
Loading...
Searching...
No Matches
SpDCCols.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 _SP_DCCOLS_H
31#define _SP_DCCOLS_H
32
33#include <iostream>
34#include <fstream>
35#include <cmath>
36#include "SpMat.h" // Best to include the base class first
37#include "SpHelper.h"
38#include "StackEntry.h"
39#include "dcsc.h"
40#include "Isect.h"
41#include "Semirings.h"
42#include "MemoryPool.h"
43#include "LocArr.h"
44#include "Friends.h"
45#include "CombBLAS.h"
46#include "FullyDist.h"
47
48namespace combblas {
49
50template <class IT, class NT>
51class SpDCCols: public SpMat<IT, NT, SpDCCols<IT, NT> >
52{
53public:
54 typedef IT LocalIT;
55 typedef NT LocalNT;
56
57 // Constructors :
61 SpDCCols (IT nRow, IT nCol, IT nnz1, const std::tuple<IT, IT, NT> * rhs, bool transpose);
62
63 SpDCCols (const SpDCCols<IT,NT> & rhs); // Actual copy constructor
65
66 template <typename NNT> operator SpDCCols<IT,NNT> () const;
67 template <typename NIT, typename NNT> operator SpDCCols<NIT,NNT> () const;
68
69 // Member Functions and Operators:
73 SpDCCols<IT,NT> operator() (const std::vector<IT> & ri, const std::vector<IT> & ci) const;
74 bool operator== (const SpDCCols<IT, NT> & rhs) const
75 {
76 if(rhs.nnz == 0 && nnz == 0)
77 return true;
78 if(nnz != rhs.nnz || m != rhs.m || n != rhs.n)
79 return false;
80 return ((*dcsc) == (*(rhs.dcsc)));
81 }
82
83
84 class SpColIter
85 {
86 public:
87 class NzIter
88 {
89 public:
90 NzIter(IT * ir = NULL, NT * num = NULL) : rid(ir), val(num) {}
91
92 bool operator==(const NzIter & other)
93 {
94 return(rid == other.rid); // compare pointers
95 }
96 bool operator!=(const NzIter & other)
97 {
98 return(rid != other.rid);
99 }
100 bool operator<(const NzIter & other)
101 {
102 return(rid < other.rid);
103 }
105 {
106 rid+=inc;
107 val+=inc;
108 return(*this);
109 }
111 {
112 rid-=inc;
113 val-=inc;
114 return(*this);
115 }
116 NzIter & operator++() // prefix operator
117 {
118 ++rid;
119 ++val;
120 return(*this);
121 }
122 NzIter operator++(int) // postfix operator
123 {
124 NzIter tmp(*this);
125 ++(*this);
126 return(tmp);
127 }
129 {
130 return (*rid);
131 }
133 {
134 return (*val);
135 }
136 private:
137 IT * rid;
138 NT * val;
139
140 };
141
142 SpColIter(IT * cp = NULL, IT * jc = NULL) : cptr(cp), cid(jc) {}
143 bool operator==(const SpColIter& other)
144 {
145 return(cptr == other.cptr); // compare pointers
146 }
147 bool operator!=(const SpColIter& other)
148 {
149 return(cptr != other.cptr);
150 }
151
152 SpColIter& operator++() // prefix operator
153 {
154 ++cptr;
155 ++cid;
156 return(*this);
157 }
158 SpColIter operator++(int) // postfix operator (common)
159 {
160 SpColIter tmp(*this);
161 ++(*this);
162 return(tmp);
163 }
165 {
166 cptr+=inc;
167 cid+=inc;
168 return(*this);
169 }
171 {
172 cptr-=inc;
173 cid-=inc;
174 return(*this);
175 }
177 {
178 return (*cid);
179 }
180 IT colptr() const
181 {
182 return (*cptr);
183 }
185 {
186 return (*(cptr+1));
187 }
188 IT nnz() const
189 {
190 return (colptrnext() - colptr());
191 }
192 private:
193 IT * cptr;
194 IT * cid;
195 };
196
198 {
199 if( nnz > 0 )
200 return SpColIter(dcsc->cp, dcsc->jc);
201 else
202 return SpColIter(NULL, NULL);
203 }
204 SpColIter begcol(int i) // multithreaded version
205 {
206 if( dcscarr[i] )
207 return SpColIter(dcscarr[i]->cp, dcscarr[i]->jc);
208 else
209 return SpColIter(NULL, NULL);
210 }
211
213 {
214 if( nnz > 0 )
215 return SpColIter(dcsc->cp + dcsc->nzc, NULL);
216 else
217 return SpColIter(NULL, NULL);
218 }
219
220 SpColIter endcol(int i) //multithreaded version
221 {
222 if( dcscarr[i] )
223 return SpColIter(dcscarr[i]->cp + dcscarr[i]->nzc, NULL);
224 else
225 return SpColIter(NULL, NULL);
226 }
227
229 {
230 return typename SpColIter::NzIter( dcsc->ir + ccol.colptr(), dcsc->numx + ccol.colptr() );
231 }
232
234 {
235 return typename SpColIter::NzIter( dcsc->ir + ccol.colptrnext(), NULL );
236 }
237
238 typename SpColIter::NzIter begnz(const SpColIter & ccol, int i)
239 {
240 return typename SpColIter::NzIter( dcscarr[i]->ir + ccol.colptr(), dcscarr[i]->numx + ccol.colptr() );
241 }
242
243 typename SpColIter::NzIter endnz(const SpColIter & ccol, int i)
244 {
245 return typename SpColIter::NzIter( dcscarr[i]->ir + ccol.colptrnext(), NULL );
246 }
247
248 template <typename _UnaryOperation>
250 {
251 if(nnz > 0)
252 dcsc->Apply(__unary_op);
253 }
254
255 template <typename _UnaryOperation, typename GlobalIT>
257 template <typename _UnaryOperation>
259 template <typename _BinaryOperation>
261 template <typename _BinaryOperation>
263
264 void PruneColumnByIndex(const std::vector<IT>& ci);
265
266 template <typename _BinaryOperation>
268 {
269 if(nnz > 0 && dcsc != NULL)
270 dcsc->UpdateDense(array, __binary_op);
271 }
272
274 void EWiseMult (const SpDCCols<IT,NT> & rhs, bool exclude);
276
277 void Transpose();
280
282 {
283 BooleanRowSplit(*this, numsplits); // only works with boolean arrays
284 }
285
286 void ColSplit(int parts, std::vector< SpDCCols<IT,NT> > & matrices);
287 void ColSplit(int parts, std::vector< SpDCCols<IT,NT>* > & matrices);
288 void ColSplit(std::vector<IT> & cutSizes, std::vector< SpDCCols<IT,NT> > & matrices);
289 void ColSplit(std::vector<IT> & cutSizes, std::vector< SpDCCols<IT,NT>* > & matrices);
290 void ColConcatenate(std::vector< SpDCCols<IT,NT> > & matrices);
291 void ColConcatenate(std::vector< SpDCCols<IT,NT>* > & matrices);
292
295
296 void CreateImpl(const std::vector<IT> & essentials);
297 void CreateImpl(IT size, IT nRow, IT nCol, std::tuple<IT, IT, NT> * mytuples);
298 void CreateImpl(IT * _cp, IT * _jc, IT * _ir, NT * _numx, IT _nz, IT _nzc, IT _m, IT _n);
299
300
302 std::vector<IT> GetEssentials() const;
303 const static IT esscount;
304
305 bool isZero() const { return (nnz == 0); }
306 IT getnrow() const { return m; }
307 IT getncol() const { return n; }
308 IT getnnz() const { return nnz; }
309 IT getnzc() const { return (nnz == 0) ? 0: dcsc->nzc; }
310 int getnsplit() const { return splits; }
311
312 std::ofstream& put(std::ofstream & outfile) const;
313 std::ifstream& get(std::ifstream & infile);
314 void PrintInfo() const;
315 void PrintInfo(std::ofstream & out) const;
316
317 template <typename SR>
319
320 template <typename SR>
322
323 template <typename SR>
325
326 template <typename SR>
328
329 Dcsc<IT, NT> * GetDCSC() const // only for single threaded matrices
330 {
331 return dcsc;
332 }
333
334 Dcsc<IT, NT> * GetDCSC(int i) const // only for split (multithreaded) matrices
335 {
336 return dcscarr[i];
337 }
338
339 auto GetInternal() const { return GetDCSC(); }
340 auto GetInternal(int i) const { return GetDCSC(i); }
341
342
343private:
344 void CopyDcsc(Dcsc<IT,NT> * source);
345 SpDCCols<IT,NT> ColIndex(const std::vector<IT> & ci) const;
346
347 template <typename SR, typename NTR>
349
350 template <typename SR, typename NTR>
352
353 SpDCCols (IT size, IT nRow, IT nCol, const std::vector<IT> & indices, bool isRow); // Constructor for indexing
354 SpDCCols (IT nRow, IT nCol, Dcsc<IT,NT> * mydcsc); // Constructor for multiplication
355
356 // Anonymous union
357 union {
360 };
361
362 IT m;
363 IT n;
364 IT nnz;
365
366 int splits; // for multithreading
367
368 template <class IU, class NU>
369 friend class SpDCCols; // Let other template instantiations (of the same class) access private members
370
371 template <class IU, class NU>
372 friend class SpTuples;
373
374 // AL: removed this because it appears illegal and causes this compiler warning:
375 // warning: dependent nested name specifier 'SpDCCols<IU, NU>::' for friend class declaration is not supported; turning off access control for 'SpDCCols'
376 //template <class IU, class NU>
377 //friend class SpDCCols<IU, NU>::SpColIter;
378
379 template<typename IU>
380 friend void BooleanRowSplit(SpDCCols<IU, bool> & A, int numsplits);
381
382 template<typename IU, typename NU1, typename NU2>
384
385 template<typename N_promote, typename IU, typename NU1, typename NU2, typename _BinaryOperation>
387
388 template <typename RETT, typename IU, typename NU1, typename NU2, typename _BinaryOperation, typename _BinaryPredicate>
390
391 template<class SR, class NUO, class IU, class NU1, class NU2>
393 (const SpDCCols<IU, NU1> & A, const SpDCCols<IU, NU2> & B, bool clearA, bool clearB);
394
395 template<class SR, class NUO, class IU, class NU1, class NU2>
397 (const SpDCCols<IU, NU1> & A, const SpDCCols<IU, NU2> & B, bool clearA, bool clearB);
398
399 template<class SR, class NUO, class IU, class NU1, class NU2>
401 (const SpDCCols<IU, NU1> & A, const SpDCCols<IU, NU2> & B, bool clearA, bool clearB);
402
403 template<class SR, class NUO, class IU, class NU1, class NU2>
405 (const SpDCCols<IU, NU1> & A, const SpDCCols<IU, NU2> & B, bool clearA, bool clearB);
406
407 template <typename SR, typename IU, typename NU, typename RHS, typename LHS>
408 friend void dcsc_gespmv (const SpDCCols<IU, NU> & A, const RHS * x, LHS * y);
409
410 template <typename SR, typename IU, typename NU, typename RHS, typename LHS>
411 friend void dcsc_gespmv_threaded (const SpDCCols<IU, NU> & A, const RHS * x, LHS * y);
412
413 template <typename SR, typename IU, typename NU, typename RHS, typename LHS>
414 friend void dcsc_gespmv_threaded_nosplit (const SpDCCols<IU, NU> & A, const RHS * x, LHS * y);
415
416 template <typename SR, typename IU, typename NUM, typename DER, typename IVT, typename OVT>
417 friend int generic_gespmv_threaded (const SpMat<IU,NUM,DER> & A, const int32_t * indx, const IVT * numx, int32_t nnzx,
418 int32_t * & sendindbuf, OVT * & sendnumbuf, int * & sdispls, int p_c);
419};
420
421// At this point, complete type of of SpDCCols is known, safe to declare these specialization (but macros won't work as they are preprocessed)
422// General case #1: When both NT is the same
423template <class IT, class NT> struct promote_trait< SpDCCols<IT,NT> , SpDCCols<IT,NT> >
424 {
426 };
427// General case #2: First is boolean the second is anything except boolean (to prevent ambiguity)
428template <class IT, class NT> struct promote_trait< SpDCCols<IT,bool> , SpDCCols<IT,NT>, typename combblas::disable_if< combblas::is_boolean<NT>::value >::type >
429 {
431 };
432// General case #3: Second is boolean the first is anything except boolean (to prevent ambiguity)
433template <class IT, class NT> struct promote_trait< SpDCCols<IT,NT> , SpDCCols<IT,bool>, typename combblas::disable_if< combblas::is_boolean<NT>::value >::type >
434 {
436 };
437template <class IT> struct promote_trait< SpDCCols<IT,int> , SpDCCols<IT,float> >
438 {
440 };
441
442template <class IT> struct promote_trait< SpDCCols<IT,float> , SpDCCols<IT,int> >
443 {
445 };
446template <class IT> struct promote_trait< SpDCCols<IT,int> , SpDCCols<IT,double> >
447 {
449 };
450template <class IT> struct promote_trait< SpDCCols<IT,double> , SpDCCols<IT,int> >
451 {
453 };
454
455
456// Below are necessary constructs to be able to define a SpMat<NT,IT> where
457// all we know is DER (say SpDCCols<int, double>) and NT,IT
458// in other words, we infer the templated SpDCCols<> type
459// This is not a type conversion from an existing object,
460// but a type inference for the newly created object
461// NIT: New IT, NNT: New NT
462template <class DER, class NIT, class NNT>
463struct create_trait
464{
465 // none
466};
467
468// Capture everything of the form SpDCCols<OIT, ONT>
469// it may come as a surprise that the partial specializations can
470// involve more template parameters than the primary template
471template <class NIT, class NNT, class OIT, class ONT>
472struct create_trait< SpDCCols<OIT, ONT> , NIT, NNT >
473{
475};
476
477}
478
479#include "SpDCCols.cpp"
480
481#endif
int64_t IT
double NT
Definition test.cpp:53
Iterate over the nonzeros of the sparse column.
Definition SpDCCols.h:88
NzIter(IT *ir=NULL, NT *num=NULL)
Definition SpDCCols.h:90
bool operator!=(const NzIter &other)
Definition SpDCCols.h:96
bool operator==(const NzIter &other)
Definition SpDCCols.h:92
IT rowid() const
< Return the "local" rowid of the current nonzero entry.
Definition SpDCCols.h:128
bool operator<(const NzIter &other)
Definition SpDCCols.h:100
Iterate over (sparse) columns of the sparse matrix.
Definition SpDCCols.h:85
SpColIter operator+(IT inc)
Definition SpDCCols.h:164
IT colid() const
< Return the "local" colid of the current column.
Definition SpDCCols.h:176
SpColIter(IT *cp=NULL, IT *jc=NULL)
Definition SpDCCols.h:142
bool operator!=(const SpColIter &other)
Definition SpDCCols.h:147
SpColIter operator-(IT inc)
Definition SpDCCols.h:170
bool operator==(const SpColIter &other)
Definition SpDCCols.h:143
friend SpTuples< IU, NUO > * Tuples_AtXBt(const SpDCCols< IU, NU1 > &A, const SpDCCols< IU, NU2 > &B, bool clearA, bool clearB)
Definition Friends.h:633
SpDCCols< IT, NT > & operator=(const SpDCCols< IT, NT > &rhs)
Definition SpDCCols.cpp:365
SpDCCols(IT nRow, IT nCol, IT nnz1, const std::tuple< IT, IT, NT > *rhs, bool transpose)
void EWiseMult(const SpDCCols< IT, NT > &rhs, bool exclude)
friend class SpDCCols
Definition SpDCCols.h:369
void PrintInfo() const
SpDCCols< IT, NT > * PruneI(_UnaryOperation __unary_op, bool inPlace, GlobalIT rowOffset, GlobalIT colOffset)
void Apply(_UnaryOperation __unary_op)
Definition SpDCCols.h:249
IT getnrow() const
Definition SpDCCols.h:306
std::ofstream & put(std::ofstream &outfile) const
SpColIter endcol(int i)
Definition SpDCCols.h:220
SpDCCols(const SpTuples< IT, NT > &rhs, bool transpose)
void CreateImpl(IT size, IT nRow, IT nCol, std::tuple< IT, IT, NT > *mytuples)
std::ifstream & get(std::ifstream &infile)
void ColSplit(int parts, std::vector< SpDCCols< IT, NT > > &matrices)
Dcsc< IT, NT > * GetDCSC() const
Definition SpDCCols.h:329
void ColSplit(std::vector< IT > &cutSizes, std::vector< SpDCCols< IT, NT > > &matrices)
int PlusEq_AnXBn(const SpDCCols< IT, NT > &A, const SpDCCols< IT, NT > &B)
friend void dcsc_gespmv_threaded_nosplit(const SpDCCols< IU, NU > &A, const RHS *x, LHS *y)
SpMV with dense vector (multithreaded version)
Definition Friends.h:82
void PruneColumnByIndex(const std::vector< IT > &ci)
SpColIter::NzIter begnz(const SpColIter &ccol)
Definition SpDCCols.h:228
friend SpTuples< IU, NUO > * Tuples_AnXBn(const SpDCCols< IU, NU1 > &A, const SpDCCols< IU, NU2 > &B, bool clearA, bool clearB)
Definition Friends.h:609
void ColConcatenate(std::vector< SpDCCols< IT, NT > > &matrices)
SpDCCols< IT, NT > * Prune(_UnaryOperation __unary_op, bool inPlace)
SpDCCols< IT, NT > * PruneColumn(IT *pinds, NT *pvals, _BinaryOperation __binary_op, bool inPlace)
void ColSplit(std::vector< IT > &cutSizes, std::vector< SpDCCols< IT, NT > * > &matrices)
Dcsc< IT, NT > * dcsc
Definition SpDCCols.h:358
static const IT esscount
Definition SpDCCols.h:303
friend SpDCCols< IU, N_promote > EWiseApply(const SpDCCols< IU, NU1 > &A, const SpDCCols< IU, NU2 > &B, _BinaryOperation __binary_op, bool notB, const NU2 &defaultBVal)
Definition Friends.h:1036
SpColIter::NzIter begnz(const SpColIter &ccol, int i)
Definition SpDCCols.h:238
SpColIter::NzIter endnz(const SpColIter &ccol, int i)
Definition SpDCCols.h:243
void UpdateDense(NT **array, _BinaryOperation __binary_op) const
Definition SpDCCols.h:267
std::vector< IT > GetEssentials() const
bool isZero() const
Definition SpDCCols.h:305
void Merge(SpDCCols< IT, NT > &partA, SpDCCols< IT, NT > &partB)
Dcsc< IT, NT > * GetDCSC(int i) const
Definition SpDCCols.h:334
void ColSplit(int parts, std::vector< SpDCCols< IT, NT > * > &matrices)
int PlusEq_AnXBt(const SpDCCols< IT, NT > &A, const SpDCCols< IT, NT > &B)
int getnsplit() const
Definition SpDCCols.h:310
void PrintInfo(std::ofstream &out) const
SpDCCols< IT, NT > operator()(IT ri, IT ci) const
friend int generic_gespmv_threaded(const SpMat< IU, NUM, DER > &A, const int32_t *indx, const IVT *numx, int32_t nnzx, int32_t *&sendindbuf, OVT *&sendnumbuf, int *&sdispls, int p_c)
IT getnnz() const
Definition SpDCCols.h:308
SpDCCols< IT, NT > & operator+=(const SpDCCols< IT, NT > &rhs)
Definition SpDCCols.cpp:394
SpDCCols< IT, NT > TransposeConst() const
Const version, doesn't touch the existing object.
void SetDifference(const SpDCCols< IT, NT > &rhs)
void Transpose()
Mutator version, replaces the calling object.
friend SpTuples< IU, NUO > * Tuples_AtXBn(const SpDCCols< IU, NU1 > &A, const SpDCCols< IU, NU2 > &B, bool clearA, bool clearB)
Definition Friends.h:646
SpDCCols(const SpDCCols< IT, NT > &rhs)
int PlusEq_AtXBt(const SpDCCols< IT, NT > &A, const SpDCCols< IT, NT > &B)
void EWiseScale(NT **scaler, IT m_scaler, IT n_scaler)
SpColIter endcol()
Definition SpDCCols.h:212
Dcsc< IT, NT > ** dcscarr
Definition SpDCCols.h:359
SpColIter::NzIter endnz(const SpColIter &ccol)
Definition SpDCCols.h:233
friend SpDCCols< IU, typename promote_trait< NU1, NU2 >::T_promote > EWiseMult(const SpDCCols< IU, NU1 > &A, const SpDCCols< IU, NU2 > &B, bool exclude)
Definition Friends.h:1011
void CreateImpl(const std::vector< IT > &essentials)
auto GetInternal() const
Definition SpDCCols.h:339
SpDCCols< IT, NT > * TransposeConstPtr() const
Arr< IT, NT > GetArrays() const
void ColConcatenate(std::vector< SpDCCols< IT, NT > * > &matrices)
friend void dcsc_gespmv_threaded(const SpDCCols< IU, NU > &A, const RHS *x, LHS *y)
Definition Friends.h:140
SpColIter begcol()
Definition SpDCCols.h:197
friend SpTuples< IU, NUO > * Tuples_AnXBt(const SpDCCols< IU, NU1 > &A, const SpDCCols< IU, NU2 > &B, bool clearA, bool clearB)
Definition Friends.h:566
void RowSplit(int numsplits)
Definition SpDCCols.h:281
SpDCCols< IT, NT > * PruneColumn(NT *pvals, _BinaryOperation __binary_op, bool inPlace)
auto GetInternal(int i) const
Definition SpDCCols.h:340
friend void dcsc_gespmv(const SpDCCols< IU, NU > &A, const RHS *x, LHS *y)
SpMV with dense vector.
Definition Friends.h:64
bool operator==(const SpDCCols< IT, NT > &rhs) const
Definition SpDCCols.h:74
IT getnzc() const
Definition SpDCCols.h:309
int PlusEq_AtXBn(const SpDCCols< IT, NT > &A, const SpDCCols< IT, NT > &B)
SpColIter begcol(int i)
Definition SpDCCols.h:204
friend void BooleanRowSplit(SpDCCols< IU, bool > &A, int numsplits)
Definition Friends.h:482
void CreateImpl(IT *_cp, IT *_jc, IT *_ir, NT *_numx, IT _nz, IT _nzc, IT _m, IT _n)
void Split(SpDCCols< IT, NT > &partA, SpDCCols< IT, NT > &partB)
IT getncol() const
Definition SpDCCols.h:307
SpDCCols(IT size, IT nRow, IT nCol, IT nzc)
int size
Definition common.h:20
double A
signed int int32_t
Definition stdint.h:77