Canonical way of handling read-only data in interfaces taking mdspans
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 507
- Forks
- 87
- Avg merge
- 15h 27m
- Merged PRs (30d)
- 2
Description
I have not found any information on this topic. The dot_product example just doesn't take care of const-correctness at all. With span one can just have span<T const> and an implicit cast happens when passing a span<T>. With mdspan things are less straightforward when one wants to stay generic with respect to the AccessorPolicy, i.e.
template <typename T, typename IndexT, std::size_t x_extent,
std::size_t y_extent, class L, template <typename> class A>
void bar(std::mdspan<T const, std::extents<IndexT, x_extent, y_extent>, L, A<T const>> input);
void foo() {
auto mat = std::mdspan<float, std::dextents<int, 2>>{};
bar(mat);
}
doesn't compile as the compiler can't both deduce L and A and cast from an element type of float to an element type of float const. The best solution I could come up with is some kind of explicit cast like
template <typename T, class Extents, class Layout,
template <typename> class Accessor>
auto read_only_cast(std::mdspan<T, Extents, Layout, Accessor<T>> other) {
using CT = std::add_const_t<T>;
return std::mdspan<CT, Extents, Layout, Accessor<CT>>{other};
}
which then allows
void foo() {
auto mat = std::mdspan<float, std::dextents<int, 2>>{};
bar(read_only_cast(mat));
}
This naive implementation is probably flawed in its assumption that every Accessor has a single template argument (can be fixed by adding a variadic overload of read_only_cast) and generally I would not expect that the implementation of this kind of facility is left to the user. Is there a simpler way that avoids the need to explicitly specify any template parameters to bar()? I think https://github.com/kokkos/mdspan/blob/a9c54ccd8254cc3d159fdf2adf650dca4e048c97/examples/dot_product/dot_product.cpp#L48-L56 should be updated to reflect any canonical way of specifying that a function takes read-only, input mdspans.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with examples/dot_product/dot_product.cpp at the referenced lines and review the mdspan interface shown in the issue. Determine the canonical generic read-only pattern for accessor policies and update the example to demonstrate it. Done means the approach is agreed upon and the example reflects it without explicit template arguments at the call site.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- backend-api-design
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100