1

我有一个 C++ 程序,我使用命令行将两个双精度值作为输入传递给它

int main(int argc, char *argv[]){
    double a,b;
    a = atof(argv[1]);
    b = atof(argv[2]);
    further code.....

我使用该实用程序在集群上运行代码,qsub并且我有一个名为“jobsub.sh”的 Bash 脚本来提交如下所示的作业:

#!/bin/csh -f
hostname
cd /home/roy/codes/3D             # Change directory first -- replace Mysubdir
set startdir = `pwd`               # Remember the directory we're in
if( ! -d /scratch/$USER ) then
    mkdir /scratch/$USER       # Create scratch directory
endif                              # If it does not exist
#cp infile12 /scratch/$USER     # Copy input file to scratch directory
cd /scratch/$USER                  # Change to scratch directory
#rm *.*
$HOME/codes/3D/autoA100.out 2.1 2.2          # Run a program
cp * $startdir         # Copy outputfiles back to where we started

在我做的终端qsub jobsub.sh

但是,我想为不同的值运行相同的可执行文件,ab在不同的内核上并行运行。是否可以在 Bash 脚本中编写一个for循环,以便我可以执行类似的操作,

for i=1;i<=10;i++ {
   $HOME/codes/3D/autoA100.out 2+i*0.1 2+i*0.2
}
4

2 回答 2

3

如果您将执行脚本提交给批处理加载程序,那么就无法像您想要的那样让一个简单的循环执行,因为整个脚本都在每个节点上运行。但是,大多数批处理加载程序都会向用户提供环境变量。

例如,PBS 有$PBS_ARRAYID,它指定作业运行时的唯一 ID。因此,您的脚本可以具有以下内容,而不是使用循环:

a=$(echo "2+($PBS_ARRAYID+1)*0.1" | bc -l)
b=$(echo "2+($PBS_ARRAYID+1)*0.2" | bc -l)
$HOME/codes/3D/autoA100.out $a $b

请注意我在上面添加1$PBS_ARRAYID因为 ID 开始于0而您的循环开始于1. 还值得一提的是,虽然 bash 本身可以做一些算术运算,但它不能处理实数。这就是为什么我不得不调用bc.

于 2012-07-06T00:26:40.310 回答
0

您始终可以在后台运行作业(在这种情况下,shell 将继续执行下一条指令)

MY_PIDS=
for i in 1 2 3 4 5 6 7 8 9 10
do
    yourjob "$(a i)" "$(b i)" &   # Note the ampersand (background)
    MY_PIDS="$MY_PIDS "$!         # Do not write the $! inside the quotes, bash will
                                  # break (better, don't use bash but plain sh)
done

# Wait wor each job to complete and cleanup
for p in $MY_PIDS
do
    wait "$p"
    echo "Return code of job $p is $?"
    do_cleanup_for "$p"
done

但当然,您需要确保并行运行的作业不会互相踩踏(例如写入同一个文件)。

于 2012-07-06T00:26:27.387 回答