34 #include "SparseMatrix.hpp"
57 template<
typename _t_value,
typename _i_value=LI >
105 BICRS( std::string file, _t_value zero = 0 ) {
120 BICRS( _i_value* row, _i_value* col, _t_value* val, ULI
m, ULI
n, ULI nz, _t_value zero ) {
121 this->
load( row, col, val, m, n, nz, zero );
128 this->
load( input, m, n, zero );
138 ULI nz = input.size();
139 _i_value* row =
new _i_value[ nz ];
140 _i_value* col =
new _i_value[ nz ];
141 _t_value* val =
new _t_value[ nz ];
142 unsigned long int c = 0;
143 typename std::vector< Triplet< _t_value > >::iterator it = input.begin();
144 for( ; it!=input.end(); it++, c++ ) {
145 row[ c ] = (*it).i();
146 col[ c ] = (*it).j();
147 val[ c ] = (*it).value;
149 load( row, col, val, m, n, nz, zero );
150 assert(
vals != val );
157 void load( _i_value* row, _i_value* col, _t_value* val, ULI
m, ULI
n, ULI nz, _t_value zero ) {
159 std::cerr <<
"Warning: _DEBUG flag set." << std::endl;
172 _i_value prevrow = row[ 0 ];
173 for(
unsigned long int i=1; i<this->
nnz; i++ ) {
174 if( row[ i ] != prevrow )
179 std::cout <<
jumps <<
" row jumps found." << std::endl;
183 vals =
new _t_value[ this->
nnz ];
184 for(
unsigned long int i=0; i<this->
nnz; ++i )
vals[i] = val[i];
189 int prevcol = col[ 0 ];
190 r_end = row[ nz - 1 ];
191 c_end = col[ nz - 1 ];
194 std::cout <<
"c_inc: " << prevcol << std::endl;
195 std::cout <<
"r_inc: " << prevrow << std::endl;
198 for(
unsigned long int i=1; i<this->
nnz; i++ ) {
199 this->
c_inc[ i-1 ] = col[ i ] - prevcol;
200 if( row[ i ] != prevrow ) {
202 this->
r_inc[ c++ ] = row[ i ] - prevrow;
204 std::cout <<
"c_inc: " <<
ntt << std::endl;
205 std::cout <<
"r_inc: " << row[ i ] - prevrow << std::endl;
211 std::cout <<
"c_inc: " << col[ i ] - prevcol << std::endl;
221 std::cout <<
"Construction done." << std::endl;
236 virtual void zxa(
const _t_value*__restrict__ x_p, _t_value*__restrict__ y_p ) {
237 const _t_value * y = y_p;
238 _i_value *__restrict__ c_inc_p =
c_inc;
239 _i_value *__restrict__ r_inc_p =
r_inc;
240 _t_value *__restrict__ v_p =
vals;
243 const _t_value * x = x_p;
244 const _t_value *
const x_end = x+this->
nor;
245 const _i_value *__restrict__
const c_inc_end =
c_inc+this->
nnz+1;
247 const _t_value *
const y_end = y+this->
noc;
248 const _t_value *__restrict__
const v_end =
vals+this->
nnz;
252 while( v_p < v_end ) {
254 assert( y_p < y_end );
255 assert( v_p >=
vals );
256 assert( v_p < v_end );
258 assert( x_p < x_end );
259 assert( c_inc_p >=
c_inc );
260 assert( c_inc_p < c_inc_end );
261 assert( r_inc_p >=
r_inc );
262 while( y_p < y_end ) {
264 std::cout << (x_p-x) <<
"," << (y_p-y) <<
" next increment: " << (*(c_inc_p+1))<< std::endl;
266 *y_p += *v_p++ * *x_p;
279 virtual void zax(
const _t_value*__restrict__ x_p, _t_value*__restrict__ y_p ) {
280 const _t_value * x = x_p;
281 _i_value *__restrict__ c_inc_p =
c_inc;
282 _i_value *__restrict__ r_inc_p =
r_inc;
283 _t_value *__restrict__ v_p =
vals;
286 const _t_value * y = y_p;
287 const _t_value *
const y_end = y+this->
nor;
288 const _i_value *__restrict__
const c_inc_end =
c_inc+this->
nnz;
290 const _t_value *
const x_end = x+this->
noc;
291 const _t_value *__restrict__
const v_end =
vals+this->
nnz;
295 while( v_p < v_end ) {
297 assert( y_p < y_end );
298 assert( v_p >=
vals );
299 assert( v_p < v_end );
301 assert( x_p < x_end );
302 assert( c_inc_p >=
c_inc );
303 assert( c_inc_p < c_inc_end );
304 assert( r_inc_p >=
r_inc );
305 while( x_p < x_end ) {
307 std::cout << (y_p-y) <<
"," << (x_p-x) <<
" next increment: " << (*(c_inc_p+1))<< std::endl;
309 *y_p += *v_p++ * *x_p;
322 virtual void zax_fb( _t_value*__restrict__ x_f, _t_value*__restrict__ y_f ) {
323 const _t_value * x = x_f;
324 _i_value *__restrict__ c_inc_f =
c_inc;
325 _i_value *__restrict__ r_inc_f =
r_inc;
326 _i_value *__restrict__ c_inc_b =
c_inc+this->
nnz - 1;
327 _i_value *__restrict__ r_inc_b =
r_inc+this->
jumps;
328 _t_value *__restrict__ v_f =
vals;
329 _t_value *__restrict__ v_b =
vals+this->nnz - 1;
330 _t_value *__restrict__ x_b = x_f + this->
noc - 1;
331 _t_value *__restrict__ y_b = y_f + this->
nor - 1;
333 const _t_value * y = y_f;
334 const _t_value *
const y_end = y+this->
nor;
335 const _i_value *__restrict__
const c_inc_end =
c_inc+this->
nnz;
337 const _t_value *
const x_end = x+this->
noc;
338 const _t_value *__restrict__
const v_end =
vals+this->
nnz;
344 while( v_f < v_end && v_b >=
vals ) {
346 assert( y_f < y_end );
348 assert( y_b < y_end );
349 assert( v_f >=
vals );
350 assert( v_f < v_end );
351 assert( v_b >=
vals );
352 assert( v_b < v_end );
354 assert( x_f < x_end );
356 assert( x_b < x_end );
357 assert( c_inc_f >=
c_inc );
358 assert( c_inc_f < c_inc_end );
359 assert( c_inc_b >=
c_inc );
360 assert( c_inc_b < c_inc_end );
361 assert( r_inc_b >=
r_inc );
362 assert( r_inc_f >=
r_inc );
364 std::cout << (y_p-y) <<
"," << (x_p-x) <<
" next increment: " << (*(c_inc_p+1))<< std::endl;
366 *y_f += *v_f++ * *x_f;
372 if( v_b < v_f )
break;
373 *y_b += *v_b-- * *x_b;
383 return sizeof( ULI ) * 4 +
sizeof( _i_value ) * ( this->
nnz +
jumps + 1 ) +
sizeof( _t_value ) * this->
nnz;
ULI nnz
Number of non-zeros.
Definition: SparseMatrix.hpp:58
ULI c_end
Stores the column end position.
Definition: BICRS.hpp:72
virtual void zxa(const _t_value *__restrict__ x_p, _t_value *__restrict__ y_p)
Calculates y=xA, but does not allocate y itself.
Definition: BICRS.hpp:236
ULI c_start
Stores the column start position.
Definition: BICRS.hpp:66
virtual size_t bytesUsed()
Function to query the amount of storage required by this sparse matrix.
Definition: BICRS.hpp:382
Bi-directional Incremental Compressed Row Storage scheme.
Definition: BICRS.hpp:58
virtual void zax_fb(_t_value *__restrict__ x_f, _t_value *__restrict__ y_f)
Calculates y=Ax, but does not allocate y itself.
Definition: BICRS.hpp:322
virtual unsigned long int m()
Queries the number of rows this matrix contains.
Definition: SparseMatrix.hpp:107
BICRS()
Base constructor.
Definition: BICRS.hpp:99
virtual ~BICRS()
Base deconstructor.
Definition: BICRS.hpp:92
void loadFromFile(const std::string file, const _t_valuezero=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
BICRS(std::vector< Triplet< _t_value > > &input, ULI m, ULI n, _t_value zero=0)
Base constructor.
Definition: BICRS.hpp:127
ULI noc
Number of columns.
Definition: SparseMatrix.hpp:55
ULI r_start
Stores the row start position.
Definition: BICRS.hpp:63
void load(_i_value *row, _i_value *col, _t_value *val, ULI m, ULI n, ULI nz, _t_value zero)
Definition: BICRS.hpp:157
virtual void load(std::vector< Triplet< _t_value > > &input, ULI m, ULI n, _t_value zero)
This function will rewrite the std::vector< Triplet > structure to one suitable for the other load fu...
Definition: BICRS.hpp:137
BICRS(_i_value *row, _i_value *col, _t_value *val, ULI m, ULI n, ULI nz, _t_value zero)
Base constructor.
Definition: BICRS.hpp:120
_i_value * r_inc
Stores the row jumps; size is at maximum the number of nonzeros.
Definition: BICRS.hpp:78
ULI nor
Number of rows.
Definition: SparseMatrix.hpp:52
virtual void zax(const _t_value *__restrict__ x_p, _t_value *__restrict__ y_p)
Calculates y=Ax, but does not allocate y itself.
Definition: BICRS.hpp:279
virtual void getFirstIndexPair(ULI &row, ULI &col)
Returns the first nonzero index, per reference.
Definition: BICRS.hpp:226
_i_value * c_inc
Stores the column jumps; size is exactly the number of nonzeros.
Definition: BICRS.hpp:81
_t_value zero_element
The element considered to be zero.
Definition: SparseMatrix.hpp:63
BICRS(std::string file, _t_value zero=0)
Base constructor.
Definition: BICRS.hpp:105
virtual unsigned long int n()
Queries the number of columns this matrix contains.
Definition: SparseMatrix.hpp:115
_t_value * vals
Stores the values of the individual nonzeros.
Definition: BICRS.hpp:84
A single triplet value.
Definition: Triplet.hpp:52
_i_value ntt
Caches n times two.
Definition: BICRS.hpp:87
ULI jumps
Stores the number of row jumps.
Definition: BICRS.hpp:75
ULI r_end
Stores the row end position.
Definition: BICRS.hpp:69