老问题,但是我在浏览顶部的 Rcpp 标签时偶然发现了它,所以也许这个答案仍然有用。
我认为当您编写的代码完全调试并执行您想要的操作时,Dirk 的答案是正确的,但是为示例中的一小段代码编写新包可能会很麻烦。您可以做的是导出代码块,导出编译源代码并运行帮助程序的“帮助程序”函数。这将使 CXX 函数可用,然后使用另一个辅助函数来调用它。例如:
# Snow must still be installed, but this functionality is now in "parallel" which ships with base r.
library(parallel)
# Keep your source as an object
src1 <- '
Rcpp::NumericMatrix xbem(xbe);
int nrows = xbem.nrow();
Rcpp::NumericVector gv(g);
for (int i = 1; i < nrows; i++) {
xbem(i,_) = xbem(i-1,_) * gv[0] + xbem(i,_);
}
return xbem;
'
# Save the signature
sig <- signature(xbe = "numeric", g="numeric")
# make a function that compiles the source, then assigns the compiled function
# to the global environment
c.inline <- function(name, sig, src){
library(Rcpp)
funCXX <- inline::cxxfunction(sig = sig, body = src, plugin="Rcpp")
assign(name, funCXX, envir=.GlobalEnv)
}
# and the function which retrieves and calls this newly-compiled function
c.namecall <- function(name,...){
funCXX <- get(name)
funCXX(...)
}
# Keep your example matrix
A <- matrix(rnorm(400), 20,20)
# What are we calling the compiled funciton?
fxname <- "TestCXX"
## Parallel
cl <- makeCluster(2, type = "PSOCK")
# Export all the pieces
clusterExport(cl, c("src1","c.inline","A","fxname"))
# Call the compiler function
clusterCall(cl, c.inline, name=fxname, sig=sig, src=src1)
# Notice how the function now named "TestCXX" is available in the environment
# of every node?
clusterCall(cl, ls, envir=.GlobalEnv)
# Call the function through our wrapper
clusterCall(cl, c.namecall, name=fxname, A, 0.5)
# Works with my testing
我编写了一个包ctools(无耻的自我推销),它包含了用于集群计算的并行和 Rhpc 包中的许多功能,包括 PSOCK 和 MPI。我已经有一个名为“c.sourceCpp”的函数,它在每个节点上调用“Rcpp::sourceCpp”,其方式与上述方式大致相同。我将添加一个“c.inlineCpp”,现在我看到了它的用处。
编辑:
鉴于 Coatless 的评论,Rcpp::cppFunction()
事实上c.inline
这里否定了对助手的需要,尽管c.namecall
仍然需要。
src2 <- '
NumericMatrix TestCpp(NumericMatrix xbe, int g){
NumericMatrix xbem(xbe);
int nrows = xbem.nrow();
NumericVector gv(g);
for (int i = 1; i < nrows; i++) {
xbem(i,_) = xbem(i-1,_) * gv[0] + xbem(i,_);
}
return xbem;
}
'
clusterCall(cl, Rcpp::cppFunction, code=src2, env=.GlobalEnv)
# Call the function through our wrapper
clusterCall(cl, c.namecall, name="TestCpp", A, 0.5)