How to use gnu-parallel for nested loops with inner one depending on the outer one?

浪尽此生 提交于 2019-12-24 14:46:35

问题


I would like to use gnu-parallel to parallelize the following snippet

for a in `seq 5` ; do
    for b in `seq $a` ; do
        echo $a $b
    done
done

Is this possible to achieve using something like

parallel echo {1} {2} ::: $(seq 5) ::: $(seq {1})

The above syntax does not work.

Motivation:

I would like to use parallel in order to submit a number of jobs to a cluster, as explained here http://docs.rcc.uchicago.edu/software/scheduler/parallel/README.html


回答1:


GNU Parallel can do the cartesian product. What you want is half of that (the lower triangle of the product). GNU Parallel cannot do that directly. So you will have to skip the upper triangle:

parallel [ {2} -gt {1} ] '||' echo {1} {2} ::: $(seq 5) ::: $(seq 5)

For more complex tasks use $job->skip():

parallel echo {=1' $arg[2] > $arg[1] and $job->skip();' =} {2} ::: $(seq 5) ::: $(seq 5)


来源:https://stackoverflow.com/questions/21691813/how-to-use-gnu-parallel-for-nested-loops-with-inner-one-depending-on-the-outer-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!