[英]NumericMatrix not recognized as a type in RcppParallel Package
我正在学习在我的工作中使用 RcppParallel,并试图安装一个用 Rcpp.package.skeleton() 制作的简单包。 该包包含三个源文件,Rcpp 的 HelloWorld (rcpp_hello_world.cpp) 和在 RcppParallel 网站 ( http://gallery.rcpp.org/articles/parallel-matrix-transform/ ) 中找到的矩阵变换函数的两个版本。 串行版本 (matrixSqrt.cpp) 和并行版本 (parallelMatrixSqrt.cpp)。 此外,我对描述和命名空间文件进行了必要的添加,并使用建议的行制作了 Makevars 和 Makevars.win。
问题是,当我尝试安装该软件包时,出现以下错误:
parallelMatrixSqrt.cpp:14:21: 错误:'NumericMatrix' 未命名类型 SquareRoot(const NumericMatrix 输入,NumericMatrix 输出)
不知道是不是链接器的问题。 Makevars 文件如下所示:
Makevars
PKG_LIBS += $(shell ${R_HOME}/bin/Rscript -e "RcppParallel::RcppParallelLibs()")
Makevars.win
PKG_CXXFLAGS += -DRCPP_PARALLEL_USE_TBB=1
PKG_LIBS += $(shell "${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" \
-e "RcppParallel::RcppParallelLibs()")
编辑:这就是 parallelMatrixSqrt.cpp 的样子
#include <RcppParallel.h>
using namespace RcppParallel;
struct SquareRoot : public Worker
{
// source matrix
const RMatrix<double> input;
// destination matrix
RMatrix<double> output;
// initialize with source and destination
SquareRoot(const NumericMatrix input, NumericMatrix output)
: input(input), output(output) {}
// take the square root of the range of elements requested
void operator()(std::size_t begin, std::size_t end) {
std::transform(input.begin() + begin,
input.begin() + end,
output.begin() + begin,
::sqrt);
}
};
// [[Rcpp::export]]
NumericMatrix parallelMatrixSqrt(NumericMatrix x) {
// allocate the output matrix
NumericMatrix output(x.nrow(), x.ncol());
// SquareRoot functor (pass input and output matrixes)
SquareRoot squareRoot(x, output);
// call parallelFor to do the work
parallelFor(0, x.length(), squareRoot);
// return the output matrix
return output;
}
谢谢
NumericMatrix
类由Rcpp
提供,因此您需要通过拉入 Rcpp 命名空间来访问它
using namespace Rcpp;
或显式前缀命名空间名称,例如
Rcpp::NumericMatrix
请注意,警告 re:避免使用 R/Rcpp API 意味着避免在RcppParallel::Worker
函数的定义中使用它们。 您想避免在并行上下文中使用 R/Rcpp API 的首要原因是这些例程可能:
longjmp
(如果我理解正确,这是 C++ 程序中未定义行为的允许结果) 您通常可以从Rcpp
对象构造您的Worker
对象,但为了安全起见,您通常希望使用本地RcppParallel::RMatrix<T>
对象存储关联数据,因为该对象“更安全”,因为它只提供在并行上下文中使用是安全的——特别是,它提供了迭代器,允许您将它与 C++ STL 一起使用,这在许多场景中应该足够了。
#include <RcppParallel.h>
using namespace RcppParallel;
使用以下几行而不是代码中的上述几行修补您的代码。 您尚未包含 Rcpp 名称空间,这对我有用。
// [[Rcpp::depends(RcppParallel)]]
#include <Rcpp.h>
#include <RcppParallel.h>
using namespace Rcpp;
using namespace RcppParallel;
您还没有拉取 Rcpp 命名空间,这将在空间 NumericVector 或 NumericMatrix 的编译期间需要。
所以,工作代码将是
// [[Rcpp::depends(RcppParallel)]]
#include <Rcpp.h>
#include <RcppParallel.h>
#include <limits>
using namespace Rcpp;
using namespace RcppParallel;
struct SquareRoot : public Worker
{
// source matrix
const RMatrix<double> input;
// destination matrix
RMatrix<double> output;
// initialize with source and destination
SquareRoot(const NumericMatrix input, NumericMatrix output)
: input(input), output(output) {}
// take the square root of the range of elements requested
void operator()(std::size_t begin, std::size_t end) {
std::transform(input.begin() + begin,
input.begin() + end,
output.begin() + begin,
::sqrt);
}
};
// [[Rcpp::export]]
NumericMatrix parallelMatrixSqrt(NumericMatrix x) {
// allocate the output matrix
NumericMatrix output(x.nrow(), x.ncol());
// SquareRoot functor (pass input and output matrixes)
SquareRoot squareRoot(x, output);
// call parallelFor to do the work
parallelFor(0, x.length(), squareRoot);
// return the output matrix
return output;
}
但是,我收到“没有匹配的函数调用转换”的错误,如果您知道答案可能是,请回复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.