我目前正在处理一些大型数据集,因此并行化工作流程是唯一的方法。
我需要在开始时将一些包加载到每个线程(即:for(this.thread in threads) { #load some packages }
.
不幸的是,我不知道该怎么做。
以下代码进一步说明了我的问题,我试图magrittr
在 a中使用管道运算符%dopar%
:
.
library(parallel)
library(doParallel)
library(foreach)
library(magrittr)
# Generate some random data and function :
# -----------------------------------------
randomData = runif(10^3)
randomFunction = function(x) {x * (2^x) }
randomData[1] %>% randomFunction #Works
# And now ... The parallel part :
# --------------------------------
myCluster = makeCluster(6)
registerDoParallel(myCluster)
# Test that the do par is up and running:
foreach(i = randomData) %dopar% { i }
# Use magrittr pipe operator:
# Error in { : task 1 failed - "could not find function "%>%""
foreach(i = randomData) %dopar% { i %>% randomFunction }
# Load the library at each loop: (ie: length(data) times !)
# Other than unnecessarily loading the library (length(data) - numberOfThreads) times,
# it works nicely
foreach(i = randomData) %dopar% { library(magrittr); i %>% randomFunction }
# Now try without re-loading:
# Tararaa - (ie: Works nicely)
foreach(i = randomData) %dopar% { i %>% randomFunction }
.
有任何想法吗?