【问题标题】:script for Running MPI program with three input files as command line arguments使用三个输入文件作为命令行参数运行 MPI 程序的脚本
【发布时间】:2018-03-20 12:51:13
【问题描述】:

enter code here MPI 程序采用 3 个命令行参数(2 个输入文件和 SIZE,这对文件对来说是相同的)。例如,在同一目录中,我有这些文件。

abc.mtx,  abc.txt  SIZE is same for these 2 files

def.mtx,  def.txt  SIZE is same for these two files

qas.mtx,  qas.txt  SIZE is same for these two files

and so on .....

请注意:文件名相同但扩展名不同。

我想运行我的代码

mpirun -np 4 ./myexe file1.mtx file1.txt -SIZE 10  //.myexe is executable

我想用不同数量的进程执行我的程序,比如 -np 2、4、6、8 和 10。我有一百多个文件。我想从命令行执行我的代码一次,使用指定的进程数一一读取这些文件。

例如

abc.mtx and abc.txt should run first with 2,4,6,8,10 processes 

and then next two files def.mtx and def.txt with 2,4,6,8,10  processes and so on....

对于串行代码,我尝试了以下命令,它通过一个一个地获取所有 .txt 文件来工作。(仅适用于 txt 或 mtx 文件,但不能同时使用)

find . -name "*.txt" | awk -F"/" '{system ("./myexe." $2)}'

如何使用 2 个具有不同扩展名的输入文件运行,即(mtx、txt)。第三个参数是 SIZE 的最佳方法是什么。我是否应该创建另一个包含 SIZE 并提供三个输入文件参数作为输入的文件。?

编辑 这是一个脚本

#!/bin/bash

while read base size; do
   mtx="${base}.mtx"
   txt="${base}.txt"
   for np in 2 4 6 8 10; do
      echo mpirun -np $np ./myexe "$mtx" "$txt" -SIZE $size
   done
done < jobs

jobs.txt 文件看起来像

bus 490
bcs_B 10
arc 1178
tk18 99

我正在使用以下命令来执行

./script.sh jobs.txt ./new

也试过

bash script.sh jobs.txt ./new

编辑 2

Jobs.txt 看起来像

494_bus 494
arc130 130
bcsstk02 66
bcsstk18 11948

脚本是

#!/bin/bash
while read base size; do
   mtx="${base}.mtx"
   txt="${base}.txt"
   for np in 2 4; do
      mpirun -np $np ./new "$txt" "$mtx" -SIZE $size
   done
done < "$1"

我只是从我的代码中打印矩阵的维度。输出是

Dimension of the matrix is = 494 
Dimension of the matrix is = 494 
Dimension of the matrix is = 494 
Dimension of the matrix is = 494 
Dimension of the matrix is = 494 
Dimension of the matrix is = 494 

它只取第一对文件,用 -np 2 和 -np 4 执行它们,但不执行其余的。

如果我在 mpirun 显示之前在脚本中编写 Echo

mpirun -np 2 ./new 494_bus.txt 494_bus.mtx -SIZE 494
mpirun -np 4 ./new 494_bus.txt 494_bus.mtx -SIZE 494
mpirun -np 2 ./new arc130.txt arc130.mtx -SIZE 130
mpirun -np 4 ./new arc130.txt arc130.mtx -SIZE 130
mpirun -np 2 ./new bcsstk02.txt bcsstk02.mtx -SIZE 66
mpirun -np 4 ./new bcsstk02.txt bcsstk02.mtx -SIZE 66
mpirun -np 2 ./new bcsstk18.txt bcsstk18.mtx -SIZE 11948
mpirun -np 4 ./new bcsstk18.txt bcsstk18.mtx -SIZE 11948

如果我分别执行这些命令中的每一个,它们都可以正常工作。例如

mpirun -np 4 ./new arc130.txt arc130.mtx -SIZE 130
mpirun -np 2 ./new bcsstk18.txt bcsstk18.mtx -SIZE 11948

这些运行命令可以正常工作,但不能与脚本一起运行。 谢谢

编辑 3

cat jobs.txt 

494_bus 494
arc130 130
bcsstk02 66
bcsstk18 11948



cat -vet jobs.txt

494_bus 494$
arc130 130$
bcsstk02 66$
bcsstk18 11948$

猫脚本.sh

#!/bin/bash

while read base size; do
   mtx="${base}.mtx"
   txt="${base}.txt"
   for np in 2 4; do
      mpirun -np $np ./new "$txt" "$mtx" -SIZE $size
   done
done < "$1"

cat -vet script.sh

#!/bin/bash$
$
while read base size; do$
   mtx="${base}.mtx"$
   txt="${base}.txt"$
   for np in 2 4; do$
      mpirun -np $np ./new "$txt" "$mtx" -SIZE $size$
   done$
done < "$1"$

编辑 4

bash -xv script2.sh jobs.txt
#!/bin/bash

while read base size; do
   mtx="${base}.mtx"
   txt="${base}.txt"
   for np in 2 4; do
      mpirun -np $np ./new "$txt" "$mtx" -SIZE $size
   done
done < "$1"
+ read base size
+ mtx=494_bus.mtx
+ txt=494_bus.txt
+ for np in 2 4
+ mpirun -np 2 ./new 494_bus.txt 494_bus.mtx -SIZE 494
Dimension of the matrix is = 494 
Dimension of the matrix is = 494 
+ for np in 2 4
+ mpirun -np 4 ./new 494_bus.txt 494_bus.mtx -SIZE 494
Dimension of the matrix is = 494 
Dimension of the matrix is = 494 
Dimension of the matrix is = 494 
Dimension of the matrix is = 494 
+ read base size

【问题讨论】:

  • 我没有看到任何 C....
  • 也不是真正的 MPI 问题 ...
  • 为什么要使用 [batch-file] 标签,而您显然想要bash

标签: bash


【解决方案1】:

更新答案

我怀疑 MPI 程序正在消耗部分/全部 stdin,因此我建议将整个作业列表读取到 bash 数组中:

#!/bin/bash

# Read entire jobs file into array jobs[]
IFS=$'\n' jobs=($(cat "$1"))

for j in "${jobs[@]}"; do
   IFS=" " read base size <<< "$j"
   mtx="${base}.mtx"
   txt="${base}.txt"
   for np in 2 4 6 8 10; do
      echo mpirun -np $np ./myexe "$mtx" "$txt" -SIZE $size
   done
done

更新答案

根据您的 cmets,最简单的方法可能是拥有一个名为 jobs 的文件,其内容如下:

abc 108
def 120
qas 196

然后将脚本更改为如下所示:

#!/bin/bash

while read base size; do
   mtx="${base}.mtx"
   txt="${base}.txt"
   for np in 2 4 6 8 10; do
      echo mpirun -np $np ./myexe "$mtx" "$txt" -SIZE $size
   done
done < "$1"

样本输出

mpirun -np 2 ./myexe abc.mtx abc.txt -SIZE 108
mpirun -np 4 ./myexe abc.mtx abc.txt -SIZE 108
mpirun -np 6 ./myexe abc.mtx abc.txt -SIZE 108
mpirun -np 8 ./myexe abc.mtx abc.txt -SIZE 108
mpirun -np 10 ./myexe abc.mtx abc.txt -SIZE 108
mpirun -np 2 ./myexe def.mtx def.txt -SIZE 120
mpirun -np 4 ./myexe def.mtx def.txt -SIZE 120
mpirun -np 6 ./myexe def.mtx def.txt -SIZE 120
mpirun -np 8 ./myexe def.mtx def.txt -SIZE 120
mpirun -np 10 ./myexe def.mtx def.txt -SIZE 120
mpirun -np 2 ./myexe qas.mtx qas.txt -SIZE 196
mpirun -np 4 ./myexe qas.mtx qas.txt -SIZE 196
mpirun -np 6 ./myexe qas.mtx qas.txt -SIZE 196
mpirun -np 8 ./myexe qas.mtx qas.txt -SIZE 196
mpirun -np 10 ./myexe qas.mtx qas.txt -SIZE 196

原答案

不确定为什么在使用awkfind 时将其标记为batch-file(这是一个令人讨厌的Windows 事物)?

无论如何,这就是你所要求的:

#!/bin/bash

for f in *.mtx; do
   # Get name of corresponding text file
   t="${f%.*}.txt"
   for np in 2 4 6 8 10; do
      echo mpirun -np $np ./myexe "$f" "$t" -SIZE 10
   done
done

样本输出

