GemaCoreLib
The GeMA Core library
Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | List of all members
GmSparseMatrix< IndexType, T > Class Template Reference

A class for wrapping up a sparse matrix either using CSR or CSC format. Can also differ on the type used for storing line and column indices. More...

#include <gmSparseMatrix.h>

Inheritance diagram for GmSparseMatrix< IndexType, T >:
Inheritance graph
[legend]
Collaboration diagram for GmSparseMatrix< IndexType, T >:
Collaboration graph
[legend]

Public Member Functions

 GmSparseMatrix (int n, const GmSparseMatrix< IndexType, T > *sharedLayout, GmSparseMatrixOptions options, bool arma)
 Constructor for a sparse square matrix stored either in CSR or CSC format. If sharedLayout is different from NULL, this matrix layout will be given by it. When the arma flag is set to true, the layout and data vector includes extra positions for storing sentinell values needed by the Armadillo CSC format. More...
 
virtual ~GmSparseMatrix ()
 Destructor.
 
virtual bool supportsBatchInsert () const
 Does this matrix supports batch inserts? See comments on the class documentation.
 
virtual bool supportsSparseLayouts () const
 Does this matrix supports sparse layouts? See comments on the class documentation.
 
virtual bool supportsRandomSet () const
 Does this matrix supports setting a value on a random position outside the matrix initialization process (either by a batch insert or a layout builder)?
 
virtual ParallelAddMode supportedParallelAddMode () const
 Returns the supported mode for calling add.
 
virtual bool beginBatchInsert (size_t expectedEntries=0)
 Begins a batch insert process. Important: see comments on the class documentation. More...
 
virtual bool endBatchInsert (bool discardData=false)
 Ends a batch insert process. Important: see comments on the class documentation. More...
 
virtual GmSparseMatrixLayoutBuilderlayoutBuilder () const
 If the matrix supports sparse layouts, returns a builder object that can be used to initialize the matrix layout. Otherwise returns NULL. If the layout is being shared by several matrices, only the main matrix, owning the layout should return it.
 
virtual bool emptyLayout () const
 If the matrix supports sparse layouts and the layout is currently empty, returns true. Otherwise, returns false.
 
virtual bool symmetric () const
 Returns true if the matrix was marked as symmetric by setSymmetric() (it does not check for matrix symmetry itself) More...
 
virtual void setSymmetric (bool sym)
 Marks the matrix as symmetric or not. More...
 
virtual int nlin () const
 Returns the number of lines in the matrix. IMPORTANT: This function implementation MUST be thread safe.
 
virtual int ncol () const
 Returns the number of columns in the matrix. IMPORTANT: This function implementation MUST be thread safe.
 
virtual int layoutSize () const
 Returns the size of the sparse matrix layout. More...
 
virtual double at (int lin, int col) const
 Returns the value in the position Mat[lin][col].
 
virtual bool inLayout (int lin, int col) const
 Returns true if the given position belongs to the matrix sparse layout. More...
 
virtual void set (int lin, int col, double value)
 Sets the value in the position Mat[lin][col] to the specified value. More...
 
virtual void add (int lin, int col, double value)
 Adds the given value to the value in the position Mat[lin][col]. More...
 
virtual void set (GmSolverMatrix *A, GmSolverMatrix *B, double c, bool sameStructure)
 Sets the values of the whole matrix to the result of the expresion A + c * B, where A and B are matrices with the same size as this one and c is a constant. More...
 
virtual void clear (bool keepSparseLayout)
 Clears the matrix, filling it with zeros. The keepSparseLayout flag is a hint that the matrix layout (non zero positions) will remain the same when the matrix is re-filled with data, and so, if the matrix supports sparse layouts, they should not be removed. This function marks the matrix as unsymmetric.
 
virtual void clearLineAndColumnSet (const QList< int > &indexList, bool setDiagonal, bool keepSparseLayout)
 Clears a set of lines and columns from the matrix, filling them with zeroes, optionally puting a 1.0 at the diagonal. More...
 
virtual void ensureDiagonal ()
 Updates any zero diagonal value to 1.0. If diagonal values do not belong to the layout, the matrix must NOT be shared!
 
virtual void matAdd (const GmVector &a, const GmVector &b, double zeroTol=0.0)
 Adds to the current matrix ('X') the (dense) matrix resulting from multiplying the column vector 'a' by the transpose of the column vector 'b' (X = X + a * b.t()). More...
 
