GemaCoreLib
The GeMA Core library
gmSparseMatrix.h
Go to the documentation of this file.
1 /************************************************************************
2 **
3 ** Copyright (C) 2014 by Carlos Augusto Teixera Mendes
4 ** All rights reserved.
5 **
6 ** This file is part of the "GeMA" software. It's use should respect
7 ** the terms in the license agreement that can be found together
8 ** with this source code.
9 ** It is provided AS IS, with NO WARRANTY OF ANY KIND,
10 ** INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR
11 ** A PARTICULAR PURPOSE.
12 **
13 ************************************************************************/
14 
24 #ifndef _GEMA_SPARSE_MATRIX_H_
25 #define _GEMA_SPARSE_MATRIX_H_
26 
27 #include "gmSolverMatrix.h"
29 #include "gmSparseMatrixOptions.h"
30 #include "gmThreadManager.h"
31 
32 class GmSimulationData;
33 class LuaTable;
34 
39 {
40  Q_OBJECT
41 
42 protected slots:
43  virtual void matrixLayoutReady() = 0;
44 };
45 
49 template<class IndexType, GmSparseMatrixLayoutTypes T>
51 {
52 public:
53 
54  GmSparseMatrix(int n, const GmSparseMatrix<IndexType, T>* sharedLayout,
55  GmSparseMatrixOptions options, bool arma);
56  virtual ~GmSparseMatrix();
57 
58  // See comments on the base class
59  virtual bool supportsBatchInsert() const { return _opt.mode() == GM_TRIPLET_LIST; }
60 
61  // See comments on the base class
62  virtual bool supportsSparseLayouts() const { return _opt.mode() != GM_TRIPLET_LIST; }
63 
64  // See comments on the base class
65  virtual bool supportsRandomSet() const { return false; }
66 
67  // See comments on the base class
69  {
70  return (_opt.mode() == GM_TRIPLET_LIST) ? THREAD_SAFE_SUPPORT : REENTRANT_SUPPORT;
71  }
72 
73  virtual bool beginBatchInsert(size_t expectedEntries = 0);
74  virtual bool endBatchInsert(bool discardData = false);
75 
76  // See comments on the base class
78 
79  // See comments on the base class
80  virtual bool emptyLayout() const
81  {
83  return (_opt.mode() == GM_TRIPLET_LIST) ? false : _layout->empty();
84  }
85 
86  // See comments on the base class
87  virtual bool symmetric() const { return _sym.loadAcquire(); }
88 
89  // See comments on the base class
90  virtual void setSymmetric(bool sym) { _sym.storeRelease(sym); }
91 
92  // See comments on the base class
93  virtual int nlin() const { return _n; }
94 
95  // See comments on the base class
96  virtual int ncol() const { return _n; }
97 
98  // See comments on the base class
99  virtual int layoutSize() const { assert(GmThreadManager::inMainThread()); assert(_layout); return _layout->_nnz; }
100 
101  // See comments on the base class
102  virtual double at(int lin, int col) const
103  {
105  assert(_layout);
106  assert(lin >= 0 && lin < _n);
107  assert(col >= 0 && col < _n);
108  assert(!_batch);
109 
110  if(_layout->empty())
111  return 0.0;
112 
113  int index = _layout->index(lin, col);
114  assert(index >= -1 && index < _layout->_nnz);
115 
116  return index >= 0 ? _values[index] : 0.0;
117  }
118 
119  // See comments on the base class
120  virtual bool inLayout(int lin, int col) const
121  {
123  assert(_layout);
124  assert(lin >= 0 && lin < _n);
125  assert(col >= 0 && col < _n);
126  assert(!_batch);
127 
128  if(_layout->empty())
129  return false;
130  return (_layout->index(lin, col) >= 0);
131  }
132 
133  // See comments on the base class
134  virtual void set(int lin, int col, double value)
135  {
137  assert(_layout && !_layout->empty());
138  assert(lin >= 0 && lin < _n);
139  assert(col >= 0 && col < _n);
140  assert(!_batch);
141 
142  int index = _layout->index(lin, col);
143  assert(index >= 0 && index < _layout->_nnz); // This matrix does not support random sets
144 
145  _values[index] = value;
146  }
147 
148  // See comments on the base class
149  virtual void add(int lin, int col, double value)
150  {
151  assert(_batch || (_layout && !_layout->empty()));
152  assert(lin >= 0 && lin < _n);
153  assert(col >= 0 && col < _n);
154 
155  if(_batch)
156  addBatch(lin, col, value);
157  else
158  {
159  if(value == 0.0)
160  return;
161 
162  int index = _layout->index(lin, col);
163  assert(index >= 0 && index < _layout->_nnz); // This matrix does not support random sets
164  _values[index] += value;
165  }
166  }
167 
168  virtual void set(GmSolverMatrix* A, GmSolverMatrix* B, double c, bool sameStructure);
169 
170  virtual void clear(bool keepSparseLayout);
171 
172  virtual void clearLineAndColumnSet(const QList<int>& indexList, bool setDiagonal, bool keepSparseLayout);
173 
174  virtual void ensureDiagonal();
175 
176  virtual void matAdd(const GmVector& a, const GmVector& b, double zeroTol = 0.0);
177 
178  virtual void mul(const GmVector& a, GmVector& b) const;
179 
180  virtual void mulAdd(const GmVector& a, GmVector& b) const;
181 
182  virtual void mulSub(const GmVector& a, GmVector& b) const;
183 
184  virtual void columnMulAdd(int col, GmVector& f, double v, const bool* skipRows = NULL) const;
185 
186  virtual size_t usedMemory() const;
187 
188 #ifdef ENABLE_TESTS
189  virtual void checkLayoutData() const;
190 #endif
191 
192 private:
193  Q_DISABLE_COPY(GmSparseMatrix);
194 
195 protected:
196  virtual void matrixLayoutReady();
197 
198  void addBatch(int lin, int col, double value);
199  void addToPtrIndexFromThread(int tid, int ptr, int index, double value);
200 
203 
205  bool _batch;
206  int _n;
208  double* _values;
209 
212 
214 
216 };
217 
218 #endif
GmSparseMatrixLayoutMode mode() const
Returns the mode for building the sparse matrix.
Definition: gmSparseMatrixOptions.h:64
A class for wrapping up a sparse matrix either using CSR or CSC format. Can also differ on the type u...
Definition: gmSparseMatrix.h:50
virtual int ncol() const
Returns the number of columns in the matrix. IMPORTANT: This function implementation MUST be thread s...
Definition: gmSparseMatrix.h:96
int _n
The number of lines and columns in the matrix.
Definition: gmSparseMatrix.h:206
virtual void setSymmetric(bool sym)
Marks the matrix as symmetric or not.
Definition: gmSparseMatrix.h:90
Base interface class for Solver Matrix objects.
Definition: gmSolverMatrix.h:97
double * _values
The vector with matrix values.
Definition: gmSparseMatrix.h:208
Batch mode with a (per thread) triplet list.
Definition: gmSparseMatrixOptions.h:37
GmSparseMatrixOptions _opt
The matrix options, including the mode used to build the matrix.
Definition: gmSparseMatrix.h:204
QAtomicInt _sym
Flag marking the matrix as symmetric or not.
Definition: gmSparseMatrix.h:207
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' ...
Definition: gmSparseMatrix.cpp:825
GmSparseMatrixLayoutBuilder * _layoutBuilder
The layout builder if _masterLayout is not NULL.
Definition: gmSparseMatrix.h:213
virtual GmSparseMatrix< IndexType, T > * detachAndClear()
Creates a new matrix that inherits the layout and matrix data from this matrix and clears this matrix...
Definition: gmSparseMatrix.cpp:1064
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 differe...
Definition: gmSparseMatrix.cpp:55
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:134
An interface for building the layout structure of a sparse matrix.
Definition: gmSparseMatrixLayoutBuilder.h:39
bool _batch
Flag to signal that the matrix is inside a batch op (for _opt.mode() == TRIPLET_LIST/STRIPLET_LIST)
Definition: gmSparseMatrix.h:205
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.
Definition: gmSparseMatrix.cpp:1021
virtual int layoutSize() const
Returns the size of the sparse matrix layout.
Definition: gmSparseMatrix.h:99
virtual bool supportsSparseLayouts() const
Does this matrix supports sparse layouts? See comments on the class documentation.
Definition: gmSparseMatrix.h:62
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
Set of configuration options for Sparse matrices.
Definition: gmSparseMatrixOptions.h:54
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:211
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).
Definition: gmSparseMatrix.cpp:910
Auxiliar class used to store the complete set of simulation data.
Definition: gmSimulationData.h:51
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....
Definition: gmSparseMatrix.cpp:573
GmSparseMatrixLayoutMode
Supported modes for building the sparse matrix.
Definition: gmSparseMatrixOptions.h:35
Declaration of the GmThreadManager class.
virtual bool supportsRandomSet() const
Does this matrix supports setting a value on a random position outside the matrix initialization proc...
Definition: gmSparseMatrix.h:65
A base class derived from QObject. Needed since GmSparseMatrix is a template and so can't inherit dir...
Definition: gmSparseMatrix.h:38
ParallelAddMode
Supported modes for calling add from multiple threads in parallel.
Definition: gmSolverMatrix.h:107
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).
Definition: gmSparseMatrix.cpp:985
virtual ~GmSparseMatrix()
Destructor.
Definition: gmSparseMatrix.cpp:107
virtual bool inLayout(int lin, int col) const
Returns true if the given position belongs to the matrix sparse layout.
Definition: gmSparseMatrix.h:120
virtual void ensureDiagonal()
Updates any zero diagonal value to 1.0. If diagonal values do not belong to the layout,...
Definition: gmSparseMatrix.cpp:742
Declaration of the GmSparseMatrixTripletData and GmSparseMatrixTripletBuffer template classes.
virtual bool endBatchInsert(bool discardData=false)
Ends a batch insert process. Important: see comments on the class documentation.
Definition: gmSparseMatrix.cpp:170
virtual size_t usedMemory() const
Returns an estimative of the memory used by the matrix in bytes.
Definition: gmSparseMatrix.cpp:1093
virtual bool beginBatchInsert(size_t expectedEntries=0)
Begins a batch insert process. Important: see comments on the class documentation.
Definition: gmSparseMatrix.cpp:139
T loadAcquire() const const
virtual void clear(bool keepSparseLayout)
Clears the matrix, filling it with zeros. The keepSparseLayout flag is a hint that the matrix layout ...
Definition: gmSparseMatrix.cpp:384
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).
Definition: gmSparseMatrix.cpp:948
void storeRelease(T newValue)
An especialization of GmAppendBufffer for triplet data with an extra method for returning the buffer ...
Definition: gmSparseMatrixTripletData.h:120
A structure for storing a matrix layout in either CSR or CSC format. The template type defines the ty...
Definition: gmSparseMatrixLayout.h:123
#define GMC_API_EXPORT
Macro for controling if the class is being exported (GEMA_CORE_LIB defined) or imported (GEMA_CORE_LI...
Definition: gmCoreConfig.h:35
void addBatch(int lin, int col, double value)
Worker function for adding an entry to the matrix while inside a beginBatchInsert() / endBatchInsert(...
Definition: gmSparseMatrix.cpp:329
const GmCSxSparseMatrixLayout< IndexType, T > * _layout
The read-only matrix layout data. Can be shared from other matrices.
Definition: gmSparseMatrix.h:210
GmSparseMatrixLayoutBuilder * createLayoutBuilder(GmSparseMatrixLayoutMode mode, GmCSxSparseMatrixLayout< IndexType, T > *layout)
Creates a new layout builder from the given layout mode and layout.
Definition: gmSparseMatrix.cpp:121
void addToPtrIndexFromThread(int tid, int ptr, int index, double value)
Worker function for adding an entry to the matrix while inside a beginBatchInsert() / endBatchInsert(...
Definition: gmSparseMatrix.cpp:348
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
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:149
Declaration of the GmSolverMatrix interface classes.
virtual double at(int lin, int col) const
Returns the value in the position Mat[lin][col].
Definition: gmSparseMatrix.h:102
static bool inMainThread()
Is the current thread the main thread? Equivalent to comparing the currentId() with 0.
Definition: gmThreadManager.h:169
virtual bool emptyLayout() const
If the matrix supports sparse layouts and the layout is currently empty, returns true....
Definition: gmSparseMatrix.h:80
virtual ParallelAddMode supportedParallelAddMode() const
Returns the supported mode for calling add.
Definition: gmSparseMatrix.h:68
arma::vec GmVector
The basic type for a GeMA vector object. Currently based on an Armadillo vector.
Definition: gmVector.h:34
GmSparseMatrixTripletBuffer< T > * _batchData
Intermediate structure while building the matrix with a triplet list.
Definition: gmSparseMatrix.h:215
virtual bool supportsBatchInsert() const
Does this matrix supports batch inserts? See comments on the class documentation.
Definition: gmSparseMatrix.h:59
virtual void matrixLayoutReady()
Allocates space for matrix values when notified that the layout is ready.
Definition: gmSparseMatrix.cpp:361
virtual int nlin() const
Returns the number of lines in the matrix. IMPORTANT: This function implementation MUST be thread saf...
Definition: gmSparseMatrix.h:93
Declaration of the GmSparseMatrixOptions class.