34 #include "Triplet.hpp"
35 #include "SparseMatrix.hpp"
61 template<
typename T >
81 if ( one.
j() < two.
j() )
83 if ( one.
j() > two.
j() )
96 bool find(
const ULI col_index,
const ULI search_start,
const ULI search_end, ULI &ret ) {
97 for( ULI i=search_start; i<search_end; i++ )
98 if(
col_ind[ i ] == col_index ) {
114 McCRS( std::string file, T zero = 0 ) {
127 McCRS(
const ULI number_of_nonzeros,
const ULI number_of_rows,
const ULI number_of_cols, T zero ) {
128 this->
nnz = number_of_nonzeros;
129 this->
nor = number_of_rows;
130 this->
noc = number_of_cols;
133 row_start = (ULI*) numa_alloc_interleaved( (this->
nor + 1) *
sizeof( ULI ) );
134 ds = (T*) numa_alloc_interleaved( (this->
nnz) *
sizeof( T ) );
135 col_ind = (ULI*) numa_alloc_interleaved( this->
nnz *
sizeof( ULI ) );
138 ds =
new T[ this->
nnz ];
151 row_start = (ULI*) numa_alloc_interleaved( (this->
nor + 1) *
sizeof( ULI ) );
152 ds = (T*) numa_alloc_interleaved( (this->
nnz) *
sizeof( T ) );
153 col_ind = (ULI*) numa_alloc_interleaved( (this->
nnz) *
sizeof( ULI ) );
156 ds =
new T[ this->
nnz ];
159 for( ULI i=0; i<this->
nnz; i++ ) {
160 ds[ i ] = toCopy.
ds[ i ];
163 for( ULI i=0; i<this->
nor; i++ )
178 load( input, m, n, zero );
183 std::cout <<
"\tLoading in a vector of " << input.size() <<
" triplets into McCRS..." << std::endl;
187 this->
nnz = input.size();
193 std::vector< std::vector< Triplet< T >* > >
ds( this->
nor );
196 typename std::vector< Triplet< T > >::iterator in_it = input.begin();
197 for( ; in_it != input.end(); ++in_it ) {
199 const ULI currow = in_it->i();
200 const T value = in_it->value;
205 ds.at( currow ).push_back( &(*in_it) );
210 row_start = (ULI*) numa_alloc_interleaved( (this->
nor + 1) *
sizeof( ULI ) );
211 this->ds = (T*) numa_alloc_interleaved( (this->
nnz) *
sizeof( T ) );
212 col_ind = (ULI*) numa_alloc_interleaved( (this->
nnz) *
sizeof( ULI ) );
215 this->ds =
new T[ this->
nnz ];
221 for( ULI currow = 0; currow < this->
nor; currow++ ) {
223 if( ds.at( currow ).size() == 0 )
continue;
225 typename std::vector< Triplet< T >* >::iterator row_it = ds.at( currow ).begin();
226 for( ; row_it!=ds.at( currow ).end(); row_it++ ) {
228 this->ds[ index ] = cur.
value;
234 std::cout <<
"\t" << index <<
" nonzeroes loaded into McCRS structure." << std::endl;
235 assert( index == this->
nnz );
248 std::cout <<
"Searched col_ind between " <<
row_start[ i ] <<
" and " <<
row_start[ i + 1 ] <<
", found: " << std::endl;
249 std::cout <<
"Element (" << i <<
"," << j <<
") found on index " << found_index <<
", returning " <<
ds[ found_index ] << std::endl;
251 return ds[ found_index ];
264 virtual T*
mv(
const T* x ) {
265 T* ret = (T*) numa_alloc_interleaved( this->
nor *
sizeof( T ) );
275 virtual void zxa(
const T*__restrict__ x, T*__restrict__ z ) {
276 std::cerr <<
"CRS z=xA (left-sided SpMV multiplication) is not possible to do in parallel using CRS and OpenMP parallel fors" << std::endl;
277 std::cerr <<
"Exiting..." << std::endl;
287 virtual void zax(
const T*__restrict__ x, T*__restrict__ z ) {
288 const unsigned long int nor = this->
nor;
291 T *
const ds = this->
ds;
292 #pragma omp parallel for shared( x, z ) schedule( dynamic, 8 )
293 for( ULI row = 0; row <
nor; row++ ) {
297 v_p = ds + row_start[ row ];
298 j_p = col_ind + row_start[ row ];
299 for( index = row_start[ row ]; index < row_start[ row + 1 ]; index++ ) {
300 x_e = *(x + (*j_p++));
301 sum += (*v_p++) * x_e;
308 return sizeof( ULI ) * ( this->
nnz + this->
nor ) +
sizeof( T ) * this->
nnz;
323 numa_free(
row_start, (this->
nor + 1) *
sizeof( ULI ) );
324 numa_free(
ds, (this->
nnz) *
sizeof( T ) );
325 numa_free(
col_ind, (this->
nnz) *
sizeof( ULI ) );
ULI nnz
Number of non-zeros.
Definition: SparseMatrix.hpp:58
virtual void getFirstIndexPair(ULI &row, ULI &col)
Returns the first nonzero index, per reference.
Definition: McCRS.hpp:257
The compressed row storage sparse matrix data structure.
Definition: McCRS.hpp:62
T & random_access(ULI i, ULI j)
Method which provides random matrix access to the stored sparse matrix.
Definition: McCRS.hpp:244
McCRS(std::string file, T zero=0)
Base constructor.
Definition: McCRS.hpp:114
T * values()
Returns pointer to the matrix nonzeros vector.
Definition: McCRS.hpp:318
virtual void load(std::vector< Triplet< T > > &input, ULI m, ULI n, T zero)
Definition: McCRS.hpp:182
virtual void zax(const T *__restrict__ x, T *__restrict__ z)
In-place z=Ax function.
Definition: McCRS.hpp:287
McCRS()
Base constructor.
Definition: McCRS.hpp:108
virtual unsigned long int m()
Queries the number of rows this matrix contains.
Definition: SparseMatrix.hpp:107
McCRS(std::vector< Triplet< T > > input, ULI m, ULI n, T zero)
Constructor which transforms a collection of input triplets to McCRS format.
Definition: McCRS.hpp:177
ULI * col_ind
Array containing the column indeces corresponding to the elements in ds.
Definition: McCRS.hpp:75
virtual void zxa(const T *__restrict__ x, T *__restrict__ z)
In-place z=xA function.
Definition: McCRS.hpp:275
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
static int compareTriplets(const void *left, const void *right)
Sorts 1D columnwise.
Definition: McCRS.hpp:78
ULI * row_start
Array keeping track of individual row starting indices.
Definition: McCRS.hpp:69
ULI * rowJump()
Returns pointer to the row_start vector.
Definition: McCRS.hpp:312
ULI noc
Number of columns.
Definition: SparseMatrix.hpp:55
virtual size_t bytesUsed()
Function to query the amount of storage required by this sparse matrix.
Definition: McCRS.hpp:307
T value
Value stored at this triplet.
Definition: Triplet.hpp:95
McCRS(McCRS< T > &toCopy)
Copy constructor.
Definition: McCRS.hpp:146
ULI nor
Number of rows.
Definition: SparseMatrix.hpp:52
ULI * columnIndices()
Returns pointer to the column index vector.
Definition: McCRS.hpp:315
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: McCRS.hpp:96
T zero_element
The element considered to be zero.
Definition: SparseMatrix.hpp:63
ULI j() const
Definition: Triplet.hpp:73
virtual ~McCRS()
Base deconstructor.
Definition: McCRS.hpp:321
virtual T * mv(const T *x)
Overloaded mv call; allocates output vector using numa_interleaved.
Definition: McCRS.hpp:264
T * ds
Array containing the actual nnz non-zeros.
Definition: McCRS.hpp:72
virtual unsigned long int n()
Queries the number of columns this matrix contains.
Definition: SparseMatrix.hpp:115
A single triplet value.
Definition: Triplet.hpp:52
McCRS(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: McCRS.hpp:127