Lonely_rifle
Lonely_rifle

Reputation: 43

foreach parallel computing using external packages

I created a package myself and try to apply it in parallel computing.
Suppose the package contains function1 and function2.
My code is

cl = makeCluster(2)
registerDoParallel(cl)

foreach(i=1:N,.packages='mypackage') %dopar% {
   res = function1(i)
   res
}
stopCluster(cl)

Then there is an error, the function1 is in mypackage.

Error in { : task 1 failed - "could not find function "function1""

However, if I change the code by adding

.export = 'function1'

The error disappears.

Thank you to anyone who can explain this.

Upvotes: 3

Views: 692

Answers (1)

akrun
akrun

Reputation: 886938

Either use .export as OP mentioned or specify the function as packageName::functionName

cl = makeCluster(2)
registerDoParallel(cl)

foreach(i=1:N,.packages='mypackage') %dopar% {
   res = mypackage::function1(i)
   res
}
stopCluster(cl)

Upvotes: 1

Related Questions