ARMA_NO_DEBUG 在带有 RcppArmadillo 的 R 包中
ARMA_NO_DEBUG in R package with RcppArmadillo
我想在访问 RcppArmadillo 中的矩阵元素时禁用边界检查。
Armadillo 的文档说
Armadillo can be configured via editing the file
include/armadillo_bits/config.hpp. Specific functionality can be
enabled or disabled by uncommenting or commenting out a particular
#define, listed below.
但是在 R 包的上下文中,我如何激活这个指令?
我尝试用
创建一个config.h
文件
#ifndef CONFIG_LOADED
#define CONFIG_LOADED
#define ARMA_NO_DEBUG
#endif
然后将其包含在我的 /src
文件夹的 every .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 ...
}
然后其他人只是
#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()
这里的顺序很重要。此 #define
语句必须包含在 之前 #include<RcppArmadillo.h>
一个例子:
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:
字段中指定 RcppArmadillo
和 Rcpp
,并在 Imports:
字段中包含 Rcpp
。您必须至少从 Rcpp
导入一个函数(最好是:evalCpp
)。
例如描述 必须有:
Imports: Rcpp (>= 0.12.15)
LinkingTo: Rcpp, RcppArmadillo
我想在访问 RcppArmadillo 中的矩阵元素时禁用边界检查。
Armadillo 的文档说
Armadillo can be configured via editing the file include/armadillo_bits/config.hpp. Specific functionality can be enabled or disabled by uncommenting or commenting out a particular #define, listed below.
但是在 R 包的上下文中,我如何激活这个指令?
我尝试用
创建一个config.h
文件
#ifndef CONFIG_LOADED
#define CONFIG_LOADED
#define ARMA_NO_DEBUG
#endif
然后将其包含在我的 /src
文件夹的 every .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 ...
}
然后其他人只是
#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()
这里的顺序很重要。此 #define
语句必须包含在 之前 #include<RcppArmadillo.h>
一个例子:
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:
字段中指定 RcppArmadillo
和 Rcpp
,并在 Imports:
字段中包含 Rcpp
。您必须至少从 Rcpp
导入一个函数(最好是:evalCpp
)。
例如描述 必须有:
Imports: Rcpp (>= 0.12.15)
LinkingTo: Rcpp, RcppArmadillo