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

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



我不知道这是不是链接器问题。 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

注意警告:避免使用R / Rcpp API意味着避免在RcppParallel::Worker函数的定义中使用它们。您要避免在并行上下文中使用R / Rcpp API的首要原因是这些例程可能:
  • 分配,并因此触发R垃圾收集器,如果在单独的线程上完成将导致大问题;或
  • 引发错误,并因此导致炸毁整个宇宙的longjmp(如果我理解正确,则是C++程序中未定义行为的允许结果)

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

    关于c++ - NumericMatrix在RcppParallel包中未被识别为类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38214693/

    10-09 02:46