Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eigen3 tensor slices without making a copy of the data

Tags:

eigen

eigen3

I have been testing the Tensor module from Eigen3 for a new project. Even when the module is not yet finished, it seems to have most of the functionality that I need.

But there is one part that I quite not get. Whenever I have a big Tensor and I want to extract a slice from it, Eigen makes a copy of the data.

Is there a way to not copy the data, but instead point to the original data block in the slice?

For example if I do:

Tensor<float, 3> A(100,1000,1000); A.setZero();

Eigen::array<int, 3> offsets = {0, 0, 0};
Eigen::array<int, 3> extents = {2, 2, 2};

Tensor<float, 3> c = A.slice(offsets, extents);
A(0,0,0) = 1.0;

cerr << c << endl;

But the first element of "c" is still zero, instead of mapping to the modified "A(0,0,0)" data block.

like image 665
JdlCR Avatar asked Nov 07 '22 01:11

JdlCR


1 Answers

You can use a TensorMap to create a tensor based on shared memory space of your slice. However this only works if your slice occupies contiguous portion of the data array. Otherwise you would need to do some tensor arithmetic to figure out the begin and end 1d indices of various parts of your single slice.

TensorMap<Tensor<float, 3, RowMajor> > row_major(data, ...);
like image 87
John Jiang Avatar answered Dec 07 '22 09:12

John Jiang