Skip to content
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

Adding aliases xt::xtensor_pointer and xt::xarray_pointer #2665

Merged
merged 2 commits into from
Mar 17, 2023

Conversation

tdegeus
Copy link
Member

@tdegeus tdegeus commented Mar 16, 2023

Fixes #2663

The name is up for discussion!

Copy link
Member

@JohanMabille JohanMabille left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The names sound a bit like "pointers to tensor or array", but I don't have a better alternative for now.

docs/source/api/container_index.rst Outdated Show resolved Hide resolved
include/xtensor/xadapt.hpp Outdated Show resolved Hide resolved
@tdegeus
Copy link
Member Author

tdegeus commented Mar 16, 2023

@JohanMabille Is seems to me that xt::xtensor_pointer<double, 2> is possible as I propose, but that getting xt::xarray_pointer from xt::adapt is ugly.

In particular, xarray_adaptor takes a shape type template, and it seems that there are no sufficient implicit conversion for xt::svector. With the current implementation I get

no viable conversion from 'xarray_adaptor<[2 * ...], std::vector<unsigned long>>' to 'xarray_adaptor<[2 * ...], (default) xt::svector<unsigned long, 4, std::allocator<unsigned long>, true>>'

So the only way I see is to have

template <class T, layout_type L, class SC>
using xarray_pointer = xarray_adaptor<xbuffer_adaptor<xtl::closure_type_t<T*>, xt::no_ownership, detail::default_allocator_for_ptr_t<T>>, L, SC>;

which could be called as

std::vector<size_t> shape = { 2, 2 };
std::vector<double> data = { 1, 2, 3, 4};
xt::xarray_pointer<double, XTENSOR_DEFAULT_LAYOUT, decltype(shape)> a = xt::adapt(data.data(), 4, xt::no_ownership(), shape);

which I find very ugly. Do you know a way around this? Can a proper conversion be presented to the compiler?

@JohanMabille
Copy link
Member

JohanMabille commented Mar 16, 2023

You can use xt::svector instead of std::vector for the shape to avoid passing the shape parameter type. But you should keep it in the alias definition and default it (to the same value as in xarray_adaptor).

Can a proper conversion be presented to the compiler?

The only way to do it would be to provide an implicitconversion from xarray_adaptor<T, L, SC1> to xarray_adaptor<T, L, SC2> but I am definitely not for this. That would involve a copy hidden to the user.

@tdegeus tdegeus force-pushed the alias branch 3 times, most recently from bb1dbe0 to 0d22d13 Compare March 16, 2023 17:18
@tdegeus
Copy link
Member Author

tdegeus commented Mar 16, 2023

Thanks. I went for the way of least resistance

@JohanMabille JohanMabille merged commit 5999b3e into xtensor-stack:master Mar 17, 2023
@tdegeus tdegeus deleted the alias branch March 17, 2023 08:47
@tdegeus
Copy link
Member Author

tdegeus commented Mar 17, 2023

Thanks! Quite happy that we now have this. I have worked around it a few times already, but new enough to implement it. In particular because I did not realise it could be done so simply

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide lightweight class for pointer to external data
2 participants