2

I'd like to understand how the std::layout_stride policy for the std::mdspan works. At this point, no compiler supports this new C++23 library type, although a reference implementation exists: https://github.com/kokkos/mdspan. However, I could not find a good explanation on this layout type either on the github wiki (A Gentle Introduction to mdspan) or the P0009r18 paper.

The following program, using std::layout_right (the default) for an mdspan prints

1 2 3
4 5 6
std::vector v {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
std::mdspan<int, 
            std::extents<size_t, 2, 3>,
            std::layout_right> mv{v.data()};
for (std::size_t r = 0; r < mv.extent(0); r++)
{
  for (std::size_t c = 0; c < mv.extent(1); c++)
  {
     std::cout << mv[r, c] << ' ';
  }
  std::cout << '\n';
}

If we change to std::layout_left, the output becomes:

1 3 5
2 4 6
std::mdspan<int, 
            std::extents<size_t, 2, 3>,
            std::layout_left> mv{v.data()};

My understanding is that std::layout_stride can control the stride. For instance, jumping every 2 elements (from the underlying sequence) for rows and 3 elements for columns. However, I did not find any example on this matter. Does anyone have examples showing how this really works?

It can be experimented on godbolt here: https://godbolt.org/z/Mxa7cej1a.

UPDATE

Based on the answer from @KamilCuc, I deduce the following:

    stdex::mdspan<int, 
                  stdex::extents<size_t, stdex::dynamic_extent, stdex::dynamic_extent>, 
                  stdex::layout_stride> 
    mv{ v.data(), 
       { stdex::dextents<size_t,2>{2, 3}, 
         std::array<std::size_t, 2>{3, 1}}};

result:

1 2 3
4 5 6

This is the equivalent of layout_right.

stride: std::array<std::size_t, 2>{1, 1}

1 2 3
2 3 4

stride: std::array<std::size_t, 2>{3, 2}

1 3 5
4 6 8

stride: std::array<std::size_t, 2>{9, 3}

 1  4  7
10 13 16
Marius Bancila
  • 16,053
  • 9
  • 49
  • 91
  • https://github.com/kokkos/mdspan/blob/cf374bc4150f2194024fce187fc27943223de15b/tests/test_layout_stride.cpp https://github.com/kokkos/mdspan/blob/a9489aecd1ff61a2464f62dc25e79ca754b4b0fd/include/experimental/__p0009_bits/layout_stride.hpp#L68 https://github.com/kokkos/mdspan/blob/973ef6415a6396e5f0a55cb4c99afd1d1d541681/compilation_tests/ctest_constexpr_layouts.cpp#L27 overall https://github.com/kokkos/mdspan/search?p=2&q=layout_stride – KamilCuk Apr 11 '23 at 09:59
  • Thanks. I've seen the first two, I missed the last one. There was one example there that I can learn from. – Marius Bancila Apr 11 '23 at 11:59
  • Please post the update as an answer instead of editing the question. – Cris Luengo Jun 09 '23 at 21:19

0 Answers0