-
Notifications
You must be signed in to change notification settings - Fork 25
Open
Description
Padded Layouts
- Proposes two new layouts:
layout_left_paddedandlayout_right_padded - Similar to
layout_leftandlayout_rightbut 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)forlayout_left_paddedorstride(extents_type::rank()-1)forlayout_right_padded) is the next larger multiple ofPaddingValuefor 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) == 8PaddingValuecan 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
submdspanforlayout_left,layout_right- i.e. don't have to fall back to
layout_stridebut 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
linalgproposal
- i.e. don't have to fall back to
- Note: these layouts are what was actually the default layouts for the
mdspanpredecessorKokkos::View
Impact on existing elements in C++23/26 draft
- Also provides converting constructors in
layout_leftandlayout_right- for rank > 1 has precondition checks that
stride(0) == extents().extent(0)etc. - for rank < 2 can convert
layout_left_paddedtolayout_rightetc.
- for rank > 1 has precondition checks that
- 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>>Metadata
Metadata
Assignees
Labels
No labels