SparseLibrary  Version 1.6.0
TS.hpp
1 /*
2  * Copyright (c) 2007-2014, A. N. Yzelman, Utrecht University 2007-2011;
3  * KU Leuven 2011-2014.
4  * R. H. Bisseling, Utrecht University 2007-2014.
5  *
6  * This file is part of the Sparse Library.
7  *
8  * This library was developed under supervision of Prof. dr. Rob H. Bisseling at
9  * Utrecht University, from 2007 until 2011. From 2011-2014, development continued
10  * at KU Leuven, where Prof. dr. Dirk Roose contributed significantly to the ideas
11  * behind the newer parts of the library code.
12  *
13  * The Sparse Library is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by the
15  * Free Software Foundation, either version 3 of the License, or (at your
16  * option) any later version.
17  *
18  * The Sparse Library is distributed in the hope that it will be useful, but
19  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  * for more details.
22  *
23  * You should have received a copy of the GNU General Public License along
24  * with the Sparse Library. If not, see <http://www.gnu.org/licenses/>.
25  */
26 
27 
28 /*
29  * File created by:
30  * A. N. Yzelman, Dept. of Mathematics, Utrecht University, 2008.
31  */
32 
33 
34 #include <vector>
35 #include <assert.h>
36 #include "Triplet.hpp"
37 #include "SparseMatrix.hpp"
38 
39 #ifndef _H_TS
40 #define _H_TS
41 
43 template< typename T >
44 class TS: public SparseMatrix< T, ULI > {
45 
46  private:
47 
48  protected:
49 
51  ULI* i;
52 
54  ULI* j;
55 
57  T* ds;
58 
59  public:
60 
62  TS() {}
63 
68  TS( std::string file, T zero = 0 ) {
69  this->loadFromFile( file, zero );
70  }
71 
78  TS( std::vector< Triplet< T > >& input, ULI m, ULI n, T zero = 0 ) {
79  load( input, m, n, zero );
80  }
81 
83  virtual void load( std::vector< Triplet< T > >& input, const ULI m, const ULI n, const T zero ) {
84  ULI offset = 0;
85 
86  this->zero_element = zero;
87  this->nor = m;
88  this->noc = n;
89  this->nnz = input.size();
90  ds = new T[ this->nnz ];
91  i = new ULI[ this->nnz ];
92  j = new ULI[ this->nnz ];
93  for( ULI r=0; r<this->nnz; r++ )
94  if( input[ r ].value != this->zero_element ) {
95  //ds[ r ] = input[ r ];
96  ds[ r - offset ] = input[ r ].value;
97  i[ r - offset ] = input[ r ].i();
98  j[ r - offset ] = input[ r ].j();
99  } else {
100  offset++;
101  }
102  this->nnz -= offset;
103  }
104 
106  virtual void getFirstIndexPair( ULI &row, ULI &col ) {
107  row = this->i[ 0 ];
108  col = this->j[ 0 ];
109  }
110 
117  virtual void zxa( const T* x, T* z ) {
118  for( ULI r=0; r<this->nnz; r++ ) {
119  assert( i[ r ] >= 0 );
120  assert( i[ r ] < this->nor );
121  assert( j[ r ] >= 0 );
122  assert( j[ r ] < this->noc );
123  z[ j[ r ] ] += ds[ r ] * x[ i[ r ] ];
124  }
125  }
126 
133  virtual void zax( const T* x, T* z ) {
134  for( ULI r=0; r<this->nnz; r++ ) {
135  assert( i[ r ] >= 0 );
136  assert( i[ r ] < this->nor );
137  assert( j[ r ] >= 0 );
138  assert( j[ r ] < this->noc );
139  z[ i[ r ] ] += ds[ r ] * x[ j[ r ] ];
140  }
141  }
142 
144  template< size_t k >
145  void ZaX( const T *__restrict__ const *__restrict__ const X, T *__restrict__ const *__restrict__ const Z ) {
146  for( ULI r = 0; r < this->nnz; r++ ) {
147  assert( i[ r ] >= 0 );
148  assert( i[ r ] < this->nor );
149  assert( j[ r ] >= 0 );
150  assert( j[ r ] < this->noc );
151  //process nonzero for each of the k systems simultaneously
152  for( size_t s = 0; s < k; ++s ) {
153  Z[ s ][ i[ r ] ] += ds[ r ] * X[ s ][ j[ r ] ];
154  }
155  }
156  }
157 
159  template< size_t k >
160  void ZXa( const T *__restrict__ const *__restrict__ const X, T *__restrict__ const *__restrict__ const Z ) {
161  for( ULI r = 0; r < this->nnz; r++ ) {
162  assert( i[ r ] >= 0 );
163  assert( i[ r ] < this->nor );
164  assert( j[ r ] >= 0 );
165  assert( j[ r ] < this->noc );
166  //process nonzero for each of the k systems simultaneously
167  for( size_t s = 0; s < k; ++s ) {
168  Z[ s ][ j[ r ] ] += ds[ r ] * X[ s ][ i[ r ] ];
169  }
170  }
171  }
172 
174  virtual size_t bytesUsed() {
175  return sizeof( ULI ) * 2 * this->nnz + sizeof( T ) * this->nnz;
176  }
177 
179  ~TS() {
180  delete [] i;
181  delete [] j;
182  delete [] ds;
183  }
184 
185 };
186 
187 #endif
188 
ULI nnz
Number of non-zeros.
Definition: SparseMatrix.hpp:58
virtual void zax(const T *x, T *z)
In-place z=Ax calculation algorithm.
Definition: TS.hpp:133
void ZXa(const T *__restrict__ const *__restrict__ const X, T *__restrict__ const *__restrict__ const Z)
Definition: TS.hpp:160
virtual size_t bytesUsed()
Definition: TS.hpp:174
ULI * j
The column indices of the nonzeros.
Definition: TS.hpp:54
TS()
Base constructor.
Definition: TS.hpp:62
virtual void getFirstIndexPair(ULI &row, ULI &col)
Returns the first nonzero index, per reference.
Definition: TS.hpp:106
virtual unsigned long int m()
Queries the number of rows this matrix contains.
Definition: SparseMatrix.hpp:107
The triplet scheme; a storage scheme for sparse matrices using triplets.
Definition: TS.hpp:44
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
ULI * i
The row indices of the nonzeros.
Definition: TS.hpp:51
ULI noc
Number of columns.
Definition: SparseMatrix.hpp:55
virtual void zxa(const T *x, T *z)
In-place z=xA calculation algorithm.
Definition: TS.hpp:117
void ZaX(const T *__restrict__ const *__restrict__ const X, T *__restrict__ const *__restrict__ const Z)
Definition: TS.hpp:145
virtual void load(std::vector< Triplet< T > > &input, const ULI m, const ULI n, const T zero)
Definition: TS.hpp:83
ULI nor
Number of rows.
Definition: SparseMatrix.hpp:52
TS(std::string file, T zero=0)
Base constructor.
Definition: TS.hpp:68
T zero_element
The element considered to be zero.
Definition: SparseMatrix.hpp:63
T * ds
The values of the nonzeros.
Definition: TS.hpp:57
TS(std::vector< Triplet< T > > &input, ULI m, ULI n, T zero=0)
Base constructor.
Definition: TS.hpp:78
virtual unsigned long int n()
Queries the number of columns this matrix contains.
Definition: SparseMatrix.hpp:115
~TS()
Base destructor.
Definition: TS.hpp:179
A single triplet value.
Definition: Triplet.hpp:52