virtual void mul (const GmVector &a, GmVector &b) const
 Multiplies the matrix ('X') by a vector 'a' storing the result in 'b' (b = X * a). More...
 
virtual void mulAdd (const GmVector &a, GmVector &b) const
 Multiplies the matrix ('X') by a vector 'a' adding the result to 'b' (b = b + X * a). More...
 
virtual void mulSub (const GmVector &a, GmVector &b) const
 Multiplies the matrix ('X') by a vector 'a' subtracting the result from 'b' (b = b - X * a). More...
 
virtual void columnMulAdd (int col, GmVector &f, double v, const bool *skipRows=NULL) const
 Updates the given vector adding to it a matrix column multiplied by a scalar value. More...
 
virtual size_t usedMemory () const
 Returns an estimative of the memory used by the matrix in bytes. More...
 
- Public Member Functions inherited from QObject
virtual const QMetaObjectmetaObject () const const
 
virtual void * qt_metacast (const char *)
 
virtual int qt_metacall (QMetaObject::Call, int, void **)
 
 QObject (QObject *parent)
 
virtual bool event (QEvent *e)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
QString objectName () const const
 
void setObjectName (const QString &name)
 
bool isWidgetType () const const
 
bool isWindowType () const const
 
bool signalsBlocked () const const
 
bool blockSignals (bool block)
 
QThreadthread () const const
 
void moveToThread (QThread *targetThread)
 
int startTimer (int interval, Qt::TimerType timerType)
 
int startTimer (std::chrono::milliseconds time, Qt::TimerType timerType)
 
void killTimer (int id)
 
findChild (const QString &name, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QString &name, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QRegExp &regExp, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QRegularExpression &re, Qt::FindChildOptions options) const const
 
const QObjectList & children () const const
 
void setParent (QObject *parent)
 
void installEventFilter (QObject *filterObj)
 
void removeEventFilter (QObject *obj)
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type) const const
 
bool disconnect (const char *signal, const QObject *receiver, const char *method) const const
 
bool disconnect (const QObject *receiver, const char *method) const const
 
void dumpObjectTree ()
 
void dumpObjectInfo ()
 
void dumpObjectTree () const const
 
void dumpObjectInfo () const const
 
bool setProperty (const char *name, const QVariant &value)
 
QVariant property (const char *name) const const
 
QList< QByteArraydynamicPropertyNames () const const
 
void destroyed (QObject *obj)
 
void objectNameChanged (const QString &objectName)
 
QObjectparent () const const
 
bool inherits (const char *className) const const
 
void deleteLater ()
 
- Public Member Functions inherited from GmSolverMatrix
virtual ~GmSolverMatrix ()
 Virtual destructor.
 
double operator() (int lin, int col) const
 Returns the value in the position Mat[lin][col]. More...
 
bool isSymmetric (double relTol=GM_DOUBLECMP_RELTOL, double absTol=GM_DOUBLECMP_ABSTOL) const
 Returns true if the matrix is symmetric, false otherwise. More...
 
void print (const GmLogCategory &logger, GmLogLevel level, int fieldWidth=0, char format='g', int precision=-1)
 Prints the matrix using the specified logger, level and precision fields.
 

Protected Member Functions

virtual void matrixLayoutReady ()
 Allocates space for matrix values when notified that the layout is ready.
 
void addBatch (int lin, int col, double value)
 Worker function for adding an entry to the matrix while inside a beginBatchInsert() / endBatchInsert() block.
 
void addToPtrIndexFromThread (int tid, int ptr, int index, double value)
 Worker function for adding an entry to the matrix while inside a beginBatchInsert() / endBatchInsert() block, from the given thread and with line and column defined by the matrix layot ptr and index values.
 
virtual GmSparseMatrix< IndexType, T > * detachAndClear ()
 Creates a new matrix that inherits the layout and matrix data from this matrix and clears this matrix. More...
 
GmSparseMatrixLayoutBuildercreateLayoutBuilder (GmSparseMatrixLayoutMode mode, GmCSxSparseMatrixLayout< IndexType, T > *layout)
 Creates a new layout builder from the given layout mode and layout.
 
- Protected Member Functions inherited from QObject
QObjectsender () const const
 
int senderSignalIndex () const const
 
int receivers (const char *signal) const const
 
bool isSignalConnected (const QMetaMethod &signal) const const
 
virtual void timerEvent (QTimerEvent *event)
 
