So-Bogus
A c++ sparse block matrix library aimed at Second Order cone problems
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
bogus::MappedSparseBlockMatrix< BlockT, Flags, Index_ > Class Template Reference

Mapped Sparse Block Matrix. More...

#include <MappedSparseBlockMatrix.hpp>

Inheritance diagram for bogus::MappedSparseBlockMatrix< BlockT, Flags, Index_ >:
bogus::SparseBlockMatrixBase< MappedSparseBlockMatrix< BlockT, Flags, Index_ > > bogus::BlockMatrixBase< Derived > bogus::IterableBlockObject< Derived > bogus::BlockObjectBase< Derived >

Public Types

typedef SparseBlockMatrixBase
< MappedSparseBlockMatrix
Base
 
typedef Base::Index Index
 
typedef Base::BlockPtr BlockPtr
 
typedef BlockMatrixTraits
< MappedSparseBlockMatrix
< BlockT, Flags, Index_ > > 
Traits
 
typedef Traits::MajorIndexType MajorIndexType
 
typedef Base::BlockType BlockType
 
typedef Base::Scalar Scalar
 
typedef
MajorIndexType::InnerIterator 
InnerIterator
 
typedef SparseBlockIndex
< false, Index, BlockPtr > 
UncompressedIndexType
 
typedef SparseBlockIndex< true,
Index, BlockPtr > 
CompressedIndexType
 
typedef UncompressedIndexType MinorIndexType
 
typedef CompressedIndexType TransposeIndexType
 
typedef TypeSwapIf
< Traits::is_col_major,
MajorIndexType, MinorIndexType >
::First 
RowIndexType
 
typedef TypeSwapIf
< Traits::is_col_major,
MajorIndexType, MinorIndexType >
::Second 
ColIndexType
 
typedef MutableImpl< BlockType,
true >::Type 
CopyResultType
 
typedef
Base::ConstTransposeReturnType 
ConstTransposeReturnType
 
typedef BlockTraits< BlockType >
::TransposeStorageType 
TransposeBlockType
 
typedef
ResizableSequenceContainer
< TransposeBlockType >::Type 
TransposeArrayType
 
enum  CompileTimeProperties {
  RowsPerBlock = Traits::RowsPerBlock, ColsPerBlock = Traits::ColsPerBlock, has_row_major_blocks = BlockTraits< BlockType >::is_row_major, has_square_or_dynamic_blocks = ColsPerBlock == RowsPerBlock,
  has_fixed_rows_blocks = ((int) RowsPerBlock != internal::DYNAMIC ), has_fixed_cols_blocks = ((int) ColsPerBlock != internal::DYNAMIC ), has_fixed_size_blocks = has_fixed_cols_blocks && has_fixed_rows_blocks
}
 Compile-time block properties.
 
typedef Traits::BlocksArrayType BlocksArrayType
 
enum  { is_transposed = Traits::is_transposed }
 
enum  { RowsAtCompileTime = internal::DYNAMIC, ColsAtCompileTime = internal::DYNAMIC, is_self_transpose = Traits::is_symmetric }
 
typedef Traits::TransposeObjectType TransposeObjectType
 
typedef Traits::PlainObjectType PlainObjectType
 

Public Member Functions

template<typename Derived >
 MappedSparseBlockMatrix (const SparseBlockMatrixBase< Derived > &source)
 
void mapTo (std::size_t numberOfNonZeros, const BlockT *dataPtr, const Index *outerIndexPtr, const Index *innerIndexPtr)
 
template<typename Derived >
void mapTo (const SparseBlockMatrixBase< Derived > &source)
 
BlockType & block (Index row, Index col)
 
const BlockType & block (Index row, Index col) const
 
Index rows () const
 Returns the total number of rows of the matrix ( expanding blocks )
 
Index cols () const
 Returns the total number of columns of the matrix ( expanding blocks )
 
const Traits::BlocksArrayType & blocks () const
 Access to blocks data.
 
const BlockType * data () const
 Access to blocks data as a raw pointer.
 
BlockType * data ()
 Access to blocks data as a raw pointer.
 
BlockType & diagonal (const Index row)
 
const BlockType & diagonal (const Index row) const
 
template<typename Func >
void eachBlockOfRow (const Index row, Func func) const
 Iterates over each block of a given row. Calls func( col, block )
 
