SparseLibrary  Version 1.6.0
MKLCRS.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 Computer Science, KU Leuven, 2012.
31  */
32 
33 
34 #include "CRS.hpp"
35 
36 extern "C" {
37  #include <mkl_service.h>
38  #include <mkl_spblas.h>
39 }
40 
41 //#define _DEBUG
42 
43 #ifndef _H_MKLCRS
44 #define _H_MKLCRS
45 
49 template< typename T >
50 class MKLCRS: public CRS< T > {
51 
52  private:
53 
54  protected:
55 
57  double _one;
58 
60  char trans;
61 
63  char *descr;
64 
66  int _m;
67 
69  int _n;
70 
72  int *_col_ind;
73 
75  int *_row_start;
76 
81  void prepare() {
82  _m = static_cast< int >( this->nor );
83  _n = static_cast< int >( this->noc );
84  _one = 1.0;
85 
86  _col_ind = new int[ this->nnz ];
87  for( ULI i=0; i<this->nnz; ++i )
88  _col_ind[ i ] = static_cast< int >( this->col_ind[ i ] );
89 
90  _row_start = new int[ _m + 1 ];
91  for( int i=0; i<=_m; ++i )
92  _row_start[ i ] = static_cast< int >( this->row_start[ i ] );
93 
94  trans = 'n';
95 
96  descr = new char[4];
97  descr[ 0 ] = 'G';
98  descr[ 1 ] = descr[ 2 ] = '*';
99  descr[ 3 ] = 'C';
100  }
101 
102  public:
103 
105  MKLCRS() {}
106 
111  MKLCRS( std::string file, T zero = 0 ) {
112  this->loadFromFile( file, zero );
113 
114  prepare();
115  }
116 
126  MKLCRS( const ULI number_of_nonzeros, const ULI number_of_rows, const ULI number_of_cols, T zero ) {
127  this->nnz = number_of_nonzeros;
128  this->nor = number_of_rows;
129  this->noc = number_of_cols;
130  this->zero_element = zero;
131  this->row_start = new ULI[ this->nor + 1 ];
132  this->ds = new T[ this->nnz ];
133  this->col_ind = new ULI[ this->nnz ];
134 
135  prepare();
136  }
137 
141  MKLCRS( CRS< T >& toCopy ) {
142  this->zero_element = toCopy.zero_element;
143  this->nnz = toCopy.nnz;
144  this->nor = toCopy.nor;
145  this->row_start = new ULI[ this->nor + 1 ];
146  this->ds = new T[ this->nnz ];
147  this->col_ind = new ULI[ this->nnz ];
148  for( ULI i=0; i<this->nnz; i++ ) {
149  this->ds[ i ] = toCopy.ds[ i ];
150  this->col_ind[ i ] = toCopy.col_ind[ i ];
151  }
152  for( ULI i=0; i<this->nor; i++ )
153  this->row_start[ i ] = toCopy.row_start[ i ];
154 
155  prepare();
156  }
157 
168  MKLCRS( std::vector< Triplet< T > > input, ULI m, ULI n, T zero ) {
169  load( input, m, n, zero );
170 
171  prepare();
172  }
173 
174 #ifndef _NO_LIBNUMA
175 
176  virtual T* mv( const T* x ) {
177  T* ret = (T*) numa_alloc_interleaved( this->nor * sizeof( T ) );
178  for( ULI i=0; i<this->nor; i++ )
179  ret[ i ] = this->zero_element;
180  this->zax( x, ret );
181  return ret;
182  }
183 #endif
184 
188  virtual void zxa( const T*__restrict__ x, T*__restrict__ z ) {
189  std::cerr << "MKLCRS does not implement the zxa, sorry!" << std::endl;
190  exit( EXIT_FAILURE );
191  }
192 
199  virtual void zax( const T*__restrict__ x, T*__restrict__ z ) {
200  //Warning: assumes T is a double
201  mkl_dcsrmv( &trans, &_m, &_n, &_one, descr, this->ds, _col_ind, _row_start, &( _row_start[ 1 ] ), (T*__restrict__)x, &_one, z );
202  }
203 
205  virtual ~MKLCRS() {
206  delete [] _col_ind;
207  delete [] _row_start;
208  delete [] descr;
209  }
210 
211 };
212 
213 #endif
214 
ULI nnz
Number of non-zeros.
Definition: SparseMatrix.hpp:58
void prepare()
Does the required post-processing of Sparse Library's CRS representation to one compatible with Intel...
Definition: MKLCRS.hpp:81
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
MKLCRS()
Base constructor.
Definition: MKLCRS.hpp:105
ULI * col_ind
Array containing the column indeces corresponding to the elements in ds.
Definition: CRS.hpp:65
MKLCRS(std::vector< Triplet< T > > input, ULI m, ULI n, T zero)
Constructor which transforms a collection of input triplets to CRS format.
Definition: MKLCRS.hpp:168
virtual unsigned long int m()
Queries the number of rows this matrix contains.
Definition: SparseMatrix.hpp:107
int _m
Required for call to MKL; matrix row-size.
Definition: MKLCRS.hpp:66
virtual void zxa(const T *__restrict__ x, T *__restrict__ z)
In-place z=xA function.
Definition: MKLCRS.hpp:188
The compressed row storage sparse matrix data structure.
Definition: CRS.hpp:52
MKLCRS(std::string file, T zero=0)
Base constructor.
Definition: MKLCRS.hpp:111
int * _col_ind
Required for call to MKL; a plain-int version of col_ind.
Definition: MKLCRS.hpp:72
virtual ~MKLCRS()
Base deconstructor.
Definition: MKLCRS.hpp:205
double _one
Required for call to MKL; a factor alpha=beta of 1.
Definition: MKLCRS.hpp:57
void loadFromFile(const std::string file, const T zero=0)
Function which loads a matrix from a matrix market file.
Definition: SparseMatrix.hpp:89
char trans
Required for call to MKL; (no) transposition.
Definition: MKLCRS.hpp:60
ULI noc
Number of columns.
Definition: SparseMatrix.hpp:55
MKLCRS(CRS< T > &toCopy)
Copy constructor.
Definition: MKLCRS.hpp:141
int * _row_start
Required for call to MKL; a plain-int version of row_start.
Definition: MKLCRS.hpp:75
int _n
Required for call to MKL; matrix column-size.
Definition: MKLCRS.hpp:69
ULI nor
Number of rows.
Definition: SparseMatrix.hpp:52
T zero_element
The element considered to be zero.
Definition: SparseMatrix.hpp:63
virtual void zax(const T *__restrict__ x, T *__restrict__ z)
In-place z=Ax function.
Definition: MKLCRS.hpp:199
T * ds
Array containing the actual nnz non-zeros.
Definition: CRS.hpp:62
MKLCRS(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: MKLCRS.hpp:126
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
The compressed row storage sparse matrix data structure.
Definition: MKLCRS.hpp:50
char * descr
Required for call to MKL; matrix descriptor.
Definition: MKLCRS.hpp:63
virtual T * mv(const T *x)
Overloaded mv call; allocates output vector using numa_interleaved.
Definition: MKLCRS.hpp:176