clusterExport to single thread in R parallel

前端 未结 1 970
悲&欢浪女
悲&欢浪女 2021-01-15 12:48

I would like to split a large data.frame into chunks and pass each individually to the different members of the cluster.

Something like:



        
相关标签:
1条回答
  • 2021-01-15 13:25

    Here is an example that uses clusterCall inside a for loop to send a different chunk of the data frame to each of the workers:

    library(parallel)
    cl <- makeCluster(detectCores())
    df <- data.frame(a=1:10, b=1:10)
    ix <- splitIndices(nrow(df), length(cl))
    for (i in seq_along(cl)) {
      clusterCall(cl[i], function(d) {
        assign('mydata', d, pos=.GlobalEnv)
        NULL  # don't return any data to the master
      }, df[ix[[i]],,drop=FALSE])
    }
    

    Note that the call to clusterCall is subsetting cl in order to execute the function on a single worker each time through the for loop.

    You can verify that the workers were properly initialized in this example using:

    r <- do.call('rbind', clusterEvalQ(cl, mydata))
    identical(df, r)
    

    There are easier ways to do this, but this example minimizes the memory used by the master and the amount of data sent to each of the workers. This is important when the data frame is very large.

    0 讨论(0)
提交回复
热议问题