template<typename Func >
void eachBlockOfCol (const Index col, Func func) const
 Iterates over each block of a given col. Calls func( row, block )
 
template<bool DoTranspose, typename RhsT , typename ResT >
void rowMultiply (const Index row, const RhsT &rhs, ResT &res) const
 Multiplication with a single row.
 
template<bool DoTranspose, typename RhsT , typename ResT >
void colMultiply (const Index col, const RhsT &rhs, ResT &res) const
 Multiplication with a single column.
 
const Derived & derived () const
 Returns a const reference to the implementation.
 
Derived & derived ()
 Returns a reference to the implementation.
 
Index rowOffset (Index row) const
 Returns an array containing the first index of a given row.
 
Index colOffset (Index col) const
 Returns an array containing the first index of a given columns.
 
Setting and accessing the matrix structure
void setRows (const Index nBlocks, const unsigned *rowsPerBlock)
 Defines the row structure of the matrix. More...
 
void setRows (const std::vector< unsigned > &rowsPerBlock)
 Same, using a std::vector.
 
void setRows (const Index nBlocks, const Index rowsPerBlock)
 Same, setting each block to have exactly rowsPerBlock.
 
void setRows (const Index nBlocks)
 Same, deducing the (constant) number of rows per block from the BlockType.
 
void setCols (const Index nBlocks, const unsigned *colsPerBlock)
 Defines the column structure of the matrix. More...
 
void setCols (const std::vector< unsigned > &colsPerBlock)
 Same, using a std::vector.
 
void setCols (const Index nBlocks, const Index colsPerBlock)
 Same, setting each block to have exactly rowsPerBlock.
 
void setCols (const Index nBlocks)
 Same, deducing the (constant) number of rows per block from the BlockType.
 
Index rowsOfBlocks () const
 
Index colsOfBlocks () const
 
Index blockRows (Index row) const
 
Index blockCols (Index col) const
 
Inserting and accessing blocks
void reserve (std::size_t nBlocks)
 Reserve enouch memory to accomodate nBlocks.
 
BlockType & insertBack (Index row, Index col)
 Inserts a block at the end of the matrix, and returns a reference to it. More...
 
BlockType & insertBackAndResize (Index row, Index col)
 Convenience method that insertBack() a block and immediately resize it according to the dimensions given to setRows() and setCols()
 
BlockType & insert (Index row, Index col)
 Insert a block anywhere in the matrix, and returns a reference to it. More...
 
BlockType & insertAndResize (Index row, Index col)
 Convenience method that insert() a block and immediately resize it according to the dimensions given to setRows() and setCols()
 
BlockType & insertByOuterInner (Index outer, Index inner)
 Insert a block, specifying directily the outer and inner indices instead of row and column. More...
 
void finalize ()
 
void clear ()
 Clears the matrix.
 
MappedSparseBlockMatrix
< BlockT, Flags, Index_ > & 
setZero ()
 
MappedSparseBlockMatrix
< BlockT, Flags, Index_ > & 
setIdentity ()
 Calls set_identity() on each diagonal block, discard off-diagonal blocks.
 
MappedSparseBlockMatrix
< BlockT, Flags, Index_ > & 
setBlocksToZero ()
 Sets all allocated blocks to zero. Does not update index.
 
std::size_t nBlocks () const
 Returns the number of (non-zero) blocks of the matrix.
 
std::size_t size () const
 
bool empty () const
 Returns whether the matrix is empty.
 
const BlockType & block (BlockPtr ptr) const
 
BlockType & block (BlockPtr ptr)
 
BlockPtr blockPtr (Index row, Index col) const
 Return a BlockPtr to the block a (row, col) or InvalidBlockPtr if it does not exist.
 
BlockPtr diagonalBlockPtr (Index row) const
 Return a BlockPtr to the block a (row, row) or InvalidBlockPtr if it does not exist.
 
void eachBlockOf (const Index outer, Func func) const
 Iterates over each block of a given row or col. Calls func( inner, block )
 
Accessing and manipulating indexes
bool computeMinorIndex ()
 Computes the minor index of the matrix. More...
 
void cacheTranspose ()
 Computes and caches the tranpose of the matrix. More...
 
bool transposeCached () const
 Returns whether the transpose has been cached.
 
InnerIterator innerIterator (Index outer) const
 Returns an InnerIterator to efficiently browse matrix. More...
 
const MajorIndexType & majorIndex () const
 
const MinorIndexTypeminorIndex () const
 
const TransposeIndexTypetransposeIndex () const
 
const ColIndexType & colMajorIndex () const
 
const RowIndexType & rowMajorIndex () const
 
Unsafe API
MajorIndexType & majorIndex ()
 Direct access to major index. More...
 
void prealloc (std::size_t nBlocks)
 Resizes m_blocks.
 
const TransposeArrayType & transposeBlocks () const
 Returns the blocks that have been created by cacheTranspose()
 
const TransposeBlockType * transposeData () const
 Returns the blocks that have been created by cacheTranspose(), as a raw pointer.
 
const Index * rowOffsets () const
 Returns an array containing the first index of each row.
 
const Index * colOffsets () const
 Returns an array containing the first index of each column.
 
const Locklock () const
 Reference to matrix private mutex.
 
Assignment and cloning operations
MappedSparseBlockMatrix
< BlockT, Flags, Index_ > & 
assign (const SparseBlockMatrixBase< OtherDerived > &source, const Scalar scale=1)
 Performs ( *this = scale * source ) or ( *this = scale * source.transpose() )
 
void cloneDimensions (const BlockMatrixBase< OtherDerived > &source)
 Clones the dimensions ( number of rows/cols blocks and rows/cols per block ) of source.
 
void cloneStructure (const SparseBlockMatrixBase< OtherDerived > &source)
 Clones the dimensions and the indexes of source.
 
void cloneIndex (const MajorIndexType &index)
 
Linear algebra
ConstTransposeReturnType transpose () const
 
void multiply (const RhsT &rhs, ResT &res, Scalar alpha=1, Scalar beta=0) const
 
void splitRowMultiply (const Index row, const RhsT &rhs, ResT &res) const
 
void rowMultiplyPrecompose (const Index row, const RhsT &rhs, ResT &res, const PreOp &op) const
 
void colMultiplyPostcompose (const Index row, const RhsT &rhs, ResT &res, const PostOp &op) const
 
void setFromProduct (const Product< LhsT, RhsT > &prod)
 
MappedSparseBlockMatrix
< BlockT, Flags, Index_ > & 
scale (Scalar alpha)
 Performs *this *= alpha.
 
MappedSparseBlockMatrix
< BlockT, Flags, Index_ > & 
add (const SparseBlockMatrixBase< OtherDerived > &rhs, Scalar alpha=1)
 Performs *this += alpha * rhs (SAXPY)
 
MappedSparseBlockMatrix
< BlockT, Flags, Index_ > & 
operator*= (Scalar alpha)
 Coeff-wise multiplication with a scalar.
 
MappedSparseBlockMatrix
< BlockT, Flags, Index_ > & 
operator/= (Scalar alpha)
 Coeff-wise division with a scalar.
 
Scaling
< MappedSparseBlockMatrix
< BlockT, Flags, Index_ > > 
operator- () const
 Unary minus.
 
MappedSparseBlockMatrix
< BlockT, Flags, Index_ > & 
operator+= (const BlockObjectBase< OtherDerived > &source)
 Adds another SparseBlockMatrixBase to this one.
 
MappedSparseBlockMatrix
< BlockT, Flags, Index_ > & 
operator-= (const BlockObjectBase< OtherDerived > &source)
 Substracts another SparseBlockMatrixBase from this one.
 
I/O
void serialize (Archive &ar, const unsigned int file_version)
 
Miscellaneous operations
MappedSparseBlockMatrix
< BlockT, Flags, Index_ > & 
prune (const Scalar precision=0)
 Removes all blocks for which is_zero( block, precision ) is true. More...
 
MappedSparseBlockMatrix
< BlockT, Flags, Index_ > & 
applyPermutation (const std::size_t *indices)
 Sets *this = P * (*this) * P.transpose(). More...
 
Dimension-cloning construction methods
CopyResultType Zero () const
 
CopyResultType Identity () const
 

Static Public Attributes

static const BlockPtr InvalidBlockPtr
 Return value of blockPtr( Index, Index ) for non-existing block.
 

Protected Types

enum  
 
