![]() |
Eigen
5.0.1
|
#include <Eigen/src/SparseCore/ThreadedSparseProduct.h>
Cached, thread-parallel sparse matrix * dense vector product.
Designed for iterative solvers (CG, BiCGSTAB, GMRES, LSCG) that multiply many times by the same sparse matrix and, in the case of LSCG, by its adjoint. The constructor (or analyzePattern()) computes an nnz-balanced row partition once and reuses it across every apply()/applyAdjoint() call.
The forward direction (apply) always runs a dot-product-per-outer kernel, which is embarrassingly parallel (no write conflicts). For matrices whose native storage order doesn't match the forward direction (ColMajor for forward, or RowMajor for adjoint), a transposed mirror is built lazily on first use. The mirror doubles the memory footprint of the matrix but permits conflict-free parallel writes for both directions.
The matrix is held by const reference (no copy). Caller is responsible for keeping it alive across apply calls, and for invalidating the cache when the matrix changes:
Aliasing: apply()/applyAdjoint() do NOT insert temporaries on overlap between x and y; callers must use distinct storage (the kernel writes y[i] from a sum over x[inner[k]] reads, which would mis-compute if y aliases x). Asserted in debug builds.
Thread-safety: the const apply()/applyAdjoint()/applyAddTo()/applyAdjointAddTo() methods are safe to call concurrently on the same operator – the lazy mirror is published through an atomic with double-checked locking. The non-const methods (analyzePattern()/compute()/refreshValues()) and destruction are NOT; they mutate or free the cached mirror, so they must not overlap any in-flight apply() (the same rule as calling any method concurrently with the destructor).
Layout: x and y are taken as Ref<const DenseVector> / Ref<DenseVector>. Plain vectors and unit-inner-stride views (e.g. a column of a ColMajor matrix, an unaligned Map of a contiguous buffer) bind without copying; a strided const input is copy-evaluated by Ref; a strided mutable output triggers a Ref runtime error.
| SparseMatrixType | A compressed Eigen::SparseMatrix<Scalar, Order, StorageIndex>. |
Public Member Functions | |
| ThreadedSparseProduct & | analyzePattern (const SparseMatrixType &mat) |
| void | apply (const ConstVectorRef &x, MutableVectorRef y) const |
| Overwriting forward apply: y = A * x. | |
| void | applyAddTo (const ConstVectorRef &x, MutableVectorRef y, const Scalar &alpha) const |
| Accumulating forward apply: y += alpha * A * x. | |
| void | applyAdjoint (const ConstVectorRef &x, MutableVectorRef y) const |
| Overwriting adjoint apply: y = A^H * x. | |
| void | applyAdjointAddTo (const ConstVectorRef &x, MutableVectorRef y, const Scalar &alpha) const |
| Accumulating adjoint apply: y += alpha * A^H * x. | |
| bool | hasMirror () const |
| True iff the lazy adjoint mirror has been materialized. | |
| ThreadPool * | pool () const |
| Returns the thread pool used by this operator. | |
| ThreadedSparseProduct & | refreshValues () |
|
inline |
Bind to mat and (re)compute the partition. Drops any previously built adjoint mirror.
|
inline |
Drops the cached adjoint mirror without recomputing the partition. Use after coefficient-only updates to the bound matrix so the next applyAdjoint() (or forward apply on a ColMajor A) rebuilds the mirror with the new values.