2

I am using doParallel and foreach packages for running my code on multiple cores. My code is something like

fun1 <- function(param1, param2, param3)
{
   do something
   fun2(param4, param5, param6)

   foreach(param4 = 1:length(param1) %dopar% fun2(param4, param5, param6)
   return something
}

fun2 <- function(param4, param5, param6)
{
   do something
   fun3(param7, param8, param9)
   return something
}

fun3 <- function(param7, param8, param9)
{
   do something
   fun4(param10, param11, param12)
   return something
}

fun4 <- function(param10, param11, param12)
{
   do something

   return something
}

Call: fun1(a,b,c)

When I call fun1, the first instance of fun2 is executed successfully, but the instance where I am calling fun2 with foreach gives an error Error in fun2(param4, param5, : task 1 failed - "could not find function "fun2""

Sim101011
  • 305
  • 1
  • 13
  • 2
    Read http://stackoverflow.com/help/mcve – user31264 Dec 20 '16 at 07:38
  • You have to export any functions you wish to use in parallel to the cluster – alexwhitworth Dec 20 '16 at 08:18
  • 2
    Functions you use explicitly are exported to the cluster automatically, but their dependencies are not. So, you need to do this yourself, e.g., using `clusterExport `. For simple functions you could make the function definition also part of the parallel code. For more complex cases, it's often best to create a package. – Roland Dec 20 '16 at 08:36

0 Answers0