AGX Dynamics  2.35.0.1
SparseMatrix.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGX_SPARSE_MATRIX_H
18 #define AGX_SPARSE_MATRIX_H
19 
20 #include <agx/Referenced.h>
21 #include <agx/HashTable.h>
22 
23 #include <agx/agx_valarray_types.h>
24 #include <agx/MatrixTypes.h>
25 
26 namespace agx
27 {
28 
30 
35  {
36  public:
37  virtual agx::Real* getBlock(int i, int j, bool& writeTransposed, unsigned int& lda) = 0;
38 
39  virtual void reset() = 0;
40 
41  virtual void factor() = 0;
42  virtual void solve( agx::Real* ) = 0;
43 
44  virtual size_t unitSolve( agx::Real*, int = -1 );
45 
46  virtual void multiply( const agx::Real* x, agx::Real* r ) = 0;
47 
48  virtual void multiplyAdd( const agx::Real* x, agx::Real* r ) = 0;
49 
50  virtual void multiplyAdd( const agx::Real& alpha, const agx::Real* x, const agx::Real& beta, agx::Real* r ) = 0;
51 
52  virtual void setLogicalStructure( size_t n, const unsigned int* pointers, const unsigned int* indices, const unsigned int* dimensions, const int* flags, unsigned int numThreads );
53 
54  virtual agx::Bool getRow( agx::UInt rowNumber, const agx::RealValarray& signs, agx::RealValarray& retRow ) const = 0;
55  virtual agx::Bool getColumn( agx::UInt colNumber, agx::RealValarray& retCol, agx::Bool& signFlip ) const = 0;
56 
57  virtual agx::UInt getBlockPermutedStartRowIndex( agx::UInt blockIndex ) const = 0;
58  virtual agx::UInt permuteRowIndex( agx::UInt rowIndex ) const;
59  virtual agx::UInt invPermuteRowIndex( agx::UInt permutedRowIndex ) const;
60 
61  virtual unsigned int getNumRows() const = 0;
62  virtual unsigned int getNumColumns() const = 0;
63 
64  virtual void print() const {}
65 
66  virtual bool saveH5(void* group, const agx::String& name ) const;
67  virtual bool loadH5(void* group, const agx::String& name );
68 
69  virtual void setNumBodies(size_t /*num*/) {}
70 
71  virtual agx::Real getFactorTime() const { return -1; }
72  virtual agx::Real getSolveTime() const { return -1; }
73 
74  protected:
75  virtual ~SparseMatrixRep() {}
76  };
77 
78 
86  {
87  public:
88 
93  enum BlockFlags {
94  DENSE = 0,
95  DIAGONAL = 1,
96  LOWER_TRIANGULAR = 2,
97  UPPER_TRIANGULAR = 3
98  };
99 
100 
107 
112 
123  void setLogicalStructure( size_t n, const unsigned int* pointers, const unsigned int* indices, const unsigned int* dimensions, const int* flags = nullptr, unsigned int numThreads = 1 );
124 
132  agx::Bool getRow( agx::UInt rowNumber, const agx::RealValarray& signs, agx::RealValarray& retRow ) const;
133 
140  agx::Bool getColumn( agx::UInt colNumber, agx::RealValarray& retCol ) const;
141 
142 
150  agx::Bool getColumn( agx::UInt colNumber, agx::RealValarray& retCol, agx::Bool& signFlip ) const;
151 
160  agx::Real* getBlock(int i, int j, bool& writeTransposed, unsigned int& leadingDimension);
161 
165  void reset();
166 
170  void factor();
171 
175  void solve( agx::Real* );
176 
181  size_t unitSolve( agx::Real*, int = -1 );
182 
186  unsigned int getNumRows() const;
187 
191  unsigned int getNumColumns() const;
192 
196  void multiply( const agx::Real* x, agx::Real* r );
197 
201  void multiplyAdd( const agx::Real* x, agx::Real* r );
202 
206  void multiplyAdd( const agx::Real& alpha, const agx::Real* x, const agx::Real& beta, agx::Real* r );
207 
212  agx::UInt getBlockPermutedStartRowIndex( agx::UInt blockIndex ) const;
213 
218  agx::UInt permuteRowIndex( agx::UInt rowIndex ) const;
219 
224  agx::UInt invPermuteRowIndex( agx::UInt permutedRowIndex ) const;
225 
229  MatrixType& getType();
230 
231 
234  void print() const;
235 
242  bool saveH5(void* group, const agx::String& name ) const;
243 
250  bool loadH5(void* group, const agx::String& name );
251 
256  void setNumBodies( int num );
257 
259  // can perform the necessary operations in a reasonable way.
261  const SparseMatrixRep* getRep() const;
262 
263 
271 
274 
275  private:
276  SparseMatrixRepRef m_rep;
277  MatrixType m_type;
278  };
279 
284  {
285  public:
286  ColumnStorage( const SparseMatrix& matrix, const RealValarray& signs )
287  : m_matrix( matrix ), m_signs( signs ) {}
288 
290  {
291  return getOrCreateColRow( col )->column;
292  }
293 
295  {
296  return getOrCreateColRow( row )->asRow( m_signs );
297  }
298 
299  private:
300  struct ColRow : public Referenced
301  {
302  RealValarray column;
303  RealValarray row;
304  Bool signFlip;
305 
306  const RealValarray& asRow( const RealValarray& signs )
307  {
308  if ( column.size() == 0 )
309  throw;
310 
311  if ( row.size() == column.size() )
312  return row;
313  else
314  row.resize( column.size() );
315 
316  row = column;
317  Real sign = signFlip ? Real( -1 ) : Real( 1 );
318  for ( UInt i = 0, numRows = row.size(); i < numRows; ++i )
319  row[ i ] *= sign * signs[ i ];
320  return row;
321  }
322  };
323 
324  typedef ref_ptr< ColRow > ColRowRef;
325 
326  private:
327  typedef HashTable< UInt, ColRowRef > ColRowContainer;
328 
329  private:
330  ColumnStorage& operator = ( const ColumnStorage& ) { return *this; }
331 
332  ColRowRef getOrCreateColRow( UInt col )
333  {
334  ColRowContainer::iterator it = m_colRowStorage.find( col );
335  if ( it == m_colRowStorage.end() ) {
336  it = m_colRowStorage.insert( col, new ColRow() );
337  m_matrix.getColumn( col, it->second->column, it->second->signFlip );
338  }
339 
340  return it->second;
341  }
342 
343  private:
344  const SparseMatrix& m_matrix;
345  const RealValarray& m_signs;
346  ColRowContainer m_colRowStorage;
347  };
348 
350  //
351  // SparseMatrix
352 
354  {
355  return m_type;
356  }
357 
358 
360  size_t n, const unsigned int* pointers, const unsigned int* indices,
361  const unsigned int* dimensions, const int* flags, unsigned int numThreads )
362  {
363  m_rep->setLogicalStructure(n,pointers, indices, dimensions, flags, numThreads);
364  }
365 
366  AGX_FORCE_INLINE agx::Real* SparseMatrix::getBlock(int i, int j, bool& writeTransposed, unsigned int& leadingDimension)
367  {
368  return m_rep->getBlock(i, j, writeTransposed, leadingDimension);
369  }
370 
372  {
373  m_rep->reset();
374  }
375 
377  {
378  m_rep->factor();
379  }
380 
382  {
383  m_rep->solve( rhs );
384  }
385 
386  AGX_FORCE_INLINE size_t SparseMatrix::unitSolve( agx::Real* rhs, int startRow /*= -1*/ )
387  {
388  return m_rep->unitSolve( rhs, startRow );
389  }
390 
392  {
393  m_rep->multiply(x,r);
394  }
395 
397  {
398  m_rep->multiplyAdd(x, r);
399  }
400 
401  AGX_FORCE_INLINE void SparseMatrix::multiplyAdd( const agx::Real& alpha, const agx::Real* x, const agx::Real& beta, agx::Real* r )
402  {
403  m_rep->multiplyAdd(alpha, x, beta, r );
404  }
405 
407  {
408  return m_rep->getBlockPermutedStartRowIndex( blockIndex );
409  }
410 
411  inline UInt SparseMatrix::permuteRowIndex( UInt rowIndex ) const
412  {
413  return m_rep->permuteRowIndex( rowIndex );
414  }
415 
416  inline UInt SparseMatrix::invPermuteRowIndex( UInt permutedRowIndex ) const
417  {
418  return m_rep->invPermuteRowIndex( permutedRowIndex );
419  }
420 
422  {
423  return m_rep->getNumRows();
424  }
425 
427  {
428  return m_rep->getNumColumns();
429  }
430 
431  inline void SparseMatrix::print() const
432  {
433  return m_rep->print();
434  }
435 
436  inline bool SparseMatrix::saveH5(void* group, const agx::String& name ) const
437  {
438  return m_rep->saveH5( group, name );
439  }
440 
441  inline bool SparseMatrix::loadH5(void* group, const agx::String& name )
442  {
443  return m_rep->loadH5( group, name );
444  }
445 
447  {
448  m_rep->setNumBodies( num );
449  }
450 }
451 #endif
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
#define AGXPHYSICS_EXPORT
Class that stores rows and columns of the matrix.
Definition: SparseMatrix.h:284
ColumnStorage(const SparseMatrix &matrix, const RealValarray &signs)
Definition: SparseMatrix.h:286
const RealValarray & getOrCreateRow(UInt row)
Definition: SparseMatrix.h:294
const RealValarray & getOrCreateColumn(UInt col)
Definition: SparseMatrix.h:289
iterator end()
Iterator marking end of hash table.
insert_iterator insert(const KeyT &key, const ValueT value)
Insert a key/value pair into the hash table.
iterator find(const KeyT &key)
Find a key/value pair in the hash table given a key.
A class to distinguish between various types of matrices, both in terms of storage properties,...
Definition: MatrixTypes.h:48
Base class providing referencing counted objects.
Definition: Referenced.h:114
SparseMatrixRep.
Definition: SparseMatrix.h:35
virtual void print() const
Definition: SparseMatrix.h:64
virtual agx::Real * getBlock(int i, int j, bool &writeTransposed, unsigned int &lda)=0
virtual void solve(agx::Real *)=0
virtual size_t unitSolve(agx::Real *, int=-1)
virtual ~SparseMatrixRep()
Definition: SparseMatrix.h:75
virtual void setLogicalStructure(size_t n, const unsigned int *pointers, const unsigned int *indices, const unsigned int *dimensions, const int *flags, unsigned int numThreads)
virtual agx::Real getFactorTime() const
Definition: SparseMatrix.h:71
virtual unsigned int getNumColumns() const =0
virtual agx::Real getSolveTime() const
Definition: SparseMatrix.h:72
virtual agx::UInt permuteRowIndex(agx::UInt rowIndex) const
virtual void reset()=0
virtual bool loadH5(void *group, const agx::String &name)
virtual void setNumBodies(size_t)
Definition: SparseMatrix.h:69
virtual void factor()=0
virtual void multiplyAdd(const agx::Real *x, agx::Real *r)=0
virtual void multiply(const agx::Real *x, agx::Real *r)=0
virtual bool saveH5(void *group, const agx::String &name) const
virtual agx::Bool getColumn(agx::UInt colNumber, agx::RealValarray &retCol, agx::Bool &signFlip) const =0
virtual agx::Bool getRow(agx::UInt rowNumber, const agx::RealValarray &signs, agx::RealValarray &retRow) const =0
virtual unsigned int getNumRows() const =0
virtual agx::UInt getBlockPermutedStartRowIndex(agx::UInt blockIndex) const =0
virtual agx::UInt invPermuteRowIndex(agx::UInt permutedRowIndex) const
virtual void multiplyAdd(const agx::Real &alpha, const agx::Real *x, const agx::Real &beta, agx::Real *r)=0
agx::SparseMatrix holds a matrix representation which contains the data.
Definition: SparseMatrix.h:86
bool loadH5(void *group, const agx::String &name)
Loads the matrix.
Definition: SparseMatrix.h:441
agx::UInt invPermuteRowIndex(agx::UInt permutedRowIndex) const
Definition: SparseMatrix.h:416
void setRep(SparseMatrixRep *rep)
Replace the Sparse Matrix Representation.
agx::Real * getBlock(int i, int j, bool &writeTransposed, unsigned int &leadingDimension)
Fetches the location for a block of the matrix.
Definition: SparseMatrix.h:366
size_t unitSolve(agx::Real *, int=-1)
Solve, the right hand side is overwritten with the result.
Definition: SparseMatrix.h:386
BlockFlags
Flags that can be used to tag blocks so that the matrix representation can optimize the calculations.
Definition: SparseMatrix.h:93
void solve(agx::Real *)
Solve, the right hand side is overwritten with the result.
Definition: SparseMatrix.h:381
agx::Bool getColumn(agx::UInt colNumber, agx::RealValarray &retCol) const
Extract a column from the matrix.
agx::Real getFactorTime() const
agx::UInt getBlockPermutedStartRowIndex(agx::UInt blockIndex) const
Definition: SparseMatrix.h:406
agx::UInt permuteRowIndex(agx::UInt rowIndex) const
Definition: SparseMatrix.h:411
SparseMatrixRep * getRep()
agx::Real getSolveTime() const
MatrixType & getType()
Definition: SparseMatrix.h:353
void reset()
Clears the matrix.
Definition: SparseMatrix.h:371
~SparseMatrix()
Destructor.
bool saveH5(void *group, const agx::String &name) const
Stores the matrix in HDF5 format.
Definition: SparseMatrix.h:436
agx::Bool getColumn(agx::UInt colNumber, agx::RealValarray &retCol, agx::Bool &signFlip) const
Extract a column from the matrix.
const SparseMatrixRep * getRep() const
unsigned int getNumRows() const
Definition: SparseMatrix.h:421
SparseMatrix(MatrixType type=MatrixType(), SparseMatrixRep *rep=0)
Constructor.
void multiplyAdd(const agx::Real *x, agx::Real *r)
Calculates r = Ax + r.
Definition: SparseMatrix.h:396
unsigned int getNumColumns() const
Definition: SparseMatrix.h:426
void setNumBodies(int num)
Sets the number of bodies, can be used by the representation to help making the permutation.
Definition: SparseMatrix.h:446
agx::Bool getRow(agx::UInt rowNumber, const agx::RealValarray &signs, agx::RealValarray &retRow) const
Extract a row from the matrix.
void factor()
Factors the matrix.
Definition: SparseMatrix.h:376
void print() const
Definition: SparseMatrix.h:431
void multiply(const agx::Real *x, agx::Real *r)
Calculates r = A x.
Definition: SparseMatrix.h:391
void setLogicalStructure(size_t n, const unsigned int *pointers, const unsigned int *indices, const unsigned int *dimensions, const int *flags=nullptr, unsigned int numThreads=1)
Sets the logical structure of the matrix.
Definition: SparseMatrix.h:359
#define AGX_FORCE_INLINE
Definition: macros.h:58
AGXCORE_EXPORT Type * getType(const agx::String &name)
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
T sign(T v)
Definition: Math.h:328
bool Bool
Definition: Integer.h:48
uint32_t UInt
Definition: Integer.h:34
double Real
Definition: Real.h:42
std::valarray< agx::Real > RealValarray
For this module, vectors are defined using valarray.