[R] "Safe" use of iterator (package iterators)
Doran, Harold
HDoran at air.org
Fri Dec 9 18:15:38 CET 2016
I believe I now see the light vis-à-vis iterators when combined with foreach() calls in R. I have now been able to reduce computational workload to minutes instead of hours. I want to verify that the way I am using them is "safe". By safe I mean does the iterator traverse elements in the same way as I have below in my toy example to illustrate what I mean.
In the first "traditional" example, I have only one index variable for the loop and so I know that the same list in r1 and r2 are always being grabbed. That is, in iteration 1 it is guaranteed to use r1[[1]] + r2[[1]].
In the example that uses the iterators, is this also guaranteed even though I now have two iterator objects? That is, will the index for element i always be the same as the index for element j when using this across many different cores?
It seems to be true and in all my test cases so far I am seeing it to be true. But, that could be just luck, so I wonder if there is a condition under which that would NOT be true.
Thank you
Harold
library(foreach)
library(doParallel)
cl <- makeCluster(2)
registerDoParallel(cl)
### Create random data
r1 <- vector("list", 20)
for(i in 1:20){
r1[[i]] <- rnorm(10)
}
### Create random data
r2 <- vector("list", 20)
for(i in 1:20){
r2[[i]] <- rnorm(10)
}
### Use a for loop traditionally
result1 <- vector("list", 20)
for(i in 1:20){
result1[[i]] <- r1[[i]] + r2[[i]]
}
### Use iterators
itx1 <- iter(r1)
itx2 <- iter(r2)
result2 <- foreach(i = itx1, j = itx2) %dopar% {
i + j
}
all.equal(result1, result2)
More information about the R-help
mailing list