typedef
SparseBlockMatrixFinalizer
< Traits::is_symmetric > 
Finalizer
 

Protected Member Functions

void computeMinorIndex (MinorIndexType &cmIndex) const
 
ColIndexType & colMajorIndex ()
 
RowIndexType & rowMajorIndex ()
 
TransposeBlockType * transposeData ()
 
BlockType & allocateBlock (BlockPtr &ptr)
 Pushes a block at the back of m_blocks.
 
const MinorIndexTypegetOrComputeMinorIndex (MinorIndexType &tempIndex) const
 
void setInnerOffets (IndexT &index, const Index nBlocks, const unsigned *blockSizes) const
 

Protected Attributes

TransposeArrayType m_transposeBlocks
 
MajorIndexType m_majorIndex
 
MinorIndexType m_minorIndex
 
TransposeIndexType m_transposeIndex
 
Lock m_lock
 
Index m_rows
 
Index m_cols
 
BlocksArrayType m_blocks
 

Detailed Description

template<typename BlockT, int Flags, typename Index_>
class bogus::MappedSparseBlockMatrix< BlockT, Flags, Index_ >

Mapped Sparse Block Matrix.

Allows bogus to operate on an external, immutable matrix using a compressed index ( ala MKL BSR – Block Sparse Row ) Only const (or caching) operations are available.

To obtain a valid MappedSparseBlockMatrix from a set of raw pointers, two operations are necessary 1 Set the size of each row and column of blocks using the setRows()/setCols() or cloneDimensions() methods 2 Map the block data and index using the mapTo() method

Template Parameters
BlockTthe type of the blocks of the matrix. Can be scalar, Eigen dense of sparse matrices, or basically anything provided a few functions are specialized
Flagsa combination of the values defined in bogus::flags
Theinteger type used for indexing

Member Function Documentation

MappedSparseBlockMatrix< BlockT, Flags, Index_ > & bogus::SparseBlockMatrixBase< MappedSparseBlockMatrix< BlockT, Flags, Index_ > >::applyPermutation ( const std::size_t *  indices)
inherited

Sets *this = P * (*this) * P.transpose().

P is build such that its non-zeros blocks are P( i, indices[i] ) = Id. This means that after a call to this this function, the block that was originally at (indices[i],indices[j]) will end up at (i, j).

Warning
The number of rows of blocks have to be equal to the number of columns of blocks.
This method will move the blocks data so it remains cache-coherent. This is costly.
template<typename Derived>
BlockType& bogus::BlockMatrixBase< Derived >::block ( Index  row,
Index  col 
)
inherited
Warning
block has to exist
template<typename Derived>
const BlockType& bogus::BlockMatrixBase< Derived >::block ( Index  row,
Index  col 
) const
inherited
Warning
block has to exist
void bogus::SparseBlockMatrixBase< MappedSparseBlockMatrix< BlockT, Flags, Index_ > >::cacheTranspose ( )
inherited

Computes and caches the tranpose of the matrix.

This will speed up some operations, especially splitRowMultiply() on symmetric matrices ( At a cost of doubling the memory storage, obviously )

void bogus::SparseBlockMatrixBase< MappedSparseBlockMatrix< BlockT, Flags, Index_ > >::cloneIndex ( const MajorIndexType &  index)
inherited

Clone an external index and allocate corresponding blocks. Rows and Cols dimensions should be set beforhand

bool bogus::SparseBlockMatrixBase< MappedSparseBlockMatrix< BlockT, Flags, Index_ > >::computeMinorIndex ( )
inherited

Computes the minor index of the matrix.

That is, the column major index for row-major matrices, and vice versa. This may speed up some operations, such as matrix/matrix multiplication under some circumstances.

template<typename Derived>
BlockType& bogus::BlockMatrixBase< Derived >::diagonal ( const Index  row)
inherited
Warning
block has to exist
template<typename Derived>
const BlockType& bogus::BlockMatrixBase< Derived >::diagonal ( const Index  row) const
inherited
Warning
block has to exist
void bogus::SparseBlockMatrixBase< MappedSparseBlockMatrix< BlockT, Flags, Index_ > >::finalize ( )
inherited

Finalizes the matrix.

