R语言并行计算snow包文档(beta)

来源:互联网 发布:matlab向量化编程 编辑:程序博客网 时间:2024/05/24 00:57

1.snow-clusterCluster-Level on a snow cluster

snow-clusterCluster-Level on a snow clusterclusterSplit(cl, seq)clusterCall(cl, fun, …)clusterApply(cl, x, fun, …)clusterApplyLB(cl, x, fun, …)clusterEvalQ(cl, expr)clusterExport(cl, list, envir = .GlobalEnv)clusterMap(cl, fun, …, MoreArgs = NULL, RECYCLE = TRUE)Arguments说明cl集群对象fun函数expr每个节点需执行的表达式seq需拆分给每个节点的向量list传递给每个节点的全局变量列表envir变量模式(默认全局)x矩阵…欲传递给fun的附加参数MoreArgsfun的附加参数RECYCLE逻辑值;如果真,短参数被回收

详细说明 
这些是用于集群计算得基本函数。所有在子节点的运行的程序都在tryCatch函数下运行。即如果任何子节点有错误都会在主节点标示出。更能多细节将在以后假如考虑之中。

clusterCall(cl, fun, …) 
对每个节点使用同一个函数fun,以列表形式输出每个节点的结果。

clusterEvalQ(cl, expr) 
对每个节点使用fun,与clustercall不同的是没有其他参数输入。

clusterApply(cl, x, fun, …) 
将矩阵x的每个参数分别分配给每个节点,若x参数超过节点数则循环分配进行计算。

clusterApplyLB(cl, x, fun, …) 
clusterApply的均衡加载模式。如果x参数超过节点数剩余参数将分配给已经执行完任务的节点。使用此函数可以更好地利用集群,但是,这种方式降低了计算性能。

clusterMap(cl, fun, …, MoreArgs = NULL, RECYCLE = TRUE) 
类似mapply,例子

clusterMap(cl,function(x, y) seq_len(x) + y,c(a =  1, b = 2, c = 3),c(A = 10, B = 0, C = -10))[[1]][1] 11[[2]][1] 1 2[[3]][1] -9 -8 -7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

clusterExport(cl, list, envir = .GlobalEnv) 
将list参数作为全局变量传递给每个节点。

clusterSplit(cl, seq) 
将seq序列,分配给各个节点,现版本要求序列长度与节点数相同.

> clusterSplit(cl,c(1,2))[[1]][1] 1[[2]][1] 2
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

综合性例子

cl <- makeSOCKcluster(c("localhost","localhost"))#建立集群clusterApply(cl, 1:2, get("+"), 3)#第一个节点算1+3第二个节点算2+3clusterEvalQ(cl, library(boot))#每个节点加载libx<-1clusterExport(cl, "x")#每个节点加载x作为环境变量clusterCall(cl, function(y) x + y, 2)#每个节点计算一次1+3
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2.snow-parallel———-Higher Level SNOW functions

snow-parallel———-Higher Level SNOW functionsparLapply(cl, x, fun, …)parSapply(cl, X, FUN, …, simplify = TRUE, USE.NAMES = TRUE)parApply(cl, X, MARGIN, FUN, …)parRapply(cl, x, fun, …)parCapply(cl, x, fun, …)parMM(cl, A, B)Arguments说明cl集群对象fun,FUN函数x,X,A,B矩阵…欲传递给fun的附加参数MoreArgsfun的附加参数MARGIN指定要使用的向量维度simplify逻辑值;参见sapplyUSE.NAMES逻辑值;参见sapply

详细说明 
1.parLapplyparSapplyparApplylapply,sapply,apply的平行计算版本。 
2.parRapplyparCapplyapply的行、列计算的对应版本;比parApply更有效率。 
3.parMM:is a very simple(minded) parallel matrix multiply; it is intended as an illustration(还没搞懂功能) 
4.更多细节参见:Simple Network of Workstations for R 
例子

## Not run:cl <- makeSOCKcluster(c("localhost","localhost"))parSapply(cl, 1:20, get("+"), 3)## End(Not run)
  • 1
  • 2
  • 3
  • 4
[1]  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
  • 1
snow-rand 在集群产生随机数clusterSetupRNG (cl, type = “RNGstream”, …)clusterSetupRNGstream (cl, seed=rep(12345,6), …)clusterSetupSPRNG (cl, seed = round(2^32 * runif(1)), prngkind = “default”, para = 0, …)

描述 
初始化集群中的随机数,使用 L’Ecuyer或SPRNG的包产生随机数(分别需要rlecuyerSPRING包)。

参数说明cl集群type随机数产生方式 type=”RNGstream”(default) type=”SPRNG”seed随机种子prngkindSPRNG的发生器形式para发生器的附加参数

clusterSetupRNG (cl, type = “RNGstream”, …) 
1.如果设置了SPRNG参数,会调用clusterSetupSPRNG函数。2.如果设置RNGstream参数,会调用clusterSetupSPRNG函数。 
clusterSetupRNGstream (cl, seed=rep(12345,6), …) 
加载rsprng包并分别的每个节点初始化。进一步的细节参见init.sprng文档. 
clusterSetupSPRNG (cl, seed = round(2^32 * runif(1)), prngkind = “default”, para = 0, …) 
加载rlecuyer包,在每个节点创建一个数据流并分配给每个节点。

