Bash 将阵列传入GNU并行以替换for循环 a)我想并行运行2个脚本 b) 我希望在这些脚本中并行执行for循环。
在我使用此代码之前:Bash 将阵列传入GNU并行以替换for循环 a)我想并行运行2个脚本 b) 我希望在这些脚本中并行执行for循环。,bash,parallel-processing,netcdf,gnu-parallel,Bash,Parallel Processing,Netcdf,Gnu Parallel,在我使用此代码之前: for year in 2000 2001 2002 2003; do echo $year" LST data being merged" cd $base_data_dir/$year # this is the part that takes a long time cdo -f nc2 mergetime *.nc $output_dir/LST_$year.nc done 我想使用GNU并行来尝试并行运行这个程序 我尝
for year in 2000 2001 2002 2003; do
echo $year" LST data being merged"
cd $base_data_dir/$year
# this is the part that takes a long time
cdo -f nc2 mergetime *.nc $output_dir/LST_$year.nc
done
我想使用GNU并行来尝试并行运行这个程序
我尝试了以下方法:
a) 创建一个调用其他脚本的“控制器”脚本
b) 将数组作为参数传递给GNU parallel
控制器脚本
这应并行运行以下各项:
bash create_yearly_LST_files.sh 2000
bash create_yearly_LST_files.sh 2001
...
bash create_yearly_NDVI_files.sh 2000
bash create_yearly_NDVI_files.sh 2001
...
处理脚本(与NDVI相同)
因此,命令应为:
cd $base_data_dir/2000
cdo -f nc2 mergetime *.nc $output_dir/LST_2000.nc
cd $base_data_dir/2001
cdo -f nc2 mergetime *.nc $output_dir/LST_2001.nc
...
cd $base_data_dir/2000
cdo -f nc2 mergetime *.nc $output_dir/NDVI_2000.nc
cd $base_data_dir/2001
cdo -f nc2 mergetime *.nc $output_dir/NDVI_2001.nc
...
我的问题:
在我的新代码中,这些进程仍然可以工作,但性能没有提高
谁能帮我理解如何通过每年的平行运行
同时并行运行这两个脚本(
create_yearly_LST_files.sh
和create_yearly_NDVI_files.sh
)是什么阻止了你
for year in 2000 2001 2002 2003; do
echo $year" LST data being merged"
cd $base_data_dir/$year
# this is the part that takes a long time
cdo -f nc2 mergetime *.nc $output_dir/LST_$year.nc &
done
wait
与GNU并行时:
cd $working_dir
parallel 'cd {}; cdo -f nc2 mergetime *.nc xxx/LST_{}.nc' ::: {2000..2003}
也许这会奏效:
doit() {
cd "$base_data_dir"/"$1"
cdo -f nc2 mergetime *.nc "$output_dir"/${2}_${1}.nc"
}
export -f doit
export base_data_dir
export output_dir
parallel doit ::: {2000..2018} ::: LST NDVI
&
是否意味着循环中后面的$year
也将在不同的内核上运行?&
将任务置于后台;这意味着它不会等到命令完成后再继续<底部的代码>等待等待所有后台任务完成,然后继续执行脚本。Linux是智能的,将使用不同的内核,是的。其他echo
语句将写入到哪里?比如说,如果我有一行echo$year“LST data merged”
该写在哪里?所有echo语句都写在同一个地方,stdout。是的,我介意。我回答了你原来的问题,你既没有投赞成票,也没有接受我的回答。嗨@马克,cd{}
中的空括号必须用cd{$base\u data\u dir/$year}
填充吗?抱歉bash对我来说有点陌生!否。当每个并行作业启动时,{}
将(由GNU parallel)用当前参数填充,因此第一个作业将用2000填充,第二个是2001,以此类推。如果您想查看它将执行什么而不实际执行任何操作,可以使用parallel--dry run…
。您可以使用man parallel
查看内置帮助,按空格键向前移动一页,然后按q
退出。
cd $working_dir
parallel 'cd {}; cdo -f nc2 mergetime *.nc xxx/LST_{}.nc' ::: {2000..2003}
doit() {
cd "$base_data_dir"/"$1"
cdo -f nc2 mergetime *.nc "$output_dir"/${2}_${1}.nc"
}
export -f doit
export base_data_dir
export output_dir
parallel doit ::: {2000..2018} ::: LST NDVI