virtual void childEvent (QChildEvent *event)
 
virtual void customEvent (QEvent *event)
 
virtual void connectNotify (const QMetaMethod &signal)
 
virtual void disconnectNotify (const QMetaMethod &signal)
 

Protected Attributes

GmSparseMatrixOptions _opt
 The matrix options, including the mode used to build the matrix.
 
bool _batch
 Flag to signal that the matrix is inside a batch op (for _opt.mode() == TRIPLET_LIST/STRIPLET_LIST)
 
int _n
 The number of lines and columns in the matrix.
 
QAtomicInt _sym
 Flag marking the matrix as symmetric or not.
 
double * _values
 The vector with matrix values.
 
const GmCSxSparseMatrixLayout< IndexType, T > * _layout
 The read-only matrix layout data. Can be shared from other matrices.
 
GmCSxSparseMatrixLayout< IndexType, T > * _masterLayout
 The master layout data (equal to _layout) if this matrix is the layout owner, NULL for matrices sharing a layout.
 
GmSparseMatrixLayoutBuilder_layoutBuilder
 The layout builder if _masterLayout is not NULL.
 
GmSparseMatrixTripletBuffer< T > * _batchData
 Intermediate structure while building the matrix with a triplet list.
 

Private Member Functions

 Q_DISABLE_COPY (GmSparseMatrix)
 

Additional Inherited Members

- Public Types inherited from GmSolverMatrix
enum  ParallelAddMode { NO_SUPPORT, THREAD_SAFE_SUPPORT, REENTRANT_SUPPORT }
 Supported modes for calling add from multiple threads in parallel. More...
 
- Static Public Member Functions inherited from QObject
QString tr (const char *sourceText, const char *disambiguation, int n)
 
QString trUtf8 (const char *sourceText, const char *disambiguation, int n)
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, Functor functor)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type)
 
bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *method)
 
bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method)
 
bool disconnect (const QMetaObject::Connection &connection)
 
bool disconnect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method)
 
- Protected Slots inherited from GmSparseMatrixBase
- Properties inherited from QObject
 objectName
 

Detailed Description

template<class IndexType, GmSparseMatrixLayoutTypes T>
class GmSparseMatrix< IndexType, T >

A class for wrapping up a sparse matrix either using CSR or CSC format. Can also differ on the type used for storing line and column indices.

Constructor & Destructor Documentation

◆ GmSparseMatrix()

template<class IndexType , GmSparseMatrixLayoutTypes T>
GmSparseMatrix< IndexType, T >::GmSparseMatrix ( int  n,
const GmSparseMatrix< IndexType, T > *  sharedLayout,
GmSparseMatrixOptions  options,
bool  arma 
)

Constructor for a sparse square matrix stored either in CSR or CSC format. If sharedLayout is different from NULL, this matrix layout will be given by it. When the arma flag is set to true, the layout and data vector includes extra positions for storing sentinell values needed by the Armadillo CSC format.

IMPORTANT: When sharing a layout with another matrix, this matrix will become invalid if the matrix owning the layout is destroyed.

Member Function Documentation

◆ add()

template<class IndexType, GmSparseMatrixLayoutTypes T>
virtual void GmSparseMatrix< IndexType, T >::add ( int  lin,
int  col,
double  value 
)
inlinevirtual

Adds the given value to the value in the position Mat[lin][col].

In general this function can be used only to change values over positions that belong to the matrix sparse layout (its set of non zero positions) or inside a beginBatchInsert() / endBatchInsert() region. It can only be used to set values on random positions if supportsRandomSet() returns true.

See the class description for comments about calling this function from multiple threads in parallel.

Implements GmSolverMatrix.

◆ beginBatchInsert()

template<class IndexType , GmSparseMatrixLayoutTypes T>
bool GmSparseMatrix< IndexType, T >::beginBatchInsert ( size_t  expectedEntries = 0)
virtual

Begins a batch insert process. Important: see comments on the class documentation.

The expectedEntries parameters is an estimative of the number of times that add() will be called for this beginBatchInsert() / endBatchInsert() block.

Uses a 64 bits value for the size since this list can grow quite quickly and a 32 bits limit can be achieved. An hex27 element with 5 dofs per node generates a 135 x 135 local matrix, translating into 135 * 135 = 18.225 calls to append() per mesh element. That will overflow a standard int with less than 120.000 elements.

Implements GmSolverMatrix.

◆ clearLineAndColumnSet()

