在多个内核bash的运行过程循环 [英] Run a looped process in bash across multiple cores

查看:168
本文介绍了在多个内核bash的运行过程循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含下面的循环的shell脚本。

I have a shell script that contains the following loop.

i=0  
upperlimit=$verylargevariable  
do  
   complexstuff RunManager file $i  
   i= 'expr $i +1'  
done

本脚本中的四核机器上运行,并根据,使用每个核心的约15%,而在执行一次循环迭代。我想它在四个核心分配,使得每次循环呢 complexstuff 四次,一个在每个核心,这样的资源将得到更有效的使用。我们谈论的计算,目前需要几个小时,效率比刚刚好做法更多的在这里。 (每次迭代的输出是明显独立于previous之一。)

This script runs on a quad core machine, and according to top, uses about 15% of each core while executing one iteration of the loop. I'd like to distribute it across the four cores so that each iteration of the loop does complexstuff four times, one on each core, so the resources will be used more efficiently. We're talking about computation that currently takes several hours so efficiency is more than just good practice here. (The output of each iteration is obviously independent of the previous one.)

PS:主机运行美分操作系统的服务器,如果这能帮助

PS: Host is a server running Cent-OS, if that helps.

推荐答案

除了奥莱丹下的解决方案(即看起来很大),如果你的计算有pretty类似的持续时间,你可以尝试这样的事:

Apart the Ole Tange solution (that looks great), if your computations have pretty similar durations, you can try something like this :

i=0  
upperlimit=$verylargevariable  
do  
   complexstuff RunManager file $i &
   i= 'expr $i + 1'
   complexstuff RunManager file $i &
   i= 'expr $i + 1'
   complexstuff RunManager file $i &
   i= 'expr $i + 1'
   complexstuff RunManager file $i &
   i= 'expr $i + 1'
   wait
done

这样,在循环的每次运行,将创建4 bash的子进程,将启动您的计算(和系统是伟大的,它会派遣他们在不同的内核)。如果有4个进程是不够烧所有的CPU,提高在每个循环创建的进程数。

This way, on each run of the loop, you will create 4 bash subprocesses that will launch your computations (and as system is great, it will dispatch them on the different cores). If with 4 processes it is not enough to burn all your cpus, raise the number of processes created on each loop.

这篇关于在多个内核bash的运行过程循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