xtensor-stack / xtensor

C++ tensors with broadcasting and lazy computing
BSD 3-Clause "New" or "Revised" License
3.32k stars 396 forks source link

Avoiding template explosion on views #2779

Closed bwehlin closed 4 months ago

bwehlin commented 5 months ago

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!

spectre-ns commented 5 months ago

@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.

bwehlin commented 5 months ago

@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.

spectre-ns commented 4 months ago

@bwehlin did this fix your issue?

bwehlin commented 4 months ago

@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.