RcppArmadillo中的ARMA_NO_DEBUG问题

4
我想禁用在RcppArmadillo中访问矩阵元素时的边界检查。
Armadillo文档说:
“可以通过编辑文件include/armadillo_bits/config.hpp来配置Armadillo。可以通过取消或注释掉特定的#define来启用或禁用特定功能,如下所示。”
但在R包的上下文中,我该如何激活这个指令?
我尝试创建了一个config.h文件,其中包含:
#ifndef CONFIG_LOADED
#define CONFIG_LOADED
#define ARMA_NO_DEBUG
#endif 

然后将其包含在我的/src文件夹中的每个.cpp文件中,但我不确定它是否正常工作,或者是否有其他方法而不是在每个.cpp文件中添加#include "config.h"


目前我有一个.cpp文件(包含主要算法),它以以下方式开始:

#include "configs.h"
#include <RcppArmadillo.h>

using namespace Rcpp;
using namespace arma;

// [[Rcpp::export]]
SEXP sample_gibbs_cpp(const arma::vec& v_n, const arma::mat& W, 
arma::vec h_n, double alpha = 1, double beta = 1, int iter=100,
double burnin = 0.5){
... code ...
}

还有一些其他的内容是关于IT技术的。

#include <RcppArmadillo.h>

using namespace Rcpp;
using namespace arma;
... code ...

我的DESCRIPTION文件:

Package: mypackage
Title: What the Package Does (one line, title case)
Version: 0.0.0.9000
Authors@R: person("First", "Last", email = "first.last@example.com", role = c("aut", "cre"))
Description: What the package does (one paragraph).
Depends:
    R (>= 3.2.3)
License: What license is it under?
Encoding: UTF-8
LazyData: true
RoxygenNote: 5.0.1
Imports:
    ggplot2,
    dplyr,
    tidyr,
    rstan
LinkingTo: Rcpp, RcppArmadillo, RcppEigen
SystemRequirements: C++11

我使用以下方式编译我的软件包:

devtools::load_all()

2
请发布您的软件包代码... 特别是,您是如何调用此标题以及何时调用的?顺序很重要。 - coatless
1个回答

6

顺序很重要。在包含#include<RcppArmadillo.h>之前,必须先包含该#define语句。

示例:

custom_config.h

#ifndef CONFIG_LOADED
#define CONFIG_LOADED
#define ARMA_NO_DEBUG
#endif 

example_compiled_file.cpp

#include "custom_config.h"
#include <RcppArmadillo.h>

// [[Rcpp::export]]
void test_pkg(const arma::vec& x) {

   // Should not trigger error bound checking with debug flag on.
   double my_val_protected = x(0);

   // Never triggers error bound checking
   double my_val = x.at(0);
}
注意: 由于这是一个软件包,因此不需要使用// [[Rcpp::depends(RcppArmadillo)]]。相反,您必须在DESCRIPTION文件的LinkingTo:字段中指定RcppArmadilloRcpp,并在Imports:字段中包含Rcpp。您至少需要从Rcpp导入一个函数(最好是:evalCpp)。

例如,DESCRIPTION 必须具备:

Imports: Rcpp (>= 0.12.15)
LinkingTo: Rcpp, RcppArmadillo

1
谢谢coatless。当RccpArmadillo已经在LinkingTo中时,是否必须将其放入Import中?我需要在每个.cpp文件中添加#include "custom_config.h"吗?我不明白您所说的最小导入是什么意思,我只是像从我的R/example.R文件中调用它们一样调用它们。 - alberto
@alberto 哎呀,说错了。Rcpp 应该在 Import: 中,而 RcppArmadillo 则应该与 Rcpp 一起包含在 LinkingTo: 中。 - coatless
这需要放在 custom_config.h 的包含语句之后。然后,是的。 - coatless
当然,但我只是想知道 #include custom_config.h 是否要放在所有 .cpp 文件中? - alberto
你必须在每个想使用#define指令的.cpp文件中都包含 #include "custom_config.h" - coatless
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接