I'm trying out the Kokkos reference mdspan implementation to see if it could be used to simplify some bits of code in our codebase. One thing that I would have intuitively assumed to be possible is to pick a row of a two dimensional std::mdspan
and assign it to a std::span
, e.g. somewhat like
float* data = ...
std::mdspan matrix (data, 2, 5);
std::span vector = matrix[0]; // <-- should be a std::span<float, std::dynamic_extent> viewing row 0 of matrix
After some research I didn't find an obvious straightforward way to achieve that, neither with member functions of mdspan nor with free functions from the std library. The only possibility I see at the moment I going back to the raw pointer level and write my own free functions to do that, which is not really as elegant as I expected. Am I overlooking something or is that really no feature of mdspan?
You can slice the original 2d-mdspan
into a 1-d mdspan
through C++26 std::submdspan()
.
Since the underlying sequence of result mdspan
is contiguous, it can be used to construct the std::span
float* data = ...;
std::mdspan matrix(data, 2, 5);
std::mdspan row0 = std::submdspan(matrix, 0, std::full_extent);
std::span vector0(row0.data_handle(), row0.size()); // row 0 of matrix
std::mdspan row1 = std::submdspan(matrix, 1, std::full_extent);
std::span vector1(row1.data_handle(), row1.size()); // row 1 of matrix