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 :
58 SpDCCols ();
59 SpDCCols (IT size, IT nRow, IT nCol, IT nzc);
60 SpDCCols (const SpTuples<IT,NT> & rhs, bool transpose);
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
64 ~SpDCCols();
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 template <typename _BinaryOperation>
266 {
267 if(nnz > 0 && dcsc != NULL)
268 dcsc->UpdateDense(array, __binary_op);
269 }
270
272 void EWiseMult (const SpDCCols<IT,NT> & rhs, bool exclude);
273
274 void Transpose();
277
279 {
280 BooleanRowSplit(*this, numsplits); // only works with boolean arrays
281 }
282
283 void ColSplit(int parts, std::vector< SpDCCols<IT,NT> > & matrices);
284 void ColConcatenate(std::vector< SpDCCols<IT,NT> > & matrices);
285
288
289 void CreateImpl(const std::vector<IT> & essentials);
290 void CreateImpl(IT size, IT nRow, IT nCol, std::tuple<IT, IT, NT> * mytuples);
291 void CreateImpl(IT * _cp, IT * _jc, IT * _ir, NT * _numx, IT _nz, IT _nzc, IT _m, IT _n);
292
293
294 Arr<IT,NT> GetArrays() const;
295 std::vector<IT> GetEssentials() const;
296 const static IT esscount;
297
298 bool isZero() const { return (nnz == 0); }
299 IT getnrow() const { return m; }
300 IT getncol() const { return n; }
301 IT getnnz() const { return nnz; }
302 IT getnzc() const { return (nnz == 0) ? 0: dcsc->nzc; }
303 int getnsplit() const { return splits; }
304
305 std::ofstream& put(std::ofstream & outfile) const;
306 std::ifstream& get(std::ifstream & infile);
307 void PrintInfo() const;
308 void PrintInfo(std::ofstream & out) const;
309
310 template <typename SR>
311 int PlusEq_AtXBt(const SpDCCols<IT,NT> & A, const SpDCCols<IT,NT> & B);
312
313 template <typename SR>
314 int PlusEq_AtXBn(const SpDCCols<IT,NT> & A, const SpDCCols<IT,NT> & B);
315
316 template <typename SR>
317 int PlusEq_AnXBt(const SpDCCols<IT,NT> & A, const SpDCCols<IT,NT> & B);
318
319 template <typename SR>
320 int PlusEq_AnXBn(const SpDCCols<IT,NT> & A, const SpDCCols<IT,NT> & B);
321
322 Dcsc<IT, NT> * GetDCSC() const // only for single threaded matrices
323 {
324 return dcsc;
325 }
326
327 Dcsc<IT, NT> * GetDCSC(int i) const // only for split (multithreaded) matrices
328 {
329 return dcscarr[i];
330 }
331
332 auto GetInternal() const { return GetDCSC(); }
333 auto GetInternal(int i) const { return GetDCSC(i); }
334
335
336private:
337 void CopyDcsc(Dcsc<IT,NT> * source);
338 SpDCCols<IT,NT> ColIndex(const std::vector<IT> & ci) const;
339
340 template <typename SR, typename NTR>
342
343 template <typename SR, typename NTR>
345
346 SpDCCols (IT size, IT nRow, IT nCol, const std::vector<IT> & indices, bool isRow); // Constructor for indexing
347 SpDCCols (IT nRow, IT nCol, Dcsc<IT,NT> * mydcsc); // Constructor for multiplication
348
349 // Anonymous union
350 union {
353 };
354
355 IT m;
356 IT n;
357 IT nnz;
358
359 int splits; // for multithreading
360
361 template <class IU, class NU>
362 friend class SpDCCols; // Let other template instantiations (of the same class) access private members
363
364 template <class IU, class NU>
365 friend class SpTuples;
366
367 // AL: removed this because it appears illegal and causes this compiler warning:
368 // warning: dependent nested name specifier 'SpDCCols<IU, NU>::' for friend class declaration is not supported; turning off access control for 'SpDCCols'
369 //template <class IU, class NU>
370 //friend class SpDCCols<IU, NU>::SpColIter;
371
372 template<typename IU>
373 friend void BooleanRowSplit(SpDCCols<IU, bool> & A, int numsplits);
374
375 template<typename IU, typename NU1, typename NU2>
377
378 template<typename N_promote, typename IU, typename NU1, typename NU2, typename _BinaryOperation>
380
381 template <typename RETT, typename IU, typename NU1, typename NU2, typename _BinaryOperation, typename _BinaryPredicate>
383
384 template<class SR, class NUO, class IU, class NU1, class NU2>
386 (const SpDCCols<IU, NU1> & A, const SpDCCols<IU, NU2> & B, bool clearA, bool clearB);
387
388 template<class SR, class NUO, class IU, class NU1, class NU2>
390 (const SpDCCols<IU, NU1> & A, const SpDCCols<IU, NU2> & B, bool clearA, bool clearB);
391
392 template<class SR, class NUO, class IU, class NU1, class NU2>
394 (const SpDCCols<IU, NU1> & A, const SpDCCols<IU, NU2> & B, bool clearA, bool clearB);
395
396 template<class SR, class NUO, class IU, class NU1, class NU2>
398 (const SpDCCols<IU, NU1> & A, const SpDCCols<IU, NU2> & B, bool clearA, bool clearB);
399
400 template <typename SR, typename IU, typename NU, typename RHS, typename LHS>
401 friend void dcsc_gespmv (const SpDCCols<IU, NU> & A, const RHS * x, LHS * y);
402
403 template <typename SR, typename IU, typename NU, typename RHS, typename LHS>
404 friend void dcsc_gespmv_threaded (const SpDCCols<IU, NU> & A, const RHS * x, LHS * y);
405
406 template <typename SR, typename IU, typename NUM, typename DER, typename IVT, typename OVT>
407 friend int generic_gespmv_threaded (const SpMat<IU,NUM,DER> & A, const int32_t * indx, const IVT * numx, int32_t nnzx,
408 int32_t * & sendindbuf, OVT * & sendnumbuf, int * & sdispls, int p_c);
409};
410
411// At this point, complete type of of SpDCCols is known, safe to declare these specialization (but macros won't work as they are preprocessed)
412// General case #1: When both NT is the same
413template <class IT, class NT> struct promote_trait< SpDCCols<IT,NT> , SpDCCols<IT,NT> >
414 {
416 };
417// General case #2: First is boolean the second is anything except boolean (to prevent ambiguity)
418template <class IT, class NT> struct promote_trait< SpDCCols<IT,bool> , SpDCCols<IT,NT>, typename combblas::disable_if< combblas::is_boolean<NT>::value >::type >
419 {
421 };
422// General case #3: Second is boolean the first is anything except boolean (to prevent ambiguity)
423template <class IT, class NT> struct promote_trait< SpDCCols<IT,NT> , SpDCCols<IT,bool>, typename combblas::disable_if< combblas::is_boolean<NT>::value >::type >
424 {
426 };
427template <class IT> struct promote_trait< SpDCCols<IT,int> , SpDCCols<IT,float> >
428 {
430 };
431
432template <class IT> struct promote_trait< SpDCCols<IT,float> , SpDCCols<IT,int> >
433 {
435 };
436template <class IT> struct promote_trait< SpDCCols<IT,int> , SpDCCols<IT,double> >
437 {
439 };
440template <class IT> struct promote_trait< SpDCCols<IT,double> , SpDCCols<IT,int> >
441 {
443 };
444
445
446// Below are necessary constructs to be able to define a SpMat<NT,IT> where
447// all we know is DER (say SpDCCols<int, double>) and NT,IT
448// in other words, we infer the templated SpDCCols<> type
449// This is not a type conversion from an existing object,
450// but a type inference for the newly created object
451// NIT: New IT, NNT: New NT
452template <class DER, class NIT, class NNT>
454{
455 // none
456};
457
458// Capture everything of the form SpDCCols<OIT, ONT>
459// it may come as a surprise that the partial specializations can
460// involve more template parameters than the primary template
461template <class NIT, class NNT, class OIT, class ONT>
466
467}
468
469#include "SpDCCols.cpp"
470
471#endif
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:578
SpDCCols< IT, NT > & operator=(const SpDCCols< IT, NT > &rhs)
Definition SpDCCols.cpp:365
friend class SpDCCols
Definition SpDCCols.h:362
void PrintInfo() const
SpDCCols< IT, NT > * PruneI(_UnaryOperation __unary_op, bool inPlace, GlobalIT rowOffset, GlobalIT colOffset)
Definition SpDCCols.cpp:431
void Apply(_UnaryOperation __unary_op)
Definition SpDCCols.h:249
IT getnrow() const
Definition SpDCCols.h:299
std::ofstream & put(std::ofstream &outfile) const
SpColIter endcol(int i)
Definition SpDCCols.h:220
std::ifstream & get(std::ifstream &infile)
void ColSplit(int parts, std::vector< SpDCCols< IT, NT > > &matrices)
Definition SpDCCols.cpp:897
Dcsc< IT, NT > * GetDCSC() const
Definition SpDCCols.h:322
int PlusEq_AnXBn(const SpDCCols< IT, NT > &A, const SpDCCols< IT, NT > &B)
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:554
void ColConcatenate(std::vector< SpDCCols< IT, NT > > &matrices)
Definition SpDCCols.cpp:939
SpDCCols< IT, NT > * Prune(_UnaryOperation __unary_op, bool inPlace)
Definition SpDCCols.cpp:478
Dcsc< IT, NT > * dcsc
Definition SpDCCols.h:351
static const IT esscount
Definition SpDCCols.h:296
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:960
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:265
std::vector< IT > GetEssentials() const
Definition SpDCCols.cpp:748
bool isZero() const
Definition SpDCCols.h:298
void Merge(SpDCCols< IT, NT > &partA, SpDCCols< IT, NT > &partB)
Definition SpDCCols.cpp:990
Dcsc< IT, NT > * GetDCSC(int i) const
Definition SpDCCols.h:327
int PlusEq_AnXBt(const SpDCCols< IT, NT > &A, const SpDCCols< IT, NT > &B)
int getnsplit() const
Definition SpDCCols.h:303
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:301
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.
Definition SpDCCols.cpp:838
void Transpose()
Mutator version, replaces the calling object.
Definition SpDCCols.cpp:815
friend SpTuples< IU, NUO > * Tuples_AtXBn(const SpDCCols< IU, NU1 > &A, const SpDCCols< IU, NU2 > &B, bool clearA, bool clearB)
Definition Friends.h:591
int PlusEq_AtXBt(const SpDCCols< IT, NT > &A, const SpDCCols< IT, NT > &B)
void EWiseScale(NT **scaler, IT m_scaler, IT n_scaler)
Definition SpDCCols.cpp:663
SpColIter endcol()
Definition SpDCCols.h:212
Dcsc< IT, NT > ** dcscarr
Definition SpDCCols.h:352
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:935
void CreateImpl(const std::vector< IT > &essentials)
Definition SpDCCols.cpp:695
auto GetInternal() const
Definition SpDCCols.h:332
SpDCCols< IT, NT > * TransposeConstPtr() const
Definition SpDCCols.cpp:852
Arr< IT, NT > GetArrays() const
Definition SpDCCols.cpp:787
friend void dcsc_gespmv_threaded(const SpDCCols< IU, NU > &A, const RHS *x, LHS *y)
SpMV with dense vector (multithreaded version)
Definition Friends.h:82
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:511
void RowSplit(int numsplits)
Definition SpDCCols.h:278
SpDCCols< IT, NT > * PruneColumn(NT *pvals, _BinaryOperation __binary_op, bool inPlace)
Definition SpDCCols.cpp:527
auto GetInternal(int i) const
Definition SpDCCols.h:333
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:302
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:439
void Split(SpDCCols< IT, NT > &partA, SpDCCols< IT, NT > &partB)
Definition SpDCCols.cpp:867
IT getncol() const
Definition SpDCCols.h:300
int size
Definition common.h:20
double A
double B
Definition options.h:15
signed int int32_t
Definition stdint.h:77