I have a function that contains a loop
myfun = function(z1.d, r, rs){
x = z1.d[,r]
or.d = order(as.vector(x), decreasing=TRUE)[rs]
zz1.d = as.vector(x)
r.l = zz1.d[or.d]
y=vector()
for (i in 1:9)
{
if(i<9) y[i]=mean( x[(x[,r] >= r.l[i] & x[,r] < r.l[i+1]),r] ) else{
y[i] = mean( z1.d[(x >= r.l[9]),r] )}
}
return(y)
}
rs is a numeric vector, z1.d is a zoo and y is also a numeric vector.
When I try to run the function inside a parallel loop:
cls = makePSOCKcluster(8)
registerDoParallel(cls)
rlarger.d.1 = foreach(r=1:dim(z1.d)[2], .combine = "cbind") %dopar% {
myfun(z1.d, r, rs)}
stopCluster(cls)
I get the following error:
Error in { : task 1 failed - "incorrect number of dimensions"
I don't know why, but I realized if I take the loop out of my function it does not give an error.
Also, if I run the exact same code with %do% instead of %dopar% (so not runing in parallel) it works fine (slow but without errors).
EDIT: as requested here is a sample of the parameters:
dim(z1.d)
[1] 8766 107
> z1.d[1:4,1:6]
AU_10092 AU_10622 AU_12038 AU_12046 AU_13017 AU_14015
1966-01-01 23:00:00 NA NA NA 1.816 0 4.573
1966-01-02 23:00:00 NA NA NA 9.614 0 4.064
1966-01-03 23:00:00 0 NA NA 0.000 0 0.000
1966-01-04 23:00:00 0 NA NA 0.000 0 0.000
> rs
[1] 300 250 200 150 100 75 50 30 10
r is defined in the foreach loop
foreach
but usually, when working with parallel cores, variables need to be "send" to the cores environments. In your case, I do not see where you declarez1.d
andrs
in the cores environments. As I said, I dont really knowforeach
but I would use it something like:rlarger.d.1 = foreach(r=1:dim(z1.d)[2], z1.d = z1.d, rs = rs, .combine = "cbind") %dopar% { myfun(z1.d, r, rs)}
. By the way, usually the variable parameter of a function liker
here should be defined first in your parameters functionsmyfun = function(r, z1.d, rs)
. – Elbow