Is there any way to break out of a foreach loop?

前端 未结 6 1194
耶瑟儿~
耶瑟儿~ 2020-12-04 22:29

I am using the R package foreach() with %dopar% to do long (~days) calculations in parallel. I would like the ability to stop the entire set of cal

6条回答
  •  我在风中等你
    2020-12-04 23:05

    Instead of trying to break out of a loop, I write a small file to the disk when I've reached my terminal loop, then have all remaining iterations simply skip based on the existence of that file.

    Checking if a file exists costs us less than a milisecond of computing time.

    # 1.4 seconds to check if a file exists a million times
    system.time(lapply(1:1e6, function(x) file.exists("checker.txt")))
       user  system elapsed 
      1.204   0.233   1.437 
    

    This is great when you don't have a fixed number of iterations or your process can finish before all of the iterations are complete (like a convergence, for example)

    library(foreach)
    
    alist <- foreach(i = 1:5000) %dopar% { 
      if(file.exists("checker.txt")) {
        return(NULL)
      } else {
        if(i = 20) {
          write("", "checker.txt") # write an empty file
        }
        return(i)
      }
    }
    
    file.remove("checker.txt")
    

    The great thing about this is that even if your list is extremely long, if you just unlist() you only get the values.

    > length(alist)
    [1] 5000
    
    > unlist(res)
     [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
    

    Don't bother trying to break, instead, just "skip the rest"!

提交回复
热议问题