【问题标题】:ARMA_NO_DEBUG in R package with RcppArmadillo带有 RcppArmadillo 的 R 包中的 ARMA_NO_DEBUG
【发布时间】:2018-02-02 17:29:37
【问题描述】:

我想在访问 RcppArmadillo 中的矩阵元素时禁用边界检查。

犰狳的文献说

可以通过编辑文件来配置犰狳 包括/armadillo_bits/config.hpp。具体功能可以 通过取消注释或注释掉特定的启用或禁用 #define,如下所列。

但是在 R 包的上下文中,我怎样才能激活这个指令呢?

我尝试使用

创建一个config.h 文件
#ifndef CONFIG_LOADED
#define CONFIG_LOADED
#define ARMA_NO_DEBUG
#endif 

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


目前我有一个 .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 ...
}

然后其他的只是

#include <RcppArmadillo.h>

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

我的描述文件:

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()

【问题讨论】:

  • 包裹的邮政编码...特别是,您如何以及何时调用此标头?订单很重要。

标签: c++ r rcpp armadillo


【解决方案1】:

这里的订单很重要。此#define 语句必须包含在之前包含#include&lt;RcppArmadillo.h&gt;

一个例子:

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)。

例如描述必须有:

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

【讨论】:

  • 谢谢无衣。是否必须将 RccpArmadillo 放入 Import: 当它已经在 LinkingTo: 中时?我需要在每个 .cpp 中添加#include "custom_config.h" 吗?我不明白您所说的最小导入是什么意思,我只是将它们称为 R/example.R 文件中的 R 函数。
  • @alberto 哎呀。说错了。 Rcpp 应包含在 Import: 中,RcppArmadillo 应包含在 LinkingTo:Rcpp 中。
  • 哎呀,我从来没有把 Rcpp 放在 Import 中! (尽管我在 roxygen2 生成的 NAMESPACE 文件中有 importFrom(Rcpp, sourceCpp),但 RccpArmadillo 仍然可以工作)——所以我想要优化的文件中的 #include 就足够了吗?
  • 这需要在custom_config.h 包含之后。那么,是的。
  • 对不起,我的意思是#include "custom_config.h" !
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-24
相关资源
最近更新 更多