ubuntu 18.04、R 3.5.0、库TMB、安装警告和执行过程中的致命错误

Alb_Sim

全新安装的 ubuntu 18.04。-> 好的

从这个 repo 安装的 R 3.5.0:

deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/

使用安全 apt 密钥添加:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E084DAB9

R 安装正确,甚至可以从 rstudio 启动。尝试TMB使用此命令在超级用户模式下从 R 控制台或终端 R安装库、安装命令(在尝试第二个之前尝试了两个并删除了一个,因此用户库和 R 库之间可能没有冲突):

install.packages("TMB",type = "source")

安装开始,它会安装依赖包(Rcpp、RcppEigen 等)。

在完全 RcppEigen 的安装/编译期间,控制台开始发出 10000 行警告(例如,如果缺少某些编译器链接),这是它偏离正常安装的第一行:

** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded
* DONE (Rcpp)
* installing *source* package ‘RcppEigen’ ...
** package ‘RcppEigen’ successfully unpacked and MD5 sums checked
** libs
g++  -I"/usr/share/R/include" -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include"   -I../inst/include -fpic  -g -O2 -fdebug-prefix-map=/build/r-base-O3QT2W/r-base-3.5.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c RcppEigen.cpp -o RcppEigen.o
In file included from ../inst/include/Eigen/Core:388:0,
                 from ../inst/include/Eigen/Dense:1,
                 from ../inst/include/RcppEigenForward.h:30,
                 from ../inst/include/RcppEigen.h:25,
                 from RcppEigen.cpp:22:
../inst/include/Eigen/src/Core/arch/SSE/PacketMath.h:60:39: warning: ignoring attributes on template argument ‘__m128 {aka __vector(4) float}’ [-Wignored-attributes]
 template<> struct is_arithmetic<__m128>  { enum { value = true }; };
                                       ^
../inst/include/Eigen/src/Core/arch/SSE/PacketMath.h:61:40: warning: ignoring attributes on template argument ‘__m128i{aka __vector(2) long long int}’ [-Wignored-attributes]
 template<> struct is_arithmetic<__m128i> { enum { value = true }; };
                                        ^
../inst/include/Eigen/src/Core/arch/SSE/PacketMath.h:62:40: warning: ignoring attributes on template argument ‘__m128d{aka __vector(2) double}’ [-Wignored-attributes]
 template<> struct is_arithmetic<__m128d> { enum { value = true }; };
                                        ^
../inst/include/Eigen/src/Core/arch/SSE/PacketMath.h:161:43: warning: ignoring attributes on template argument ‘Eigen::internal::Packet4f {aka __vector(4) float}’ [-Wignored-attributes]
 template<> struct unpacket_traits<Packet4f> { typedef float  type; enum {size=4, alignment=Aligned16}; typedef Packet4f half; };

这些是 install.package("TMB") 命令的最后几行:

/home/alberto/R/x86_64-pc-linux-gnu-library/3.5/RcppEigen/include/Eigen/src/Core/DenseCoeffsBase.h:55:30: warning: ignoring attributes on template argument ‘Eigen::internal::packet_traits<double>::type {aka __vector(4) double}’ [-Wignored-attributes]
                      >::type PacketReturnType;
                         ^~~~~~~~~~~~~~~~
