0

I am stuck with an issue when trying to parallelize a function that I have coded. The function contains several for loops within for loops (which is required to fill multiple arrays with data).

I have try to code up a stupid small example that shows my issue:

result <- foreach(i = 1:3)%dopar%{
  x <- array(0,c(4,1))
    for(j in nrow(x)){
      y<-array(0,c(4,1))
      for(k in nrow(y)){
        y[k]<-1
      }
      x[j]<-5
    }
    c(y,x)
}

In case of the small example above, the result is

> result
[[1]]
[1] 0 0 0 1 0 0 0 5

[[2]]
[1] 0 0 0 1 0 0 0 5

[[3]]
[1] 0 0 0 1 0 0 0 5

where essentially only the last element of y and x is filled with 1 and 5, respectively.

Now, in my real code, the many for-loops are within a function and I would like to parallelize this function, i.e., run the function a few hundred times, where each run is, in theory, completely independend of the other runs.

Does this have to do with scoping issues when the parallelization happens? Can somebody help me on this?

Also, what would be a way of fixing this?

1
  • Your function above completes and you never say what is broken which leaves us to wonder what different output you're looking for that would be the 'fix'.
    – Chris
    Commented Mar 20 at 2:51

0

Browse other questions tagged or ask your own question.