36 #include "SparseMatrix.hpp"
37 #include "HilbertTriplet.hpp"
38 #include "HilbertTripletCompare.hpp"
39 #include "Triplet.hpp"
50 template<
typename T >
61 std::vector< HilbertTriplet< T > >
ds;
66 if( left.
i() == right.
i() && left.
j() == right.
j() )
return true;
68 const std::vector< unsigned long int > h_one = left.getHilbert();
69 const std::vector< unsigned long int > h_two = right.getHilbert();
71 unsigned long int max = h_one.size();
72 bool max_is_one =
true;
73 if ( h_two.size() < max ) { max = h_two.size(); max_is_one =
false; }
74 for(
unsigned long int i=0; i<max; i++ )
75 if( h_one[ i ] != h_two[ i ] )
76 return h_one[ i ] < h_two[ i ];
78 std::cout <<
"expand, ";
81 left.morePrecision( this->
nor, this->
noc );
83 right.morePrecision( this->
nor, this->
noc );
85 return cmp( left, right );
97 std::cout <<
"left: " << left <<
", right: " << right << std::endl;
99 if(
ds[left].getHilbert().size() <
ds[
minexp].getHilbert().size() )
minexp = left;
100 if(
ds[right].getHilbert().size() <
ds[
minexp].getHilbert().size() )
minexp = right;
102 if ( left == right )
return left;
103 if ( left+1 == right )
return right;
104 if (
cmp( x,
ds[ left ] ) )
return left;
105 if (
cmp(
ds[ right ], x ) )
return right+1;
107 ULI mid =
static_cast< unsigned long int >( ( left + right ) / 2 );
109 std::cout <<
"mid: " << mid << std::endl;
111 if (
cmp( x,
ds[ mid ] ) )
112 return find( x, left, mid );
114 return find( x, mid, right );
126 HTS( std::string file, T zero = 0 ) {
139 load( input, m, n, zero );
147 this->
nnz = input.size();
148 for( ULI i=0; i<this->
nnz; i++ ) {
150 ds[ i ].calculateHilbertCoordinate();
153 std::sort(
ds.begin(),
ds.end(), compare );
155 for( ULI i=0; i<this->
nnz; i++ )
156 std::cout <<
ds[i].getMostSignificantHilbertBits() <<
" " <<
ds[i].getLeastSignificantHilbertBits() << std::endl;
173 virtual void zxa(
const T* x, T* z ) {
174 for( ULI i=0; i<this->
nnz; i++ )
175 z[
ds[i].j() ] +=
ds[i].value * x[
ds[i].i() ];
185 virtual void zax(
const T* x, T* z ) {
186 for( ULI i=0; i<this->
nnz; i++ )
187 z[
ds[i].i() ] +=
ds[i].value * x[
ds[i].j() ];
191 return sizeof( ULI ) * 2 * this->
nnz +
sizeof( T ) * this->
nnz;
204 std::cerr <<
"Warning: assuming binary file was saved by HTS scheme, i.e., that it is pre-ordered using Hilbert coordinates." << std::endl;
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
std::vector< HilbertTriplet< T > > ds
Vector storing the non-zeros and their locations.
Definition: HTS.hpp:61
unsigned long int find(HilbertTriplet< T > &x, ULI &left, ULI &right)
Binary search for finding a given triplet in a given range.
Definition: HTS.hpp:95
bool cmp(HilbertTriplet< T > &left, HilbertTriplet< T > &right)
HilbertCoordinate comparison function.
Definition: HTS.hpp:64
Hilbert-coordinate-aware triplet.
Definition: HilbertTriplet.hpp:46
virtual size_t bytesUsed()
Function to query the amount of storage required by this sparse matrix.
Definition: HTS.hpp:190
void saveBinary(const std::string fn)
Saves the current HTS.
Definition: HTS.hpp:198
virtual unsigned long int m()
Queries the number of rows this matrix contains.
Definition: SparseMatrix.hpp:107
Class-comparator of HilbertTriplet.
Definition: HilbertTripletCompare.hpp:41
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
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
ULI noc
Number of columns.
Definition: SparseMatrix.hpp:55
void loadBinary(const std::string fn)
Loads from binary triplets, assumes Hilbert ordering already done.
Definition: HTS.hpp:203
ULI nor
Number of rows.
Definition: SparseMatrix.hpp:52
T zero_element
The element considered to be zero.
Definition: SparseMatrix.hpp:63
unsigned long int i() const
Definition: HilbertTriplet.hpp:65
ULI minexp
Minimum number of expansions.
Definition: HTS.hpp:58
The Hilbert triplet scheme.
Definition: HTS.hpp:51
virtual void getFirstIndexPair(ULI &row, ULI &col)
Returns the first nonzero index, per reference.
Definition: HTS.hpp:161
HTS(std::string file, T zero=0)
Base constructor.
Definition: HTS.hpp:126
HTS(std::vector< Triplet< T > > &input, ULI m, ULI n, T zero)
Base constructor.
Definition: HTS.hpp:138
virtual void load(std::vector< Triplet< T > > &input, const ULI m, const ULI n, const T zero)
Definition: HTS.hpp:143
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
HTS()
Base constructor.
Definition: HTS.hpp:120
virtual void zax(const T *x, T *z)
Calculates z=Ax.
Definition: HTS.hpp:185
unsigned long int j() const
Definition: HilbertTriplet.hpp:68
virtual void zxa(const T *x, T *z)
Calculates z=xA.
Definition: HTS.hpp:173