SparseLibrary  Version 1.6.0
CompressedHilbert.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 #ifndef _H_COMPRESSED_HILBERT
35 #define _H_COMPRESSED_HILBERT
36 
37 #include <vector>
38 
39 #include "Triplet.hpp"
40 #include "SparseMatrix.hpp"
41 #include "custom_quicksort.hpp"
42 #include "HilbertArray.hpp"
43 
51 template< typename T >
52 class CompressedHilbert: public SparseMatrix< T, ULI > {
53 
54  private:
55 
56  protected:
57 
59  T* values;
60 
63 
64  public:
65 
67  virtual ~CompressedHilbert() {
68  delete [] values;
69  delete indices;
70  }
71 
74  values = NULL;
75  indices = NULL;
76  }
77 
79  CompressedHilbert( std::string file, T zero = 0 ) {
80  this->loadFromFile( file, zero );
81  }
82 
84  CompressedHilbert( std::vector< Triplet< T > >& input, const ULI m, const ULI n, const T zero = 0 ) {
85  load( input, m, n, zero );
86  }
87 
89  virtual void load( std::vector< Triplet< T > >& input, const ULI m, const ULI n, const T zero ) {
90  this->zero_element = 0;
91  this->nor = m;
92  this->noc = n;
93  this->nnz = input.size();
94 
95  //calculate hilbert coordinates
96  std::vector< BigInt > hilbert_values;
97  typename std::vector< Triplet< T > >::const_iterator it;
98  size_t h1, h2;
99  for( it = input.begin(); it != input.end(); ++it ) {
100  Matrix2HilbertCoordinates::IntegerToHilbert( static_cast< size_t >(it->i()), static_cast< size_t >(it->j()), h1, h2 );
101  hilbert_values.push_back( BigInt( h1, h2 ) );
102  }
103 
104  //sort triplets and hilbert coordinates according to hilbert coordinates
105  custom_quicksort< BigInt, Triplet< T > >( hilbert_values, input, 0, input.size() );
106 
107  //check everything indeed is in ascending order
108  std::vector< BigInt >::iterator b_it1 = hilbert_values.begin();
109  std::vector< BigInt >::iterator b_it2 = hilbert_values.begin() + 1;
110  for( ; b_it2 != hilbert_values.end(); ++b_it1, ++b_it2 ) {
111  assert( *b_it1 < *b_it2 );
112  }
113 
114  //now use the HilbertArray class to get a compressed version of hilbert_values
115  indices = getDiffArray< T >( hilbert_values, m, n );
116 
117  //store nonzero values
118  values = new T[ this->nnz ];
119  size_t c = 0;
120  for( it = input.begin(); it != input.end(); ++it )
121  values[ c++ ] = it->value;
122 
123  //initialisation done
124  }
125 
127  virtual void getFirstIndexPair( ULI &row, ULI &col ) {
128  row = static_cast< ULI >( indices->getFirstRowIndex() );
129  col = static_cast< ULI >( indices->getFirstColumnIndex() );
130  }
131 
138  virtual void zxa( const T* x, T* z ) {
139  //handle boundary case
140  if( indices == NULL )
141  return;
142  //get pointers
143  const T * const val_end = values + this->nnz;
144  const T *__restrict__ p_v = values;
145  //do zxa
146  indices->zxa( x, z, p_v, val_end );
147  }
148 
155  virtual void zax( const T* x, T* z ) {
156  //handle boundary case
157  if( indices == NULL )
158  return;
159  //get pointers
160  const T * const val_end = values + this->nnz;
161  const T *__restrict__ p_v = values;
162  //do zax
163  indices->zax( x, z, p_v, val_end );
164 
165  /* Non-flat implementation:
166  //get restricted pointers as well as the end position
167  const T * const val_end = values + this->nnz;
168  const T *__restrict__ const p_x = x;
169  const T *__restrict__ p_v = values;
170  T *__restrict__ const p_z = z;
171  //initialise iterations through Hilbert indices
172  indices->moveToStart( p_x, p_z, *p_v++ );
173  //do iterations
174  while( p_v < val_end )
175  indices->moveToNext( p_x, p_z, *p_v++ );
176  */
177  }
178 
180  virtual size_t bytesUsed() {
181  if( indices == NULL )
182  return 0;
183  else
184  return indices->bytesUsed();
185  }
186 
187 };
188 
189 #endif
190 
ULI nnz
Number of non-zeros.
Definition: SparseMatrix.hpp:58
A Hilbert scheme backed by a specialised storage format.
Definition: CompressedHilbert.hpp:52
virtual void getFirstIndexPair(ULI &row, ULI &col)
Returns the first nonzero index, per reference.
Definition: CompressedHilbert.hpp:127
CompressedHilbert(std::vector< Triplet< T > > &input, const ULI m, const ULI n, const T zero=0)
Base constructor initialising from direct input.
Definition: CompressedHilbert.hpp:84
virtual void zxa(const T *x, T *z)
Calculates z=xA.
Definition: CompressedHilbert.hpp:138
CompressedHilbert(std::string file, T zero=0)
Base constructor initialising from file.
Definition: CompressedHilbert.hpp:79
Class providing an interface to an efficient storage of a 1D array of Hilbert coordinate increments...
Definition: HilbertArray.hpp:49
A 128-bit integer, with overloaded comparison operators.
Definition: BigInt.hpp:38
virtual unsigned long int m()
Queries the number of rows this matrix contains.
Definition: SparseMatrix.hpp:107
static void IntegerToHilbert(const size_t i, const size_t j, size_t &h1, size_t &h2)
New method, October 2010.
Definition: Matrix2HilbertCoordinates.cpp:48
void loadFromFile(const std::string file, const T zero=0)
Function which loads a matrix from a matrix market file.
Definition: SparseMatrix.hpp:89
Interface common to all sparse matrix storage schemes.
Definition: SparseMatrix.hpp:46
virtual size_t bytesUsed()
Definition: CompressedHilbert.hpp:180
ULI noc
Number of columns.
Definition: SparseMatrix.hpp:55
HilbertArrayInterface< T > * indices
Pointer to indices in Hilbert-coordinate form.
Definition: CompressedHilbert.hpp:62
ULI nor
Number of rows.
Definition: SparseMatrix.hpp:52
virtual ~CompressedHilbert()
Base deconstructor.
Definition: CompressedHilbert.hpp:67
T zero_element
The element considered to be zero.
Definition: SparseMatrix.hpp:63
T * values
Array of nonzero values.
Definition: CompressedHilbert.hpp:59
virtual void load(std::vector< Triplet< T > > &input, const ULI m, const ULI n, const T zero)
Definition: CompressedHilbert.hpp:89
CompressedHilbert()
Base constructor.
Definition: CompressedHilbert.hpp:73
virtual unsigned long int n()
Queries the number of columns this matrix contains.
Definition: SparseMatrix.hpp:115
virtual void zax(const T *x, T *z)
Calculates z=Ax.
Definition: CompressedHilbert.hpp:155
A single triplet value.
Definition: Triplet.hpp:52