SpECTRE  v2026.11.17
Loading...
Searching...
No Matches
SimpleSparseMatrix Class Reference

A simple fast sparse matrix. More...

#include <SimpleSparseMatrix.hpp>

Public Member Functions

void fill (const std::vector< SparseMatrixElement > &data)
 Fills with data. Normally called only by SparseMatrixFiller. ASSUMES that data has already been sorted by row, then column.
template<typename T>
void increment_multiply_on_right (gsl::not_null< T * > a, size_t a_offset, size_t a_stride, const T &b, size_t b_offset, size_t b_stride) const
 Does a += m*b where m is the sparse matrix. T is a container of doubles, like a std::vector<double>. Assumes b points into contiguous storage that allows one to access b[i] for b_offset <= i <= b_offset+max(column_indices). Assumes a points into contiguous storage that allows one to access a[i] for a_offset <= a <= a_offset+max(row_indices).
void increment_multiply_on_right_all_offsets (gsl::not_null< gsl::span< double > * > a, const gsl::span< double > &b, size_t stride) const
 Does a += m * b for stride right-hand sides at once.
size_t size () const
 Number of nonzero elements.
double operator() (size_t row_dest_index, size_t column_src_index) const
 Obtains element at (destindex,srcindex). Slow, should not use very often in critical situations.

Static Public Attributes

static constexpr size_t max_fused_stride = 24
 Largest stride for which increment_multiply_on_right_all_offsets uses the fused kernel. One kernel is compiled per stride up to this value, which bounds the amount of generated code.

Friends

bool operator== (const SimpleSparseMatrix &lhs, const SimpleSparseMatrix &rhs)=default
 Compares the stored nonzero elements.

Detailed Description

A simple fast sparse matrix.

Supports only a limited number of operations to keep it simple: filling the matrix, accessing elements, and multiplication by a vector on the right.

We use SimpleSparseMatrix because filling a Blaze sparse matrix is enormously slow. For example, here are timings for creating a rank-one TensorYlmFilter sparse matrix using SimpleSparseMatrix (column labeled "this") and using blaze::CompressedMatrix (column labeled "Blaze"). The column "Blaze-wo-blaze" is the same as the "Blaze" column (which includes timing of all the TensorYlmFilter code that doesn't explicitly depend on blaze) minus the timings of the blaze calls that 1. resize the matrix, 2. reserve elements, and

  1. fill the blaze matrix. The timings for those individual blaze calls are in subsequent columns. Note that "Blaze-wo-blaze" is roughly equivalent to "this" in speed. Also note that the "this" timings were done in SpEC, which uses SimpleSparseMatrix [which was ported from SpEC to SpECTRE and simplified here].

l_max this Blaze Blaze-wo-blaze blaze.resize blaze.reserve blaze.fill

8 0ms 5ms 1ms 1ms 2ms 0ms 18 1ms 101ms 2ms 39ms 51ms 8ms 28 3ms 479ms 3ms 181ms 246ms 48ms 38 4ms 1555ms 4ms 571ms 822ms 157ms 48 5ms 3668ms 6ms 1395ms 1873ms 393ms 58 6ms 7604ms 6ms 2908ms 3855ms 832ms 68 7ms 14176ms 8ms 5416ms 7214ms 1536ms 78 8ms 24234ms 8ms 9288ms 12304ms 2632ms

Member Function Documentation

◆ increment_multiply_on_right()

template<typename T>
void SimpleSparseMatrix::increment_multiply_on_right ( gsl::not_null< T * > a,
size_t a_offset,
size_t a_stride,
const T & b,
size_t b_offset,
size_t b_stride ) const

Does a += m*b where m is the sparse matrix. T is a container of doubles, like a std::vector<double>. Assumes b points into contiguous storage that allows one to access b[i] for b_offset <= i <= b_offset+max(column_indices). Assumes a points into contiguous storage that allows one to access a[i] for a_offset <= a <= a_offset+max(row_indices).

Both source and destination are allowed to have a stride.

◆ increment_multiply_on_right_all_offsets()

void SimpleSparseMatrix::increment_multiply_on_right_all_offsets ( gsl::not_null< gsl::span< double > * > a,
const gsl::span< double > & b,
size_t stride ) const

Does a += m * b for stride right-hand sides at once.

The result is the same as calling increment_multiply_on_right(a, k, stride, b, k, stride) once for every k in [0, stride).

This is faster than calling increment_multiply_on_right once per offset for two reasons:

  • The sparse matrix (its values and its index arrays) is read from memory once instead of stride times.
  • For every matrix entry, the stride source values are read as one contiguous block and added to stride independent partial sums, so the compiler can use vector instructions. The per-offset loop instead picks one value out of each block and adds it to a single running sum, so every addition has to wait for the previous one.

For each offset, the entries of a row are summed in the same order as in increment_multiply_on_right. Source and destination must not overlap. Does not allocate.

Strides up to max_fused_stride use this fused kernel. Larger strides fall back to one call of increment_multiply_on_right per offset.


The documentation for this class was generated from the following file:
  • src/DataStructures/SimpleSparseMatrix.hpp