template<class IndexType , GmSparseMatrixLayoutTypes T>
void GmSparseMatrix< IndexType, T >::clearLineAndColumnSet ( const QList< int > &  indexList,
bool  setDiagonal,
bool  keepSparseLayout 
)
virtual

Clears a set of lines and columns from the matrix, filling them with zeroes, optionally puting a 1.0 at the diagonal.

This function is semantically equivalent to the code below, but potentially quicker for sparse matrices. It should be used only for square matrices.

foreach(int i, indexList)
{
clearLine(i);
clearColumn(i)
if(setDiagonal)
set(i, i, 1.0);
}

If keepSparseLayout is set to true, matrix values will be changed without updating the internal matrix sparse layout. This can be important for performance if the matrix is later reused to host another set of values with the same layout as the one before removing the set of lines and columns.

IMPORTANT: When enabling both keepSparseLayout and setDiagonal, it is NECESSARY that the diagonal items of the selected rows belong to the current sparse layout. In most cases this is true anyway. Failing to do so will result in undefined behaviour.

If keepSpraseLayout is set to false, the matrix layout can NOT be shared with other matrices since this operation would change the layout.

Implements GmSolverMatrix.

◆ columnMulAdd()

template<class IndexType , GmSparseMatrixLayoutTypes T>
void GmSparseMatrix< IndexType, T >::columnMulAdd ( int  col,
GmVector f,
double  v,
const bool *  skipRows = NULL 
) const
virtual

Updates the given vector adding to it a matrix column multiplied by a scalar value.

Given the matrix column 'col', for each row 'i' in the matrix makes f[i] += Mat[i][col] * v. If the boolean vector skipRows is not NULL, the above operation will be executed only if skipRows[i] is not true.

This function is semantically equivalent to the code below, but potentially quicker for sparse matrices.

foreach row i in the matrix
{
if(skipRows && skipRows[i])
continue;
f[i] += Mat(i, col) * v;
}

Implements GmSolverMatrix.

◆ detachAndClear()

template<class IndexType , GmSparseMatrixLayoutTypes T>
GmSparseMatrix< IndexType, T > * GmSparseMatrix< IndexType, T >::detachAndClear ( )
protectedvirtual

Creates a new matrix that inherits the layout and matrix data from this matrix and clears this matrix.

Since the internal layout will be changed, it should not be shared...

◆ endBatchInsert()

template<class IndexType , GmSparseMatrixLayoutTypes T>
bool GmSparseMatrix< IndexType, T >::endBatchInsert ( bool  discardData = false)
virtual

Ends a batch insert process. Important: see comments on the class documentation.

The discardData parameter is a flag that when set to true informs that the matrix building process was interrupted and so the current matrix data is invalid and should be discarded.

Implements GmSolverMatrix.

◆ inLayout()

template<class IndexType, GmSparseMatrixLayoutTypes T>
virtual bool GmSparseMatrix< IndexType, T >::inLayout ( int  lin,
int  col 
) const
inlinevirtual

Returns true if the given position belongs to the matrix sparse layout.

This is different from testing if the value is zero since it is possible for a zero value to belong to the layout in antecipation of a future set opertaion. If the matrix does not support the layout concept, it should just test that the value is non zero.

Implements GmSolverMatrix.

◆ layoutSize()

template<class IndexType, GmSparseMatrixLayoutTypes T>
virtual int GmSparseMatrix< IndexType, T >::layoutSize ( ) const
inlinevirtual

Returns the size of the sparse matrix layout.

The returned size is aproximatelly equal to the number of nonzero entries in the matrix. It will be different if the layout includes places for zero values that might change in the future. If the matriz does not have that information, returns -1.

Implements GmSolverMatrix.

◆ matAdd()

template<class IndexType , GmSparseMatrixLayoutTypes T>
void GmSparseMatrix< IndexType, T >::matAdd ( const GmVector a,
const GmVector b,
double  zeroTol = 0.0 
)
virtual

Adds to the current matrix ('X') the (dense) matrix resulting from multiplying the column vector 'a' by the transpose of the column vector 'b' (X = X + a * b.t()).

In order to keep some kind of sparsisity of the matrix, if the resulting values is smaller than the given tolerance, it will not be stored (set to zero).

Implements GmSolverMatrix.

◆ mul()

template<class IndexType , GmSparseMatrixLayoutTypes T>
void GmSparseMatrix< IndexType, T >::mul ( const GmVector a,
GmVector b 
) const
virtual

