24#ifndef _GEMA_SPARSE_MATRIX_H_
25#define _GEMA_SPARSE_MATRIX_H_
43 virtual void matrixLayoutReady() = 0;
49template<
class IndexType, GmSparseMatrixLayoutTypes T>
70 return (_opt.mode() ==
GM_TRIPLET_LIST) ? THREAD_SAFE_SUPPORT : REENTRANT_SUPPORT;
87 virtual bool symmetric()
const {
return _sym.loadAcquire(); }
95 virtual int nlin()
const {
return _n; }
98 virtual int ncol()
const {
return _n; }
104 virtual double at(
int lin,
int col)
const
108 assert(lin >= 0 && lin < _n);
109 assert(col >= 0 && col < _n);
115 int index = _layout->index(lin, col);
116 assert(index >= -1 && index < _layout->_nnz);
118 return index >= 0 ? _values[index] : 0.0;
126 assert(lin >= 0 && lin < _n);
127 assert(col >= 0 && col < _n);
132 return (_layout->index(lin, col) >= 0);
136 virtual void set(
int lin,
int col,
double value)
139 assert(_layout && !_layout->empty());
140 assert(lin >= 0 && lin < _n);
141 assert(col >= 0 && col < _n);
144 int index = _layout->index(lin, col);
145 assert(index >= 0 && index < _layout->_nnz);
147 _values[index] = value;
151 virtual void add(
int lin,
int col,
double value)
153 assert(_batch || (_layout && !_layout->empty()));
154 assert(lin >= 0 && lin < _n);
155 assert(col >= 0 && col < _n);
158 addBatch(lin, col, value);
164 int index = _layout->index(lin, col);
165 assert(index >= 0 && index < _layout->_nnz);
166 _values[index] += value;
172 virtual void clear(
bool keepSparseLayout);
193 for(
int i=0; i<_layout->_nnz; i++)
195 if(std::isnan(_values[i]) || std::isinf(_values[i]))
204 virtual void checkLayoutData()
const;
211 virtual void matrixLayoutReady();
213 void addBatch(
int lin,
int col,
double value);
214 void addToPtrIndexFromThread(
int tid,
int ptr,
int index,
double value);
Auxiliar class used to store the complete set of simulation data.
Definition gmSimulationData.h:55
Base interface class for Solver Matrix objects.
Definition gmSolverMatrix.h:100
virtual void clear(bool keepSparseLayout)=0
Clears the matrix, filling it with zeros. The keepSparseLayout flag is a hint that the matrix layout ...
virtual void mul(const GmVector &a, GmVector &b) const =0
Multiplies the matrix ('X') by a vector 'a' storing the result in 'b' (b = X * a).
virtual void columnMulAdd(int col, GmVector &f, double v, const bool *skipRows=NULL) const =0
Updates the given vector adding to it a matrix column multiplied by a scalar value.
virtual void clearLineAndColumnSet(const QVector< int > &indexList, bool setDiagonal, bool keepSparseLayout)=0
Clears a set of lines and columns from the matrix, filling them with zeroes, optionally putting a 1....
virtual void mulAdd(const GmVector &a, GmVector &b) const =0
Multiplies the matrix ('X') by a vector 'a' adding the result to 'b' (b = b + X * a).
virtual void ensureDiagonal()=0
Updates any zero diagonal value to 1.0. If diagonal values do not belong to the layout,...
virtual bool isSymmetric(double relTol=GM_DOUBLECMP_RELTOL, double absTol=GM_DOUBLECMP_ABSTOL) const
Returns true if the matrix is symmetric, false otherwise.
Definition gmSolverMatrix.cpp:35
virtual void set(int lin, int col, double value)=0
Sets the value in the position Mat[lin][col] to the specified value.
virtual void matAdd(const GmVector &a, const GmVector &b, double zeroTol=0.0)=0
Adds to the current matrix ('X') the (dense) matrix resulting from multiplying the column vector 'a' ...
virtual bool endBatchInsert(bool discardData=false)=0
Ends a batch insert process. Important: see comments on the class documentation.
virtual size_t usedMemory() const =0
Returns an estimative of the memory used by the matrix in bytes.
ParallelAddMode
Supported modes for calling add from multiple threads in parallel.
Definition gmSolverMatrix.h:110
virtual bool saveMatrixCoordinatesToFile(FILE *f) const =0
Helper function to GmNumSolver saving capabilities, responsible for saving the matrix contents to the...
virtual void mulSub(const GmVector &a, GmVector &b) const =0
Multiplies the matrix ('X') by a vector 'a' subtracting the result from 'b' (b = b - X * a).
virtual bool beginBatchInsert(size_t expectedEntries=0)=0
Begins a batch insert process. Important: see comments on the class documentation.
A base class derived from QObject. Needed since GmSparseMatrix is a template and so can't inherit dir...
Definition gmSparseMatrix.h:39
A class for wrapping up a sparse matrix, either using CSR or CSC format. Can also differ on the type ...
Definition gmSparseMatrix.h:51
virtual ParallelAddMode supportedParallelAddMode() const
Returns the supported mode for calling add.
Definition gmSparseMatrix.h:68
bool _batch
Flag to signal that the matrix is inside a batch op (for _opt.mode() == TRIPLET_LIST/STRIPLET_LIST)
Definition gmSparseMatrix.h:220
virtual void set(int lin, int col, double value)
Sets the value in the position Mat[lin][col] to the specified value.
Definition gmSparseMatrix.h:136
virtual bool supportsBatchInsert() const
Does this matrix supports batch inserts? See comments on the class documentation.
Definition gmSparseMatrix.h:59
virtual int ncol() const
Returns the number of columns in the matrix. IMPORTANT: This function implementation MUST be thread s...
Definition gmSparseMatrix.h:98
virtual bool supportsSparseLayouts() const
Does this matrix supports sparse layouts? See comments on the class documentation.
Definition gmSparseMatrix.h:62
const GmCSxSparseMatrixLayout< IndexType, T > * _layout
The read-only matrix layout data. Can be shared from other matrices.
Definition gmSparseMatrix.h:225
virtual bool supportsRandomSet() const
Does this matrix supports setting a value on a random position outside the matrix initialization proc...
Definition gmSparseMatrix.h:65
virtual bool inLayout(int lin, int col) const
Returns true if the given position belongs to the matrix sparse layout.
Definition gmSparseMatrix.h:122
virtual void add(int lin, int col, double value)
Adds the given value to the value in the position Mat[lin][col].
Definition gmSparseMatrix.h:151
virtual bool emptyLayout() const
If the matrix supports sparse layouts and the layout is currently empty, returns true....
Definition gmSparseMatrix.h:80
virtual double at(int lin, int col) const
Returns the value in the position Mat[lin][col].
Definition gmSparseMatrix.h:104
GmCSxSparseMatrixLayout< IndexType, T > * _masterLayout
The master layout data (equal to _layout) if this matrix is the layout owner, NULL for matrices shari...
Definition gmSparseMatrix.h:226
int _n
The number of lines and columns in the matrix.
Definition gmSparseMatrix.h:221
virtual GmSparseMatrixLayoutBuilder * layoutBuilder() const
If the matrix supports sparse layouts, returns a builder object that can be used to initialize the ma...
Definition gmSparseMatrix.h:77
virtual void setSymmetric(bool sym)
Marks the matrix as symmetric or not.
Definition gmSparseMatrix.h:90
virtual bool symmetric() const
Returns true if the matrix was marked as symmetric by setSymmetric() (it does not check for matrix sy...
Definition gmSparseMatrix.h:87
QAtomicInt _sym
Flag marking the matrix as symmetric or not.
Definition gmSparseMatrix.h:222
GmSparseMatrixOptions _opt
The matrix options, including the mode used to build the matrix.
Definition gmSparseMatrix.h:219
double * _values
The vector with matrix values.
Definition gmSparseMatrix.h:223
GmSparseMatrixLayoutBuilder * _layoutBuilder
The layout builder if _masterLayout is not NULL.
Definition gmSparseMatrix.h:228
GmSparseMatrixTripletBuffer< T > * _batchData
Intermediate structure while building the matrix with a triplet list.
Definition gmSparseMatrix.h:230
virtual int layoutSize() const
Returns the size of the sparse matrix layout.
Definition gmSparseMatrix.h:101
virtual int nlin() const
Returns the number of lines in the matrix. IMPORTANT: This function implementation MUST be thread saf...
Definition gmSparseMatrix.h:95
virtual bool hasNanInf() const
Returns true if the matrix contains nan or inf values.
Definition gmSparseMatrix.h:191
An interface for building the layout structure of a sparse matrix.
Definition gmSparseMatrixLayoutBuilder.h:40
Set of configuration options for Sparse matrices.
Definition gmSparseMatrixOptions.h:55
An especialization of GmAppendBufffer for triplet data with an extra method for returning the buffer ...
Definition gmSparseMatrixTripletData.h:121
static bool inMainThread()
Is the current thread the main thread? Equivalent to comparing the currentId() with 0.
Definition gmThreadManager.h:174
#define GMC_API_EXPORT
Macro for controlling if the class is being exported (GEMA_CORE_LIB defined) or imported (GEMA_CORE_L...
Definition gmCoreConfig.h:35
#define GM_DOUBLECMP_ABSTOL
Tolerância absoluta entre valores para comparar valores próximos de zero.
Definition gmDoubleCmp.h:64
#define GM_DOUBLECMP_RELTOL
Tolerância relativa entre valores = 0.000001%.
Definition gmDoubleCmp.h:61
Declaration of the GmSolverMatrix interface classes.
Declaration of the GmSparseMatrixOptions class.
GmSparseMatrixLayoutMode
Supported modes for building the sparse matrix.
Definition gmSparseMatrixOptions.h:36
@ GM_TRIPLET_LIST
Batch mode with a (per thread) triplet list.
Definition gmSparseMatrixOptions.h:37
Declaration of the GmSparseMatrixTripletData and GmSparseMatrixTripletBuffer template classes.
Declaration of the GmThreadManager class.
arma::vec GmVector
The basic type for a GeMA vector object. Currently based on an Armadillo vector.
Definition gmVector.h:34
A structure for storing a matrix layout in either CSR or CSC format. The template type defines the ty...
Definition gmSparseMatrixLayout.h:124