我有一个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
但是,我想在不同的内核上并行运行相同的可执行文件,用于不同的a和b值.是否可以在bash脚本中编写for循环以便我可以执行
就像是,
- for i=1;i<=10;i++ {
- $HOME/codes/3D/autoA100.out 2+i*0.1 2+i*0.2
- }
如果要将执行脚本提交给批处理加载器,则无法通过简单的循环执行所需的执行,因为整个脚本在每个节点上运行.但是,大多数批处理程序为用户提供环境变量.
例如,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的原因.