xtensor icon indicating copy to clipboard operation
xtensor copied to clipboard

Avoiding template explosion on views

Open bwehlin opened this issue 1 year ago • 3 comments

Hi,

I need to take views of views, and views of views of views, and so on. This leads to a template explosion since the datatype of, e.g., xt::strided_view(array, ...) is different from that of xt::strided_view(xt::strided_view(array, ...), ...), and so on.

Is there a way to cast views into some kind of unified type that can handle arbitrary view "depths"? Ideally, this would also handle e.g., reshape views.

Thanks!

bwehlin avatar Apr 04 '24 16:04 bwehlin

@bwehlin The easiest way to solve this is using xt::eval which will evaluate your view. This could be sub-optimal for performance but chances are you will exceed the inlining ability of the compiler anyways if your types are too nested. Every compiler has a depth limit for inlining functions.

spectre-ns avatar Apr 20 '24 23:04 spectre-ns

@spectre-ns this looks promising. My case is a bit nonstandard in that I'm storing non-POD types in my xt::arrays and the heavy computation happens outside xtensor. In other words, I use xtensor more as a data container than for computation. As such, I don't think there will be a big issue in terms of performance. Thanks, I will try this for some realistic cases later.

bwehlin avatar Apr 25 '24 17:04 bwehlin

@bwehlin did this fix your issue?

spectre-ns avatar May 04 '24 22:05 spectre-ns

@spectre-ns I haven't fully tested this yet but it appears to do what I want. I'll close this issue for now and if something else pops up I can perhaps reopen this or open a new ticket. Either way, thanks for your help.

bwehlin avatar May 07 '24 09:05 bwehlin