简体   繁体   中英

Parallel cpp functions in R?

So I have 2 custom Rcpp functions CustFunc1(x,y) and CustFunc2(a,b) . Both are computationally demanding (thus the c++). My question is there a way in R to run the concurrently with the dopar package? Or can I do a system call to directly access the cpp code and try to do parallel via some command line tools?

Right now the flow is:

result1=CustFunc(x,y) ##Takes 20 minutes
result2=CustFunc(a,b) ## Take 20 minutes

get_both <- function(x) {
foreach(i = seq_along(x)) %dopar% {
result1=CustFunc(x,y)
result2=CustFunc(a,b)
}
}
get_both$result1 == result1 #???
get_both$result2 == result2 ##??

If your functions take on the order of 20 min I would really consider rewriting them using the RcppPaallel package, it isn't too much harder than straight cpp. That way you can use more threads, but run your functions one at a time. When I did this with my own code, I cut my computation time down from about 15 min to less than 2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM