0
rm(list=ls())
library(doParallel)
dyn.load("bar.so")
foo <- function(x) {
        if (!is.numeric(x))
                stop("arg x must be numeric")
        out <- .Fortran("bar",
                n=as.integer(length(x)),
                x=as.double(x))
        return(out$x)
}
foo2 <- function(y) {
        y2 <- y * y
        return(y2)
}
cl <- makeCluster(2)
registerDoParallel(cl)
result <- array(data=0.0,c(5,3))
for(num in 10:14){
  num_list <- c(num, num+3, num-1)
  result[num-9,] <-foo(num_list)
  cat(paste("num = ",num," ", result[num-9,],"\n"))
}
result2 <- array(data=0.0,c(5,3))
for(num in 10:14){
  num_list <- c(num, num+3, num-1)
  result2[num-9,] <-foo2(num_list)
  cat(paste("num = ",num," ", result2[num-9,],"\n"))
}
result_par <- array(data=0.0,c(5,3))
foreach(num = 10:14, .verbose=TRUE) %dopar%{
  num_list <- c(num, num+3, num-1)
  filename <- paste0("Result_",num)
  result_par[num-9,] <-foo(num_list)
  #result_par[num-9,] <-foo2(num_list)
  write.csv(result_par[num-9,],filename)
}
stopCluster(cl)

The above is some test code that I have written which reproduces the error that I am getting with my real code. The actual Fortran code is long and complicated and would take too long to recode, which is why I'm trying to call it by loading it as a shared object. The shared object was created from the command prompt with the line: R CMD SHLIB bar.f

It works fine until I try to call the Fortran subroutine from within a foreach loop. Then I get the following error:

automatically exporting the following variables from the local environment:
  foo, num_list, result_par 
numValues: 5, numResults: 0, stopped: TRUE
got results for task 1
accumulate got an error result
numValues: 5, numResults: 1, stopped: TRUE
returning status FALSE
got results for task 2
numValues: 5, numResults: 2, stopped: TRUE
returning status FALSE
got results for task 3
numValues: 5, numResults: 3, stopped: TRUE
returning status FALSE
got results for task 4
numValues: 5, numResults: 4, stopped: TRUE
returning status FALSE
got results for task 5
numValues: 5, numResults: 5, stopped: TRUE
not calling combine function due to errors
returning status TRUE
Error in { : 
  task 1 failed - "Fortran symbol name "bar" not in load table"

If I use foo2 rather than foo in the foreach loop, it appears to work fine. So it looks like the issue is something to do with using the shared library.

I am using:

R version 3.3.1 (2016-06-21) -- "Bug in Your Hair" Copyright (C) 2016 The R Foundation for Statistical Computing

Platform: x86_64-pc-linux-gnu (64-bit)

  • Maybe it has something to do with `doParallel`. Have you tried calling function `foo` directly outside of any `foreach`? If you have and that worked correctly then it may have something to do with `doParallel`. – Bhas Dec 07 '17 at 16:13
  • Yes, the function "foo" works fine in a normal for loop. So you are probably right, it is something to do with doParallel. I suspect that it might not be possible to call "foo" from within a foreach loop. – Yvonne Collingham Dec 07 '17 at 16:59

0 Answers0