Multiplies the matrix ('X') by a vector 'a' storing the result in 'b' (b = X * a).

If X is a matrix with size equal to M x N, a should have size N and b will have size M.

Implements GmSolverMatrix.

◆ mulAdd()

template<class IndexType , GmSparseMatrixLayoutTypes T>
void GmSparseMatrix< IndexType, T >::mulAdd ( const GmVector a,
GmVector b 
) const
virtual

Multiplies the matrix ('X') by a vector 'a' adding the result to 'b' (b = b + X * a).

If X is a matrix with size equal to M x N, a should have size N and b will have size M.

Implements GmSolverMatrix.

◆ mulSub()

template<class IndexType , GmSparseMatrixLayoutTypes T>
void GmSparseMatrix< IndexType, T >::mulSub ( const GmVector a,
GmVector b 
) const
virtual

Multiplies the matrix ('X') by a vector 'a' subtracting the result from 'b' (b = b - X * a).

If X is a matrix with size equal to M x N, a should have size N and b will have size M.

Implements GmSolverMatrix.

◆ set() [1/2]

template<class IndexType, GmSparseMatrixLayoutTypes T>
virtual void GmSparseMatrix< IndexType, T >::set ( int  lin,
int  col,
double  value 
)
inlinevirtual

Sets the value in the position Mat[lin][col] to the specified value.

In general this function can be used only to set values over positions that belong to the matrix sparse layout (its set of non zero positions). It can only be used to set values on random positions if supportsRandomSet() returns true. (For performance reasons, no exception is made, even when setting a value of 0.0 over an already 0.0 value since that would probably add an if to the critical path).

This function can not be called at all while inside a beginBatchInsert() / endBatchInsert() region.

Implements GmSolverMatrix.

◆ set() [2/2]

template<class IndexType , GmSparseMatrixLayoutTypes T>
void GmSparseMatrix< IndexType, T >::set ( GmSolverMatrix A,
GmSolverMatrix B,
double  c,
bool  sameStructure 
)
virtual

Sets the values of the whole matrix to the result of the expresion A + c * B, where A and B are matrices with the same size as this one and c is a constant.

Conceptually this operation is equivalent to calling set(i, j, A(i, j) + c * B(i, j)) for every line & column of the matrices, but when working with sparse matrices doing it in a batch can be much faster.

If sameStructure is set to true, this functions assumes that A, B and the current matrix object have all the SAME layout (same non-zero structure). When set to false, the matrix layout can not be shared with other matrices since it would change the layout.

Keep in mind that matrix A or matrix B CAN be equal to the current matrix (this).

If the current matrix is marked as symmetric and either A or B is not marked as symmetric, the current matrix will be marked as unsymmetric.

Implements GmSolverMatrix.

◆ setSymmetric()

template<class IndexType, GmSparseMatrixLayoutTypes T>
virtual void GmSparseMatrix< IndexType, T >::setSymmetric ( bool  sym)
inlinevirtual

Marks the matrix as symmetric or not.

Marking a symmetric matrix as such might help the numeric solver. When constructed, the matrix is marked as NOT symmetric

Bear in mind that this method DOES NOT CHECK weather the matrix is really symmetric or not. You can do that by calling isSymmetric().

It is also a user responsability to call setSymmetric() again if a matrix operation turns the matrix as unsymmetric.

IMPORTANT: This function implementation MUST be thread safe

Implements GmSolverMatrix.

◆ symmetric()

template<class IndexType, GmSparseMatrixLayoutTypes T>
virtual bool GmSparseMatrix< IndexType, T >::symmetric ( ) const
inlinevirtual

Returns true if the matrix was marked as symmetric by setSymmetric() (it does not check for matrix symmetry itself)

IMPORTANT: This function implementation MUST be thread safe

Implements GmSolverMatrix.

◆ usedMemory()

template<class IndexType , GmSparseMatrixLayoutTypes T>
size_t GmSparseMatrix< IndexType, T >::usedMemory ( ) const
virtual

Returns an estimative of the memory used by the matrix in bytes.

It returns in general the variable memory that depends on the number of set entries but does not include constant overheads used by the class for control

In particular, this method might return different values before and after a call to endBatchInsert(). If the matrix shares a layout with another matrix, only the layout owner will account for its size.

Implements GmSolverMatrix.


The documentation for this class was generated from the following files: