36 #include "Triplet.hpp"
37 #include "SparseMatrix.hpp"
45 template<
typename T,
int number_of_diagonals,
int diagonal_offsets[] >
51 typedef unsigned long int ULI;
92 load( input, m, n, zero );
110 for( ULI k=0; k<number_of_diagonals; k++ ) {
111 if( diagonal_offsets[ k ] < 0 ) {
112 this->
nnz += m+diagonal_offsets[ k ] < n ? m+diagonal_offsets[ k ] :
n;
114 this->
nnz += n-diagonal_offsets[ k ] < m ? m-diagonal_offsets[ k ] :
m;
119 full = m > n ? m-n+1 : n-m+1;
122 bytes = 2 *
sizeof(ULI) +
sizeof(T**);
123 bytes += number_of_diagonals *
sizeof(T*);
124 bytes += number_of_diagonals *
d *
sizeof(T);
135 this->
nnz = input.size();
137 nzs =
new T*[ number_of_diagonals ];
138 bytes = number_of_diagonals *
sizeof(T*) + 2 *
sizeof(ULI) +
sizeof(T**);
140 full = m > n ? m-n+1 : n-m+1;
143 ULI *diags =
new ULI[ m + n - 1 ];
144 for( ULI r=0; r<m+n-1; r++ ) {
147 for( ULI r=0; r<this->
nnz; r++ ) {
149 diags[input[ r ].j()-input[ r ].i()+m-1] = 1;
153 for( ULI r=0; r<m+n-1; r++ ) {
154 if( diags[ r ] == 1 ) {
161 assert( number_of_diagonals == nl1 );
162 for( ULI r=0; r<m+n-1; r++ ) {
163 if( diags[ r ] == 1 ) {
164 assert( static_cast< int >( r ) - static_cast< int >( m ) + 1 == diagonal_offsets[ c++ ] );
169 for( ULI r=0; r<number_of_diagonals; r++ ) {
170 ULI curdiag = diagonal_offsets[ r ];
171 const size_t allocLength = curdiag <=
full ?
d :
d-(curdiag-
full);
172 nzs[r] =
new T[ allocLength ];
173 bytes += allocLength *
sizeof(T);
177 for( ULI r=0; r<m+n-1; r++ ) {
178 diags[ r ] =
static_cast< ULI
>( -1 );
180 for( ULI r=0; r<number_of_diagonals; r++ ) {
181 diags[ diagonal_offsets[ r ] + m - 1 ] = r;
184 for( ULI r=0; r<this->
nnz; r++ ) {
186 ULI cur = input[ r ].j() - input[ r ].i() + m-1;
187 if( input[ r ].i() > input[ r ].j() )
188 nzs[ diags[ cur ] ][ input[ r ].j() ] = input[ r ].value;
190 nzs[ diags[ cur ] ][ input[ r ].i() ] = input[ r ].value;
215 virtual void zxa(
const T* x, T* z ) {
216 for( ULI j=0; j<
d; j++ ) {
219 for( ULI k=0; k<number_of_diagonals; k++ ) {
220 const ULI i = j + diagonal_offsets[ k ];
221 if( i >= this->
nor )
continue;
222 if( diagonal_offsets[ k ] < 0 )
223 z[ j ] +=
nzs[ k ][ j ] * x[ i ];
225 z[ j ] +=
nzs[ k ][ i ] * x[ i ];
236 virtual void zax(
const T* x, T* z ) {
238 for( ULI i=0; i<
d; i++ ) {
241 for( ULI k=0; k<number_of_diagonals; k++ ) {
242 const ULI j = i + diagonal_offsets[ k ];
244 if( j >= this->
noc )
continue;
246 if( diagonal_offsets[ k ] < 0 )
247 z[ i ] +=
nzs[ k ][ j ] * x[ j ];
249 z[ i ] +=
nzs[ k ][ i ] * x[ j ];
263 for( ULI k = 0; k<number_of_diagonals; k++ )
unsigned long int nnz
Number of non-zeros.
Definition: SparseMatrix.hpp:58
DD_MATRIX()
Base constructor.
Definition: DD_MATRIX.hpp:73
ULI d
What the main diagonal length is (longest diagonal).
Definition: DD_MATRIX.hpp:62
DD_MATRIX(std::vector< Triplet< T > > &input, ULI m, ULI n, T zero)
Base constructor.
Definition: DD_MATRIX.hpp:91
The dense diagonal matrix scheme; a storage scheme for sparse matrices consisting of only dense diago...
Definition: DD_MATRIX.hpp:46
T ** nzs
The values of the nonzeros.
Definition: DD_MATRIX.hpp:56
~DD_MATRIX()
Base destructor.
Definition: DD_MATRIX.hpp:260
virtual unsigned long int m()
Queries the number of rows this matrix contains.
Definition: SparseMatrix.hpp:107
virtual void load(std::vector< Triplet< T > > &input, const ULI m, const ULI n, const T zero)
Definition: DD_MATRIX.hpp:130
void loadFromFile(const std::string file, const T zero=0)
Function which loads a matrix from a matrix market file.
Definition: SparseMatrix.hpp:89
virtual unsigned long int nzs()
Queries the number of nonzeroes stored in this matrix.
Definition: SparseMatrix.hpp:123
Interface common to all sparse matrix storage schemes.
Definition: SparseMatrix.hpp:46
unsigned long int noc
Number of columns.
Definition: SparseMatrix.hpp:55
size_t bytes
Keeps track of the number of bytes spent for this matrix.
Definition: DD_MATRIX.hpp:68
ULI full
How many full length diagonals this (possible not square matrix) contains.
Definition: DD_MATRIX.hpp:59
virtual void zax(const T *x, T *z)
In-place z=Ax calculation algorithm.
Definition: DD_MATRIX.hpp:236
unsigned long int nor
Number of rows.
Definition: SparseMatrix.hpp:52
T zero_element
The element considered to be zero.
Definition: SparseMatrix.hpp:63
bool SELF_ALLOCATED
Whether or not nzs was allocated by this instance itself.
Definition: DD_MATRIX.hpp:65
virtual void zxa(const T *x, T *z)
In-place z=xA calculation algorithm.
Definition: DD_MATRIX.hpp:215
size_t bytesUsed()
Definition: DD_MATRIX.hpp:255
virtual unsigned long int n()
Queries the number of columns this matrix contains.
Definition: SparseMatrix.hpp:115
DD_MATRIX(std::string file, T zero=0)
Base constructor.
Definition: DD_MATRIX.hpp:81
A single triplet value.
Definition: Triplet.hpp:52
virtual void getFirstIndexPair(unsigned long int &row, unsigned long int &col)
Returns the first nonzero index, per reference.
Definition: DD_MATRIX.hpp:204
DD_MATRIX(T **nonzeroes, ULI m, ULI n, T zero)
Dense diagonal matrix specific constructor.
Definition: DD_MATRIX.hpp:104