Compressed Sparse Blocks  1.2
 All Classes Files Functions Variables Typedefs Friends Macros Pages
sym.h
Go to the documentation of this file.
1 #ifndef _SYM_H
2 #define _SYM_H
3 
4 #include <cmath>
5 #include "csc.h"
6 using namespace std;
7 
8 // Disclaimer: This class heavily uses bitwise operations for indexing. Prefer ITYPE to be "unsigned"
9 // The >> operator in C and C++ is not necessarily an arithmetic shift.
10 // Usually it is only an arithmetic shift if used on a signed integer type;
11 // but if it is used on an unsigned integer type, it will be a logical shift
12 
13 template <class T, class ITYPE>
14 class Sym
15 {
16 public:
17  Sym ():nz(0), m(0), n(0), ntop(0), nbc(0), nbr(0) {} // default constructor (dummy)
18 
19  Sym (ITYPE size,ITYPE rows, ITYPE cols);
20  Sym (const Sym<T, ITYPE> & rhs); // copy constructor
21  ~Sym();
22  Sym<T,ITYPE> & operator=(const Sym<T,ITYPE> & rhs); // assignment operator
23  Sym (Csc<T, ITYPE> & csc);
24 
25  ITYPE colsize() const { return n;}
26  ITYPE rowsize() const { return m;}
27  ITYPE getntop() const { return ntop; }
28 
29  void Transpose();
30 
31 private:
32  void Init();
33  void SubSpMV(ITYPE * btop, ITYPE bstart, ITYPE bend, const T * x, T * suby) const;
34  void BMult(ITYPE * btop, ITYPE bstart, ITYPE bend, const T * x, T * y, ITYPE ysize) const;
35  static void BlockPrefetch(void * addr, int total, int ssize);
36 
37  ITYPE ** top ; // pointers array (indexed by higher-order bits of the coordinate index), size ~= ntop+1
38  ITYPE * bot; // contains lower-order bits of the coordinate index, size nnz
39  T * num; // contains numerical values, size nnz
40 
41  ITYPE nz; // # nonzeros
42  ITYPE m; // # rows
43  ITYPE n; // # columns
44  ITYPE ntop; // size of top array ~= sqrt(mn)
45 
46  ITYPE nbc; // #{column blocks} = #{blocks in any block row}
47  ITYPE nbr; // #{block rows)
48 
49  ITYPE rowlowbits; // # lower order bits for rows
50  ITYPE rowhighbits;
51  ITYPE highrowmask; // mask with the first log(m)/2 bits = 1 and the other bits = 0
52  ITYPE lowrowmask;
53 
54  ITYPE collowbits; // # lower order bits for columns
55  ITYPE colhighbits;
56  ITYPE highcolmask; // mask with the first log(n)/2 bits = 1 and the other bits = 0
57  ITYPE lowcolmask;
58 
59  static const int CACHEBLOCK = 16; // block prefetch blocks of 16 ints/doubles
60  static int p_fetch; // anchor variable to fool the C optimizer
61 
62  template <typename U, typename UTYPE>
63  friend void sym_gaxpy (const Sym<U, UTYPE> & A, const U * x, U * y);
64 };
65 
66 
67 // const int* pX; --> changeable pointer to constant int
68 // int* const pY; --> constant pointer to changeable int
69 
70 // Operation y = A*x+y
71 template <typename T, typename ITYPE>
72 void sym_gaxpy (const Sym<T, ITYPE> & A, const T * x, T * y)
73 {
74  // Some bitwise algebra for reference
75  // If b = (a & mask) >> shift, then how to recover a back?
76  // Easy... a = (b << shift) | (~mask)
77 
78  ITYPE ysize = A.lowrowmask + 1; // size of the output subarray (per block row)
79 
80  cilk_for (ITYPE i = 0 ; i < A.nbr ; ++i) // for all blocks rows of A
81  {
82  ITYPE * btop = A.top [i]; // get the pointer to this block row
83  ITYPE rhi = ((i << A.rowlowbits) & A.highrowmask);
84  T * suby = &y[rhi];
85 
86 #ifdef PAR2D
87  A.BMult(btop, 0, A.nbc, x, suby, ysize);
88 #else
89  A.SubSpMV(btop, 0, A.nbc, x, suby);
90 #endif
91  }
92 }
93 
94 
95 #include "sym.cpp" // Template member function definitions need to be known to the compiler
96 #endif
ITYPE rowsize() const
Definition: sym.h:26
Definition: sym.h:14
void sym_gaxpy(const Sym< T, ITYPE > &A, const T *x, T *y)
Definition: sym.h:72
ITYPE colsize() const
Definition: sym.h:25
ITYPE getntop() const
Definition: sym.h:27
Definition: csc.h:15
Sym()
Definition: sym.h:17