AGX Dynamics 2.38.0.2
Loading...
Searching...
No Matches
SparseMatrix.h
Go to the documentation of this file.
1/*
2Copyright 2007-2024. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx 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
24#include <agx/MatrixTypes.h>
25
26namespace 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
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:254
#define AGXPHYSICS_EXPORT
Class that stores rows and columns of the matrix.
const RealValarray & getOrCreateRow(UInt row)
ColumnStorage(const SparseMatrix &matrix, const RealValarray &signs)
const RealValarray & getOrCreateColumn(UInt col)
iterator insert(const KeyT &key, const ValueT value)
Insert a key/value pair into the hash table.
iterator end()
Iterator marking end of 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:120
SparseMatrixRep.
virtual void print() const
virtual void solve(agx::Real *)=0
virtual agx::Real * getBlock(int i, int j, bool &writeTransposed, unsigned int &lda)=0
virtual size_t unitSolve(agx::Real *, int=-1)
virtual ~SparseMatrixRep()
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
virtual unsigned int getNumColumns() const =0
virtual agx::Real getSolveTime() const
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)
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.
bool loadH5(void *group, const agx::String &name)
Loads the matrix.
agx::UInt invPermuteRowIndex(agx::UInt permutedRowIndex) const
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.
size_t unitSolve(agx::Real *, int=-1)
Solve, the right hand side is overwritten with the result.
BlockFlags
Flags that can be used to tag blocks so that the matrix representation can optimize the calculations.
void solve(agx::Real *)
Solve, the right hand side is overwritten with the result.
SparseMatrixRep * getRep()
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
agx::UInt permuteRowIndex(agx::UInt rowIndex) const
agx::Real getSolveTime() const
MatrixType & getType()
void reset()
Clears the matrix.
~SparseMatrix()
Destructor.
bool saveH5(void *group, const agx::String &name) const
Stores the matrix in HDF5 format.
agx::Bool getColumn(agx::UInt colNumber, agx::RealValarray &retCol, agx::Bool &signFlip) const
Extract a column from the matrix.
unsigned int getNumRows() const
SparseMatrix(MatrixType type=MatrixType(), SparseMatrixRep *rep=0)
Constructor.
const SparseMatrixRep * getRep() const
void multiplyAdd(const agx::Real *x, agx::Real *r)
Calculates r = Ax + r.
unsigned int getNumColumns() const
void setNumBodies(int num)
Sets the number of bodies, can be used by the representation to help making the permutation.
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.
void print() const
void multiply(const agx::Real *x, agx::Real *r)
Calculates r = A x.
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.
#define AGX_FORCE_INLINE
Definition macros.h:58
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:40
uint64_t UInt
Definition Integer.h:27
double Real
Definition Real.h:42
std::valarray< agx::Real > RealValarray
For this module, vectors are defined using valarray.