Search code examples
rprogress-barmclapply

Is there way to track progress on a mclapply?


I love the setting .progress = 'text' in plyr's llply. However, it causes my much anxiety to not know how far along an mclapply (from package multicore) is since list items are sent to various cores and then collated at the end.

I've been outputting messages like *currently in sim_id # ....* but that's not very helpful because it doesn't give me an indicator of what percentage of list items are complete (although it is helpful to know that that my script isn't stuck and moving along).

Can someone suggest other ideas that would allow me to look at my .Rout file and get a sense of progress? I've thought about adding a manual counter but can't see how I would implement that since mclapply must finish processing all list items before it can give out any feedback.


Solution

  • Due to the fact that mclapply spawns multiple processes, one might want to use fifos, pipes, or even sockets. Now consider the following example:

    library(multicore)
    
    finalResult <- local({
        f <- fifo(tempfile(), open="w+b", blocking=T)
        if (inherits(fork(), "masterProcess")) {
            # Child
            progress <- 0.0
            while (progress < 1 && !isIncomplete(f)) {
                msg <- readBin(f, "double")
                progress <- progress + as.numeric(msg)
                cat(sprintf("Progress: %.2f%%\n", progress * 100))
            } 
            exit()
        }
        numJobs <- 100
        result <- mclapply(1:numJobs, function(...) {
            # Dome something fancy here
            # ...
            # Send some progress update
            writeBin(1/numJobs, f)
            # Some arbitrary result
            sample(1000, 1)
        })
        close(f)
        result
    })
    
    cat("Done\n")
    

    Here, a temporary file is used as fifo, and the main process forks a child whose only duty is to report the current progress. The main process continues by calling mclapply where the expression (more precisely, the expression block) that is to be evaluated writes partial progress information to the fifo buffer by means of writeBin.

    As this is only a simple example, you'll probably have to adapt the whole output stuff to your needs. HTH!