Rcpp 并行化返回 XPtr 的函数? [英] Rcpp parallelize functions that return XPtr?
问题描述
以XPtr
函数为例:
test.cpp
#include <Rcpp.h>
// [[Rcpp::export]]
SEXP funx()
{
/* creating a pointer to a vector<int> */
std::vector<int>* v = new std::vector<int> ;
v->push_back( 1 ) ;
v->push_back( 2 ) ;
/* wrap the pointer as an external pointer */
/* this automatically protected the external pointer from R garbage
collection until p goes out of scope. */
Rcpp::XPtr< std::vector<int> > p(v, true) ;
/* return it back to R, since p goes out of scope after the return
the external pointer is no more protected by p, but it gets
protected by being on the R side */
return( p ) ;
}
R
library(Rcpp)
sourceCpp("test.cpp")
xp <- funx()
xp
<pointer: 0x9618cc0>
但是如果我尝试并行化,我会得到空指针
But if I try to parallelize this I get null pointers
library(parallel)
out <- mclapply(1:2, function(x) funx())
out
[[1]]
<pointer: (nil)>
[[2]]
<pointer: (nil)>
有没有可能实现这种功能?
Is it possible to achieve this kind of functionality?
编辑
值得注意的是,尽管存在重复问题,但似乎没有真正解决此问题的方法.根据我现在的理解,XPtr
不能是多线程的.所以基本上这不能在 R 中完成.
It is worth noting that despite a duplicate question there appears to be no true solution to this problem. From what I understand now, an XPtr
is not able to be multi-threaded. So essentially this cannot be done in R.
例如,当我将函数放入包 test
并尝试使用 snow
时,它仍然无法返回指针.
For example, when I put the function inside package test
and try to use snow
it still fails to return the pointers.
library(test)
library(snow)
fun <- function(){
library(test)
test:::funx()
}
cl <- makeCluster(2, type = "SOCK")
clusterExport(cl, 'fun')
clusterCall(cl, fun)
[[1]]
<pointer: (nil)>
[[2]]
<pointer: (nil)>
推荐答案
关于
有没有可能实现这种功能?
Is it possible to achieve this kind of functionality?
我会说答案是非常坚决的不",因为 搏击俱乐部的第一条规则 在这里适用:您只是不能仅仅通过希望它能够工作来并行化底层 R 实例.像 RcppParallel 这样的包对于将非 R 数据结构用于多线程非常小心工作.
I would say the answer is a pretty firm 'nope' as the First Rule of Fight Club applies here: you simply cannot parallelise the underlying R instance merely by hoping it would work. Packages like RcppParallel are very careful about using non-R data structures for multithreaded work.
我可能过于悲观,但我会将集合级别"更深一层,并且只将其聚合结果返回给 R.
I may be too pessimistic but I would place the 'collection level' one level deeper, and only return its aggregated result to R.
这篇关于Rcpp 并行化返回 XPtr 的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!