stan-dev / math

The Stan Math Library is a C++ template library for automatic differentiation of any order using forward, reverse, and mixed modes. It includes a range of built-in functions for probabilistic modeling, linear algebra, and equation solving.
https://mc-stan.org
BSD 3-Clause "New" or "Revised" License
723 stars 183 forks source link

more efficient sparse matrix multiplies #3045

Open bob-carpenter opened 2 months ago

bob-carpenter commented 2 months ago

Description

Replace the uses of Eigen::SparseMatrix (e.g., in CSR multiply) with more efficient custom code. As an example, @blackwer wrote faster code than Eigen for a project we did here on kmers for biome models (https://github.com/bob-carpenter/kmers) and I'm guessing it should be easy to use that code.

Current Version:

v4.8.1

blackwer commented 2 months ago

wrote faster code than Eigen for a project we did here on kmers for biome models

This isn't exactly verified. The matrix multiplies were done on the python side of that code using MKL's sparse multiply routines via the package sparse_dot_mkl. I haven't actually written any custom multiply routines. Most of the innovation in that project is on building the matrices quickly.

That said, I haven't heard great things about working with sparse matrices in Eigen. Does Eigen support external backends for sparse matrices like it does for classic BLAS/LAPACK stacks? It would be good to get a view of the current landscape regarding the speed and interop of various libraries for sparse matrix operations, basically.