NumericMatrix 未被识别为 RcppParallel 包中的类型
NumericMatrix not recognized as a type in RcppParallel Package
我正在学习在我的工作中使用 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 文件如下所示:
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
请注意警告:避免使用 R / Rcpp API 意味着避免在 RcppParallel::Worker
函数的定义中使用它们。您希望避免在并行上下文中使用 R / Rcpp API 的首要原因是这些例程可能:
- 分配,并因此触发 R 垃圾收集器,如果在单独的线程上完成,这将导致大问题;或
- 抛出错误,从而导致
longjmp
爆炸宇宙(如果我理解正确,这是 C++ 程序中未定义行为的允许结果)
您通常可以从 Rcpp
对象构建 Worker
对象,但为了安全起见,您通常希望使用本地 RcppParallel::RMatrix<T>
对象存储关联数据,因为该对象是'safer' 因为它只提供在并行上下文中安全使用的例程——特别是,它提供迭代器,允许您将它与 C++ STL 一起使用,这在许多情况下应该足够了。
#include <RcppParallel.h>
using namespace RcppParallel;
用下面的行而不是上面的行来修补你的代码。您没有包含 Rcpp 名称 space,这对我有用。
// [[Rcpp::depends(RcppParallel)]]
#include <Rcpp.h>
#include <RcppParallel.h>
using namespace Rcpp;
using namespace RcppParallel;
您还没有提取 Rcpp 名称space,编译 space 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;
}
但是,我收到“没有匹配的函数来调用转换”的错误,如果您知道答案可能是,请回复。
我正在学习在我的工作中使用 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 文件如下所示:
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
提供,因此您需要通过使用
using namespace Rcpp;
或明确地为命名空间名称添加前缀,例如
Rcpp::NumericMatrix
请注意警告:避免使用 R / Rcpp API 意味着避免在 RcppParallel::Worker
函数的定义中使用它们。您希望避免在并行上下文中使用 R / Rcpp API 的首要原因是这些例程可能:
- 分配,并因此触发 R 垃圾收集器,如果在单独的线程上完成,这将导致大问题;或
- 抛出错误,从而导致
longjmp
爆炸宇宙(如果我理解正确,这是 C++ 程序中未定义行为的允许结果)
您通常可以从 Rcpp
对象构建 Worker
对象,但为了安全起见,您通常希望使用本地 RcppParallel::RMatrix<T>
对象存储关联数据,因为该对象是'safer' 因为它只提供在并行上下文中安全使用的例程——特别是,它提供迭代器,允许您将它与 C++ STL 一起使用,这在许多情况下应该足够了。
#include <RcppParallel.h>
using namespace RcppParallel;
用下面的行而不是上面的行来修补你的代码。您没有包含 Rcpp 名称 space,这对我有用。
// [[Rcpp::depends(RcppParallel)]]
#include <Rcpp.h>
#include <RcppParallel.h>
using namespace Rcpp;
using namespace RcppParallel;
您还没有提取 Rcpp 名称space,编译 space 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;
}
但是,我收到“没有匹配的函数来调用转换”的错误,如果您知道答案可能是,请回复。