SparseLibrary  Version 1.6.0
SparseMatrix.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, 2009.
31  */
32 
33 
34 #ifndef _H_SM
35 #define _H_SM
36 
37 #include "ULIdef.hpp"
38 #include "Matrix.hpp"
39 #include "FileToVT.hpp"
40 #include "alignment.hpp"
41 
45 template< typename T, typename IND >
46 class SparseMatrix: public Matrix< T > {
47  private:
48 
49  protected:
50 
52  IND nor;
53 
55  IND noc;
56 
58  IND nnz;
59 
60  public:
61 
64 
67 
69  SparseMatrix( const IND nzs, const IND nr, const IND nc, const T zero ):
70  nnz( nzs ), nor( nr ), noc( nc ), zero_element( zero ) {}
71 
73  virtual ~SparseMatrix() {}
74 
82  virtual void load( std::vector< Triplet< T > >& input, const IND m, const IND n, const T zero ) = 0;
83 
89  void loadFromFile( const std::string file, const T zero=0 ) {
90  ULI m, n;
91  const size_t pos = file.find_last_of( '.' );
92  const std::string ext = file.substr( pos + 1, file.length() );
93  std::vector< Triplet< T > > VT;
94  if( ext.compare( "trp" ) == 0 ) {
95  VT = Triplet< T >::load( file, m, n );
96  } else if( ext.compare( "crs" ) == 0 || ext.compare( "csr" ) == 0 ) {
97  VT = Triplet< T >::loadCRS( file, m, n );
98  } else //assume default matrix-market format
99  VT = FileToVT::parse( file, m, n );
100  this->load( VT, m, n, zero );
101  }
102 
107  virtual unsigned long int m() {
108  return static_cast< unsigned long int >( nor );
109  }
110 
115  virtual unsigned long int n() {
116  return static_cast< unsigned long int >( noc );
117  }
118 
123  virtual unsigned long int nzs() {
124  return static_cast< unsigned long int >( nnz );
125  }
126 
128  virtual void getFirstIndexPair( IND &row, IND &col ) = 0;
129 
142  virtual T* mv( const T* x ) {
143  T* ret = NULL;
144  //allocate output vector, catch error
145  if( posix_memalign( (void**)&ret, 64, nor * sizeof( T ) ) != 0 ) {
146  std::cerr << "SparseMatrix::mv(): could not allocate output vector; returning NULL..." << std::endl;
147  return NULL;
148  }
149  //initialise output vector
150  for( IND i=0; i<nor; i++ ) {
151  ret[ i ] = zero_element;
152  }
153  //call in-place z=Ax
154  zax( x, ret );
155  //return new output vector
156  return ret;
157  }
158 
165  virtual void zax( const T*__restrict__ x, T*__restrict__ z ) = 0;
166 
173  virtual void zxa( const T*__restrict__ x, T*__restrict__ z ) = 0;
174 
175 };
176 
177 #endif //_H_SM
178 
IND nnz
Number of non-zeros.
Definition: SparseMatrix.hpp:58
static std::vector< Triplet< T > > load(const std::string fn, ULI &m, ULI &n)
Loads an array of triplets from a binary file.
Definition: Triplet.hpp:123
static std::vector< Triplet< double > > parse(std::string filename)
Parses a matrix-market input file.
Definition: FileToVT.cpp:36
virtual ~SparseMatrix()
Base deconstructor.
Definition: SparseMatrix.hpp:73
File created by: A.
SparseMatrix(const IND nzs, const IND nr, const IND nc, const T zero)
Base constructor.
Definition: SparseMatrix.hpp:69
virtual void zxa(const T *__restrict__ x, T *__restrict__ z)=0
In-place z=xA function.
SparseMatrix()
Base constructor.
Definition: SparseMatrix.hpp:66
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 IND m, const IND n, const T zero)=0
Function reading in from std::vector< Triplet< T > > format.
void loadFromFile(const std::string file, const T zero=0)
Function which loads a matrix from a matrix market file.
Definition: SparseMatrix.hpp:89
virtual unsigned long int nzs()
Queries the number of nonzeroes stored in this matrix.
Definition: SparseMatrix.hpp:123
Interface common to all sparse matrix storage schemes.
Definition: SparseMatrix.hpp:46
IND noc
Number of columns.
Definition: SparseMatrix.hpp:55
Defines operations common to all matrices, which are implemented in this library. ...
Definition: Matrix.hpp:70
static std::vector< Triplet< T > > loadCRS(const std::string fn, ULI &m, ULI &n)
Loads a CRS text file and transforms it into a vector of Triplets.
Definition: Triplet.hpp:160
IND nor
Number of rows.
Definition: SparseMatrix.hpp:52
virtual void zax(const T *__restrict__ x, T *__restrict__ z)=0
In-place z=Ax function.
T zero_element
The element considered to be zero.
Definition: SparseMatrix.hpp:63
virtual T * mv(const T *x)
Calculates and returns z=Ax.
Definition: SparseMatrix.hpp:142
virtual void getFirstIndexPair(IND &row, IND &col)=0
Returns the first nonzero index, per reference.
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