gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:43:09 24 4
gpt4 key购买 nike

我正在学习在我的工作中使用 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。

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

parallelMatrixSqrt.cpp:14:21: error: ‘NumericMatrix’ does not name a type SquareRoot(const NumericMatrix input, NumericMatrix output)

不知道是不是链接器的问题。 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类(class)由 Rcpp 提供,因此您需要通过使用

拉入 Rcpp 命名空间来访问它
using namespace Rcpp;

或显式地为命名空间名称添加前缀,例如

Rcpp::NumericMatrix

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

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

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

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com