34 #include "SparseMatrix.hpp"
35 #include "Triplet.hpp"
54 template<
typename T >
87 if( one.
i() < two.
i() )
89 if ( one.
i() > two.
i() )
91 return one.
j() < two.
j();
116 ZZ_ICRS(
const myULI number_of_nonzeros,
const myULI number_of_rows,
const myULI number_of_cols, T zero ):
117 SparseMatrix< T, ULI >( number_of_nonzeros, number_of_rows, number_of_cols, zero ) {
118 ds =
new T[ this->
nnz ];
132 ds =
new T[ this->
nnz ];
135 for( myULI i=0; i<this->
nnz; i = i + 1 ) {
136 ds[ i ] = toCopy.
ds[ i ];
153 load( input, m, n, zero );
157 virtual void load( std::vector<
Triplet< T > >& input,
const myULI
m,
const myULI
n,
const T zero ) {
163 typename std::vector< Triplet< T > >::iterator in_it;
173 std::vector< myULI > r_ind_temp;
174 typename std::vector< Triplet< T > >::iterator it = input.begin();
175 unsigned long int prev_row = (*it).i();
176 r_ind_temp.push_back( prev_row );
178 for( ; it!=input.end(); it++ ) {
179 if( (*it).i() > prev_row ) {
180 r_ind_temp.push_back( (*it).i() - prev_row );
181 prev_row = (*it).i();
184 this->
nnz = input.size();
187 jumps = r_ind_temp.size();
188 ds =
new T[ this->
nnz ];
197 for( myULI i=0; i<static_cast< myULI >( r_ind_temp.size() ); i++ )
198 r_ind[ i ] = r_ind_temp[ i ];
204 for( myULI i=0; i<this->
nnz; ++i ) {
206 const unsigned long int currow = cur.
i();
207 const unsigned long int curcol = cur.
j();
208 if( currow == prev_row ) {
209 c_ind[ i ] = curcol - prev_col;
217 while( j < this->nnz && input[ j ].i() == static_cast< unsigned long int >( currow ) )
223 ds[ i ] = input[ j ].value;
224 c_ind[ i ] = this->
noc + ( input[ j ].j() - prev_col );
225 prev_col = input[ j ].j();
230 for( ; j>=stop; j-- ) {
231 ds[ i ] = input[ j ].value;
232 c_ind[ i ] = prev_col - input[ j ].j();
233 prev_col = input[ j ].j();
237 if( i >= this->nnz )
break;
242 c_ind[ i ] = this->
noc + input[ i ].j();
244 c_ind[ i ] = this->
noc + ( prev_col - input[ i ].j() );
247 ds[ i ] = input[ i ].value;
248 prev_col = input[ i ].j();
249 prev_row = input[ i ].i();
256 row =
static_cast< unsigned long int >( this->
r_ind[ 0 ] );
257 col =
static_cast< unsigned long int >( this->
c_ind[ 0 ] );
266 virtual void zxa(
const T*__restrict__ pDataX, T*__restrict__ pDataZ ) {
269 T *__restrict__ pDataA =
ds;
270 myULI *__restrict__ pIncRow =
r_ind;
271 myULI *__restrict__ pIncCol =
c_ind;
272 const T *
const pDataZbeg = pDataZ;
273 const T *
const pDataZend = pDataZ + this->
noc;
274 const T *__restrict__
const pDataAend =
ds + this->
nnz;
286 while( pDataA < pDataAend ) {
290 rowshadow += *pIncRow;
293 while( pDataZ < pDataZend ) {
295 *pDataZ += *pDataA * *pDataX;
303 if( pDataA >= pDataAend )
break;
306 pDataZ -= this->
noc ;
310 assert( rowshadow >= 0 );
311 assert( rowshadow < this->
nor );
314 while( pDataZbeg <= pDataZ ) {
316 *pDataZ += *pDataA * *pDataX;
340 virtual void zax(
const T*__restrict__ pDataX, T*__restrict__ pDataZ ) {
343 T *__restrict__ pDataA =
ds;
344 myULI *__restrict__ pIncRow =
r_ind;
345 myULI *__restrict__ pIncCol =
c_ind;
346 const T *
const pDataXbeg = pDataX;
347 const T *
const pDataXend = pDataX + this->
noc;
348 const T *__restrict__
const pDataAend =
ds + this->
nnz;
358 while( pDataA < pDataAend ) {
362 rowshadow += *pIncRow;
365 while( pDataX < pDataXend ) {
367 *pDataZ += *pDataA * *pDataX;
375 if( pDataA >= pDataAend )
break;
378 pDataX -= this->
noc ;
382 assert( rowshadow >= 0 );
383 assert( rowshadow < this->
nor );
386 while( pDataXbeg <= pDataX ) {
388 *pDataZ += *pDataA * *pDataX;
406 return sizeof( myULI ) * ( this->
nnz +
jumps + 1 ) +
sizeof( T ) * this->
nnz;
LI nnz
Number of non-zeros.
Definition: SparseMatrix.hpp:58
ZZ_ICRS(ZZ_ICRS< T > &toCopy)
Copy constructor.
Definition: ZZ_ICRS.hpp:127
virtual void zxa(const T *__restrict__ pDataX, T *__restrict__ pDataZ)
In-place z=xA multiplication algorithm.
Definition: ZZ_ICRS.hpp:266
ULI i() const
Definition: Triplet.hpp:70
ZZ_ICRS(const myULI number_of_nonzeros, const myULI number_of_rows, const myULI number_of_cols, T zero)
Base constructor which only initialises the internal arrays.
Definition: ZZ_ICRS.hpp:116
virtual void zax(const T *__restrict__ pDataX, T *__restrict__ pDataZ)
In-place z=Ax multiplication algorithm.
Definition: ZZ_ICRS.hpp:340
static bool compareTriplets(const Triplet< T > &one, const Triplet< T > &two)
Comparison function used for sorting input data.
Definition: ZZ_ICRS.hpp:86
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 myULI m, const myULI n, const T zero)
Definition: ZZ_ICRS.hpp:157
size_t jumps
The number of row jumps.
Definition: ZZ_ICRS.hpp:74
void loadFromFile(const std::string file, const T zero=0)
Function which loads a matrix from a matrix market file.
Definition: SparseMatrix.hpp:89
Interface common to all sparse matrix storage schemes.
Definition: SparseMatrix.hpp:46
The zig-zag incremental compressed row storage sparse matrix data structure.
Definition: ZZ_ICRS.hpp:58
virtual size_t bytesUsed()
Function to query the amount of storage required by this sparse matrix.
Definition: ZZ_ICRS.hpp:405
myULI * c_ind
Array containing the column jumps.
Definition: ZZ_ICRS.hpp:80
LI noc
Number of columns.
Definition: SparseMatrix.hpp:55
~ZZ_ICRS()
Base deconstructor.
Definition: ZZ_ICRS.hpp:410
ZZ_ICRS(std::vector< Triplet< T > > &input, const myULI m, const myULI n, const T zero)
Constructor which transforms a collection of input triplets to CRS format.
Definition: ZZ_ICRS.hpp:152
ZZ_ICRS(std::string file, T zero=0)
Base constructor.
Definition: ZZ_ICRS.hpp:103
T value
Value stored at this triplet.
Definition: Triplet.hpp:95
T * ds
Array containing the actual this->nnz non-zeros.
Definition: ZZ_ICRS.hpp:77
LI nor
Number of rows.
Definition: SparseMatrix.hpp:52
T zero_element
The element considered to be zero.
Definition: SparseMatrix.hpp:63
ULI j() const
Definition: Triplet.hpp:73
ZZ_ICRS()
Base constructor.
Definition: ZZ_ICRS.hpp:97
myULI * r_ind
Array containing the row jumps.
Definition: ZZ_ICRS.hpp:83
virtual unsigned long int n()
Queries the number of columns this matrix contains.
Definition: SparseMatrix.hpp:115
virtual void getFirstIndexPair(myULI &row, myULI &col)
Returns the first nonzero index, per reference.
Definition: ZZ_ICRS.hpp:255
A single triplet value.
Definition: Triplet.hpp:52