kokkos / kokkos/mdspan

How to "flip" mdspan using submdspan (or any other utility)?

Open
#352 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
507
Forks
87
Avg merge
15h 27m
Merged PRs (30d)
2

Description

I've been experimenting with mdspan for a few days, and one thing remains unclear to me: Is it possible to use submdspan to create an mdspan that "views" the original data in reverse order?

For example, consider a span with 9 rows and 10 columns:

constexpr int num_rows = 9;
constexpr int num_columns = 10;
auto data = std::views::iota(0, num_columns * num_rows) | std::ranges::to<std::vector>();
const exstd::mdspan<const int, exstd::extents<int, num_rows, num_columns>, exstd::layout_right> span{data.data()};

This represents the following table:

      0,   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,  31,  32,  33,  34,  35,  36,  37,  38,  39
     40,  41,  42,  43,  44,  45,  46,  47,  48,  49
     50,  51,  52,  53,  54,  55,  56,  57,  58,  59
     60,  61,  62,  63,  64,  65,  66,  67,  68,  69
     70,  71,  72,  73,  74,  75,  76,  77,  78,  79
     80,  81,  82,  83,  84,  85,  86,  87,  88,  89

Is there a way to call the submdspan function to get the same data but with the rows flipped?

     80,  81,  82,  83,  84,  85,  86,  87,  88,  89
     70,  71,  72,  73,  74,  75,  76,  77,  78,  79
     60,  61,  62,  63,  64,  65,  66,  67,  68,  69
     50,  51,  52,  53,  54,  55,  56,  57,  58,  59
     40,  41,  42,  43,  44,  45,  46,  47,  48,  49
     30,  31,  32,  33,  34,  35,  36,  37,  38,  39
     20,  21,  22,  23,  24,  25,  26,  27,  28,  29
     10,  11,  12,  13,  14,  15,  16,  17,  18,  19
      0,   1,   2,   3,   4,   5,   6,   7,   8,   9

I tried to achieve this by passing a negative stride into strided_slice, but it returned incorrect results (with negative extents):

exstd::submdspan(span, exstd::strided_slice{.offset = 5, .extent = 5, .stride = -1}, exstd::full_extent)

At this point, I think I do not understand how this is supposed to be done. As far as I understand, the original intention of this proposal was to have a tool similar to indexing in NumPy, MATLAB, or Fortran, which can handle this task easily.

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

No source files or tests are named. Start by tracing the submdspan and strided_slice APIs used in the examples, then determine how reversed rows should be represented and add coverage for the intended behavior or document the supported limitation.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.