/home/alberto/R/x86_64-pc-linux-gnu-library/3.5/RcppEigen/include/Eigen/src/Core/DenseCoeffsBase.h: In instantiation of ‘class Eigen::DenseCoeffsBase<Eigen::Transpose<const Eigen::Block<const Eigen::Block<Eigen::Block<Eigen::Map<Eigen::Matrix<double, -1, -1>, 0, Eigen::Stride<0, 0> >, -1, -1, false>, -1, -1, false>, 1, -1, false> >, 0>’:
/home/alberto/R/x86_64-pc-linux-gnu-library/3.5/RcppEigen/include/Eigen/src/Core/DenseCoeffsBase.h:478:7:   required from ‘class Eigen::DenseCoeffsBase<Eigen::Transpose<const Eigen::Block<const Eigen::Block<Eigen::Block<Eigen::Map<Eigen::Matrix<double, -1, -1>, 0, Eigen::Stride<0, 0> >, -1, -1, false>, -1, -1, false>, 1, -1, false> >, 2>’
/home/alberto/R/x86_64-pc-linux-gnu-library/3.5/RcppEigen/include/Eigen/src/Core/DenseBase.h:41:34:   required from ‘class Eigen::DenseBase<Eigen::Transpose<const Eigen::Block<const Eigen::Block<Eigen::Block<Eigen::Map<Eigen::Matrix<double, -1, -1>, 0, Eigen::Stride<0, 0> >, -1, -1, false>, -1, -1, false>, 1, -1, false> > >’
/home/alberto/R/x86_64-pc-linux-gnu-library/3.5/RcppEigen/include/Eigen/src/Core/MatrixBase.h:48:34:   required from ‘class Eigen::MatrixBase<Eigen::Transpose<const Eigen::Block<const Eigen::Block<Eigen::Block<Eigen::Map<Eigen::Matrix<double, -1, -1>, 0, Eigen::Stride<0, 0> >, -1, -1, false>, -1, -1, false>, 1, -1, false> > >’
/home/alberto/R/x86_64-pc-linux-gnu-library/3.5/RcppEigen/include/Eigen/src/Core/Transpose.h:115:37:   required from ‘class Eigen::TransposeImpl<const Eigen::Block<const Eigen::Block<Eigen::Block<Eigen::Map<Eigen::Matrix<double, -1, -1>, 0, Eigen::Stride<0, 0> >, -1, -1, false>, -1, -1, false>, 1, -1, false>, Eigen::Dense>’
/home/alberto/R/x86_64-pc-linux-gnu-library/3.5/RcppEigen/include/Eigen/src/Core/Transpose.h:52:37:   required from ‘class Eigen::Transpose<const Eigen::Block<const Eigen::Block<Eigen::Block<Eigen::Map<Eigen::Matrix<double, -1, -1>, 0, Eigen::Stride<0, 0> >, -1, -1, false>, -1, -1, false>, 1, -1, false> >’
/home/alberto/R/x86_64-pc-linux-gnu-library/3.5/RcppEigen/include/Eigen/src/Core/ProductEvaluators.h:552:40:   [ skipping 22 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/home/alberto/R/x86_64-pc-linux-gnu-library/3.5/RcppEigen/include/Eigen/src/Core/PlainObjectBase.h:816:7:   required from ‘void Eigen::PlainObjectBase<Derived>::_init1(const Eigen::DenseBase<ElseDerived>&) [with T = Eigen::Inverse<Eigen::Matrix<double, -1, -1> >; OtherDerived = Eigen::Inverse<Eigen::Matrix<double, -1, -1> >; Derived = Eigen::Matrix<double, -1, -1>]’
/home/alberto/R/x86_64-pc-linux-gnu-library/3.5/RcppEigen/include/Eigen/src/Core/Matrix.h:296:31:   required from ‘Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::Matrix(const T&) [with T = Eigen::Inverse<Eigen::Matrix<double, -1, -1> >; _Scalar = double; int _Rows = -1; int _Cols = -1; int _Options = 0; int _MaxRows = -1; int _MaxCols = -1]’
/home/alberto/R/x86_64-pc-linux-gnu-library/3.5/TMB/include/tmbutils/vector.hpp:93:22:   required from ‘tmbutils::matrix<Type>::matrix(T1) [with T1 = Eigen::Inverse<Eigen::Matrix<double, -1, -1> >; Type = double]’
/home/alberto/R/x86_64-pc-linux-gnu-library/3.5/TMB/include/expm.hpp:36:14:   required from ‘atomic::Block<Type> atomic::Block<Type>::inverse() [with Type = double]’
/home/alberto/R/x86_64-pc-linux-gnu-library/3.5/TMB/include/expm.hpp:180:17:   required from ‘matrix_pade atomic::expm(matrix_pade) [with matrix_pade = atomic::nestedTriangle<0>]’
/home/alberto/R/x86_64-pc-linux-gnu-library/3.5/TMB/include/expm.hpp:192:52:   required from here`

install.packages("TMB",type="source") 的最后一行输出是:

g++ -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o simple.so simple.o -L/usr/lib/R/lib -lR
Build time 16.121 seconds

* DONE (TMB)

安装似乎已完成(控制台最后给出 * DOME(TMB)),但运行 TMB(线性回归)的基本示例会给出致命的 R 会话错误。

R 代码中的示例:

tmb_model <- "
// linear regression
#include <TMB.hpp>

template<class Type>
Type objective_function<Type>::operator() () {
// data:
DATA_VECTOR(x);
DATA_VECTOR(y);

// parameters:
PARAMETER(a); // intercept
PARAMETER(b); // slope
PARAMETER(log_sigma); // log(residual SD)
// we fit sigma on a log scale to keep it > 0

// procedures: (transformed parameters)
Type sigma = exp(log_sigma);

int n = y.size(); // get number of data points to loop over

Type nll = 0.0; // initialize negative log likelihood

for(int i = 0; i < n; i++){ // C++ starts loops at 0!
// get negative log likelihood (last argument is log = TRUE)
nll -= dnorm(y[i], a + b * x[i], sigma, true);
}

return nll;
}"
write(tmb_model, file = "regression.cpp")
library(TMB)
compile("regression.cpp")

使用 TMB::compile 会给出大量警告(这些是最后一次磨损 + gcc 行输出):

/home/alberto/R/x86_64-pc-linux-gnu-library/3.5/RcppEigen/include/Eigen/src/Core/DenseCoeffsBase.h:55:30: warning: ignoring attributes on template argument ‘Eigen::internal::packet_traits<double>::type {aka __vector(4) double}’ [-Wignored-attributes]
                      >::type PacketReturnType;
                              ^~~~~~~~~~~~~~~~
In file included from /home/alberto/R/x86_64-pc-linux-gnu-library/3.5/RcppEigen/include/Eigen/Core:436:0,
                 from /home/alberto/R/x86_64-pc-linux-gnu-library/3.5/RcppEigen/include/Eigen/Dense:1,
                 from /home/alberto/R/x86_64-pc-linux-gnu-library/3.5/TMB/include/TMB.hpp:53,
                 from regression.cpp:3:
/home/alberto/R/x86_64-pc-linux-gnu-library/3.5/RcppEigen/include/Eigen/src/Core/CoreEvaluators.h: In instantiation of ‘struct Eigen::internal::evaluator<Eigen::Block<const Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<double>, Eigen::Matrix<double, -1, -1> >, 1, -1, false> >’:
/home/alberto/R/x86_64-pc-linux-gnu-library/3.5/RcppEigen/include/Eigen/src/Core/AssignEvaluator.h:730:20:   required from ‘void Eigen::internal::call_dense_assignment_loop(DstXprType&, const SrcXprType&, const Functor&) [with DstXprType = Eigen::Block<Eigen::Matrix<double, -1, -1>, 1, -1, false>; SrcXprType = Eigen::Block<const Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<double>, Eigen::Matrix<double, -1, -1> >, 1, -1, false>; Functor = Eigen::internal::assign_op<double, double>]’
/home/alberto/R/x86_64-pc-linux-gnu-library/3.5/RcppEigen/include/Eigen/src/Core/AssignEvaluator.h:879:31:   required from ‘static void Eigen::internal::Assignment<DstXprType, SrcXprType, Functor, Eigen::internal::Dense2Dense, Weak>::run(DstXprType&, const SrcXprType&, const Functor&) [with DstXprType = Eigen::Block<Eigen::Matrix<double, -1, -1>, 1, -1, false>; SrcXprType = Eigen::Block<const Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<double>, Eigen::Matrix<double, -1, -1> >, 1, -1, false>; Functor = Eigen::internal::assign_op<double, double>; Weak = void]’
/home/alberto/R/x86_64-pc-linux-gnu-library/3.5/RcppEigen/include/Eigen/src/Core/AssignEvaluator.h:836:49:   required from ‘void Eigen::internal::call_assignment_no_alias(Dst&, const Src&, const Func&) [with Dst = Eigen::Block<Eigen::Matrix<double, -1, -1>, 1, -1, false>; Src = Eigen::Block<const Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<double>, Eigen::Matrix<double, -1, -1> >, 1, -1, false>; Func = Eigen::internal::assign_op<double, double>]’
/home/alberto/R/x86_64-pc-linux-gnu-library/3.5/RcppEigen/include/Eigen/src/Core/AssignEvaluator.h:804:27:   required from ‘void Eigen::internal::call_assignment(Dst&, const Src&, const Func&, typename Eigen::internal::enable_if<(! Eigen::internal::evaluator_assume_aliasing<Src>::value), void*>::type) [with Dst = Eigen::Block<Eigen::Matrix<double, -1, -1>, 1, -1, false>; Src = Eigen::Block<const Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<double>, Eigen::Matrix<double, -1, -1> >, 1, -1, false>; Func = Eigen::internal::assign_op<double, double>; typename Eigen::internal::enable_if<(! Eigen::internal::evaluator_assume_aliasing<Src>::value), void*>::type = void*]’
/home/alberto/R/x86_64-pc-linux-gnu-library/3.5/RcppEigen/include/Eigen/src/Core/AssignEvaluator.h:782:18:   required from ‘void Eigen::internal::call_assignment(Dst&, const Src&) [with Dst = Eigen::Block<Eigen::Matrix<double, -1, -1>, 1, -1, false>; Src = Eigen::Block<const Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<double>, Eigen::Matrix<double, -1, -1> >, 1, -1, false>]’
/home/alberto/R/x86_64-pc-linux-gnu-library/3.5/RcppEigen/include/Eigen/src/Core/Assign.h:66:28:   [ skipping 26 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/home/alberto/R/x86_64-pc-linux-gnu-library/3.5/RcppEigen/include/Eigen/src/Core/PlainObjectBase.h:816:7:   required from ‘void Eigen::PlainObjectBase<Derived>::_init1(const Eigen::DenseBase<ElseDerived>&) [with T = Eigen::Inverse<Eigen::Matrix<double, -1, -1> >; OtherDerived = Eigen::Inverse<Eigen::Matrix<double, -1, -1> >; Derived = Eigen::Matrix<double, -1, -1>]’
/home/alberto/R/x86_64-pc-linux-gnu-library/3.5/RcppEigen/include/Eigen/src/Core/Matrix.h:296:31:   required from ‘Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::Matrix(const T&) [with T = Eigen::Inverse<Eigen::Matrix<double, -1, -1> >; _Scalar = double; int _Rows = -1; int _Cols = -1; int _Options = 0; int _MaxRows = -1; int _MaxCols = -1]’
/home/alberto/R/x86_64-pc-linux-gnu-library/3.5/TMB/include/tmbutils/vector.hpp:93:22:   required from ‘tmbutils::matrix<Type>::matrix(T1) [with T1 = Eigen::Inverse<Eigen::Matrix<double, -1, -1> >; Type = double]’
/home/alberto/R/x86_64-pc-linux-gnu-library/3.5/TMB/include/expm.hpp:36:14:   required from ‘atomic::Block<Type> atomic::Block<Type>::inverse() [with Type = double]’
/home/alberto/R/x86_64-pc-linux-gnu-library/3.5/TMB/include/expm.hpp:180:17:   required from ‘matrix_pade atomic::expm(matrix_pade) [with matrix_pade = atomic::nestedTriangle<0>]’
/home/alberto/R/x86_64-pc-linux-gnu-library/3.5/TMB/include/expm.hpp:192:52:   required from here
/home/alberto/R/x86_64-pc-linux-gnu-library/3.5/RcppEigen/include/Eigen/src/Core/CoreEvaluators.h:960:8: warning: ignoring attributes on template argument ‘Eigen::internal::packet_traits<double>::type {aka __vector(4) double}’ [-Wignored-attributes]
   enum {
        ^
g++ -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o regression.so regression.o -L/usr/lib/R/lib -lR
[1] 0

在执行此 R 代码期间,它在 rstudio(R 控制台)上立即给我致命错误,完全取自 TMB 示例:

set.seed(123)
x <- runif(20, 1, 10)
y <- rnorm(20, mean = 1.8 + 2.4 * x, sd = exp(0.3))
plot(x, y)
obj <- MakeADFun(
  data = list(x = x, y = y), 
  parameters = list(a = 0, b = 0, log_sigma = 0),
  DLL = "regression")
opt <- nlminb(start = obj$par, obj = obj$fn, gr = obj$gr)
rep <- sdreport(obj)

编译器:

gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 7.3.0-16ubuntu3' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --with-as=/usr/bin/x86_64-linux-gnu-as --with-ld=/usr/bin/x86_64-linux-gnu-ld --program-suffix=-7 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 7.3.0 (Ubuntu 7.3.0-16ubuntu3)

~.R/Makevars(之前接触过 RcppArmadillo,也许是我的错,错误在 CXXFLAGS 上):

CFLAGS= -O -mtune=native
CXXFLAGS= -O2 -mtune=native -march=native -pthread -fopenmp $(BLAS_LIBS) $(LAPACK_LIBS)
FFLAGS=-g -O -mtune=native
FCFLAGS=-g -O -mtune=native

编辑(我自己)我尝试 RcppEigen 示例:http ://dirk.eddelbuettel.com/code/rcpp.eigen.html它运行正常,推送许多警告(通常不会出现在 Rcpp 中),调用sourceCpp(file="input.cc"),它编译并正确执行。

我尝试在 TMB 上找到的第一个例子https://github.com/nwfsc-assess/geostatistical_delta-GLMM/wiki/Steps-to-install-TMB它给了我:

0 regions found.
Using 8 threads
TMB has received an error from Eigen. The following condition was not met:
index >= 0 && index < size()
Please check your matrix-vector bounds etc., or run your program through a debugger.

崩溃前

Alb_Sim

解决了:

1)为了避免嘈杂的警告输出,添加标志

-Wno-ignored-attributes

2) 为避免 adfun 调用时出现运行时错误,请移除该标志

-fopenmp

在编译目标函数之前,如果模型(的 C++ 代码objective_function<Type>::operator())不使用openMP(例如#pragma omp 某物)。

来源(我在这里问):https : //github.com/kaskr/adcomp/issues/277

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章