ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SparseAssembly.h File Reference

The assembly of a sparse matrix from its triplets, beyond the book: the entries \( ( i , j , v ) \) in any order, with repetitions, become the arrays of the compressed form that the solvers of linear programs take (\( \mathit{beg} \), \( \mathit{ind} \) and \( \mathit{val} \), by columns or by rows), written in the arrays of the caller, with no matrix object in between and no other allocation than a workspace that a caller may keep from one assembly to the next. More...

#include "ADS/Contract.h"
#include "ADS/numeric/DenseMatrix.h"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <utility>
#include <type_traits>
#include <vector>
#include "ADS/numeric/impl/SparseAssembly.tpp"

Go to the source code of this file.

Classes

struct  ADS::AssemblyWorkspace< I, V >
 the scratch of compressTriplets(), which keeps its memory from one call to the next More...

Namespaces

namespace  ADS

Functions

template<class I, class V, class J>
std::size_t ADS::compressTriplets (std::size_t nmajor, std::size_t nminor, std::size_t z, const J *major, const J *minor, const V *value, I *beg, I *ind, V *val, AssemblyWorkspace< I, V > &work)
 the compressed form, by major index, of the \( z \) triplets \( ( \mathit{major}_k , \mathit{minor}_k , \mathit{value}_k ) \) of a matrix of \( \mathit{nmajor} \times \mathit{nminor} \), the values of equal places summed; the number of the entries
template<class I, class V, class J>
std::size_t ADS::compressTriplets (std::size_t nmajor, std::size_t nminor, std::size_t z, const J *major, const J *minor, const V *value, I *beg, I *ind, V *val)
 the form of compressTriplets() above, with a workspace of its own
template<class I, class V, class J>
std::size_t ADS::assembleCsc (std::size_t m, std::size_t n, std::size_t z, const J *row, const J *col, const V *value, I *beg, I *ind, V *val, AssemblyWorkspace< I, V > &work)
 the compressed sparse columns of the \( m \times n \) matrix of the \( z \) triplets \( ( \mathit{row}[ k ] , \mathit{col}[ k ] , \mathit{value}[ k ] ) \): beg has \( n + 1 \) places, ind holds the rows; the number of the entries
template<class I, class V, class J>
std::size_t ADS::assembleCsr (std::size_t m, std::size_t n, std::size_t z, const J *row, const J *col, const V *value, I *beg, I *ind, V *val, AssemblyWorkspace< I, V > &work)
 the compressed sparse rows of the same: beg has \( m + 1 \) places, ind holds the columns; the number of the entries

Detailed description

The assembly of a sparse matrix from its triplets, beyond the book: the entries \( ( i , j , v ) \) in any order, with repetitions, become the arrays of the compressed form that the solvers of linear programs take (\( \mathit{beg} \), \( \mathit{ind} \) and \( \mathit{val} \), by columns or by rows), written in the arrays of the caller, with no matrix object in between and no other allocation than a workspace that a caller may keep from one assembly to the next.

compressTriplets() first distributes the entries by their major index (the column of the compressed sparse columns, the row of the compressed sparse rows), by the counting sort (section 5.2 of Knuth): one read of the major indices counts them, the prefix sums give \( \mathit{beg} \), and a second read of the triplets writes each entry at the cursor of its major index in \( \mathit{ind} \) and \( \mathit{val} \), straight into the arrays of the caller, so that each major index has its entries in the order of the input. Then each major index sums its repetitions and sorts its distinct entries: a mark per minor index, the place written last for it plus 1, tells whether a minor index already has a place in the current major index (the places of the earlier ones all come before it), and if so the value is added there, in the order of the input, otherwise it takes the next place (chapter 2 of Davis); the distinct entries are then sorted by their minor indices, by insertion up to 32 of them, i.e., at most \( k ( k - 1 ) / 2 \) moves for \( k \) of them and none if they are already in order, and by the sort of the library beyond. Only one pass scatters the entries over memory, where the sort by the least significant index first, or the transposition of a matrix built by the other index (as in setFromTriplets() of Eigen), scatters them twice; the time is \( O( z + m + n + \sum_i \min( k_i^2 , k_i \log k_i ) ) \) for \( z \) triplets of an \( m \times n \) matrix whose major index \( i \) has \( k_i \) distinct entries, i.e., \( O( z + m + n ) \) for rows or columns of bounded length, and the workspace holds a cursor per major index, a mark per minor one and the pairs of the longest major index past 32 entries.

assembleCsc() and assembleCsr() name the two orientations: \( \mathit{beg} \) has one entry more than the major indices, \( \mathit{beg}[ k ] \) being the first entry of the major index \( k \) and its last entry the number of the entries, \( \mathit{ind} \) the minor index of each entry and \( \mathit{val} \) its value. The entries summed to 0 stay, as the solvers accept them.

The contracts [see Contract.h]: the indices of the triplets are below the dimensions, \( \mathit{beg} \) has room for the major indices plus 1, \( \mathit{ind} \) and \( \mathit{val} \) for the triplets, and the number of the triplets fits the index type I. The functions are defined in SparseAssembly.tpp.

See also
D. E. Knuth, The Art of Computer Programming, vol. 3, Sorting and Searching, 2nd ed., Addison-Wesley, 1998, section 5.2 (the distribution counting, and the sort by the least significant digit first)
F. G. Gustavson, Two fast algorithms for sparse matrices: multiplication and permuted transposition, ACM Transactions on Mathematical Software 4(3), 250-269, 1978, doi:10.1145/355791.355796
T. A. Davis, Direct Methods for Sparse Linear Systems, SIAM, 2006, doi:10.1137/1.9780898718881, chapter 2 (the compression of the triplets and the sum of the duplicates)
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa