LoginSignup
4
9

More than 5 years have passed since last update.

任意のコマンドをコア数分並列実行する

Last updated at Posted at 2017-09-05

備忘録です.

シミュレーションなどで同じスクリプトを何回も回したい時があると思います.
その際にコア数分だけ並列に実行します.
TIMEを必要回数分設定し,write scriptと書いてある部分に実行したいスクリプトを書いてください.

#!/bin/bash

TIME=22

core=`nproc`
quo=$(( $TIME / $core ))
mod=$(( $TIME % $core ))
[ $mod -eq 0 ] && rep=$quo || rep=$(( $quo + 1 ))

for j in `seq 1 $quo`;do
    echo $j"/"$rep
    for i in `seq 1 $core`;do
        n=$(( $core * ($j - 1) + $i ))
        # write script
        echo $n
    done
    wait
done

echo $rep"/"$rep
for i in `seq 1 $mod`;do
    n=$(( $core * $quo + $i ))
    # write script
    echo $n
done
4
9
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
9