## Not run:clusterSetupSPRNG(cl)clusterSetupSPRNG(cl, seed=1234)clusterSetupRNG(cl, seed=rep(1,6))## End(Not run)
  • 1
  • 2
  • 3
  • 4
  • 5

3.snow-startstop

snow-startstop Starting and Stopping SNOW ClustersmakeCluster(spec,type = getClusterOption(“type”), …)stopCluster(cl)setDefaultClusterOptions(…)makeSOCKcluster(names,…,options=defaultClusterOptions)makePVMcluster(count,…,options=defaultClusterOptions)makeMPIcluster(count,…,options=defaultClusterOptions)makeNWScluster(names,…,options=defaultClusterOptions)getMPIcluster()Arguments参数spec建立节点的节点参数count创建节点的个数names节点名向量options集群参数设置cl建立节点的参数type节点的类型

详细介绍 
makeCluster建立集群的函数,支持的集群类型有“sock”,“PVM”,“MPI”,“NWS”。 
1. 对于“PVM”和“MPI”集群spec参数应设置为整数形式的节点数量。 
2. 对于“SOCK”和“NWS”集群spec 参数应该设置为对应节点组成的字符型向量(ip或hosts地址名)。 
3. 对于“SOCK”和“NWS”集群spec 参数在本地模式下也可以设置为数字。 
4. 选项rscriptsnowlib很有用,参见下面的例子。

stopCluster在退出R之前使用关闭集群。

setDefaultClusterOptions设置集群默认参数。其中最有用的参数为typehomogeneous。 
如果Rmpi在搜索路径中,type参数会设置给MPI 
如果安装了rpvm包,type参数会设置给PVM` 
若上述两个都没找到,设置给“SOCK”。

homogeneous 如果要使用非均匀的集群设置为FLASE;这需要一些额外的设置。默认的设置是TRUE除非在master节点上设置了R_SNOW_LIB非空的数值。

optionoutfile可以用来制定slave节点输出的位置。默认是/dev/null//;这项设置有助于在安装时做故障排除。将路径设置在“”/dev/tty可以slave输出在mater节点上。

makeSOCKclustermakePVMclustermakeMPIcluster, and makeNWScluster用来启用相应类型的集群。

在MPI中的进程配置中像用来开启主节点和子节点的mpirun 是不能用的,因为集群在之前已经配置好,可以使用getMPIcluster来获得。

对于SOCKNWS集群选项manual=TRUE可以强制手动开启工作进程。设置outfile选项可以用来做故障排除。 
更多细节参见: http://www.stat.uiowa.edu/~luke/R/cluster/cluster.html 
例子

## Not run:## Two workers run on the local machine as a SOCK cluster.cl <- makeCluster(c("localhost","localhost"), type = "SOCK")clusterApply(cl, 1:2, get("+"), 3)stopCluster(cl)## Another approach to running on the local machine as a SOCK cluster.cl <- makeCluster(2, type = "SOCK")clusterApply(cl, 1:2, get("+"), 3)stopCluster(cl)## A SOCK cluster with two workers on Mac OS X, two on Linux, and two## on Windows:macOptions <-list(host = "owasso",rscript = "/Library/Frameworks/R.framework/Resources/bin/Rscript",snowlib = "/Library/Frameworks/R.framework/Resources/library")lnxOptions <-list(host = "itasca",rscript = "/usr/lib64/R/bin/Rscript",snowlib = "/home/luke/tmp/lib")winOptions <-list(host="192.168.1.168",rscript="C:/Program Files/R/R-2.7.1/bin/Rscript.exe",snowlib="C:/Rlibs")cl <- makeCluster(c(rep(list(macOptions), 2), rep(list(lnxOptions), 2),rep(list(winOptions), 2)), type = "SOCK")clusterApply(cl, 1:6, get("+"), 3)stopCluster(cl)## End(Not run)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

4.snow-timing

snow-timing Timing SNOW CLusters
snow.time(expr)## S3 method for class ' snowTimingData 'print(x, ...)## S3 method for class ' snowTimingData 'plot(x, xlab = "Elapsed Time", ylab = "Node",title = "Cluster Usage", ...)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
Arguments参数说明expr需要计算时间的表达式x需要画图的时间数据xlab,ylabx,y坐标名title图头

说明

snow.time输出 snowTimingData格式的时间数据。 
snowTimingData的print方法输出了总运行时间,在主节点和自己点的通信时间和每个节点的计算时间。 
plot展示了计算时间的线条图,绿色计算矩形代表计算时间,蓝色横线代表一个子节点的等待时间,红线代表主/子节点的通信时间。 
例子

## Not run:cl <- makeCluster(2,type="SOCK")x <- rnorm(1000000)tm <- snow.time(clusterCall(cl, function(x) for (i in 1:100) sum(x), x))print(tm)plot(tm)stopCluster(cl)## End(Not run)