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

<mdspan>: layout_left improvements #3603

Merged

Conversation

JMazurkiewicz
Copy link
Contributor

@JMazurkiewicz JMazurkiewicz commented Mar 30, 2023

<mdspan>:

  • Implement layout_left::mapping's preconditions,
  • Fix is_(unique/exhaustive/strided) functions - they should be static,
  • Add exposition-only public member _Fwd_prod_of_extents to extents,
  • Fix some issues with signed-unsigned conversions,
  • Fix mdspan's constructor constraints,
  • Address <mdspan>: Various cleanups (mostly renames) #3593 (comment).

Testing:

  • Introduce test_mdspan_support.hpp header,
  • Add separate tests for layout_left: regular tests and death tests.

@JMazurkiewicz JMazurkiewicz requested a review from a team as a code owner March 30, 2023 13:25
@StephanTLavavej StephanTLavavej added the mdspan C++23 mdspan label Mar 30, 2023
_NODISCARD constexpr reference operator()(const _OtherIndexTypes... _Indices) const {
return _Acc.access(_Ptr, _Map(static_cast<index_type>(_STD move(_Indices))...));
_NODISCARD constexpr reference operator()(_OtherIndexTypes... _Indices) const {
return _Acc.access(_Ptr, static_cast<size_t>(_Map(static_cast<index_type>(_STD move(_Indices))...)));
Copy link
Contributor

Choose a reason for hiding this comment

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

This added cast isn't depicted in [mdspan.mdspan.members]/4. That seems deliberate, given the other places that do explicitly show similar casts. This is silencing a warning that the user has opted into about signed-to-unsigned conversions, so I'm not sure it's appropriate. Is there an existing policy about this situation?

There also seems to be a tension in the design that index_type can be signed but the second parameter to access/offset is size_t. Maybe they should take a ptrdiff_t or the layout mapping should return a size_type. LWG issue needed?

Copy link
Member

Choose a reason for hiding this comment

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

Our usual policy is that we avoid emitting warnings that are produced entirely within our code, but we will emit warnings if the user has asked us to perform an operation on their behalf (i.e. involving types they've specified) that would warn if they wrote it themselves.

Copy link
Contributor Author

@JMazurkiewicz JMazurkiewicz Mar 30, 2023

Choose a reason for hiding this comment

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

There also seems to be a tension in the design that index_type can be signed but the second parameter to access/offset is size_t. Maybe they should take a ptrdiff_t or the layout mapping should return a size_type.

The result of _Map(indices...) must be non-negative and less or equal to limits<size_t>::max ([mdspan.layout.reqmts]/8), so changing parameters/return types looks like overkill to me. IMHO static_casting to size_t is enough and maybe standard should explicitly say it.

LWG issue needed?

I'm not sure if it is necessary. Maybe we could submit PR to https://github.com/cplusplus/draft with explicit static_cast (since this would not change behaviour of the code).

stl/inc/mdspan Show resolved Hide resolved
stl/inc/mdspan Outdated Show resolved Hide resolved

template <class _OtherExtents>
requires is_constructible_v<extents_type, _OtherExtents>
constexpr explicit(!is_convertible_v<_OtherExtents, extents_type>)
mapping(const mapping<_OtherExtents>& _Other) noexcept
: _Exts(_Other.extents()) {}
: _Exts(_Other.extents()) {
_STL_VERIFY(_STD in_range<index_type>(_Other.required_span_size()),
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we usually check for precondition violations, at least outside of debug mode. Maybe _STL_ASSERT would be better?

Copy link
Member

Choose a reason for hiding this comment

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

Correct. As mentioned in a previous review, we should find a pattern for precondition checks and follow it consistently. It seemed that the pattern here was IDL=2 for iterator stuff and CDL for checks outside the iterators. (_STL_ASSERT is directly controlled by _DEBUG. Yeah, we've developed inconsistency here.)

&& ...);
}
(make_index_sequence<extents_type::rank()>{});
_STL_VERIFY(_Verify, "For all r in the range [0, extents_type::rank()), other.stride(r) must be equal to "
Copy link
Contributor

Choose a reason for hiding this comment

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

Even if this were changed to _STL_ASSERT, the computation above it would still be done in release mode, unless the optimizer notices that it's a dead store. As with the other instances of _STL_VERIFY, I'm not sure that's a worthwhile tradeoff.

Copy link
Member

Choose a reason for hiding this comment

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

Agreed, this is a case where the two parts should be wrapped in an #if.

tests/std/include/test_mdspan_support.hpp Outdated Show resolved Hide resolved
tests/std/include/test_mdspan_support.hpp Show resolved Hide resolved
tests/std/include/test_mdspan_support.hpp Outdated Show resolved Hide resolved
stl/inc/mdspan Outdated Show resolved Hide resolved
tests/std/tests/P0009R18_mdspan_layout_left_death/test.cpp Outdated Show resolved Hide resolved
tests/std/tests/P0009R18_mdspan_layout_left/test.cpp Outdated Show resolved Hide resolved
@StephanTLavavej
Copy link
Member

Happy to merge this now and followup later, or you can iterate on this PR, your pick.

@JMazurkiewicz
Copy link
Contributor Author

Happy to merge this now and followup later, or you can iterate on this PR, your pick.

I'd like to iterate on this PR, since I've already made some changes.

@StephanTLavavej
Copy link
Member

LGTM. Want to merge this now?

@JMazurkiewicz
Copy link
Contributor Author

Yes, please

@StephanTLavavej StephanTLavavej merged commit 53d73ee into microsoft:feature/mdspan2 Mar 31, 2023
@JMazurkiewicz JMazurkiewicz deleted the mdspan/layout_left branch March 31, 2023 22:47
@StephanTLavavej
Copy link
Member

Thanks! 😻 🚀 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mdspan C++23 mdspan
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants