简体   繁体   English

在bash中并行嵌套for循环

[英]Parallel nested for loop in bash

I am trying to run ac executable through bash. 我试图通过bash运行ac可执行文件。 The executable will take a different argument in each iteration, and I want to do it in parallel since I have 12 cores available. 可执行文件将在每次迭代中使用不同的参数,我想并行执行,因为我有12个可用核心。

I tried 我试过了

w=1;
for i in {1..100}
do
l=$(($i-1));
for j in {12*l..12*i}
do
./run $w/100 > "$w"_out &
done
expr=$w % 12;
if ["$expr" -eq "0"]
then wait;
fi;
done

run is the c executable. run是c可执行文件。 I want to run it with increasing argument w in each step, and I want to wait until all processes are done if 12 of the cores are in use. 我希望在每个步骤中使用增加的参数w来运行它,并且我想等到所有进程完成后如果正在使用12个核心。 SO basically, I will run 12 executables at the same time, then wait until they are completed, and then move to the next 12. 基本上,我将同时运行12个可执行文件,然后等待它们完成,然后移动到下一个12。

Hope I made my point clear. 希望我明白我的观点。

Cheers. 干杯。

Use gnu parallel instead: 改为使用gnu parallel

parallel ./myscript {1} ::: {1..100}

You can specify the number of parallel processes with the -P option, but it defaults to the number of cores in the system. 您可以使用-P选项指定并行进程数,但默认为系统中的核心数。

You can also specify -k to keep the output order and redirect the file. 您还可以指定-k以保持输出顺序并重定向文件。

To redirect the output to individual files, you can specify the output redirection, but you have to quote it, so that it is not parsed by the shell. 要将输出重定向到单个文件,您可以指定输出重定向,但必须引用它,以便shell不会对其进行解析。 For example: 例如:

parallel ./run {1} '>' {1}_out ::: {1..10}

is equivalent to running ./run 1 > 1_out to ./run 10 > 10_out 相当于运行./run 1 > 1_out./run 10 > 10_out

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM