Consider the following function definitions
library(doParallel)
f_print <- function(x)
{
print(x)
}
f_foreach <- function(l)
{
foreach (i=l) %do%
{
f_print(i)
}
}
f_foreach_parallel <- function(l)
{
doParallel::registerDoParallel(1)
foreach (i=l) %dopar%
{
f_print(i)
}
}
Function use :
> f_foreach(c(1,2))
[1] 1
[1] 2
[[1]]
[1] 1
[[2]]
[1] 2
> f_foreach_parallel(c(1,2))
Show Traceback
Rerun with Debug
Error in { :
task 1 failed - "impossible de trouver la fonction "f_print""
[Error: could not find function f_print]
>
Can you help explain why the f_print()
is not visible when parallelism is involved in foreach
? How can we use f_print()
in this paralleled foreach
?Any documentations related to this point ?