我正在使用 R 在具有多个节点和多个内核的集群上并行调用外部程序。外部程序需要三个输入数据文件并生成一个输出文件(所有文件都存储在同一个子文件夹中)。为了并行运行程序(或者更确切地说以并行方式调用它),我最初将该foreach
函数与doParallel
库一起使用。只要我只是在单个节点上使用多个内核,它就可以正常工作。
但是,我想使用具有多个内核的多个节点。因此,我相应地修改了我的代码以doSNOW
结合使用该库foreach
(我尝试了Rmpi
and doMPI
,但我没有设法使用这些库在多个节点上运行代码)。这工作正常,即外部程序现在确实在多个节点(具有多个核心)上运行,并且集群日志文件显示它产生了所需的结果。然而,我现在面临的问题是,外部程序不再将结果/输出文件存储在主节点上/工作目录的指定子文件夹中(当我使用doParallel
. 这使我无法将结果导入 R。
事实上,如果我检查相关文件夹的内容,它不包含任何输出文件,尽管日志文件清楚地显示外部程序运行成功。我猜它们存储在不同的节点上(?)。
我必须对我的foreach
功能或设置集群的方式进行哪些修改,才能将这些文件保存在主节点上/工作目录中指定的子文件夹中?
这里有一些示例 R 代码,以展示我在做什么:
# #Set working directory in non-interactive mode
setwd(system("pwd", intern = T))
# #Load some libraries
library(foreach)
library(parallel)
library(doParallel)
# ####Parallel tasks####
# #Create doSNOW cluster for parallel tasks
library(doSNOW)
nCoresPerNode <- as.numeric(Sys.getenv("PBS_NUM_PPN"))
nodeNames <- system("cat $PBS_NODEFILE | uniq", intern=TRUE)
machines <- rep(nodeNames, each = nCoresPerNode)
cl <- makeCluster(machines, type = "SOCK")
registerDoSNOW(cl)
# #How many workers are we using?
getDoParWorkers()
#####DUMMY CODE#####
# #The following 3 lines of code are just dummy code:
# #The idea is to create input files for the external program "myprogram"
external_Command_Script.cmd # #command file necessary for external program "myprogram" to run
startdata # #some input data for "myprogram"
enddata # #additional input data for "myprogram"
####DUMMY CODE######
# #Write necessary command and data files for external program: THIS WORKS!
for(i in 1:100)){
write(external_Command_Script.cmd[[i]], file=paste("./mysubfolder/external_Command_Script.",i,".cmd", sep=""))
write.table(startdata, file=paste("./mysubfolder/","startdata.",i,".txt", sep=""), col.names = FALSE, quote=FALSE)
write.table(enddata, file=paste("./mysubfolder/","enddata.",i,".txt", sep=""), col.names = FALSE, quote=FALSE)
}
# #Run external program "myprogram" in parallel: THIS WORKS!
foreach(i = 1:100)) %dopar% {
system(paste('(cd ./mysubfolder && ',"myprogram",' ' ,"enddata.",i,".txt ", "startdata.",i,".txt", sep="",' < external_Command_Script.',i,'.cmd)'))
}
# #Import results of external program: THIS DOES NOT WORK WHEN RUN ON MULTIPLE NODES!
results <- list()
for(i in 1:100)){
results[[i]] = read.table(paste("./mysubfolder/","enddata.txt.",i,".log.txt", sep=""), sep = "\t", quote="\"", header = TRUE)
}
# #The import does NOT work as the files created by the external program are NOT stored on the master node/in the
# #subfolder of the working directory!
# #Instead I get the following error message:
# #sh: line 0: cd: ./mysubfolder: No such file or directory
# #Error in { : task 6 failed - "cannot open the connection"
我的集群 pbs 脚本如下所示:
#!/bin/bash
# request resources:
#PBS -l nodes=2:ppn=8
#PBS -l walltime=00:30:00
module add languages/R-3.3.3-ATLAS
export PBS_O_WORKDIR="/panfs/panasas01/gely/xxxxxxx/workingdirectory"
# on compute node, change directory to 'submission directory':
cd $PBS_O_WORKDIR
# run your program and time it:
time Rscript ./R_script.R