12

I would like to know if / how it is possible to parallelize a while loop in R.

If it is not possible or reasonably considered too difficult to accomplish, I would greatly appreciate an alternative, possibly using a parallel for loop instead.

4
  • It is extremely unlikely that you should be using a while loop in R. Commented Mar 30, 2017 at 14:51
  • Thanks for your reply. Would you mind elaborating as to why? From a conceptual point of view, it seems that I should be using a while loop. What makes a while loop a bad choice for R (as I assume it is not a bad choice in general)? Commented Mar 30, 2017 at 14:56
  • 3
    It’s conceptually impossible to parallelise a generic while loop. Commented Mar 30, 2017 at 15:14
  • Thanks for your reply, the only one so far that actually answers the question posed. Impossible is a strong word, no wonder you had to qualify the statement for it to be valid. But it serves my purposes for now. Commented Mar 30, 2017 at 15:28

1 Answer 1

17

You can use futures with any looping R construct, including for() and while() loops. Futures are implemented in the future package (I'm the author).

It is not clear what condition you want to use for your while loop. That would have helped give a more precise answer. Here is an example where the while condition does not depend on any of the results calculated:

library("listenv")
library("future")
plan(multisession, workers = 2L)

res <- listenv()

ii <- 1L
while (ii <= 5) {
  ## This is evaluated in parallel and will only block
  ## if all workers are busy.
  res[[ii]] %<-% {
    list(iteration = ii, pid = Sys.getpid())
  }
  ii <- ii + 1L
}

## Resolve all futures (blocks if not already finished)
res <- as.list(res)

str(res)
List of 5
 $ :List of 2
  ..$ iteration: int 1
  ..$ pid      : int 15606
 $ :List of 2
  ..$ iteration: int 2
  ..$ pid      : int 15608
 $ :List of 2
  ..$ iteration: int 3
  ..$ pid      : int 15609
 $ :List of 2
  ..$ iteration: int 4
  ..$ pid      : int 15610
 $ :List of 2
  ..$ iteration: int 5
  ..$ pid      : int 15611

If you want your while condition to depend on the outcome of one or more of the parallel results (in res[[ii]]) then things become much more complicated, because then you need to figure out what should happen if the future (= parallel task) is still not resolved; should you check back, say, 5 iterations later, or should you wait. That's a design issue of the parallel algorithm and not how to implement. Anything is possible.

PS. I don't understand the downvotes this question received. It could be that it's because your question is poorly phrased / not very clear - remember to try as far as possible to "help the helper help you" by being as precise as possible. If the downvotes are because of the while loop, I (obviously) beg to disagree.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much Henrik! Unfortunately the terminating condition of my while loop depends on calculated results, so I fear parallelization is quite difficult, but no biggie if it can't be done. Admittedly my question should have been clearer, for example by stating the type of terminating condition employed. The reason for such a vague question is that I had no idea the type of terminating condition could make such a big difference (I'm new to parallelization, and learning on my own). As for the while loop, as far as I know it is a perfectly valid loop type in R (and probably in any language).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.