ORNL / ORNL/cpp-proposals-pub

LEWG Kona 11/2023 Padded Layouts Review

Open
#433 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
HTML
Stars
29
Forks
26
PR merge metrics
No merged PRs in 30d

Description

Padded Layouts

  • Proposes two new layouts: layout_left_padded and layout_right_padded
  • Similar to layout_left and layout_right but store one stride, allowing padding of the leftmost or rightmost dimension

Compare memory storage of layout_left and layout_left_padded:

int data[] = {0, 1, 2, 3, 4, 5};
mdspan<int, dextents<int,2>, layout_left>
  m(data,extents(3,2));

for(int col=0; col<m.extents(0); col++) {
  for(int row =0; row<m.extents(1); row++) 
    std::cout << m[col,row] << " ";
  std::cout << std::endl;
}
// prints:
//  0 3
//. 1 4
//  2 5

// Want colums to be aligned to cache lines - i.e. pad with dummy values 
int data[] = {0, 1, 2, 999, 3, 4, 5, 999};
mdspan<int, dextents<int,2>, layout_left_padded<4>>
  m(data,extents(3,2));

for(int col=0; col<m.extents(0); col++) {
  for(int row =0; row<m.extents(1); row++) 
    std::cout << m[col,row] << " ";
  std::cout << std::endl;
}
// prints:
//  0 3
//. 1 4
//  2 5
  • Layouts take padding parameter: layout_left_padded<PaddingValue>
  • The padding stride (i.e. stride(1) for layout_left_padded or stride(extents_type::rank()-1) for layout_right_padded) is the next larger multiple of PaddingValue for the corresponding extent.
using map_t = layout_left_padded<4>::mapping<dextents<int, 2>>;
map_t m(3,2); // => m.stride(1) == 4
map_t m(4,2); // => m.stride(1) == 4
map_t m(5,2); // => m.stride(1) == 8
  • PaddingValue can be static or dynamic (std::dynamic_extent)
    • if it is dynamic, provide the stride as an argument to the constructor
Use cases:
  • alignment of rows/columns/subtensors to cache lines/page boundaries etc.
  • more efficient support for very common subsets of submdspan for layout_left, layout_right
    • i.e. don't have to fall back to layout_stride but preserve the stride-1 compile time knowledge for a lot of common cases
    • Extremely common in linear algebra: these layouts were originally part of the linalg proposal
  • Note: these layouts are what was actually the default layouts for the mdspan predecessor Kokkos::View
Impact on existing elements in C++23/26 draft
  • Also provides converting constructors in layout_left and layout_right
    • for rank > 1 has precondition checks that stride(0) == extents().extent(0) etc.
    • for rank < 2 can convert layout_left_padded to layout_right etc.
  • Modifies C++26 submdspan
mdspan<int, dextents<int, 3>, layout_left> m(ptr, 6, 3, 3);
auto m_sub = submdspan(m, pair{1,3}, pair{2,3}, 0);
// Previous: decltype(m_sub) => mdspan<int, dextents<int, 2>, layout_stride>
// Now: decltype(m_sub) => mdspan<int, dextents<int, 2>, layout_left_padded<dynamic_extent>>

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the proposed layout_left_padded and layout_right_padded behavior in the issue, including padding, dynamic stride construction, conversions, and submdspan results. No repository files, tests, or entry points are named; done would require resolving the design review and documenting an agreed proposal.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.