Closed Sebanisu closed 3 years ago
Such a thing is possible. However, there are a few issues.
mdspan
permits nonunique layouts. What does a "range" mean for those? Indices have no such ambiguity.cartesian_product
has a fixed order.Solving these problems would amount to writing a high-level domain-specific language for multidimensional array loops. Many of us coauthors are at least somewhat familiar with that extensive field of research. I personally would consider that out of scope for P0009. It could be an interesting set of follow-on papers, though.
Thanks for the reply. Those are definitely things I didn't think of.
I guess even if we don't have something built in the user can throw a function together. I would probably make something like this when cpp23 comes around.
[[nodiscard]] constexpr auto extent_range(auto &&stdex::mdspan m, size_t i) {
return std::views::iota(i, m.extent(i));
}
[[nodiscard]] constexpr auto cartesian_product(auto &stdex::&mdspan m, std::unsigned_integral auto &&...i) {
// assert to check if all possible extents are used?
return std::views::cartesian_product(
extent_range(std::forward<decltype(m)>(m), static_cast<size_t>(i))...);
}
@Sebanisu you might like to look at existing solutions, such as Kokkos' MDRangePolicy
. These address many of the concerns I pointed out above.
I would probably make something like this when cpp23 comes around.
FYI, we're hopeful that mdspan will make C++23, but it hasn't been voted in yet.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2374r1.html
I was thinking in cpp23 you could use
views::cartesian_product
to makemdspan
into arange
. You could convert the extents to aviews::iota
(0...extent(0)
and pass those toviews::cartesian_product
. The result would be atuple
of coordinates. That we could use to read the value. Maybe we could have a function convert thattuple
to areference
to the value. Could maybe wrap theviews::cartesian_product
withviews::transform
to get thereferences
if you want that.I'm not 100% sure on the syntax. I'm not a pro programmer.