【问题标题】:doParallel performance on a tensor in RR中张量的doParallel性能
【发布时间】:2018-11-20 22:25:46
【问题描述】:

我需要对张量执行一些操作,并且我想让这个并行。 考虑以下示例:

# first part without doParallel

N = 8192
M = 128
F = 64

ma <- function(x,n=5){filter(x,rep(1/n,n), sides=2)}


m <- array(rexp(N*M*F), dim=c(N,M,F))

new_m <- array(0, dim=c(N,M,F))

system.time ( for(i in 1:N) {
        for(j in 1:F) {
            ma_r <- ma(m[i,,j],2)
            ma_r <- c(ma_r[-length(ma_r)], ma_r[(length(ma_r)-1)])
            new_m[i,,j] <- ma_r       
        }
    }
)

这在我的笔记本电脑上大约需要 38 秒。 以下是doParallel:

# second part with doParallel

library(doParallel)  
no_cores <- detectCores() - 1  
cl <- makeCluster(no_cores, type="FORK")  
registerDoParallel(cl)


calcMat <- function(x){

    n <- dim(x)[1]
    m <- dim(x)[2]

    new_x <- matrix(0, nrow=n, ncol=m)

    for(j in 1:ncol(x)) {
        ma_r <- ma(x[,j],2)
        ma_r <- c(ma_r[-length(ma_r)], ma_r[(length(ma_r)-1)])
        new_x[,j] <- ma_r       
    }

    return(new_x)

}


system.time ( a_list <- foreach(i=1:N) %dopar% {
    m_m <- m[i,,]
    new_m_m <- calcMat(m_m)
 }
)


Y <- array(unlist(a_list), dim = c(nrow(a_list[[1]]), ncol(a_list[[1]]), length(a_list)))
Y <- aperm(Y, c(3,1,2))


stopCluster(cl) 

第二个大约需要 36 秒。 所以我看不到时间上有任何改善。 有谁知道这是什么原因?

【问题讨论】:

    标签: r performance tensor doparallel


    【解决方案1】:

    当您想要使用并行化时,您需要注意某些事项。 第一个是由于通信和可能的序列化而产生的开销。 作为一个非常粗略的例子, 考虑以下几点:

    num_cores <- 2L
    cl <- makeCluster(num_cores, type="FORK")
    registerDoParallel(cl)
    
    exec_time <- system.time({
        a_list <- foreach(i=1L:2L) %dopar% {
            system.time({
                m_m <- m[i,,]
                new_m_m <- calcMat(m_m)
            })
        }
    })
    

    在我的系统中, exec_time 显示经过的时间为 1.264 秒, 而a_list 中的经过时间分别显示 0.003 秒。 因此,以一种非常简单的方式,我们可以说 99.7% 的执行时间是开销。 这与task granularity 有关。 不同类型的任务受益于不同类型的粒度。 在你的情况下, 您可以以粗略的方式分块您的任务。 这基本上意味着您以减少通信开销的方式对任务数量进行分组:

    chunks <- splitIndices(N, num_cores)
    str(chunks)
    List of 2
     $ : int [1:4096] 1 2 3 4 5 6 7 8 9 10 ...
     $ : int [1:4096] 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 ...
    

    每个块都有几个任务的索引, 所以你需要适当地修改你的代码:

    exec_time_chunking <- system.time({
        a_list <- foreach(chunk=chunks, .combine=c) %dopar% {
            lapply(chunk, function(i) {
                m_m <- m[i,,]
                calcMat(m_m)
            })
        }
    })
    

    以上在我的系统中在 17.978 秒内完成, 使用 2 个并行工作器。

    编辑:作为旁注, 我认为通常没有充分的理由将并行工作人员的数量设置为detectCores() - 1L, 由于主 R 进程必须等待所有并行工作人员完成, 但也许你还有其他原因, 也许保持系统响应能力。

    【讨论】:

    • 另外,您可能想查看bigmemory package。
    • 这是一个 3 维数组,我不明白 bigmemory 包对矩阵有多大用处。
    • 我已经测试了您的解决方案并且它有效。我可以节省 30% 的时间。但是,我希望有更大的改进。还有其他想法吗?
    • 不适用于foreach。如果性能很关键,您应该考虑将您的逻辑(从没有doParallel 的第一个解决方案)迁移到C++,使用Rcpp 或者RcppParallel。
    • 您确定要使用所需的 num_cores 值创建 chunks 吗?我确实认为如果你有 2 个以上的内核,你应该能够节省 30% 以上。
    【解决方案2】:

    刚刚注意到,如果您将集群类型设置为“SOCK”,您的代码可以正常工作

    cl <- makeCluster(numberofcores, type = "SOCK")
    

    注意:在 Windows 上这不起作用,我使用了 doSNOW 包(发现它在多个操作系统上具有更好的兼容性)

    以下运行得更快

    library(parallel)
    library(doSNOW)
    
    numberofcores = detectCores()  # review what number of cores does for your environment
    
    cl <- makeCluster(numberofcores, type = "SOCK")
    # Register cluster so that caret will know to train in parallel.
    registerDoSNOW(cl)
    
    system.time ( foreach(i = 1:N) %dopar% {
      for(j in 1:F)  {
        ma_r <- ma(m[i,,j],2)
        ma_r <- c(ma_r[-length(ma_r)], ma_r[(length(ma_r)-1)])
        new_m[i,,j] <- ma_r       
      }
    }
    )
    
    stopCluster(cl) 
    

    【讨论】:

    • 使用 foreach(i=1:N,.packages = c('foreach','doParallel')) 而不是简单的 for 循环。
    猜你喜欢
    • 2015-05-13
    • 2016-10-08
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2019-07-08
    • 1970-01-01
    • 2021-10-04
    相关资源
    最近更新 更多