问题
如何避免特征小对角矩阵的动态分配?
上下文
我用的是艾根3.4。我有N乘N对角矩阵W:
auto W = Eigen::DiagonalMatrix<double, Dynamic>(N);如果N <= 512使用堆栈上的缓冲区,我想避免分配:
double W_buffer[512];对于法线向量和矩阵,我知道我可以使用Map
double y_buff[512];
auto y = Eigen::Map<VectorXd>( y_buff, N );然而,当我尝试使用对角矩阵时,它会给我一个错误,因为InnerStrideAtCompileTime不是Eigen::DiagonalMatrix的成员。
与DiagonalMatrix一起使用Map时的错误消息
In file included from eigen/Eigen/Core:311,
from eigen/Eigen/Dense:1,
from build/release/CMakeFiles/bench.dir/cmake_pch.hxx:5,
from <command-line>:
eigen/Eigen/src/Core/Map.h: In instantiation of ‘struct Eigen::internal::traits<Eigen::Map<Eigen::DiagonalMatrix<double, -1> > >’:
eigen/Eigen/src/Core/util/ForwardDeclarations.h:34:48: required from ‘struct Eigen::internal::accessors_level<Eigen::Map<Eigen::DiagonalMatrix<double, -1> > >’
eigen/Eigen/src/Core/util/ForwardDeclarations.h:101:75: required from ‘class Eigen::Map<Eigen::DiagonalMatrix<double, -1> >’
include/volar/estimators.hpp:203:18: required from ‘static volar::R volar::PolyLE<Degree>::estimate(volar::R, volar::ViewR, volar::ViewR, const Kernel&) [with Kernel = volar::UniformK; int Degree = 1; volar::R = double; volar::ViewR = volar::View<double>]’
include/volar/kernel_smoothing.hpp:81:64: required from ‘volar::R volar::LocalRFT<Estimator, Kernel>::operator()(volar::R) const [with Estimator = volar::EigenLinearLE; Kernel = volar::UniformK; volar::R = double]’
bench/core.cpp:43:23: required from ‘void localRF(benchmark::State&) [with Method = volar::EigenLinearLE; Kernel = volar::UniformK]’
bench/core.cpp:96:1: required from here
eigen/Eigen/src/Core/Map.h:30:53: error: ‘InnerStrideAtCompileTime’ is not a member of ‘Eigen::DiagonalMatrix<double, -1>’
30 | ? int(PlainObjectType::InnerStrideAtCompileTime)
| ^~~~~~~~~~~~~~~~~~~~~~~~
In file included from eigen/Eigen/Core:163,
from eigen/Eigen/Dense:1,
from build/release/CMakeFiles/bench.dir/cmake_pch.hxx:5,
from <command-line>:发布于 2022-04-12 18:57:15
Eigen::DiagonalMatrix的第三个模板参数MaxSizeAtCompileTime允许您这样做。
当与Eigen::Dynamic相结合时,DiagonalMatrix将有一个足够大的内部缓冲区供MaxSizeAtCompileTime使用,但它仍然是动态大小的。
例如,以下内容相当于您试图使用外部缓冲区时所做的工作:
auto W = Eigen::DiagonalMatrix<double, Eigen::Dynamic, 512>(N)显然,尝试使用大于MaxSizeAtCompileTime的大小初始化它将在运行时失败(使用断言),但这并不比使用Map时必须处理的问题更糟糕。
https://stackoverflow.com/questions/71847633
复制相似问题