xtensor icon indicating copy to clipboard operation
xtensor copied to clipboard

How to avoid memory allocation here?

Open jimmysue opened this issue 3 years ago • 2 comments

I have procedure like below, what is the best and most performance practice for below procedure?


// what is the type here ?
auto tmp = xt::zeros<float>(shape);

if (condition1) {
     tmp +=  v1;
}
if (contidion2) {
   tmp += v2
}
return tmp;

The code won't compile, I'm here to ask for help how to code correct .

jimmysue avatar May 19 '22 03:05 jimmysue

xt::zeros does not hold any value, as explained here. Therefore, if you want to use it in computed-assignment, you need to assign it to an xarray first, you cannot avoid the memory allocation.

JohanMabille avatar May 19 '22 09:05 JohanMabille

So just to be clear, you will want to do either of the following:

auto tmp = xt::eval(xt::zeros<float>(shape));
xt::xtensor<float, n> tmp = xt::zeros<float>(shape); // if you know the rank at compile time
xt::xarray<float> tmp = xt::zeros<float>(shape); // if you don't (easily) know the rank at compile time

tdegeus avatar May 19 '22 13:05 tdegeus