KONA Notes: layout_padded
Open
Nobody has claimed this yet.
- Dominant language
- HTML
- Stars
- 29
- Forks
- 26
- PR merge metrics
- No merged PRs in 30d
Description
Padded Layouts
Motivation
- padding multi dimensional arrays helps with
- enable use of instructions which require over-alignment
- cache line access optimization
- for
mdspan<double, dextents<int, 2>> A(ptr, N, 7);the code below will exhibit false sharing on cachelines of 64 byte
- for
magic_index_range range(N);
for_each(execution::par, range.begin(), range.end(), [=](int i) {
for(int j=0; j<M; j++) A[i, j] = i*100 + j;
});
- often
submdspanoflayout_leftorlayout_rightonly needs a single stride in addition to extents- used by BLAS (leading dimension)
- important in image processing libraries (pitch of a row)
- also: submdspan
layout_left_paddedandlayout_right_paddedrank-2mdspancan always preserve layout type
Proposal
- add two new layouts:
layout_left_paddedandlayout_padded_right - we "pad" the stride-1 dimension
- that means the padding stride is
stride(1)forlayout_left_paddedandstride(rank()-2)forlayout_right_padded
- that means the padding stride is
- the padding is specified via a template argument:
template<size_t Padding>
struct layout_padded_left;
Padding == dynamic_extentindicates using a runtime padding- the padding stride is the smallest multiple of
Paddinglarger then the corresponding extent- this supports the "cacheline" alignment by providing
Paddingcomputed from cacheline size and scalar size - this supports
submdspanusecase by simply passing the actual stride of a given dimension
- this supports the "cacheline" alignment by providing
layout_left_padded<4>::mapping map(extents<int, 7,5>());
// map.stride(0) == 1
// map.stride(1) == 8
layout_right_padded<4>::mapping map(extents<int, 7,5>());
// map.stride(0) == 8
// map.stride(1) == 1
layout_right_padded<dynamic_extent>::mapping
map(extents<int, 7,5>(), 4);
// map.stride(0) == 8
// map.stride(1) == 1
layout_right_padded<dynamic_extent>::mapping
map(extents<int, 7,5>(), 13);
// map.stride(0) == 13
// map.stride(1) == 1
Why not use layout_stride for these usecases
- the padded layouts have a number of advantages:
- one stride is still statically known to be 1: critical for good vectorization
- only needs to store a single stride - if that stride is not compile time known
layout_stridealways has runtime stored strides for each dimension
Integration with submdspan
- we want for
submdspanoflayout_leftandlayout_rightto return padded layouts instead oflayout_stridewhere possible - this would be a breaking change for submdspan if introduced later
mdspan<int, dextents<int, 3>, layout_left> A(ptr, N, M, K);
auto sub_1 = submdspan(A, pair(x,y), full_extent, 5);
// sub_1 without P2642: layout_stride, proposed: layout_left_padded
auto sub_2= submdspan(A, pair(x,y), pair(z,p), 5);
// sub_2 layout_stride (unchanged)
auto sub_3 = submdspan(A, 5, full_extent, full_extent);
// sub_3 layout_stride (unchanged)
auto sub_4 = submdspan(A, full_extent, pair(x,y), 5);
// sub_4 layout_left (unchanged)
Why not a layout_stride with compile time strides
- this question gets asked a bit, but it doesn't work
- consider this
submdspancase:
mdspan<int, extents<int, dynamic_extents,3,4,5>, layout_left> A(ptr, N);
auto sub_A = submdspan(A, pair(x,y), full_extent, full_extent, full_extent);
- with
layout_stridewe would need to store 3-runtime strides:stride(0) == 1- staticstride(1) == N- dynamicstride(2) == N*3- dynamicstride(3) == N*3*4- dynamic
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the proposed layout_left_padded and layout_right_padded mappings, including their static and dynamic padding behavior and the submdspan integration examples. No files, tests, or entry points are identified in the issue. Done would require an agreed design covering the layouts, mapping semantics, and compatibility impact.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- backend-api-design
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100