Bash: Running the same program over multiple cores

后端 未结 3 651
闹比i
闹比i 2020-12-14 10:28

I have access to a machine where I have access to 10 of the cores -- and I would like to actually use them. What I am used to doing on my own machine would be something like

3条回答
  •  眼角桃花
    2020-12-14 11:06

    You could use

    for f in *.fa; do
        myProgram (options) "./$f" "./$f.tmp" &
    done
    wait
    

    which would start all of you jobs in parallel, then wait until they all complete before moving on. In the case where you have more jobs than cores, you would start all of them and let your OS scheduler worry about swapping processes in an out.

    One modification is to start 10 jobs at a time

    count=0
    for f in *.fa; do
        myProgram (options) "./$f" "./$f.tmp" &
        (( count ++ ))        
        if (( count = 10 )); then
            wait
            count=0
        fi
    done
    

    but this is inferior to using parallel because you can't start new jobs as old ones finish, and you also can't detect if an older job finished before you manage to start 10 jobs. wait allows you to wait on a single particular process or all background processes, but doesn't let you know when any one of an arbitrary set of background processes complete.

提交回复
热议问题