To my knowledge, the results obtained from parallel computations are expected to be unordered. Does foreach perform any operations by default to ensure that the order of results matches the order of a regular for loop (since it defaults to inorder=TRUE)? However, in the example below, even when I set the inorder=FALSE parameter, why am I still getting ordered results?
library(foreach)
library(doParallel)
cl <- makeCluster(3)
registerDoParallel(cl)
temp = foreach(i=4000:1, .combine='c', .inorder=FALSE) %dopar% {
i
}
temp
stopCluster(cl)
The foreach package in R provides a convenient way to perform parallel computations, and by default, it returns results in the order of the iteration. This is because the default value for the .inorder argument is set to TRUE.
In your example, even when you explicitly set .inorder=FALSE, you might still observe ordered results. This behavior can occur due to the specific characteristics of your parallel backend (in this case, doParallel with a cluster of three workers).
The ordering of results in parallel computation is influenced by the communication and synchronization among the parallel workers. Some parallel backends may maintain partial order to simplify the combining process, especially when using a combination function like c that is sensitive to order.
If you want to ensure that the results are not ordered, you might consider using the future package with plan(multisession) for parallel processing. The future package allows for more control over parallel processing, and you can explicitly set the order of the results using the .ordered argument. Here's an example: