如何在Windows上进行并行化 - 例如?

时间:2014-05-29 05:32:17

标签: r parallel-processing

如何在Windows中使用r的代码并行化代码?包括一个简单的例子。发布这个自我回答的问题,因为这对于工作是相当不愉快的。你会发现并行软件包本身并不起作用,但是软件包可以很好地工作。

6 个答案:

答案 0 :(得分:30)

发布这个是因为这让我想起了血腥的东西。这是r中并行化的一个简单示例,它可以让您测试事情是否适合您并让您走上正确的道路。

library(snow)
z=vector('list',4)
z=1:4
system.time(lapply(z,function(x) Sys.sleep(1)))
cl<-makeCluster(###YOUR NUMBER OF CORES GOES HERE ###,type="SOCK")
system.time(clusterApply(cl, z,function(x) Sys.sleep(1)))
stopCluster(cl)

您还应该使用库doSNOW将foreach注册到snow集群,这将导致许多程序包自动并行化。要注册的命令是registerDoSNOW(cl)cl是来自makeCluster()的返回值),撤消注册的命令是registerDoSEQ()。不要忘记关闭你的集群。

答案 1 :(得分:12)

这对我有用,我使用了包doParallel,需要3行代码:

# process in parallel
library(doParallel) 
cl <- makeCluster(detectCores(), type='PSOCK')
registerDoParallel(cl)

# turn parallel processing off and run sequentially again:
registerDoSEQ()

随机森林的计算从180秒减少到120秒(在具有4核的Windows计算机上)。

答案 2 :(得分:4)

我认为这些图书馆会对您有所帮助:

foreach (facilitates executing the loop in parallel)
doSNOW (I think you already use it)
doMC (multicore functionality of the parallel package)

这些文章也可以帮助你

http://vikparuchuri.com/blog/parallel-r-loops-for-windows-and-linux/

http://www.joyofdata.de/blog/parallel-computing-r-windows-using-dosnow-foreach/

答案 3 :(得分:4)

基于信息here,我能够将以下代码转换为在Windows 7上的R Studio下工作的并行化版本。

原始代码:

#
# Basic elbow plot function
#
wssplot <- function(data, nc=20, seed=1234){
    wss <- (nrow(data)-1)*sum(apply(data,2,var))
    for (i in 2:nc){
        set.seed(seed)
        wss[i] <- sum(kmeans(data, centers=i, iter.max=30)$withinss)}
    plot(1:nc, wss, type="b", xlab="Number of clusters", 
       ylab="Within groups sum of squares")
}

并行化代码:

library("parallel")

workerFunc <- function(nc) {
  set.seed(1234)
  return(sum(kmeans(my_data_frame, centers=nc, iter.max=30)$withinss)) }

num_cores <- detectCores()
cl <- makeCluster(num_cores)
clusterExport(cl, varlist=c("my_data_frame")) 
values <- 1:20 # this represents the "nc" variable in the wssplot function
system.time(
  result <- parLapply(cl, values, workerFunc) )  # paralel execution, with time wrapper
stopCluster(cl)
plot(values, unlist(result), type="b", xlab="Number of clusters", ylab="Within groups sum of squares")

并不是说它完美甚至是最好的,只是一个初学者证明并行似乎在Windows下运行。希望它有所帮助。

答案 4 :(得分:0)

我在这里发布了跨平台的答案,因为我发现的所有其他答案对于我需要完成的工作过于复杂。我以一个示例为例,在其中阅读excel工作簿的所有表。

# read in the spreadsheet 
parallel_read <- function(file){

    # detect available cores and use 70%
    numCores  = round(parallel::detectCores() * .70)

    # check if os is windows and use parLapply
    if(.Platform$OS.type == "windows") {

        cl <- makePSOCKcluster(numCores)
        parLapply(cl, file, readxl::read_excel)
        stopCluster(cl)

        return(dfs)

    # if not Windows use mclapply
    } else {

        dfs <-parallel::mclapply(excel_sheets(file), 
                         readxl::read_excel, 
                         path = file,
                         mc.cores=numCores)

        return(dfs)

   }
}

答案 5 :(得分:0)

值得。我遇到了同样的问题,但无法解决这些问题。最终,我了解到Rstudio具有“工作”窗格,并且可以在后台运行模型,每个模型都位于其自己的核心上。所以我所做的就是将模型分成10个部分(迭代了100个向量,因此有10个脚本,每个脚本包含10个向量),然后分别运行。这样一来,当我完成一个任务时,我可以立即使用它的输出,而无需等待每个模型完成,我就可以继续处理脚本。这是所有有关使用作业https://blog.rstudio.com/2019/03/14/rstudio-1-2-jobs/

的链接