ORNL / cpp-proposals-pub

Collaborating on papers for the ISO C++ committee - public repo
26 stars 27 forks source link

P2642R2 LEWG 2023/03/28 presentation #367

Open mhoemmen opened 1 year ago

mhoemmen commented 1 year ago

P2642 (padded mdspan layouts)

Use cases

  1. Overaligned start of each contiguous segment of elements

    • Explicit SIMD usually requires overalignment by SIMD width

    • "Pitched" allocations for coalesced memory access

    • Compilers more likely to vectorize and can skip loop prelude & postlude (less code)

  2. Any submatrix of a row-major or column-major matrix

    • Layout of every contiguous submatrix of a layout_left or layout_right matrix

    • Layout of BLAS and LAPACK

    • Preserve stride-1 access in one dimension

Alternatives

Why we allow padding_stride=0 (also at compile time)

Leftmost (for layout_left_padded; rightmost for layout_right_padded) extent is zero at compile time: extents<Integral, 0, ...>.

template<int Value>
using Int = integral_constant<int, Value>;

layout_left::mapping<extents<int, 0, 10>> mapping{};
mdspan m{nullptr, mapping};
auto m_sub = submdspan(m, tuple{Int<0>{}, Int<0>{}}, full_extent);
P2642? m_sub.mapping()
Before layout_stride::mapping<extents<int, 0, 10>>
After layout_left_padded<0>::mapping<extents<int, 0, 10>>

Errata

Implementation

PR 237 in https://github.com/kokkos/mdspan

Design suggestions from Tomasz Kamiński via reflector

layout_left::padded<ps>::mapping instead of layout_left_padded<ps>::mapping

The padding_stride template parameter must live outside the mapping. Otherwise, it wouldn't be possible to construct the mapping from just an extents object.

Layout mapping conversion customization point?

Current design: Explicit converting constructors

Current design includes converting constructors from less constrained to more constrained mappings, e.g., layout_left_padded to layout_left mapping. These constructors are conditionally explicit if there are nontrivial preconditions.

We did this because it matches existing conversions like layout_stride to layout_left.

Tomasz points out two disadvantages of the current approach.

  1. Conditionally explicit mdspan constructor is also used to convert dynamic-extent to static-extent mdspan.

  2. Adding additional layouts with the current approach would require modifying existing layout classes to add constructors. Also, users could not define conversions from custom layout mappings to Standard layout mappings.

Alternative: assume_layout customization point

Tomasz proposes an alternative design, where mdspan provides an assume_layout<Layout> customization point, that changes layout from more to less generic (e.g., strided -> padded -> left).

Advantages:

Disadvantages:

mhoemmen commented 1 year ago

Feedback on Tomasz's questions

layout_left_padded vs. layout_left::padded

LEWG points out that the "topology" of layouts and layout mappings is that mappings live one level inside layouts. This matches the definition of a layout as a function from extents type to mapping type. Nesting a layout inside of another layout would break this.

Converting constructors vs. assume_layout

We don't foresee a strong need for a general layout mapping conversion customization point, for the following reasons.