首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NumericMatrix在RcppParallel包中不被识别为类型

NumericMatrix在RcppParallel包中不被识别为类型
EN

Stack Overflow用户
提问于 2016-07-06 00:07:29
回答 2查看 866关注 0票数 1

我正在学习如何在我的工作中使用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 input,NumericMatrix output)

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

马克瓦尔斯

代码语言:javascript
复制
PKG_LIBS += $(shell ${R_HOME}/bin/Rscript -e "RcppParallel::RcppParallelLibs()")

Makevars.win

代码语言:javascript
复制
PKG_CXXFLAGS += -DRCPP_PARALLEL_USE_TBB=1
PKG_LIBS += $(shell "${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" \
              -e "RcppParallel::RcppParallelLibs()")

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

代码语言:javascript
复制
#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;
}

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-07-06 18:52:23

NumericMatrix类由Rcpp提供,因此您需要通过使用

代码语言:javascript
复制
using namespace Rcpp;

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

代码语言:javascript
复制
Rcpp::NumericMatrix

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

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

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

票数 5
EN

Stack Overflow用户

发布于 2020-09-19 14:07:24

代码语言:javascript
复制
#include <RcppParallel.h>
using namespace RcppParallel;

用下面的行来修补您的代码,而不是在代码中使用上面的行。您还没有包含Rcpp名称空间,这对我来说是有效的。

代码语言:javascript
复制
// [[Rcpp::depends(RcppParallel)]]
#include <Rcpp.h>
#include <RcppParallel.h>
using namespace Rcpp;
using namespace RcppParallel;

您还没有提取Rcpp命名空间,这在空间NumericVector或NumericMatrix的编译过程中是必需的。

所以,工作代码将是

代码语言:javascript
复制
// [[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;
  }

然而,我得到了错误的“没有匹配的函数调用转换”,如果你知道答案可能是,请回答。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38214693

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档