mpirun -np 2 ./myexe abc.mtx abc.txt -SIZE 10
mpirun -np 4 ./myexe abc.mtx abc.txt -SIZE 10
mpirun -np 6 ./myexe abc.mtx abc.txt -SIZE 10
mpirun -np 8 ./myexe abc.mtx abc.txt -SIZE 10
mpirun -np 10 ./myexe abc.mtx abc.txt -SIZE 10
mpirun -np 2 ./myexe def.mtx def.txt -SIZE 10
mpirun -np 4 ./myexe def.mtx def.txt -SIZE 10
mpirun -np 6 ./myexe def.mtx def.txt -SIZE 10
mpirun -np 8 ./myexe def.mtx def.txt -SIZE 10
mpirun -np 10 ./myexe def.mtx def.txt -SIZE 10
mpirun -np 2 ./myexe qas.mtx qas.txt -SIZE 10
mpirun -np 4 ./myexe qas.mtx qas.txt -SIZE 10
mpirun -np 6 ./myexe qas.mtx qas.txt -SIZE 10
mpirun -np 8 ./myexe qas.mtx qas.txt -SIZE 10
mpirun -np 10 ./myexe qas.mtx qas.txt -SIZE 10

如果您有bash v4+,则可以将2 4 6 8 10 替换为{2..10..2}


只是为了好玩,而且因为我从未尝试过对 GNU Parallel 进行嵌套调用,所以您可以像这样以单行方式执行此操作:

parallel -j1 "parallel -j1 -I // echo mpirun -np // {} {.}.txt -SIZE 10 ::: 2 4 6 8 10" ::: *mtx

mpirun -np 2 abc.mtx abc.txt -SIZE 10
mpirun -np 4 abc.mtx abc.txt -SIZE 10
mpirun -np 6 abc.mtx abc.txt -SIZE 10
mpirun -np 8 abc.mtx abc.txt -SIZE 10
mpirun -np 10 abc.mtx abc.txt -SIZE 10
mpirun -np 2 def.mtx def.txt -SIZE 10
mpirun -np 4 def.mtx def.txt -SIZE 10
mpirun -np 6 def.mtx def.txt -SIZE 10
mpirun -np 8 def.mtx def.txt -SIZE 10
mpirun -np 10 def.mtx def.txt -SIZE 10
mpirun -np 2 qas.mtx qas.txt -SIZE 10
mpirun -np 4 qas.mtx qas.txt -SIZE 10
mpirun -np 6 qas.mtx qas.txt -SIZE 10
mpirun -np 8 qas.mtx qas.txt -SIZE 10
mpirun -np 10 qas.mtx qas.txt -SIZE 10

parallel 的最外层实例迭代 mtx 文件,而内部实例迭代进程数。两者都使用-j1,因此parallel 一次只启动一项工作,并且本身不会引入任何额外的并行性。

【讨论】:

  • 谢谢。太棒了...这就是我想要的。第三个参数 -size 10..... 数字 10 随每对文件 (.mtx,.txt) 而变化。我应该制作一个文本文件并存储每对这些文件的大小。例如 abc.mtx abc.txt 100 ..........qas.mtx qas.txt 149...等等。每次程序执行这对文件并通过匹配名称从文本文件中读取相应的大小...我想问一下最好的解决方案是什么?.....再次感谢...:)跨度>
  • 我已经更新了脚本和控制文件格式-请再看一下。
  • 太棒了...我只是想在 mac 上运行上面的脚本.. 但它给了我一个错误。 script.sh 第 9 行:jobs 没有这样的文件或目录。虽然我已经在同一目录中创建了文件(jobs.rtf)并执行了像 ./script.sh jobs.rtf ./new 这样的脚本......我已经尝试运行示例脚本以确保它们工作。
  • 不! RTF 文件包含以粗体打印、更改字体和中心文本的代码。它们用于文字处理而不是脚本。我不在我的 Mac 上,但在 TextEdit 的菜单上有一个名为 Make plain text 的选项 - 也许是 View 菜单。您需要单击它并最好删除 .rtf 扩展名。
  • 感谢您的快速回复,但我也尝试过使用 .txt,但仍然出现相同的错误...我尝试过 ./script.sh jobs.txt ./new 和 bash script.sh jobs.txt ./new....
猜你喜欢
  • 2020-08-28
  • 1970-01-01
  • 2010-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-26
  • 2013-07-05
  • 1970-01-01
相关资源
最近更新 更多