2012-07-24 110 views
2

平行R相当新颖快速提问。我有一个计算密集的算法。幸运的是,它可以很容易地分解成片段,以利用multicoresnow。我想知道的是,如果在实践中认为multicoresnow结合使用会被罚款?将多核与雪群结合起来

我想要做的是将我的负载分解为在群集中的多台机器上以及每台机器上运行。我想利用机器上的所有内核。对于这种类型的处理,将积雪与multicore混合是否合理?

+0

(你可以使用包含在'R'> = 2.14.0中'parallel'包,它基于'multicore'和'snow')。我会想到像'cl < - makeCluster(...); clusterEvalQ(cl,{library(parallel); cl < - makeCluster(4); parallel_custom_function < - function(cl,...){...}}',但是我会非常好奇地看到一些有效的代码! – lockedoff 2012-07-24 18:48:52

+0

Segue是一个完美的例子,绝对没有理由不能使用与segue相结合的多核,关键是要分散你的负载,并且理解你在哪里使用开销 – Dave 2012-08-04 18:12:17

+0

install.packages中的警告: package'parallel '不可用(对于R版本3.0.1) – 2013-07-18 14:24:19

回答

1

我已经使用lockoff上面提出的方法,即使用并行程序包在多个具有多个内核的机器上分配令人尴尬的并行工作负载。首先,工作负载分布在所有机器上,然后每台机器的工作负载分布在所有内核上。这种方法的缺点是机器之间没有负载平衡(至少我不知道如何)。

所有加载的r代码应该是相同的,并且位于所有机器(svn)上的相同位置。由于初始化群集需要相当长的一段时间,因此可以通过重新使用创建的群集来改进以下代码。

foo <- function(workload, otherArgumentsForFoo) { 
    source("/home/user/workspace/mycode.R") 
    ... 
} 

distributedFooOnCores <- function(workload) { 
    # Somehow assign a batch number to every record 
    workload$ParBatchNumber = NA 
    # Split the assigned workload into batches according to DistrParNumber 
    batches = by(workload, workload$ParBatchNumber, function(x) x) 

    # Create a cluster with workers on all machines 
    library("parallel") 
    cluster = makeCluster(detectCores(), outfile="distributedFooOnCores.log") 
    batches = parLapply(cluster, batches, foo, otherArgumentsForFoo) 
    stopCluster(cluster) 

    # Merge the resulting batches 
    results = someEmptyDataframe 
    p = 1; 
    for(i in 1:length(batches)){ 
     results[p:(p + nrow(batches[[i]]) - 1), ] = batches[[i]] 
     p = p + nrow(batches[[i]])  
    } 

    # Clean up 
    workload$ParBatchNumber = NULL 
    return(invisible(results)) 
} 

distributedFooOnMachines <- function(workload) { 
    # Somehow assign a batch number to every record 
    workload$DistrBatchNumber = NA 
    # Split the assigned activity into batches according to DistrBatchNumber 
    batches = by(workload, workload$DistrBatchNumber, function(x) x) 

    # Create a cluster with workers on all machines 
    library("parallel") 
    # If makeCluster hangs, please make sure passwordless ssh is configured on all machines 
    cluster = makeCluster(c("machine1", "etc"), master="ub2", user="", outfile="distributedFooOnMachines.log") 
    batches = parLapply(cluster, batches, foo, otherArgumentsForFoo) 
    stopCluster(cluster) 

    # Merge the resulting batches 
    results = someEmptyDataframe 
    p = 1; 
    for(i in 1:length(batches)){ 
     results[p:(p + nrow(batches[[i]]) - 1), ] = batches[[i]] 
     p = p + nrow(batches[[i]])  
    } 

    # Clean up 
    workload$DistrBatchNumber = NULL 
    return(invisible(results)) 
} 

我很感兴趣如何可以改善上述方法。