SparseLibrary  Version 1.6.0
Matrix.hpp
Go to the documentation of this file.
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 
61 #ifndef _H_M
62 #define _H_M
63 
64 #include <time.h>
65 
69 template< typename T >
70 class Matrix {
71  private:
72 
73  protected:
74 
75  public:
76 
78  Matrix() {}
79 
81  virtual ~Matrix() {}
82 
84  virtual unsigned long int m() = 0;
85 
87  virtual unsigned long int n() = 0;
88 
90  virtual unsigned long int nzs() {
91  return m() * n();
92  }
93 
101  virtual T* mv( const T* x ) = 0;
102 
109  virtual void zax( const T*__restrict__ x, T*__restrict__ z ) = 0;
110 
124  virtual void zax( const T*__restrict__ x, T*__restrict__ z, const size_t k, const clockid_t clock_id = 0, double *elapsed_time = NULL ) {
125  struct timespec start, stop;
126  if( elapsed_time != NULL ) {
127  clock_gettime( clock_id, &start);
128  }
129  for( size_t i = 0; i < k; ++i ) {
130  zax( x, z );
131  }
132  if( elapsed_time != NULL ) {
133  clock_gettime( clock_id, &stop);
134  double time = (stop.tv_sec-start.tv_sec)*1000;
135  time += (stop.tv_nsec-start.tv_nsec)/1000000.0;
136  *elapsed_time += time;
137  }
138  }
139 
152  template< size_t k >
153  void ZaX( const T*__restrict__ const *__restrict__ const X, T *__restrict__ const *__restrict__ const Z ) {
154  //loop k times
155  for( size_t i = 0; i < k; ++i ) {
156  //call regular z=Ax function
157  zax( X[i], Z[i] );
158  }
159  //done
160  }
161 
168  virtual void zxa( const T*__restrict__ x, T*__restrict__ z ) = 0;
169 
183  template< size_t k >
184  void ZXa( const T *__restrict__ const *__restrict__ const X, T *__restrict__ const *__restrict__ const Z ) {
185  //loop k times
186  for( size_t i = 0; i < k; ++i ) {
187  //call regular z=xA function
188  zxa( X[i], Z[i] );
189  }
190  //done
191  }
192 
194  virtual void zxa( const T*__restrict__ x, T*__restrict__ z, const unsigned long int repeat, const clockid_t clock_id = 0, double *elapsed_time = NULL ) {
195  struct timespec start, stop;
196  if( elapsed_time != NULL ) clock_gettime( clock_id, &start);
197  for( unsigned long int i=0; i<repeat; i++ )
198  zxa( x, z );
199  if( elapsed_time != NULL ) {
200  clock_gettime( clock_id, &stop);
201  double time = (stop.tv_sec-start.tv_sec)*1000;
202  time += (stop.tv_nsec-start.tv_nsec)/1000000.0;
203  *elapsed_time += time;
204  }
205  }
206 
212  virtual size_t bytesUsed() = 0;
213 };
214 
215 #endif //_H_M
216 
virtual unsigned long int nzs()
Definition: Matrix.hpp:90
virtual void zax(const T *__restrict__ x, T *__restrict__ z)=0
In-place z=Ax function.
virtual unsigned long int n()=0
virtual void zax(const T *__restrict__ x, T *__restrict__ z, const size_t k, const clockid_t clock_id=0, double *elapsed_time=NULL)
Wrapper function to call the zax kernel multiple times successively, while timing the duration of the...
Definition: Matrix.hpp:124
void ZaX(const T *__restrict__ const *__restrict__ const X, T *__restrict__ const *__restrict__ const Z)
In-place Z=AX function, where A is m x n, Z = m x k, and X is n x k.
Definition: Matrix.hpp:153
virtual size_t bytesUsed()=0
Function to query the amount of storage required by this sparse matrix.
virtual T * mv(const T *x)=0
Calculates z=Ax (where A is this matrix).
Matrix()
Base constructor.
Definition: Matrix.hpp:78
virtual unsigned long int m()=0
void ZXa(const T *__restrict__ const *__restrict__ const X, T *__restrict__ const *__restrict__ const Z)
In-place Z=XA function, where A is m x n, Z = k x n, and X is k x m.
Definition: Matrix.hpp:184
Defines operations common to all matrices, which are implemented in this library. ...
Definition: Matrix.hpp:70
virtual void zxa(const T *__restrict__ x, T *__restrict__ z, const unsigned long int repeat, const clockid_t clock_id=0, double *elapsed_time=NULL)
Wrapper function to call the zxa kernel multiple times successively, while timing the operation durat...
Definition: Matrix.hpp:194
virtual void zxa(const T *__restrict__ x, T *__restrict__ z)=0
In-place z=xA function.
virtual ~Matrix()
Base deconstructor.
Definition: Matrix.hpp:81