简体   繁体   English

从函数返回am Eigen :: Tensor slice

[英]Return am Eigen::Tensor slice from a function

I would like to write functions that return slices of Eigen::Tensor. 我想编写返回Eigen :: Tensor切片的函数。 In the real code, getSlice() takes some integers and the extent and offset are calculated. 在实际代码中,getSlice()接受一些整数,并计算范围和偏移量。 I would like my functions to return a view into the array so that I can access the array for reading and writing without copying. 我希望我的函数将视图返回到数组中,以便我可以访问数组以进行读取和写入而无需复制。

I can create a variable that is a slice of my array and alter the data. 我可以创建一个作为数组切片的变量并更改数据。 But when I return the same slice from a function the values are not altered. 但是,当我从函数返回相同的片段时,值不会改变。 I am guessing that the function generates a new array as the return value. 我猜该函数将生成一个新数组作为返回值。 How do I return the slice I need? 如何退回我需要的切片? Or should I do this a different way? 还是我应该采用其他方式?

#include <iostream>
#include <Eigen/Dense>
#include <unsupported/Eigen/CXX11/Tensor>

Eigen::Tensor<float,3> getSlice(Eigen::Tensor<float,3>& a,
                Eigen::array<long,3>& offset,
                Eigen::array<long,3>& extent)
{
   return a.slice(offset,extent);
}

int main()
{
   Eigen::Tensor<float,3> et = Eigen::Tensor<float,3>(3,5,4);
   et.setConstant(1.1);
   std::cout << et << std::endl;

   Eigen::array<long,3> offset = {0,0,0};
   Eigen::array<long,3> extent = {2,2,1};

   et.slice(offset,extent).setConstant(2.2);
   std::cout << "Set slice constant" << std::endl;
   std::cout << et << std::endl;

   auto sl = et.slice(offset,extent);
   sl.setConstant(3.3);
   std::cout << "Set slice constant from slice instance." << std::endl;
   std::cout << et << std::endl;

   getSlice(et,offset,extent).setConstant(4.4);
   std::cout << "Set slice constant from function." << std::endl;
   std::cout << et << std::endl;
}

Program output: 程序输出:

$ ./ta $ ./吨

1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1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.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 3.3 3.3 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 3.3 3.3 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1

Set slice constant from function. 从功能设置切片常数。

3.3 3.3 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 3.3 3.3 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 3.3 3.3 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 3.3 3.3 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1

Your observation that your implementation of getSlice returns a new Tensor object (with a copy of the original data) is correct. 您对getSlice的实现返回一个新的Tensor对象(带有原始数据的副本)的观察是正确的。 In your case the simplest solution is to change the return type to auto (even though, you should generally be careful with auto and Eigen): 在您的情况下,最简单的解决方案是将返回类型更改为auto (即使您通常应该对auto和Eigen保持谨慎):

inline auto getSlice(Eigen::Tensor<float,3>& a,
                Eigen::array<long,3>& offset,
                Eigen::array<long,3>& extent)
{
   return a.slice(offset,extent);
}

Live-Demo: https://godbolt.org/z/tLWYUz 现场演示: https : //godbolt.org/z/tLWYUz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM