主要内容

简单的基准测试PARFOR使用21点

这个例子中基准parfor构造通过不断玩21点纸牌游戏,也称为21。我们使用parfor并行多次玩纸牌游戏,不同数量的MATLAB®工人,但总是使用相同数量的球员和手。

相关例子:

并行版本

基本的并行算法使用parfor构建执行独立的通过一个循环。这是MATLAB®语言的一部分,但从本质上说,行为像一个定期循环如果你没有访问并行计算工具箱™产品。因此,我们的第一步是将一个循环的形式

i = 1: numPlayers年代(:,我)= playBlackjack ();结束

到相当于parfor循环:

parfori = 1: numPlayers年代(:,我)= playBlackjack ();结束

我们修改这个稍微通过指定一个可选的参数parfor,指示限制n工人使用的数量计算。实际的代码如下:

dbtypepctdemo_aux_parforbench
1函数S = pctdemo_aux_parforbench (numHands numPlayers, n) 2% pctdemo_aux_parforbench使用parfor玩21点。S = 3% pctdemo_aux_parforbench (numHands numPlayers n)中4% numHands blackjack numPlayers手中,并使用不超过n MATLAB (R)工人5%计算。版权2007 - 2009 6 7% MathWorks公司。8 9 S = 0 (numHands numPlayers);10 parfor (i = 1: numPlayers, n) 11(:,我)= pctdemo_task_blackjack (numHands, 1);12结束

检查并行的状态池

我们将使用并行池允许的主体parfor循环并行运行,所以我们首先检查池是否开放。我们将使用2到然后运行基准poolSize工人从这个池。

p =质量;如果isempty (p)错误(“pctexample: backslashbench: poolClosed”,(这个例子需要并行池。”“手动启动一个池使用parpool命令或一组的“你平行偏好自动启动一个池。]);结束poolSize = p.NumWorkers;

运行基准:疲软的扩展

我们的执行时间基准计算使用2poolSize工人。我们使用弱比例,也就是说,我们增加的问题大小与工人的数量。

numHands = 2000;numPlayers = 6;流(“模拟每个玩家玩% d。\ n ',numHands);t1 = 0 (1、poolSize);n = 2: poolSize抽搐;pctdemo_aux_parforbench (numHands n * numPlayers, n);t1 (n) = toc;流(模拟% d % d工人球员% f 3.2秒。\ n ',n, n * numPlayers, t1 (n));结束
模拟每个玩家玩2000手。2工人模拟12玩家在10.81秒内。3工人模拟18玩家在10.67秒内。4员工模拟24玩家在10.57秒内。5员工模拟30玩家在10.57秒内。6员工模拟36玩家在10.71秒内。7工人模拟42玩家在10.63秒内。8工人模拟48玩家在10.87秒内。9员工模拟54玩家在10.54秒内。10名员工模拟60球员为10.73秒。 11 workers simulated 66 players in 10.58 seconds. 12 workers simulated 72 players in 10.68 seconds. 13 workers simulated 78 players in 10.56 seconds. 14 workers simulated 84 players in 10.89 seconds. 15 workers simulated 90 players in 10.62 seconds. 16 workers simulated 96 players in 10.63 seconds. 17 workers simulated 102 players in 10.70 seconds. 18 workers simulated 108 players in 10.70 seconds. 19 workers simulated 114 players in 10.79 seconds. 20 workers simulated 120 players in 10.72 seconds. 21 workers simulated 126 players in 10.74 seconds. 22 workers simulated 132 players in 10.75 seconds. 23 workers simulated 138 players in 10.74 seconds. 24 workers simulated 144 players in 10.72 seconds. 25 workers simulated 150 players in 10.74 seconds. 26 workers simulated 156 players in 10.76 seconds. 27 workers simulated 162 players in 10.74 seconds. 28 workers simulated 168 players in 10.72 seconds. 29 workers simulated 174 players in 10.76 seconds. 30 workers simulated 180 players in 10.69 seconds. 31 workers simulated 186 players in 10.76 seconds. 32 workers simulated 192 players in 10.76 seconds. 33 workers simulated 198 players in 10.79 seconds. 34 workers simulated 204 players in 10.74 seconds. 35 workers simulated 210 players in 12.12 seconds. 36 workers simulated 216 players in 12.19 seconds. 37 workers simulated 222 players in 12.19 seconds. 38 workers simulated 228 players in 12.14 seconds. 39 workers simulated 234 players in 12.15 seconds. 40 workers simulated 240 players in 12.18 seconds. 41 workers simulated 246 players in 12.18 seconds. 42 workers simulated 252 players in 12.14 seconds. 43 workers simulated 258 players in 12.24 seconds. 44 workers simulated 264 players in 12.25 seconds. 45 workers simulated 270 players in 12.23 seconds. 46 workers simulated 276 players in 12.23 seconds. 47 workers simulated 282 players in 12.55 seconds. 48 workers simulated 288 players in 12.52 seconds. 49 workers simulated 294 players in 13.24 seconds. 50 workers simulated 300 players in 13.28 seconds. 51 workers simulated 306 players in 13.36 seconds. 52 workers simulated 312 players in 13.53 seconds. 53 workers simulated 318 players in 13.98 seconds. 54 workers simulated 324 players in 13.90 seconds. 55 workers simulated 330 players in 14.29 seconds. 56 workers simulated 336 players in 14.23 seconds. 57 workers simulated 342 players in 14.25 seconds. 58 workers simulated 348 players in 14.32 seconds. 59 workers simulated 354 players in 14.26 seconds. 60 workers simulated 360 players in 14.34 seconds. 61 workers simulated 366 players in 15.60 seconds. 62 workers simulated 372 players in 15.75 seconds. 63 workers simulated 378 players in 15.79 seconds. 64 workers simulated 384 players in 15.76 seconds.

我们比较这对使用定期执行在MATLAB®循环。

抽搐;S = 0 (numHands numPlayers);i = 1: numPlayers年代(:,我)= pctdemo_task_blackjack (numHands, 1);结束t1 (1) = toc;流('跑在% 3.2 f秒使用一个连续的循环。\ n”,t1 (1));
在10.70秒内跑使用顺序循环。

绘制加速

我们比较了加速使用parfor与不同数量的工人完全线性加速曲线。加速通过使用parfor取决于问题的大小以及底层硬件和网络基础设施。

加速= (1:poolSize)。* t1 (1) / t1;无花果= pctdemo_setup_blackjack (1.0);fig.Visible =“上”;ax =轴(“父”图);x =情节(ax, 1: poolSize, 1: poolSize,“——”,1:poolSize,加速,“年代”,“MarkerFaceColor”,“b”);t = ax.XTick;t (t ~ =圆(t)) = [];%删除所有非整数轴蜱虫。斧子。XTick = t;传奇(x,“线性加速”,“测量加速”,“位置”,“西北”);包含(ax,MATLAB工人参与计算的数量);ylabel (ax,“加速”);

测量速度分布

获得可靠的基准数据,我们需要多次运行基准。因此,我们运行基准多次poolSize工人让我们看的扩散加速。

numIter = 100;t2 = 0 (1、numIter);i = 1: numIter抽搐;pctdemo_aux_parforbench (numHands poolSize * numPlayers poolSize);t2 (i) = toc;如果国防部(我20)= = 0流(“基准运行% d % d *。\ n”,我,numIter);结束结束
基准运行20 100倍。基准运行40的100倍。基准运行60 100倍。基准已跑了80 100次。基准已跑了100 100次。

情节加速分布

我们仔细看看我们简单的并行程序的加速使用时的最大数量的工人。加速的直方图,使我们能够区分异常值和平均加速。

加速= t1 (1)。/ t2 * poolSize;clf(图);ax =轴(“父”图);嘘(加速,5);=轴(ax);(4)= 5 *装天花板((4)/ 5);%圆轴最近的5的倍数。轴(ax)包含(ax,“加速”);ylabel (ax,“频率”);标题(ax, sprintf (的加速parfor % d工人poolSize));m =值(加速);流([的平均加速% 3.2 f,对应的“% 3.2 f。\ n效率”),m, m / poolSize);
加速中位数是43.37,0.68对应的效率。