我正在学习如何在我的工作中使用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文件如下所示:
马克瓦尔斯
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;
}谢谢
发布于 2016-07-06 18:52:23
NumericMatrix类由Rcpp提供,因此您需要通过使用
using namespace Rcpp;或显式前缀命名空间名称,例如
Rcpp::NumericMatrix注意,警告re:避免使用R/ Rcpp意味着避免在RcppParallel::Worker函数的定义中使用R/Rcpp。您希望避免在并行上下文中使用R/ Rcpp的主要原因是,这些例程可能:
longjmp (如果我正确理解,这是C++程序中未定义行为允许的结果)。您通常可以从Worker对象构造您的Rcpp对象,但为了安全起见,您通常希望使用本地RcppParallel::RMatrix<T>对象来存储相关数据,因为该对象“更安全”,因为它只提供了在并行上下文中安全使用的例程--特别是,它提供了允许您将其与C++ STL一起使用的迭代器,这在许多情况下都是足够的。
发布于 2020-09-19 14:07:24
#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;
}然而,我得到了错误的“没有匹配的函数调用转换”,如果你知道答案可能是,请回答。
https://stackoverflow.com/questions/38214693
复制相似问题