SparseLibrary  Version 1.6.0
Hilbert.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 #include "SparseMatrix.hpp"
36 #include "HilbertTriplet.hpp"
37 #include "HilbertTripletCompare.hpp"
38 #include "Triplet.hpp"
39 
40 #ifndef _H_HILBERT
41 #define _H_HILBERT
42 
43 #define _HILBERT_COMPRESSED_BICRS
44 
45 #ifdef _DEBUG
46  #include <iostream>
47 #endif
48 
49 #ifdef _HILBERT_COMPRESSED_BICRS
50  #include "CBICRS.hpp"
51 #else
52  #include "BICRS.hpp"
53 #endif
54 
58 template< typename T >
59 class Hilbert: public SparseMatrix< T, ULI > {
60 
61  private:
62 
63  protected:
64 
66  ULI minexp;
67 
69  std::vector< HilbertTriplet< T > > ds;
70 
73 
75  static Matrix< T >* getDataStructure( std::vector< Triplet< T > > &tds, const ULI m, const ULI n, T zero ) {
76 #ifdef _HILBERT_COMPRESSED_BICRS
77  return CBICRS_factory< T >::getCBICRS( tds, m, n, zero );
78 #else
79  return new BICRS< T >( tds, m, n, zero );
80 #endif
81  }
82 
83  public:
84 
86  virtual ~Hilbert() {
87  if( ads != NULL )
88  delete ads;
89  }
90 
92  Hilbert() {
93  ads = NULL;
94  }
95 
100  Hilbert( std::string file, T zero = 0 ) {
101  ads = NULL;
102  this->loadFromFile( file, zero );
103  }
104 
113  Hilbert( std::vector< Triplet< T > >& input, ULI m, ULI n, T zero ) {
114  ads = NULL;
115  load( input, m, n, zero );
116  }
117 
119  virtual void load( std::vector< Triplet< T > >& input, const ULI m, const ULI n, const T zero ) {
120  this->zero_element = 0;
121  this->nor = m;
122  this->noc = n;
123  this->nnz = input.size();
124  for( ULI i=0; i<this->nnz; i++ ) {
125  ds.push_back( HilbertTriplet< T >( input[ i ].i(), input[ i ].j(), input[ i ].value ) );
126  ds[ i ].calculateHilbertCoordinate();
127  }
129  std::sort( ds.begin(), ds.end(), compare );
130 #ifdef _DEBUG
131  for( ULI i=0; i<this->nnz; i++ )
132  std::cout << ds[i].getMostSignificantHilbertBits() << " " << ds[i].getLeastSignificantHilbertBits() << std::endl;
133 #endif
134  //create temporary structure
135  std::vector< Triplet< T > > tds;
136  typename std::vector< HilbertTriplet< T > >::iterator it = ds.begin();
137  for( ; it!=ds.end(); ++it )
138  tds.push_back( Triplet< T >( it->i(), it->j(), it->value ) );
139  //create actual structure
140  ads = getDataStructure( tds, m, n, zero );
141  }
142 
144  virtual void getFirstIndexPair( ULI &row, ULI &col ) {
145  row = ds[ 0 ].i();
146  col = ds[ 0 ].j();
147  }
148 
156  virtual void zxa( const T* x, T* z ) {
157  ads->zxa( x, z );
158  }
159 
167  virtual void zax( const T* x, T* z ) {
168  ads->zax( x, z );
169  }
170 
172  virtual size_t bytesUsed() {
173  return ads->bytesUsed();
174  }
175 
180  void saveBinary( const std::string fn ) {
181  HilbertTriplet< T >::save( fn, ds, this->nor, this->noc );
182  }
183 
185  void loadBinary( const std::string fn ) {
186  std::cerr << "Warning: assuming binary file was saved by a HTS scheme, i.e., that it is pre-ordered using Hilbert coordinates." << std::endl;
187  std::vector< Triplet< T > > tds = Triplet< T >::load( fn, this->nor, this->noc );
188  ads = getDataStructure( tds, this->nor, this->noc, 0 );
189  }
190 
191 };
192 
193 #endif
194 
ULI 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
virtual void zxa(const T *x, T *z)
Calculates z=xA.
Definition: Hilbert.hpp:156
Hilbert-coordinate-aware triplet.
Definition: HilbertTriplet.hpp:46
void saveBinary(const std::string fn)
Saves the current Hilbert structure in binary triplet form.
Definition: Hilbert.hpp:180
Bi-directional Incremental Compressed Row Storage scheme.
Definition: BICRS.hpp:58
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 ULI m, const ULI n, const T zero)
Definition: Hilbert.hpp:119
Class-comparator of HilbertTriplet.
Definition: HilbertTripletCompare.hpp:41
The Hilbert scheme backed by (C)BICRS.
Definition: Hilbert.hpp:59
static Matrix< T > * getDataStructure(std::vector< Triplet< T > > &tds, const ULI m, const ULI n, T zero)
Gets the data structure.
Definition: Hilbert.hpp:75
static Matrix< _t_value > * getCBICRS(std::string file, _t_value zero=0)
Factory function for row-based compressed BICRS functions, file-based.
Definition: CBICRS.hpp:842
static void save(std::string fn, HilbertTriplet< T > *toWrite, const unsigned long int m, const unsigned long int n, const size_t s)
Saves an array of Hilbert triplets to a file, in binary format.
Definition: HilbertTriplet.hpp:121
virtual void zax(const T *x, T *z)
Calculates z=Ax.
Definition: Hilbert.hpp:167
void loadFromFile(const std::string file, const T zero=0)
Function which loads a matrix from a matrix market file.
Definition: SparseMatrix.hpp:89
std::vector< HilbertTriplet< T > > ds
Vector storing the non-zeros and their locations.
Definition: Hilbert.hpp:69
Interface common to all sparse matrix storage schemes.
Definition: SparseMatrix.hpp:46
virtual size_t bytesUsed()
Gets the number of bytes used by this storage scheme.
Definition: Hilbert.hpp:172
ULI noc
Number of columns.
Definition: SparseMatrix.hpp:55
Hilbert(std::vector< Triplet< T > > &input, ULI m, ULI n, T zero)
Base constructor.
Definition: Hilbert.hpp:113
Matrix< T > * ads
Actual data structure.
Definition: Hilbert.hpp:72
Defines operations common to all matrices, which are implemented in this library. ...
Definition: Matrix.hpp:70
Hilbert(std::string file, T zero=0)
Base constructor.
Definition: Hilbert.hpp:100
void loadBinary(const std::string fn)
Loads from binary triplets, assumes Hilbert ordering already done.
Definition: Hilbert.hpp:185
ULI nor
Number of rows.
Definition: SparseMatrix.hpp:52
T zero_element
The element considered to be zero.
Definition: SparseMatrix.hpp:63
virtual ~Hilbert()
Base deconstructor.
Definition: Hilbert.hpp:86
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
Hilbert()
Base constructor.
Definition: Hilbert.hpp:92
virtual void getFirstIndexPair(ULI &row, ULI &col)
Returns the first nonzero index, per reference.
Definition: Hilbert.hpp:144
ULI minexp
Minimum number of expansions.
Definition: Hilbert.hpp:66