NumericMatrix 未被识别为 RcppParallel 包中的类型

Alp*_*ess 1 c++ parallel-processing r rcpp r-package

我正在学习在工作中使用 RcppParallel,并尝试安装用 Rcpp.package.sculpture() 制作的简单包。该包包含三个源文件,Rcpp 的 HelloWorld (rcpp_hello_world.cpp) 和 RcppParallel 网站 ( http://gallery.rcpp.org/articles/parallel-matrix-transform ) 中找到的两个版本的矩阵变换函数/)。串行版本 (matrixSqrt.cpp) 和并行版本 (parallelMatrixSqrt.cpp)。另外,我对描述和命名空间文件进行了必要的添加,并使用建议的行创建了 Makevars 和 Makevars.win。

\n\n

问题是,当我尝试安装该软件包时,出现以下错误:

\n\n
\n

parallelMatrixSqrt.cpp:14:21:错误:\xe2\x80\x98NumericMatrix\xe2\x80\x99 未命名类型\n SquareRoot(const NumericMatrix 输入,NumericMatrix 输出)

\n
\n\n

不知道是不是链接器的问题。Makevars 文件如下所示:

\n\n

马克瓦斯

\n\n
PKG_LIBS += $(shell ${R_HOME}/bin/Rscript -e "RcppParallel::RcppParallelLibs()")\n
Run Code Online (Sandbox Code Playgroud)\n\n

Makevars.win

\n\n
PKG_CXXFLAGS += -DRCPP_PARALLEL_USE_TBB=1\nPKG_LIBS += $(shell "${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" \\\n              -e "RcppParallel::RcppParallelLibs()")\n
Run Code Online (Sandbox Code Playgroud)\n\n

编辑:\n这就是parallelMatrixSqrt.cpp 的样子

\n\n
#include <RcppParallel.h>\nusing namespace RcppParallel;\n\nstruct SquareRoot : public Worker\n{\n   // source matrix\n   const RMatrix<double> input;\n\n   // destination matrix\n   RMatrix<double> output;\n\n   // initialize with source and destination\n   SquareRoot(const NumericMatrix input, NumericMatrix output) \n      : input(input), output(output) {}\n\n   // take the square root of the range of elements requested\n   void operator()(std::size_t begin, std::size_t end) {\n      std::transform(input.begin() + begin, \n                     input.begin() + end, \n                     output.begin() + begin, \n                     ::sqrt);\n   }\n};\n\n// [[Rcpp::export]]\nNumericMatrix parallelMatrixSqrt(NumericMatrix x) {\n\n  // allocate the output matrix\n  NumericMatrix output(x.nrow(), x.ncol());\n\n  // SquareRoot functor (pass input and output matrixes)\n  SquareRoot squareRoot(x, output);\n\n  // call parallelFor to do the work\n  parallelFor(0, x.length(), squareRoot);\n\n  // return the output matrix\n  return output;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

谢谢

\n

Kev*_*hey 5

该类NumericMatrix由 提供Rcpp,因此您需要通过使用以下命令拉入 Rcpp 命名空间来访问它

using namespace Rcpp;
Run Code Online (Sandbox Code Playgroud)

或显式命名空间名称前缀,例如

Rcpp::NumericMatrix
Run Code Online (Sandbox Code Playgroud)

请注意,警告 re:避免使用 R / Rcpp API 意味着避免在函数定义中使用它们RcppParallel::Worker。您希望避免在并行上下文中使用 R / Rcpp API 的首要原因是这些例程可能:

  1. 分配,从而触发 R 垃圾收集器,如果在单独的线程上完成,这将导致大问题;或者
  2. 抛出一个错误,从而导致 alongjmp炸毁整个宇宙(如果我理解正确的话,这是 C++ 程序中未定义行为的允许结果)

您通常可以WorkerRcpp对象构造对象,但为了安全起见,您通常希望将关联数据存储在本地RcppParallel::RMatrix<T>对象中,因为该对象“更安全”,因为它只提供可以在并行上下文中安全使用的例程 -特别是,它提供了迭代器,允许您将其与 C++ STL 一起使用,这在许多情况下应该足够了。