MATLAB parfor比forpar慢-怎么了? [英] MATLAB parfor is slower than for -- what is wrong?

查看:362
本文介绍了MATLAB parfor比forpar慢-怎么了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理的代码具有如下循环:

the code I'm dealing with has loops like the following:

bistar = zeros(numdims,numcases); 
parfor hh=1:nt       
  bistar = bistar +  A(:,:,hh)*data(:,:,hh+1)' ;
end   

对于小nt(10).

for small nt (10).

计时之后,实际上比使用常规循环慢了 100倍 !!!我知道parfor可以进行并行求和,所以我不确定为什么这不起作用.

After timing it, it is actually 100 times slower than using the regular loop!!! I know that parfor can do parallel sums, so I'm not sure why this isn't working.

我跑步

matlabpool

使用开箱即用的配置,然后再运行我的代码.

with the out-of-the-box configurations before running my code.

我对Matlab还是比较陌生,刚开始使用并行功能,所以请不要以为我没有做任何愚蠢的事情.

I'm relatively new to matlab, and just started to use the parallel features, so please don't assume that I'm am not doing something stupid.

谢谢!

PS:我正在四核上运行代码,因此我希望看到一些改进.

PS: I'm running the code on a quad core so I would expect to see some improvements.

推荐答案

对结果进行分区和分组(划分工作和从多个线程/核心收集结果的开销)很高.这是正常现象,您不会对可以在一个简单循环中快速执行的简单任务进行数据分区.

Making the partitioning and grouping the results (overhead in dividing the work and gathering results from the several threads/cores) is high for small values of nt. This is normal, you would not partition data for easy tasks that can be performed quickly in a simple loop.

始终在循环内执行具有挑战性的操作,值得进行分区.这是一个很好的并行编程简介.

Always perform something challenging inside the loop that is worth the partitioning overhead. Here is a nice introduction to parallel programming.

线程来自线程池,因此创建线程的开销不应该存在.但是,为了从bistar大小创建部分结果n矩阵,必须计算所有部分结果,然后必须添加所有这些部分结果(重新组合).在一个直线循环中,这很可能就地完成,没有分配发生.

The threads come from a thread pool so the overhead of creating the threads should not be there. But in order to create the partial results n matrices from the bistar size must be created, all the partial results computed and then all these partial results have to be added (recombining). In a straight loop, this is with a high probability done in-place, no allocations take place.

帮助中的完整声明(感谢您在此处的链接)为:

The complete statement in the help (thanks for your link hereunder) is:

如果计算f,g和h的时间为 ,parfor将会非常明显 快于对应的 陈述,即使n相对 小.

If the time to compute f, g, and h is large, parfor will be significantly faster than the corresponding for statement, even if n is relatively small.

因此,您将看到它们的含义与我的意思完全相同,只有当您在循环中执行的操作很复杂/非常耗时时,小n值的开销才值得付出努力.

So you see they mean exactly the same as what I mean, the overhead for small n values is only worth the effort if what you do in the loop is complex/time consuming enough.

这篇关于MATLAB parfor比forpar慢-怎么了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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