29 #ifndef __SPARSEMATRIX_HPP
30 #define __SPARSEMATRIX_HPP
32 #define NEW_SPARSE_MATRIX 1
33 #define ZERO_TESTING_JACOBI 1
52 int _maxEntriesPerRow;
83 bool write(
const char* fileName )
const;
85 bool read(
const char* fileName );
94 template<
class T2 >
static int SolveCG(
const SparseMatrix<T>& M ,
ConstPointer( T2 ) b ,
int iters ,
Pointer( T2 ) x , T2 eps=1e-8 ,
int reset=1 ,
bool addDCTerm=
false ,
bool solveNormal=
false ,
int threads=1 );
98 #if !NEW_SPARSE_MATRIX
100 struct MapReduceVector
105 std::vector< T2* > out;
106 MapReduceVector(
void ) { _dim = 0; }
107 ~MapReduceVector(
void )
109 if( _dim )
for(
int t=0 ; t<int(out.size()) ; t++ )
delete[] out[t];
112 T2* operator[](
int t ) {
return out[t]; }
113 const T2* operator[](
int t )
const {
return out[t]; }
114 int threads(
void )
const {
return int( out.size() ); }
115 void resize(
int threads ,
int dim )
117 if( threads!=out.size() || _dim<dim )
119 for(
int t=0 ; t<int(out.size()) ; t++ )
delete[] out[t];
120 out.resize( threads );
121 for(
int t=0 ; t<int(out.size()) ; t++ ) out[t] =
new T2[dim];
134 Vector< T2 >
operator * (
const Vector<T2>& V )
const;
137 Vector< T2 >
Multiply(
const Vector<T2>& V )
const;
140 void Multiply(
const Vector<T2>&
In, Vector<T2>&
Out ,
bool addDCTerm=
false )
const;
143 void Multiply(
const Vector<T2>&
In, Vector<T2>&
Out , MapReduceVector< T2 >& OutScratch ,
bool addDCTerm=
false )
const;
146 void Multiply(
const Vector<T2>&
In, Vector<T2>&
Out , std::vector< T2* >& OutScratch ,
const std::vector< int >& bounds )
const;
149 static int SolveCG(
const SparseSymmetricMatrix<T>& M ,
const Vector<T2>& b ,
int iters , Vector<T2>& x , T2 eps=1e-8 ,
int reset=1 ,
int threads=0 ,
bool addDCTerm=
false ,
bool solveNormal=
false );
152 static int SolveCG(
const SparseSymmetricMatrix<T>& M ,
const Vector<T2>& b ,
int iters , Vector<T2>& x , MapReduceVector<T2>& scratch , T2 eps=1e-8 ,
int reset=1 ,
bool addDCTerm=
false ,
bool solveNormal=
false );
155 static int SolveCGAtomic(
const SparseSymmetricMatrix<T>& M ,
const Vector<T2>& b ,
int iters , Vector<T2>& x , T2 eps=1e-8 ,
int reset=1 ,
int threads=0 ,
bool solveNormal=
false );
158 static int SolveJacobi(
const SparseSymmetricMatrix<T>& M ,
const Vector<T2>& diagonal ,
const Vector<T2>& b , Vector<T2>& x , MapReduceVector<T2>& scratch , Vector<T2>& Mx , T2 sor ,
int reset );
160 static int SolveJacobi(
const SparseSymmetricMatrix<T>& M ,
const Vector<T2>& b ,
int iters , Vector<T2>& x , MapReduceVector<T2>& scratch , T2 sor=T2(1.) ,
int reset=1 );
162 static int SolveJacobi(
const SparseSymmetricMatrix<T>& M ,
const Vector<T2>& diagonal ,
const Vector<T2>& b , Vector<T2>& x , Vector<T2>& Mx , T2 sor ,
int reset );
164 static int SolveJacobi(
const SparseSymmetricMatrix<T>& M ,
const Vector<T2>& b ,
int iters , Vector<T2>& x , T2 sor=T2(1.) ,
int reset=1 );
168 ORDERING_UPPER_TRIANGULAR ,
169 ORDERING_LOWER_TRIANGULAR ,
173 static int SolveGS(
const std::vector< std::vector< int > >& mcIndices ,
const SparseSymmetricMatrix<T>& M ,
const Vector<T2>& diagonal ,
const Vector<T2>& b , Vector<T2>& x , MapReduceVector<T2>& scratch , Vector<T2>& Mx , Vector<T2>& dx ,
bool forward ,
int reset );
175 static int SolveGS(
const std::vector< std::vector< int > >& mcIndices ,
const SparseSymmetricMatrix<T>& M ,
const Vector<T2>& b ,
int iters , Vector<T2>& x , MapReduceVector<T2>& scratch ,
bool forward ,
int reset=1 );
178 static int SolveGS(
const SparseSymmetricMatrix<T>& M ,
const Vector<T2>& diagonal ,
const Vector<T2>& b , Vector<T2>& x , MapReduceVector<T2>& scratch , Vector<T2>& Mx , Vector<T2>& dx ,
bool forward ,
int reset ,
int ordering );
180 static int SolveGS(
const SparseSymmetricMatrix<T>& M ,
const Vector<T2>& b ,
int iters , Vector<T2>& x , MapReduceVector<T2>& scratch ,
bool forward ,
int reset=1 ,
int ordering=ORDERING_NONE );
182 static int SolveGS(
const SparseSymmetricMatrix<T>& M ,
const Vector<T2>& diagonal ,
const Vector<T2>& b , Vector<T2>& x , Vector<T2>& Mx , Vector<T2>& dx ,
bool forward ,
int reset ,
int ordering );
184 static int SolveGS(
const SparseSymmetricMatrix<T>& M ,
const Vector<T2>& b ,
int iters , Vector<T2>& x ,
bool forward ,
int reset=1 ,
int ordering=ORDERING_NONE );
187 void getDiagonal( Vector< T2 >& diagonal ,
int threads=1 )
const;
191 #include "SparseMatrix.inl"
#define ConstPointer(...)
static int SolveGS(const SparseMatrix< T > &M, const T2 *diagonal, const T2 *b, T2 *x, bool forward)
void SetRowSize(int row, int count)
void Multiply(const T2 *in, T2 *out, int threads=1) const
SparseMatrix(const SparseMatrix &M)
SparseMatrix< T > & operator*=(const T &V)
bool read(const char *fileName)
static int SolveJacobi(const SparseMatrix< T > &M, const T2 *b, T2 *x, T2 *Mx, T2 sor, int threads=1)
static int SolveCG(const SparseMatrix< T > &M, const T2 *b, int iters, T2 *x, T2 eps=1e-8, int reset=1, bool addDCTerm=false, bool solveNormal=false, int threads=1)
bool write(const char *fileName) const
static int SolveGS(const SparseMatrix< T > &M, const T2 *b, T2 *x, bool forward)
static int SolveJacobi(const SparseMatrix< T > &M, const T2 *diagonal, const T2 *b, T2 *x, T2 *Mx, T2 sor, int threads=1)
bool write(FILE *fp) const
MatrixEntry< T > ** m_ppElements
MatrixEntry< T > * operator[](int idx)
SparseMatrix< T > & operator=(const SparseMatrix< T > &M)
static int SolveGS(const std::vector< std::vector< int > > &mcIndices, const SparseMatrix< T > &M, const T2 *diagonal, const T2 *b, T2 *x, bool forward, int threads=1)
void getDiagonal(T2 *diagonal, int threads=1) const
SparseMatrix< T > operator*(const T &V) const
SparseMatrix(int rows, int maxEntriesPerRow)
void Resize(int rows, int maxEntriesPerRow)
static int SolveGS(const std::vector< std::vector< int > > &mcIndices, const SparseMatrix< T > &M, const T2 *b, T2 *x, bool forward, int threads=1)
void MultiplyAndAddAverage(const T2 *in, T2 *out, int threads=1) const