Skip to content

feat (sparse) : add support for the BSR sparse format - #1201

Merged
jalvesz merged 55 commits into
fortran-lang:masterfrom
jalvesz:sparse_bsr
Nov 15, 2026
Merged

jalvesz merged 55 commits into
fortran-lang:masterfrom
jalvesz:sparse_bsr

Conversation

@jalvesz

@jalvesz jalvesz commented Jul 24, 2026 •

Copy link
Copy Markdown
Contributor

PR to introduce the BSR (Block Sparse Row) matrix format. Similar to the CSR format but allowing dense NxM blocks per index entry in the sparse graph.

jalvesz and others added 30 commits January 16, 2026 23:49
- Add BSR_type to stdlib_sparse_kinds
- Add spmv_bsr implementation in new stdlib_sparse_spmv_bsr.fypp
- Register stdlib_sparse_spmv_bsr.fypp in CMakeLists.txt
- Add BLAS linkage to sparse target
- Update spmv interface with BSR subroutine declarations
- Update COO spmv to use do concurrent with entry_index naming
- Add BSR documentation to stdlib_sparse.md
- Add BSR tests to test_linalg_sparse.fypp

Rebased onto fortran-lang/stdlib master (0e45050)
# Conflicts:
#	src/sparse/stdlib_sparse_spmv_coo.fypp
#	src/sparse/stdlib_sparse_spmv_csc.fypp
#	src/sparse/stdlib_sparse_spmv_ell.fypp
feat(sparse): add BSR format and rebase sparse_bsr onto fortran-lang/stdlib master
* Initial plan

* Refactor stdlib_sparse_spmv_bsr: split OOP procedures into non-OOP kernels and thin wrappers

- Create spmv_bsr_1d_${s1}$_kernel and spmv_bsr_2d_${s1}$_kernel non-OOP subroutines
  containing the full computational logic for rank=1 and rank=2 BSR SpMV respectively
- Kernel signature: (data, rowptr, col, nrows, ncols, storage, block_shape, vec_x, vec_y, alpha, beta, op)
- Update spmv_bsr_1d_${s1}$ and spmv_bsr_2d_${s1}$ OOP module subroutines to thin wrappers
  that unpack matrix components and forward them to the corresponding kernel
- Eliminate all `associate` statements from OOP wrappers
- Preserve all existing numerical behavior: storage modes, ops, BLAS call semantics

* Apply remaining changes

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
@codecov

codecov Bot commented Jul 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 69.62%. Comparing base (a337513) to head (c45e5f9).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1201   +/-   ##
=======================================
  Coverage   69.62%   69.62%           
=======================================
  Files          20       20           
  Lines        2565     2565           
=======================================
  Hits         1786     1786           
  Misses        779      779           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jvdp1 jvdp1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM. Thank you @jalvesz .
The specs might need to be updated further.

Comment thread src/sparse/stdlib_sparse_kinds.fypp Resolved
Comment thread src/sparse/stdlib_sparse_kinds.fypp Resolved
Comment thread src/sparse/stdlib_sparse_kinds.fypp Resolved
Comment thread src/sparse/stdlib_sparse_kinds.fypp Resolved
Comment thread src/sparse/stdlib_sparse_spmv_bsr.fypp Outdated
#:for k1, t1, s1 in (KINDS_TYPES)

!> Non-OOP rank=1 kernel for BSR SpMV
subroutine spmv_kernel_bsr_1d_${s1}$(data, rowptr, col, nrows, ncols, storage, block_shape, &

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
subroutine spmv_kernel_bsr_1d_${s1}$(data, rowptr, col, nrows, ncols, storage, block_shape, &
subroutine spmv_kernel_bsr_1d_${s1}$(nrows, ncols, data, rowptr, col, storage, block_shape, &

Based on #1184 , to be discussed. The goal is to be somewhat aligned on the deprecated MKL sparse subroutines.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually we had discussed about extracting the dimensions from the array descriptors as we are using assumed-shape inputs.

jalvesz and others added 14 commits September 1, 2026 03:35
Co-authored-by: Jeremie Vandenplas <jeremie.vandenplas@gmail.com>
Co-authored-by: Jeremie Vandenplas <jeremie.vandenplas@gmail.com>
Co-authored-by: Jeremie Vandenplas <jeremie.vandenplas@gmail.com>
Co-authored-by: Jeremie Vandenplas <jeremie.vandenplas@gmail.com>
Replace the per-block gemv/gemm calls by plain-loop kernels generated for
square blocks of size 2 to 8, with a generic fallback for other shapes.
Per-block BLAS calls were 1.3-11x slower than fixed-size loops, which now
run at memory bandwidth. The BLAS dependency of the sparse library is gone.

- Add the public low-level spmv_kernel_bsr with the CSR argument order.
- Rank-2 vectors follow the COO/CSR convention vec(k,n): the same matrix
  is applied to k vectors in one pass (also fixes wrong results for k>1).
- Symmetric storage: one triangle of square blocks, full diagonal blocks;
  lower and upper share the same kernel.
- beta = 0 does not read vec_y; op 'H' on real types behaves as 'T'.
- Declare the wrapper vectors contiguous to avoid copy-in/copy-out.
- Fix at() mirroring for symmetric storage.
- Tests: fixed hand-checkable examples plus a sweep over all kernels.
- Document the BSR format and spmv_kernel_bsr.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
gfortran (seen with 11, 13 and 15) at -O1 and above reallocates the
left-hand side of `yref = 2*matmul(opA, x) - y` with its previous size
when op(A) changes shape between ops, writing past the end of yref and
aborting later with "double free or corruption" (fpm CI, exit 134).

Use explicit-shape arrays in a block for each op instead of
reallocation on assignment.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jalvesz
jalvesz marked this pull request as ready for review November 2, 2026 08:45
@jalvesz

jalvesz commented Nov 15, 2026

Copy link
Copy Markdown
Contributor Author

I'll go ahead and merge this to have it available for broader testing

@jalvesz
jalvesz merged commit 30e6cb6 into fortran-lang:master Nov 15, 2026
80 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants