创建具有其他Rcpp包依赖性的简单Rcpp包

问题描述 投票:0回答:1

我正在尝试通过使用foreach来提高循环计算速度,但是我在该循环内部定义了一个简单的Rcpp函数。我将Rcpp函数保存为mproduct.cpp,然后使用[]调出该函数

sourceCpp("mproduct.cpp")

Rcpp函数是一个简单的函数,它将在C ++中执行矩阵乘积:

// [[Rcpp::depends(RcppArmadillo, RcppEigen)]]

#include <RcppArmadillo.h>
#include <RcppEigen.h>

// [[Rcpp::export]]
SEXP MP(const Eigen::Map<Eigen::MatrixXd> A, Eigen::Map<Eigen::MatrixXd> B){
  Eigen::MatrixXd C = A * B;
  return Rcpp::wrap(C);
}

因此,Rcpp文件中的函数为MP,是指矩阵乘积。我需要执行以下foreach循环(我简化了说明代码):

foreach(j=1:n, .package='Rcpp',.noexport= c("mproduct.cpp"),.combine=rbind)%dopar%{
n=1000000
A<-matrix(rnorm(n,1000,1000))
B<-matrix(rnorm(n,1000,1000))
S<-MP(A,B)
return(S)
} 

由于矩阵A和B的大小很大,所以我要使用foreach来减轻计算成本。

但是,上面的代码不起作用,因为它为我提供了错误消息:

task 1 failed - "NULL value passed as symbol address"

我添加.noexport= c("mproduct.cpp")的原因是要遵循解决类似问题(Can't run Rcpp function in foreach - "NULL value passed as symbol address")的人员的一些建议。但这不解决我的问题。

因此,我尝试将Rcpp函数安装为库。我使用了以下代码:

Rcpp.package.skeleton('mp',cpp_files = "<my working directory>")

但是它向我返回警告消息:

The following packages are referenced using Rcpp::depends attributes however are not listed in the Depends, Imports or LinkingTo fields of the package DESCRIPTION file: RcppArmadillo, RcppEigen 

所以当我尝试使用安装程序包时

install.packages("<my working directory>",repos = NULL,type='source')

我收到警告消息:

Error in untar2(tarfile, files, list, exdir, restore_times) : 
  incomplete block on file
In R CMD INSTALL
Warning in install.packages :
  installation of package ‘C:/Users/Lenovo/Documents/mproduct.cpp’ had non-zero exit status

所以有人可以帮我解决以下问题的方法:1)将foreach与Rcpp函数MP一起使用,或2)将Rcpp文件作为软件包安装?

非常感谢大家。

我正在尝试通过使用foreach来提高循环计算速度,但是我在该循环内部定义了一个简单的Rcpp函数。我将Rcpp函数保存为mproduct.cpp,然后调用了...

foreach package rcpp
1个回答
0
投票

第一步是确保您正在优化正确的东西。对于我来说,not

© www.soinside.com 2019 - 2024. All rights reserved.