irlba
A C++ library for IRLBA
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
irlba::ParallelSparseMatrix< ValueArray_, IndexArray_, PointerArray_, EigenVector_ > Class Template Reference

Sparse matrix with customizable parallelization. More...

#include <parallel.hpp>

Public Types

typedef std::remove_const< typenamestd::remove_reference< decltype(std::declval< PointerArray_ >()[0])>::type ::type PointerType
 

Public Member Functions

 ParallelSparseMatrix ()
 
 ParallelSparseMatrix (Eigen::Index nrow, Eigen::Index ncol, ValueArray_ x, IndexArray_ i, PointerArray_ p, bool column_major, int nthreads)
 
Eigen::Index rows () const
 
Eigen::Index cols () const
 
const ValueArray_get_values () const
 
const IndexArray_get_indices () const
 
const PointerArray_get_pointers () const
 
const std::vector< size_t > & get_primary_starts () const
 
const std::vector< size_t > & get_primary_ends () const
 
const std::vector< std::vector< PointerType > > & get_secondary_nonzero_starts () const
 

Detailed Description

template<class ValueArray_ = std::vector<double>, class IndexArray_ = std::vector<int>, class PointerArray_ = std::vector<size_t>, class EigenVector_ = Eigen::VectorXd>
class irlba::ParallelSparseMatrix< ValueArray_, IndexArray_, PointerArray_, EigenVector_ >

Sparse matrix with customizable parallelization.

This provides an alternative to Eigen::SparseMatrix for parallelized multiplication of compressed sparse matrices. Unlike Eigen, this implementation is able to parallelize when the multiplication does not align well with the storage layout, e.g., multiplication of a compressed sparse column matrix by a dense vector on the right hand side. On construction, it also pre-allocates the rows and/or columns to each thread, aiming to balance the number of non-zero elements that each thread needs to process. All subsequent multiplications can then use these allocations, which is useful for compute() where the cost of pre-allocation is abrogated by repeated multiplication calls.

Some cursory testing indicates that the performance of this implementation is comparable to Eigen for OpenMP-based parallelization. However, the real purpose of this class is to support custom parallelization schemes in cases where OpenMP is not available. This is achieved by defining IRLBA_CUSTOM_PARALLEL macro to the name of a function implementing a custom scheme. Such a function should accept two arguments - an integer specifying the number of threads, and a lambda that accepts a thread number. It should then loop over the number of threads and launch one job for each thread via the lambda. Once all threads are complete, the function should return.

This class satisfies the MockMatrix interface and implements all of its methods/typedefs.

Template Parameters
ValueArray_Array class containing numeric values for the non-zero values. Should support a read-only [] operator.
IndexArray_Array class containing integer values for the indices of the non-zero values. Should support a read-only [] operator.
PointerArray_Array class containing integer values for the pointers to the row/column boundaries. Should support a read-only [] operator.
EigenVector_A floating-point Eigen::Vector class.

Member Typedef Documentation

◆ PointerType

template<class ValueArray_ = std::vector<double>, class IndexArray_ = std::vector<int>, class PointerArray_ = std::vector<size_t>, class EigenVector_ = Eigen::VectorXd>
typedef std::remove_const<typenamestd::remove_reference<decltype(std::declval<PointerArray_>()[0])>::type ::type irlba::ParallelSparseMatrix< ValueArray_, IndexArray_, PointerArray_, EigenVector_ >::PointerType

Type of the elements inside a PointerArray_.

Constructor & Destructor Documentation

◆ ParallelSparseMatrix() [1/2]

template<class ValueArray_ = std::vector<double>, class IndexArray_ = std::vector<int>, class PointerArray_ = std::vector<size_t>, class EigenVector_ = Eigen::VectorXd>
irlba::ParallelSparseMatrix< ValueArray_, IndexArray_, PointerArray_, EigenVector_ >::ParallelSparseMatrix ( )
inline

Default constructor. This object cannot be used for any operations.

◆ ParallelSparseMatrix() [2/2]

template<class ValueArray_ = std::vector<double>, class IndexArray_ = std::vector<int>, class PointerArray_ = std::vector<size_t>, class EigenVector_ = Eigen::VectorXd>
irlba::ParallelSparseMatrix< ValueArray_, IndexArray_, PointerArray_, EigenVector_ >::ParallelSparseMatrix ( Eigen::Index  nrow,
Eigen::Index  ncol,
ValueArray_  x,
IndexArray_  i,
PointerArray_  p,
bool  column_major,
int  nthreads 
)
inline
Parameters
nrowNumber of rows.
ncolNumber of columns.
xValues of non-zero elements.
iIndices of non-zero elements. Each entry corresponds to a value in x, so i should be an array of length equal to x. If column_major = true, i should contain row indices; otherwise it should contain column indices.
pPointers to the start of each column (if column_major = true) or row (otherwise). This should be an ordered array of length equal to the number of columns or rows plus 1.
column_majorWhether the matrix should be in compressed sparse column format. If false, this is assumed to be in row-major format.
nthreadsNumber of threads to be used for multiplication.

