如何并行运行这个 bash 脚本?

如何并行运行这个 bash 脚本?

我需要这个来提高效率

目前,最多需要 20 小时,具体取决于线路(这些是相当大的 MCS 数据集)。

  • 将大数据文件拆分为“镜头”
  • 创建要在 for 循环中使用的每个镜头名称的列表
  • 循环遍历每个镜头并执行相同的过程
  • 将每个镜头附加到一个新的数据文件中,以便您拥有之前相同的行 aa,但经过处理。在这种情况下,我重复过滤数据,这就是为什么我认为这可以并行运行。

您可以忽略所有 SU 命令以及 for 循环中的所有内容,我只需要知道如何并行运行它(例如 32 个节点)。这对我来说是一个相对较新的话题,所以深入的解释将不胜感激!

脚本:

#! /bin/bash    
# Split the input file into one file for each shot. NB mustclose each o/p file at the earliest opportunity otherwise it will crash!
susplit <$1 key=fldr stem=fldr_ verbose=1 close=1

# Create a list of shot files
ls fldr* > LIST

# Loop over each shot file; suppress direct wave; write to new concatenated output file
for i in `cat LIST`; do
    echo $i
    suchw key1=tstat key2=tstat a=200 < $i | suwind key=tracf min=10 max=400 tmin=0 tmax=6 | suweight a=0 | suresamp rf=4 | sustatic hdrs=1 sign=-1 | sureduce rv=1.52 | sumedian median=1 xshift=0 tshift=0 nmed=41 | suflip flip=3 | sureduce rv=1.52 | suflip flip=3 | suresamp rf=0.25 | suweight inv=1 a=0 | sustatic hdrs=1 sign=1 >> $2
done

# Tidy up files by removing single shot gathers and LIST
rm -f fldr* LIST &

答案1

我假设这是for您想要并行化的循环:

#! /bin/bash    
# Split the input file into one file for each shot. NB mustclose each o/p file at the earliest opportunity otherwise it will crash!
susplit <$1 key=fldr stem=fldr_ verbose=1 close=1

sucit() {
    i=$1
    echo $i
    suchw key1=tstat key2=tstat a=200 < $i | suwind key=tracf min=10 max=400 tmin=0 tmax=6 | suweight a=0 | suresamp rf=4 | sustatic hdrs=1 sign=-1 | sureduce rv=1.52 | sumedian median=1 xshift=0 tshift=0 nmed=41 | suflip flip=3 | sureduce rv=1.52 | suflip flip=3 | suresamp rf=0.25 | suweight inv=1 a=0 | sustatic hdrs=1 sign=1
}
export -f sucit

parallel sucit ::: fldr* > $2

# Tidy up files by removing single shot gathers and LIST
rm -f fldr* LIST &

根据不同的情况,susplit你可以让它变得更快。如果“large_data_file”中的镜头以以下开头<shot>\n并以以下结尾,</shot>\n则类似以下内容可能有效:

sucpipe() {
    suchw key1=tstat key2=tstat a=200 | suwind key=tracf min=10 max=400 tmin=0 tmax=6 | suweight a=0 | suresamp rf=4 | sustatic hdrs=1 sign=-1 | sureduce rv=1.52 | sumedian median=1 xshift=0 tshift=0 nmed=41 | suflip flip=3 | sureduce rv=1.52 | suflip flip=3 | suresamp rf=0.25 | suweight inv=1 a=0 | sustatic hdrs=1 sign=1
}
export -f sucpipe

parallel --block -1 --recstart '<shot>\n' --recend '</shot>\n' --pipepart -a $1 sucpipe > $2

它将尝试将大文件分割成n个块,其中n=核心数。分割是即时完成的,因此不会先写入临时文件。然后 GNU Parallel 会将每个块传递给一个 sucpipe。

如果大文件是二进制文件(即不是文本),标头为 3200 字节,记录长度为 1000 字节,那么这可能有效:

parallel -a bigfile  --pipepart --recend '' --block 1000 --header '.{3200}' ...

有关更多详细信息,请浏览本教程:man parallel_tutorial您的命令行会喜欢您的。

相关内容