Skip to content

Instantly share code, notes, and snippets.

@chrinide
Forked from andrie/foreach-parallel-progressbar.R
Last active August 29, 2015 14:16
Show Gist options
  • Save chrinide/7dd8301f63e45ff2143d to your computer and use it in GitHub Desktop.
Save chrinide/7dd8301f63e45ff2143d to your computer and use it in GitHub Desktop.

Revisions

  1. @andrie andrie created this gist Feb 21, 2015.
    26 changes: 26 additions & 0 deletions foreach-parallel-progressbar.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    library(foreach)
    library(iterators)
    library(doParallel)
    library(tcltk)

    # Choose number of iterations
    n <- 1000

    cl <- makeCluster(8)

    registerDoParallel(cl)

    time3 <- system.time({
    clusterExport(cl, c("n")) # Export max number of iteration to workers
    k <- foreach(i = icount(n), .packages = "tcltk", .combine = c) %dopar% {
    if(!exists("pb")) pb <- tkProgressBar("Parallel task", min=1, max=n)
    setTkProgressBar(pb, i)
    Sys.sleep(0.05)
    log2(i)
    }
    })

    #Stop the cluster
    stopCluster(cl)

    print(time3)