x, i and p represent the typical components of a compressed sparse column/row matrix. Thus, entries in i should be sorted within each column/row, where the boundaries between columns/rows are defined by p.

Member Function Documentation

◆ cols()

template<class ValueArray_ = std::vector<double>, class IndexArray_ = std::vector<int>, class PointerArray_ = std::vector<size_t>, class EigenVector_ = Eigen::VectorXd>
Eigen::Index irlba::ParallelSparseMatrix< ValueArray_, IndexArray_, PointerArray_, EigenVector_ >::cols ( ) const
inline
Returns
Number of columns in the matrix.

◆ get_indices()

template<class ValueArray_ = std::vector<double>, class IndexArray_ = std::vector<int>, class PointerArray_ = std::vector<size_t>, class EigenVector_ = Eigen::VectorXd>
const IndexArray_ & irlba::ParallelSparseMatrix< ValueArray_, IndexArray_, PointerArray_, EigenVector_ >::get_indices ( ) const
inline
Returns
Indices of non-zero elements, equivalent to i in the constructor. These are row or column indices for compressed sparse row or column format, respectively, depending on column_major.

◆ get_pointers()

template<class ValueArray_ = std::vector<double>, class IndexArray_ = std::vector<int>, class PointerArray_ = std::vector<size_t>, class EigenVector_ = Eigen::VectorXd>
const PointerArray_ & irlba::ParallelSparseMatrix< ValueArray_, IndexArray_, PointerArray_, EigenVector_ >::get_pointers ( ) const
inline
Returns
Pointers to the start of each row or column, equivalent to p in the constructor.

◆ get_primary_ends()

template<class ValueArray_ = std::vector<double>, class IndexArray_ = std::vector<int>, class PointerArray_ = std::vector<size_t>, class EigenVector_ = Eigen::VectorXd>
const std::vector< size_t > & irlba::ParallelSparseMatrix< ValueArray_, IndexArray_, PointerArray_, EigenVector_ >::get_primary_ends ( ) const
inline

This should only be called if nthreads > 1 in the constructor, otherwise it will not be initialized.

Returns
Vector of length equal to the number of threads, specifying the one-past-the-last dimension along the primary extent (e.g., column for column_major = true) that each thread works on.

◆ get_primary_starts()

template<class ValueArray_ = std::vector<double>, class IndexArray_ = std::vector<int>, class PointerArray_ = std::vector<size_t>, class EigenVector_ = Eigen::VectorXd>
const std::vector< size_t > & irlba::ParallelSparseMatrix< ValueArray_, IndexArray_, PointerArray_, EigenVector_ >::get_primary_starts ( ) const
inline

This should only be called if nthreads > 1 in the constructor, otherwise it will not be initialized. bool my_column_major;

Returns
Vector of length equal to the number of threads, specifying the first dimension along the primary extent (e.g., column for column_major = true) that each thread works on.

◆ get_secondary_nonzero_starts()

template<class ValueArray_ = std::vector<double>, class IndexArray_ = std::vector<int>, class PointerArray_ = std::vector<size_t>, class EigenVector_ = Eigen::VectorXd>
const std::vector< std::vector< PointerType > > & irlba::ParallelSparseMatrix< ValueArray_, IndexArray_, PointerArray_, EigenVector_ >::get_secondary_nonzero_starts ( ) const
inline

This should only be called if nthreads > 1 in the constructor, otherwise it will not be initialized.

Returns
Vector of length equal to the number of threads plus one. Each inner vector is of length equal to the size of the primary extent (e.g., number of columns for column_major = true). For thread i, the vectors i and i + 1 define the ranges of non-zero elements assigned to that thread within each primary dimension. This is guaranteed to contain all and only non-zero elements with indices in a contiguous range of secondary dimensions.

◆ get_values()

template<class ValueArray_ = std::vector<double>, class IndexArray_ = std::vector<int>, class PointerArray_ = std::vector<size_t>, class EigenVector_ = Eigen::VectorXd>
const ValueArray_ & irlba::ParallelSparseMatrix< ValueArray_, IndexArray_, PointerArray_, EigenVector_ >::get_values ( ) const
inline
Returns
Non-zero elements in compressed sparse row/column format. This is equivalent to x in the constructor.

◆ rows()

template<class ValueArray_ = std::vector<double>, class IndexArray_ = std::vector<int>, class PointerArray_ = std::vector<size_t>, class EigenVector_ = Eigen::VectorXd>
Eigen::Index irlba::ParallelSparseMatrix< ValueArray_, IndexArray_, PointerArray_, EigenVector_ >::rows ( ) const
inline
Returns
Number of rows in the matrix.

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