Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

named access to Vec #2201

Merged

Conversation

psychocoderHPC
Copy link
Member

@psychocoderHPC psychocoderHPC commented Dec 1, 2023

fix #2185

Provide access to the vector components via named methods. x(), y(), z() and w().
The available names depend on the dimension of the vector.

bernhardmgruber
bernhardmgruber previously approved these changes Dec 1, 2023
Copy link
Member

@bernhardmgruber bernhardmgruber left a comment

Choose a reason for hiding this comment

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

LGTM, here are a few suggestions if you like:

Comment on lines 177 to 179
template<typename TDefer = Dim>
ALPAKA_FN_HOST_ACC constexpr auto x() const
-> std::enable_if_t<std::is_same_v<TDefer, Dim> && Dim::value >= 1, TVal const&>
Copy link
Member

Choose a reason for hiding this comment

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

I am personally a fan of putting enable_if into the template parameter list for better readability. This may also remove the need to delay instantiation as you did with TDefer. I have not tested it though.

Suggested change
template<typename TDefer = Dim>
ALPAKA_FN_HOST_ACC constexpr auto x() const
-> std::enable_if_t<std::is_same_v<TDefer, Dim> && Dim::value >= 1, TVal const&>
template<std::enable_if_t<std::is_same_v<TDefer, Dim> && Dim::value >= 1, int> = 0>
ALPAKA_FN_HOST_ACC constexpr auto x() const -> TVal const&

Copy link
Member Author

Choose a reason for hiding this comment

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

I took your suggestion because this allow the usage of decltype(auto)

Copy link
Member

Choose a reason for hiding this comment

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

Why would you want to use decltype(auto) over just TVal& and TVal const&?

Copy link
Member Author

Choose a reason for hiding this comment

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

Avoids copy past errors^^. IMO defining RefType and ConstRefType or similar would be better than writing everywhere TVal&.

Copy link
Member

Choose a reason for hiding this comment

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

Avoids copy past errors^^. IMO defining RefType and ConstRefType or similar would be better than writing everywhere TVal&.

That's even worse in my opinion. I value readability and auto x() -> TVal& is more readable than decltype(auto) x() or auto x() -> RefType. The former requires me to look at the function body, the latter requires me to lookup what RefType is.

But in any case, I was just curious. I leave this up to you!

Copy link
Contributor

Choose a reason for hiding this comment

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

static_assert() works without the need for TDefer, and lets us provide a more readable error message:

        ALPAKA_FN_HOST_ACC constexpr decltype(auto) x() const
        {
            static_assert(Dim::value >= 1, ".x() is defined only for Vec types with a dimension of 1 or more.");
            return m_data[Dim::value - 1];
        }

Copy link
Member Author

Choose a reason for hiding this comment

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

@fwyzard Your suggestion will prevent you can check from the user code side if Vec is allowing the call to the method z(). I know that the defer in the template signature is ugly but the function is cleanly disabled if not supported.

Copy link
Contributor

Choose a reason for hiding this comment

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

What do you mean, to check from user code ?

Copy link
Member

Choose a reason for hiding this comment

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

The version from @psychocoderHPC is SFINAE-friendly, whereas a static_assert is not:

if constexpr (requires { v.y() }) ...

Works with the version here for a 1D vector v, but fails with a hard error using a static_assert.

test/unit/vec/src/VecTest.cpp Outdated Show resolved Hide resolved
fix alpaka-group#2185

Provide access to the vector components via named methods. `x()`, `y()`,
`z()` and `w()`.
The avalble names depends on the dimension of the vector.
@bernhardmgruber bernhardmgruber merged commit f82033f into alpaka-group:develop Dec 2, 2023
23 checks passed
@psychocoderHPC psychocoderHPC deleted the topic-namedVectorAccess branch December 4, 2023 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Proposal: add .x(), .y(), ... accessors to alpaka::Vec
3 participants