-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Q: howto create std::mdspan<T> from std::span<std::span<T>>? #221
Comments
@RalphSteinhagen thanks so much for your interest in mdspan!
I would not recommend mdspan for this case. It could work, but it would violate the spirit of the design by forcing you to couple the layout and the accessor. mdspan's designAn mdspan is just three things: a data handle, a layout mapping, and an accessor. The layout mapping maps (i, j) to a 1-D integer offset k, and the accessor maps the mdspan's data handle (e.g., pointer) and the offset k to a reference. Note that the accessor does not see (i, j). It only sees the offset k, a 1-D offset. This strongly suggests a design that decouples the layout mapping and the accessor as much as possible. It's not in the spirit of the design to use a "side channel" to pass information from the layout mapping to the accessor on every array access. Examples of "side channels" include some encoding of information into the offset (e.g., Custom layout and accessorIt's possible to write an custom layout and accessor pair that could make this work, but this would not follow the spirit of the design, because it would require a side channel between the layout mapping and the accessor. The accessor policy requirements tell us what type aliases and (member) functions a custom accessor must have. The That part is fine. The issue is with the Segmented spanOne could attempt to work around this by making the data handle itself decode the offset. This would amount to a "segmented span." For example, if the outer span However, this approach just moves the side channel from the accessor into the data handle. mdspan gets What should you do?It sounds like what you want to do is imbue the span-of-spans with mdspan's interface. If that's what you want, then you could just write a wrapper that provides, e.g., |
@mhoemmen thanks for the detailed, very educative, and well-thought-out answer! This is much appreciated!
That was exactly my intent. We have some applications where the error propagation boils down to matrix-vector multiplications, but this isn't it. The solution you pointed out is much easier, simpler, and cleaner for the intended use case (i.e. expose aggregate spans of values and corresponding measurement uncertainty) ... I like it! Thanks a lot for your time and recommendation! Again, this is much appreciated! 👍 |
... and @mhoemmen I owe you a ☕ or 🍺 in case we ever meet in person! |
@RalphSteinhagen Glad to help out! : - )
struct ComputationResult {
std::span<double> values;
std::span<double> errors;
}; or if you want to insist that class ComputationResult {
public:
ComputationResult() = default;
ComputationResult(std::span<double> the_values, std::span<double> the_errors) :
values_(the_values.data()), errors_(the_errors.data()), size_(the_values.size())
{
assert(values.size() == errors.size()); // constructor precondition
}
std::span<const double> values() const {
return {values_, size_};
}
std::span<const double> errors() const {
return {errors_, size_};
}
private:
double* values_ = nullptr;
double* errors_ = nullptr;
size_t size_ = 0;
}; |
@mhoemmen yes, that's we actually decided for as a follow-up of your earlier comment. Sometimes one (/I) just needs a gentle nudge towards ... "you can do better". Thanks for that! |
kokkos: add in-place overwriting `triangular_matrix_{left,right}_product`
Hi, first thanks bringing multi-dimensional spans into the C++ standards and for providing this implementation. Much appreciated. 👍
I am evaluating whether we could simplify our user API by aggregating multiple
std::span<T>
into onestd::[experimental::]span<T>
. For the use-case with a single backing STL container this seems to be fairly simple, i.e.but how would this look like if I want to aggregate two
std::span<T>
, e.g.I tried to overload
without much success: https://godbolt.org/z/creYGGdK8
Guess it's probably trivial once you know how ... but I don't. Would you be able to give me some pointers/hints?
Any help would be much appreciated. Thanks in advance!
The text was updated successfully, but these errors were encountered: