SparseLibrary  Version 1.6.0
CCSWrapper.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, 2010.
31  */
32 
33 
34 #include <vector>
35 
36 #include "Triplet.hpp"
37 #include "SparseMatrix.hpp"
38 
39 #ifndef _H_CCSWRAPPER
40 #define _H_CCSWRAPPER
41 
48 template< typename T, typename SparseMatrixType, typename IND >
49 class CCSWrapper : public SparseMatrix< T, IND > {
50 
51 
52  protected:
53 
55  SparseMatrixType *ds;
56 
57  protected:
58 
65  void transposeVector( std::vector< Triplet< T > > &input ) {
66  typename std::vector< Triplet< T > >::iterator it = input.begin();
67  for( ; it != input.end(); ++it )
68  it->transpose();
69  }
70 
71  public:
72 
74  CCSWrapper() : ds( NULL ) {}
75 
77  CCSWrapper( std::string file, T zero = 0 ) { loadFromFile( file, zero ); }
78 
80  CCSWrapper( const IND nnz, const IND nor, const IND noc, T zero ) {
81  ds = new SparseMatrixType( nnz, noc, nor, zero );
82  }
83 
85  CCSWrapper( std::vector< Triplet< T > > &input, IND m, IND n, T zero ) {
86  load( input, m, n, zero );
87  }
88 
90  virtual ~CCSWrapper() {
91  if ( ds != NULL ) delete ds;
92  }
93 
95  virtual void load( std::vector< Triplet< T > > &input, IND m, IND n, T zero ) {
96  transposeVector( input );
97  ds = new SparseMatrixType( input, n, m, zero );
98  }
99 
101  virtual void loadFromFile( const std::string file, const T zero = 0 ) {
102  ULI m, n;
103  std::vector< Triplet< T > > VT = FileToVT::parse( file, m, n );
104  load( VT, m, n, zero );
105  }
106 
108  virtual ULI m() { return ds->n(); }
109 
111  virtual ULI n() { return ds->m(); }
112 
114  virtual ULI nzs() { return ds->nzs(); }
115 
117  virtual void getFirstIndexPair( IND &row, IND &col ) { ds->getFirstIndexPair( col, row ); }
118 
120  virtual T* mv( const T* x ) {
121  T * const ret = new T[ m() ];
122  for( unsigned long int i = 0; i < m(); i ++ ) ret[ i ] = ds->zero_element;
123  ds->zxa( x, ret );
124  return ret;
125  }
126 
128  virtual void zax( const T*__restrict__ x, T*__restrict__ z ) {
129  ds->zxa( x, z );
130  }
131 
133  virtual void zxa( const T*__restrict__ x, T*__restrict__ z ) {
134  ds->zax( x, z );
135  }
136 
138  virtual size_t bytesUsed() {
139  return ds->bytesUsed();
140  }
141 };
142 
143 #endif
144 
IND nnz
Number of non-zeros.
Definition: SparseMatrix.hpp:58
static std::vector< Triplet< double > > parse(std::string filename)
Parses a matrix-market input file.
Definition: FileToVT.cpp:36
Automatically transforms a row-major scheme into an column-major scheme.
Definition: CCSWrapper.hpp:49
CCSWrapper()
Default constructor (initialises with invalid data).
Definition: CCSWrapper.hpp:74
virtual ULI n()
Returns the number of matrix columns (taking into account transposition).
Definition: CCSWrapper.hpp:111
CCSWrapper(std::string file, T zero=0)
Base file-based constructor.
Definition: CCSWrapper.hpp:77
virtual size_t bytesUsed()
Definition: CCSWrapper.hpp:138
virtual T * mv(const T *x)
Definition: CCSWrapper.hpp:120
virtual ~CCSWrapper()
Base destructor.
Definition: CCSWrapper.hpp:90
Interface common to all sparse matrix storage schemes.
Definition: SparseMatrix.hpp:46
SparseMatrixType * ds
Pointer to the underlying data structure.
Definition: CCSWrapper.hpp:55
virtual void zxa(const T *__restrict__ x, T *__restrict__ z)
Definition: CCSWrapper.hpp:133
IND noc
Number of columns.
Definition: SparseMatrix.hpp:55
virtual void load(std::vector< Triplet< T > > &input, IND m, IND n, T zero)
Triplet-based loader; first transposes, then calls nested constructor.
Definition: CCSWrapper.hpp:95
virtual ULI m()
Returns the number of matrix rows (taking into account transposition).
Definition: CCSWrapper.hpp:108
virtual void zax(const T *__restrict__ x, T *__restrict__ z)
Definition: CCSWrapper.hpp:128
virtual ULI nzs()
Returns the number of nonzeroes.
Definition: CCSWrapper.hpp:114
CCSWrapper(const IND nnz, const IND nor, const IND noc, T zero)
Base empty matrix constructor (sets nnz, rows, columns only).
Definition: CCSWrapper.hpp:80
IND nor
Number of rows.
Definition: SparseMatrix.hpp:52
void transposeVector(std::vector< Triplet< T > > &input)
Helper function that transposes an input matrix in Triplet format, in-place.
Definition: CCSWrapper.hpp:65
virtual void getFirstIndexPair(IND &row, IND &col)
Definition: CCSWrapper.hpp:117
CCSWrapper(std::vector< Triplet< T > > &input, IND m, IND n, T zero)
Base Triplet-based constructor.
Definition: CCSWrapper.hpp:85
A single triplet value.
Definition: Triplet.hpp:52
virtual void loadFromFile(const std::string file, const T zero=0)
File-based loader; reads file, then passes to Triplet-based loader.
Definition: CCSWrapper.hpp:101