perl mean.pl |
| 2010/12/02 | 00:02:31 | 100.32 |
| 2010/12/02 | 00:03:31 | 100.35 |
| 2010/12/02 | 00:05:21 | 100.39 |
| 2010/12/02 | 00:09:03 | 100.41 |
| 2010/12/02 | 00:13:21 | 100.90 |
| 2010/12/02 | 00:15:21 | 101.01 |
#!/usr/bin/perl #ファイルリスト作成 open(LS,"/bin/ls sample-????????.dat |"); open(OUTDATA,"> result.out"); #結果出力ハンドラ作成 while( |
#!/usr/bin/R -f
args <- commandArgs()
if(length(args)!=5){
cat(sprintf("file"))
q()
}
file <- args[5]
data <- read.table(file)
price <- na.omit(data)
lprice <- diff(log(price[,3]))
cat(sprintf("%s %f\n",file,mean(lprice)))
|
install.package("snow")
|
snowではプロセス間通信にMPI, SOCK, PVM, NWSを選択することができる. SOCK通信は特別なライブラリなしで単一コンピュータ上の複数コアでの並列計算に使用することができる. さらに, MPI(Message Passing Interface)を用いることにより, MPIライブラリを用いて大規模なクラスタ並列計算も可能である. このとき, Rmpiとsnowを組み合わせて使うことになる. Rmpiをインストールするためには計算機にOpenMPIまたはMPICHを別途インストールし RmpiをRへインストールすることが必要である.
並列計算を実装するためのsnowの関数群を紹介する (Rossini et al., 2003; Tierney et al., 2004). snowを使って並列計算を実装するコードの手順は以下のとおりである.
[文献]
Rossini, A. and Tierney, L. and Li, N. (2003) "Simple parallel statistical computing", in R. UW Biostatistics working paper series, Paper 193, University of Washington.
Tierney, L. and Rossini, A. and Li, N. and Sevcikova, H. (2004) "The snow Package: Simple Network of Workstations", Version 0.2-1.
scl <- makeCluster(4,type="SOCK") |
scl <- makeCluster() corenum <- length(clusterCall(scl,function() Sys.info())) |
mpirun -np 100 RMPISNOW --vanilla --no-save < Rscript.r > logfile.log |
f1 <- function(n){ return(mean(rnorm(n)))}
clusterCall(scl, f, 100)
|
myvar <- 666 clusterExport(scl, "myvar") |
clusterEvalQ(scl,Sys.getenv("HOME"))
|
f2 <- function(n,d){return(mean(d*rnorm(n)))}
clusterApply(scl, 1:10, f2, d=0.3)
|
f2 <- function(n,d){return(mean(d*rnorm(n)))}
clusterApply(scl, 1:10, f2, d=0.3)
|
f2 <- function(n,d){return(mean(d*rnorm(n)))}
parLapply(scl, 1:10, f2, d=0.3)
|
f2 <- function(n,d){return(mean(d*rnorm(n)))}
parSapply(scl, 1:10, f2, d=0.3)
|
x <- matrix(1:10, 2, 5)
f3 <- function(n,d){return(n*d)}
parRapply(scl, x, f3 d=0.3)
|
x <- matrix(1:10, 2, 5)
f3 <- function(n,d){return(n*d)}
parCapply(scl, x, f3, d=0.3)
|
stopCluster(scl) |
parallel-mean.pl(このサンプルコードはMacおよびLinuxで実行することを仮定している. Windowsで実行する場合には/bin/ls を dir /s /bに置き換えられたい) ではカレント・ディレクトリにあるデータ・ファイルのリストを作りRスクリプトの引数に与えて実行している. parallel-mean.plを実行すると平均値が表示されresult.outに計算結果が格納されることを確認してほしい. また, clusterApplyLB()関数を用いるとクラスタサイズに応じて適切なプログラムの並列実行がなされる. このサンプルコード内の計算負荷nopt.mean()関数を書き換えるだけでプッシュ・プル型の多くの並列計算を行うための雛形としてparallel-mean.Rは利用できる. snowを使うことにより並列計算の実行は極めて単純なコードのみで可能となる.
#!/usr/bin/perl #ファイルリスト作成 open(OUTDATA,"> result.out"); #結果出力ハンドラ作成 open(LS,"/bin/ls sample-????????.dat |"); $fl=""; while( |
args <- commandArgs()
if(length(args)<5){
cat(sprintf("file"))
q()
}
#クラスタのコア数を設定
corenum <- 4
#
library(snow)
library(Rmpi)
#ファイルリストをコマンド引数から得る
filelist <- c()
for(i in 5:length(args)){
filelist <- c(filelist,args[i])
}
# MPIクラスタの初期化
scl <- makeCluster(corenum,"MPI")
.Last <- function() { stopCluster(scl) }
# 平均値を計算する関数を定義
nopt.mean <- function(file){
if(!is.null(file)){
data <- read.table(file)
price <- na.omit(data)
lprice <- diff(log(price[,3]))
nopt.mean <- sprintf("%s %f",file,mean(lprice))
}
else{
nopt.mean <- NULL
}
}
#平均値を並列計算で得る
l<-clusterApplyLB(scl,filelist,nopt.mean)
#得られた結果を表示する
for(j in 1:length(l)){
cat(sprintf("%d %s\n",j,l[j]))
}
|
(C)2016 Takaki Hayashi and Aki-Hiro Sato. All rights reserved. 無断転用を禁ず