Skip to content

Address #2194: spgemm input/output sortedness - #3270

Open
brian-kelley wants to merge 2 commits into
kokkos:developfrom
brian-kelley:Fix2194
Open

Address #2194: spgemm input/output sortedness#3270
brian-kelley wants to merge 2 commits into
kokkos:developfrom
brian-kelley:Fix2194

Conversation

@brian-kelley

Copy link
Copy Markdown
Contributor

Addresses #2194 and fixes trilinos/Trilinos#15105.

Adds input_sorted and result_sorted as options in spgemm handle, and as parameters in non-reuse spgemm interface.

  • input_sorted means user promises A,B are sorted on input
    • default false for backward compatibility
  • result_sorted means user needs output C to be sorted
    • default true for backward compatibility
  • if a TPL is known to require sorted inputs and input_sorted=false, fall back to native automatically.

This also adds test cases for reuse and non-reuse spgemm where input_sorted is explicitly false, and where result_sorted is explicitly true. The changes to create_spgemm_handle and spgemm itself are documented on the wiki pages.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Adds configurable SpGEMM input/output sortedness, selecting native fallbacks when TPLs require sorted inputs.

Changes:

  • Adds input_sorted and result_sorted options.
  • Implements backend-aware fallback and conditional output sorting.
  • Adds documentation and sortedness tests.

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
sparse/unit_test/Test_Sparse_spgemm.hpp Adds sortedness tests.
sparse/tpls/KokkosSparse_spgemm_numeric_tpl_spec_decl.hpp Makes MKL reuse sorting conditional.
sparse/tpls/KokkosSparse_spgemm_noreuse_tpl_spec_decl.hpp Extends TPL signatures.
sparse/src/KokkosSparse_spgemm.hpp Extends non-reuse API and fallback dispatch.
sparse/src/KokkosSparse_spgemm_symbolic.hpp Adds symbolic-phase fallback.
sparse/src/KokkosSparse_spgemm_numeric.hpp Adds numeric-phase fallback.
sparse/src/KokkosSparse_spgemm_handle.hpp Stores sortedness settings and backend requirements.
sparse/src/KokkosKernels_Handle.hpp Extends handle creation API.
sparse/impl/KokkosSparse_spgemm_numeric_spec.hpp Conditionally sorts native output.
sparse/impl/KokkosSparse_spgemm_noreuse_spec.hpp Propagates sortedness options.
sparse/impl/KokkosSparse_spgemm_impl.hpp Selects native algorithms during fallback.
docs/source/API/sparse/spgemm_symbolic.rst Documents symbolic sortedness behavior.
docs/source/API/sparse/spgemm_numeric.rst Updates numeric API documentation.
docs/source/API/sparse/handle_get_create_destroy.rst Documents new handle parameters.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread sparse/src/KokkosKernels_Handle.hpp
Comment thread sparse/tpls/KokkosSparse_spgemm_noreuse_tpl_spec_decl.hpp Outdated
Comment thread sparse/unit_test/Test_Sparse_spgemm.hpp Outdated
Comment thread docs/source/API/sparse/spgemm_numeric.rst Outdated
Comment thread docs/source/API/sparse/spgemm_symbolic.rst Outdated
Comment thread sparse/src/KokkosSparse_spgemm_handle.hpp Outdated
Comment thread sparse/unit_test/Test_Sparse_spgemm.hpp Outdated
brian-kelley and others added 2 commits September 1, 2026 18:22
- Add input_sorted and result_sorted as options in
handle, and in non-reuse spgemm interface
  - input_sorted means user promises A,B are sorted on input
    - default false for backward compatibility
  - result_sorted means user needs output C to be sorted
    - default true for backward compatibility
  - if a TPL is known to require sorted inputs but input_sorted=false,
    fall back to native
- Add tests for reuse and non-reuse cases where input_sorted
  is explicitly false, and where result_sorted is explicitly true.
- Document the changes to create_spgemm_handle and spgemm itself

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Signed-off-by: Brian Kelley <bmkelle@sandia.gov>
Signed-off-by: Brian Kelley <bmkelle@sandia.gov>
template <class CMatrix, class AMatrix, class BMatrix>
CMatrix spgemm(KokkosSparse::SPGEMMAlgorithm algo, const AMatrix& A, const bool Amode, const BMatrix& B,
const bool Bmode) {
const bool Bmode, bool input_sorted = false, bool result_sorted = true) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@brian-kelley just to make sure I'm following, these new defaulted argument options keep existing behavior for backward compatibility?

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.

Correct, these are the most conservative settings for each option and match what was provided by most spgemm paths before this PR. We had all paths sort the output, and all TPLs had a debug check that inputs were sorted.

// testing)
const bool useFallback =
!spgemmHandle->get_input_sorted() && Impl::algorithm_may_require_sorted_input<c_exec_t>(algo);
if (Impl::is_spgemm_algorithm_native(algo) || useFallback) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is there a way for users to know or be warned if their request for a particular algorithm was overridden by the fallback path?

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.

There isn't a warning, but the best way to know what's running is using the kokkos-tools kernel logger or timer. Then you will see a TPL region name for TPLs, or a KK kernel name for native. But this is at least consistent with how several other kernels do fallback paths (dot, gemv, spmv).

const bool useFallback =
!spgemmHandle->get_input_sorted() && Impl::algorithm_may_require_sorted_input<c_exec_t>(algo);

if (Impl::is_spgemm_algorithm_native(algo) || useFallback) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is there a way for users to know or be warned if their request for a particular algorithm was overridden by the fallback path?

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.

See above about numeric

@ndellingwood ndellingwood left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks @brian-kelley !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Kokkos-Kernels: code throws in debug mode.

3 participants