Skip to content

[Draft] showcase potential simplification of matrix op using concepts#1823

Draft
tdavidcl wants to merge 1 commit into
Shamrock-code:mainfrom
tdavidcl:concept_matrix_op
Draft

[Draft] showcase potential simplification of matrix op using concepts#1823
tdavidcl wants to merge 1 commit into
Shamrock-code:mainfrom
tdavidcl:concept_matrix_op

Conversation

@tdavidcl
Copy link
Copy Markdown
Member

No description provided.

@tdavidcl tdavidcl changed the title [Draft] showcase potential symplication of matrix op using concepts [Draft] showcase potential simplification of matrix op using concepts May 19, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Thanks @tdavidcl for opening this PR!

You can do multiple things directly here:
1 - Comment pre-commit.ci run to run pre-commit checks.
2 - Comment pre-commit.ci autofix to apply fixes.
3 - Add label autofix.ci to fix authorship & pre-commit for every commit made.
4 - Add label light-ci to only trigger a reduced & faster version of the CI (need the full one before merge).
5 - Add label trigger-ci to create an empty commit to trigger the CI.

Once the workflow completes a message will appear displaying informations related to the run.

Also the PR gets automatically reviewed by gemini, you can:
1 - Comment /gemini review to trigger a review
2 - Comment /gemini summary for a summary
3 - Tag it using @gemini-code-assist either in the PR or in review comments on files

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors matrix operations to utilize C++20 concepts and simplifies template argument deduction across the library. Feedback suggests moving the newly introduced traits and concepts into the shammath namespace to avoid global scope pollution. Additionally, mdspan parameters should be passed by value, and constraints should be adjusted to allow for implicit type conversions in mat_set_vals and to restore type safety in mat_sub. An aliasing check for source and destination matrices is also recommended.

Comment on lines +28 to +47
template<class T>
struct is_mdspan : std::false_type {};

template<class T, class Extents, class Layout, class Accessor>
struct is_mdspan<std::mdspan<T, Extents, Layout, Accessor>> : std::true_type {};

template<class T>
inline constexpr bool is_mdspan_v = is_mdspan<std::remove_cvref_t<T>>::value;

template<class T>
concept Mdspan = is_mdspan_v<T>;

template<class T, std::size_t Rank>
concept MdspanRank = Mdspan<T> && (std::remove_cvref_t<T>::rank() == Rank);

template<class T>
using mdspan_value_t = typename std::remove_cvref_t<T>::value_type;

template<class A, class B>
concept mdspan_same_value = std::same_as<mdspan_value_t<A>, mdspan_value_t<B>>;
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.

medium

The traits and concepts introduced here are defined in the global namespace. They should be moved inside the shammath namespace to prevent name collisions and maintain a clean global scope, consistent with the rest of the library.

Comment on lines +65 to +68
inline void mat_set_vals(MdspanRank<2> auto &input, Func &&func)
requires requires(Func f, int a, int b) {
{ f(a, b) } -> std::same_as<mdspan_value_t<decltype(input)>>;
}
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.

medium

mdspan is a lightweight view and should be passed by value. Additionally, the requires clause uses std::same_as, which is overly restrictive as it prevents using functors that return types implicitly convertible to the matrix's value type (e.g., returning an int for a double matrix). Using std::convertible_to is more idiomatic and flexible.

Suggested change
inline void mat_set_vals(MdspanRank<2> auto &input, Func &&func)
requires requires(Func f, int a, int b) {
{ f(a, b) } -> std::same_as<mdspan_value_t<decltype(input)>>;
}
inline void mat_set_vals(MdspanRank<2> auto input, Func &&func)
requires requires(Func f, int a, int b) {
{ f(a, b) } -> std::convertible_to<mdspan_value_t<decltype(input)>>;
}

Comment thread src/shammath/include/shammath/matrix_op.hpp
@github-actions
Copy link
Copy Markdown
Contributor

Workflow report

workflow report corresponding to commit d010790
Commiter email is timothee.davidcleris@proton.me

Light CI is enabled. This will only run the basic tests and not the full tests.
Merging a PR require the job "on PR / all" to pass which is disabled in this case.

Pre-commit check report

Pre-commit check: ✅

trim trailing whitespace.................................................Passed
fix end of files.........................................................Passed
check for merge conflicts................................................Passed
check that executables have shebangs.....................................Passed
check that scripts with shebangs are executable..........................Passed
check for added large files..............................................Passed
check for case conflicts.................................................Passed
check for broken symlinks................................................Passed
check yaml...............................................................Passed
detect private key.......................................................Passed
No-tabs checker..........................................................Passed
Tabs remover.............................................................Passed
cmake-format.............................................................Passed
Validate GitHub Workflows................................................Passed
clang-format.............................................................Passed
ruff check...............................................................Passed
ruff format..............................................................Passed
Check doxygen headers....................................................Passed
Check license headers....................................................Passed
Check #pragma once.......................................................Passed
Check SYCL #include......................................................Passed
No ssh in git submodules remote..........................................Passed
No UTF-8 in files (except for authors)...................................Passed

Test pipeline can run.

Doxygen diff with main

Removed warnings : 0
New warnings : 5
Warnings count : 8712 → 8717 (0.1%)

Detailed changes :
+ src/shammath/include/shammath/matrix_op.hpp:106: warning: The following parameter of shammath::mat_set_identity(MdspanRank< 2 > auto input) is not documented:
+ src/shammath/include/shammath/matrix_op.hpp:106: warning: argument 'input1' of command @param is not found in the argument list of shammath::mat_set_identity(MdspanRank< 2 > auto input)
+ src/shammath/include/shammath/matrix_op.hpp:35: warning: Member is_mdspan_v (variable) of file matrix_op.hpp is not documented.
+ src/shammath/include/shammath/matrix_op.hpp:44: warning: Member mdspan_value_t (typedef) of file matrix_op.hpp is not documented.

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.

1 participant