Warning
Should always be called after all blocks have been inserted, or bad stuff may happen
InnerIterator bogus::SparseBlockMatrixBase< MappedSparseBlockMatrix< BlockT, Flags, Index_ > >::innerIterator ( Index  outer) const
inherited

Returns an InnerIterator to efficiently browse matrix.

The iterator supports the pre-increment and casting-to-bool operations ot check fot its validity. The column (resp row.) can be accessed through the inner() method, and the block index through the ptr() method.

Parameters
outerIndex of the row (resp. column) of a row-major (resp. column-major) matrix on which to iterate
See Also
Reading the contents of a SparseBlockMatrix
BlockType& bogus::SparseBlockMatrixBase< MappedSparseBlockMatrix< BlockT, Flags, Index_ > >::insert ( Index  row,
Index  col 
)
inherited

Insert a block anywhere in the matrix, and returns a reference to it.

Warning
Only available for matrices which use an Uncompressed index
Note
Out of order insertion might lead to bad cache performance, and a std::sort will be performed on each inner vector so we can keep efficient block( row, col ) look-up functions.
This method is thread-safe if and only if:
  • Concurrent insertion is done in different outer vectors,
  • A sufficient number of blocks has been allocated by calling reserve(), so that the block array never has to be resized. Otherwise, the block reference return by insert() may be invalidated.
BlockType& bogus::SparseBlockMatrixBase< MappedSparseBlockMatrix< BlockT, Flags, Index_ > >::insertBack ( Index  row,
Index  col 
)
inherited

Inserts a block at the end of the matrix, and returns a reference to it.

Warning
The insertion order must be such that the pair ( outerIndex, innerIndex ) is always strictly increasing. That is, if the matrix is row-major, the insertion should be done one row at a time, and for each row from the left-most column to the right most.
BlockType& bogus::SparseBlockMatrixBase< MappedSparseBlockMatrix< BlockT, Flags, Index_ > >::insertByOuterInner ( Index  outer,
Index  inner 
)
inherited

Insert a block, specifying directily the outer and inner indices instead of row and column.

Template Parameters
OrderedIf true, then we assume that the insertion is sequential and in stricly increasing order ( as defined in insertBack() )
MajorIndexType& bogus::SparseBlockMatrixBase< MappedSparseBlockMatrix< BlockT, Flags, Index_ > >::majorIndex ( )
inherited

Direct access to major index.

Could be used in conjunction with prealloc() to devise a custom way of building the index. Dragons, etc.

template<typename BlockT, int Flags, typename Index_>
void bogus::MappedSparseBlockMatrix< BlockT, Flags, Index_ >::mapTo ( std::size_t  numberOfNonZeros,
const BlockT *  dataPtr,
const Index *  outerIndexPtr,
const Index *  innerIndexPtr 
)
Parameters
numberOfNonZerosTotal number of blocks
dataPtrPointer to an array containing the blocks data
outerIndexPtrA.k.a rowsIndex, pntrb, pntre-1 from BSR format
innerIndexPtrA.k.a columns from BSR format
MappedSparseBlockMatrix< BlockT, Flags, Index_ > & bogus::SparseBlockMatrixBase< MappedSparseBlockMatrix< BlockT, Flags, Index_ > >::prune ( const Scalar  precision = 0)
inherited

Removes all blocks for which is_zero( block, precision ) is true.

This function compacts the blocks and rebuild the index, which can be slow

void bogus::SparseBlockMatrixBase< MappedSparseBlockMatrix< BlockT, Flags, Index_ > >::setCols ( const Index  nBlocks,
const unsigned *  colsPerBlock 
)
inherited

Defines the column structure of the matrix.

Parameters
nBlocksthe number of columns of blocks
colsPerBlockarray containing the number of columns of each block
void bogus::SparseBlockMatrixBase< MappedSparseBlockMatrix< BlockT, Flags, Index_ > >::setRows ( const Index  nBlocks,
const unsigned *  rowsPerBlock 
)
inherited

Defines the row structure of the matrix.

Parameters
nBlocksthe number of rows of blocks
rowsPerBlockarray containing the number of row of each block
MappedSparseBlockMatrix< BlockT, Flags, Index_ > & bogus::SparseBlockMatrixBase< MappedSparseBlockMatrix< BlockT, Flags, Index_ > >::setZero ( )
inherited
See Also
clear()

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