34 #include "Triplet.hpp"
35 #include "SparseMatrix.hpp"
51 template<
typename T >
71 if ( one.
j() < two.
j() )
73 if ( one.
j() > two.
j() )
86 bool find(
const ULI col_index,
const ULI search_start,
const ULI search_end, ULI &ret ) {
87 for( ULI i=search_start; i<search_end; i++ )
88 if(
col_ind[ i ] == col_index ) {
104 CRS( std::string file, T zero = 0 ) {
117 CRS(
const ULI number_of_nonzeros,
const ULI number_of_rows,
const ULI number_of_cols, T zero ) {
118 this->
nnz = number_of_nonzeros;
119 this->
nor = number_of_rows;
120 this->
noc = number_of_cols;
123 ds =
new T[ this->
nnz ];
135 ds =
new T[ this->
nnz ];
137 for( ULI i=0; i<this->
nnz; i++ ) {
138 ds[ i ] = toCopy.
ds[ i ];
141 for( ULI i=0; i<this->
nor; i++ )
156 load( input, m, n, zero );
161 std::cout <<
"\tLoading in a vector of " << input.size() <<
" triplets into CRS..." << std::endl;
165 this->
nnz = input.size();
171 std::vector< std::vector< Triplet< T >* > >
ds( this->
nor );
174 typename std::vector< Triplet< T > >::iterator in_it = input.begin();
175 for( ; in_it != input.end(); ++in_it ) {
177 const ULI currow = in_it->i();
178 const T value = in_it->value;
183 ds.at( currow ).push_back( &(*in_it) );
188 this->ds =
new T[ this->
nnz ];
193 for( ULI currow = 0; currow < this->
nor; currow++ ) {
195 if( ds.at( currow ).size() == 0 )
continue;
197 typename std::vector< Triplet< T >* >::iterator row_it = ds.at( currow ).begin();
198 for( ; row_it!=ds.at( currow ).end(); row_it++ ) {
200 this->ds[ index ] = cur.
value;
206 std::cout <<
"\t" << index <<
" nonzeroes loaded into CRS structure.\n" << std::endl;
207 assert( index == this->
nnz );
220 std::cout <<
"Searched col_ind between " <<
row_start[ i ] <<
" and " <<
row_start[ i + 1 ] <<
", found: " << std::endl;
221 std::cout <<
"Element (" << i <<
"," << j <<
") found on index " << found_index <<
", returning " <<
ds[ found_index ] << std::endl;
223 return ds[ found_index ];
237 virtual void zxa(
const T*__restrict__ x, T*__restrict__ z ) {
238 T*__restrict__ ds_p =
ds;
239 ULI*__restrict__ col_ind_p =
col_ind;
241 for( row = 0; row < this->
nor; row++, x++ ) {
243 z[ *col_ind_p++ ] += *ds_p++ * *x;
253 virtual void zax(
const T*__restrict__ x, T*__restrict__ z ) {
254 T*__restrict__ ds_p =
ds;
255 ULI*__restrict__ col_ind_p =
col_ind;
257 for( row = 0; row < this->
nor; row++, z++ ) {
259 *z += *ds_p++ * x[ *col_ind_p++ ];
265 void ZaX(
const T*__restrict__
const *__restrict__
const X, T *__restrict__
const *__restrict__
const Z ) {
266 T*__restrict__ ds_p =
ds;
267 ULI*__restrict__ col_ind_p =
col_ind;
270 const T *__restrict__ pDataX[ k ];
271 T *__restrict__ pDataZ[ k ];
273 for(
size_t s = 0; s < k; ++s ) {
277 for( row = 0; row < this->
nor; ++row ) {
279 for(
size_t s = 0; s < k; ++s ) {
280 assert( pDataZ[s] >= Z[s] );
281 assert( pDataZ[s] < Z[s]+this->nor );
282 assert( *col_ind_p < this->
noc );
283 assert( pDataX[s] == X[s] );
284 *(pDataZ[s]) += *ds_p * pDataX[s][ *col_ind_p ];
289 for(
size_t s = 0; s < k; ++s ) {
297 void ZXa(
const T *__restrict__
const *__restrict__
const X, T *__restrict__
const *__restrict__
const Z ) {
298 T*__restrict__ ds_p =
ds;
299 ULI*__restrict__ col_ind_p =
col_ind;
301 const T *__restrict__ pDataX[ k ];
302 T *__restrict__ pDataZ[ k ];
303 for(
size_t s = 0; s < k; ++s ) {
307 for( row = 0; row < this->
nor; ++row ) {
309 for(
size_t s = 0; s < k; ++s ) {
310 pDataZ[s][ *col_ind_p ] += *ds_p * *(pDataX[s]);
315 for(
size_t s = 0; s < k; ++s ) {
339 return sizeof( ULI ) * ( this->
nor + this->
nnz ) +
sizeof( T ) * this->
nnz;
ULI nnz
Number of non-zeros.
Definition: SparseMatrix.hpp:58
ULI * columnIndices()
Returns pointer to the column index vector.
Definition: CRS.hpp:325
ULI * row_start
Array keeping track of individual row starting indices.
Definition: CRS.hpp:59
virtual void load(std::vector< Triplet< T > > &input, ULI m, ULI n, T zero)
Definition: CRS.hpp:160
CRS(std::string file, T zero=0)
Base constructor.
Definition: CRS.hpp:104
ULI * col_ind
Array containing the column indeces corresponding to the elements in ds.
Definition: CRS.hpp:65
virtual unsigned long int m()
Queries the number of rows this matrix contains.
Definition: SparseMatrix.hpp:107
virtual void zax(const T *__restrict__ x, T *__restrict__ z)
In-place z=Ax function.
Definition: CRS.hpp:253
ULI * rowJump()
Returns pointer to the row_start vector.
Definition: CRS.hpp:322
The compressed row storage sparse matrix data structure.
Definition: CRS.hpp:52
T * values()
Returns pointer to the matrix nonzeros vector.
Definition: CRS.hpp:328
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
CRS(CRS< T > &toCopy)
Copy constructor.
Definition: CRS.hpp:130
void ZXa(const T *__restrict__ const *__restrict__ const X, T *__restrict__ const *__restrict__ const Z)
Definition: CRS.hpp:297
ULI noc
Number of columns.
Definition: SparseMatrix.hpp:55
virtual void zxa(const T *__restrict__ x, T *__restrict__ z)
In-place z=xA function.
Definition: CRS.hpp:237
T value
Value stored at this triplet.
Definition: Triplet.hpp:95
CRS(const ULI number_of_nonzeros, const ULI number_of_rows, const ULI number_of_cols, T zero)
Base constructor which only initialises the internal arrays.
Definition: CRS.hpp:117
ULI nor
Number of rows.
Definition: SparseMatrix.hpp:52
CRS(std::vector< Triplet< T > > input, ULI m, ULI n, T zero)
Constructor which transforms a collection of input triplets to CRS format.
Definition: CRS.hpp:155
virtual void getFirstIndexPair(ULI &row, ULI &col)
Returns the first nonzero index, per reference.
Definition: CRS.hpp:229
T zero_element
The element considered to be zero.
Definition: SparseMatrix.hpp:63
ULI j() const
Definition: Triplet.hpp:73
T * ds
Array containing the actual nnz non-zeros.
Definition: CRS.hpp:62
void ZaX(const T *__restrict__ const *__restrict__ const X, T *__restrict__ const *__restrict__ const Z)
Definition: CRS.hpp:265
virtual size_t bytesUsed()
Definition: CRS.hpp:338
bool find(const ULI col_index, const ULI search_start, const ULI search_end, ULI &ret)
Helper function which finds a value with a given column index on a given subrange of indices...
Definition: CRS.hpp:86
CRS()
Base constructor.
Definition: CRS.hpp:98
virtual unsigned long int n()
Queries the number of columns this matrix contains.
Definition: SparseMatrix.hpp:115
static int compareTriplets(const void *left, const void *right)
Sorts 1D columnwise.
Definition: CRS.hpp:68
A single triplet value.
Definition: Triplet.hpp:52
virtual ~CRS()
Base deconstructor.
Definition: CRS.hpp:331
T & random_access(ULI i, ULI j)
Method which provides random matrix access to the stored sparse matrix.
Definition: CRS.hpp:216