ubuntu 18.04,R 3.5.0,库TMB,执行期间的安装警告和致命错误

时间:2018-06-25 15:15:50

标签: r ubuntu-18.04 tmb

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

从该存储库中安装的

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启动。 尝试使用此命令从R控制台或终端R在超级用户模式下安装TMB库,安装命令(尝试并在尝试第二个之前将其删除,因此在用户库和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"),它可以正确编译并执行。

我尝试在https://github.com/nwfsc-assess/geostatistical_delta-GLMM/wiki/Steps-to-install-TMB的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.

崩溃前

2 个答案:

答案 0 :(得分:0)

看起来您可能尚未安装Eigen开发软件包(因为它是全新的ubuntu安装) 你尝试过

sudo apt-get install libeigen3-dev 

看起来您也可以通过

安装预编译的二进制包
sudo apt-get install r-cran-rcppeigen

显然,rcppeigen可能需要大量的ram才能进行编译,而在某些内存较少的机器上却会失败。

答案 1 :(得分:0)

已解决:

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

-Wno-ignored-attributes

2)为避免adfun调用发生运行时错误,请删除该标志

-fopenmp

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

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