如何在Matlab中利用并行处理 [英] How to utilise parallel processing in Matlab

查看:129
本文介绍了如何在Matlab中利用并行处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在基于时间序列进行计算.计算的每个迭代都是独立的.谁能分享一些在Matlab中使用并行处理的技巧/在线入门?如何在实际代码中指定它?

I am working on a time series based calculation. Each iteration of the calculation is independent. Could anyone share some tips / online primers on using utilising parallel processing in Matlab? How can this be specified inside the actual code?

推荐答案

由于您可以使用Parallel工具箱,因此建议您首先检查是否可以通过简单的方法完成操作.

Since you have access to the Parallel toolbox, I suggest that you first check whether you can do it the easy way.

基本上,而不是写作

for i=1:lots
   out(:,i)=do(something);
end

您写

parfor i=1:lots
   out(:,i)=do(something);
end

然后,您使用matlabpool创建许多工作程序(如果使用工具箱,您在本地计算机上最多可以有8个工作程序;如果您还拥有Distributed Computing Server许可证,则在远程群集上最多可以有8个工作程序),并且您可以运行代码,并且当迭代由8个内核而不是1个内核运行时,可以看到不错的速度提升.

Then, you use matlabpool to create a number of workers (you can have a maximum of 8 on your local machine with the toolbox, and tons on a remote cluster if you also have a Distributed Computing Server license), and you run the code, and see nice speed gains when your iterations are run by 8 cores instead of one.

即使parfor路由最简单,也可能无法立即使用,因为您可能无法正确建立索引,或者您可能以有问题的方式引用数组等.请看mlint警告在编辑器中,阅读文档,并依靠良好的旧尝试和错误,您应该比较快地找到答案.如果您有嵌套循环,通常最好只并行最内层的循环,并确保进行大量的迭代-这不仅是好的设计,而且还减少了可能给您带来麻烦的代码量.

Even though the parfor route is the easiest, it may not work right out of the box, since you might do your indexing wrong, or you may be referencing an array in a problematic way etc. Look at the mlint warnings in the editor, read the documentation, and rely on good old trial and error, and you should figure it out reasonably fast. If you have nested loops, it's often best parallelize only the innermost one and ensure it does tons of iterations - this is not only good design, it also reduces the amount of code that could give you trouble.

请注意,尤其是如果您在本地计算机上运行代码,则可能会遇到内存问题(由于正在分页,因此可能会在并行模式下以非常慢的速度执行):每个工作人员都会获得工作空间的副本,因此如果您的计算涉及创建500MB阵列,那么8个工作人员将需要总共4GB的RAM-那么您甚至还没有开始计算父进程的RAM!此外,最好只在计算机上使用N-1个内核,这样,计算机上可能仍在运行其他进程(例如强制性防病毒...),剩下一个内核.

Note that especially if you run the code on a local machine, you may run into memory issues (which might manifest in really slow execution in parallel mode because you're paging): Every worker gets a copy of the workspace, so if your calculation involves creating a 500MB array, 8 workers will need a total 4GB of RAM - and then you haven't even started counting the RAM of the parent process! In addition, it can be good to only use N-1 cores on your machine, so that there is still one core left for other processes that may run on the computer (such as a mandatory antivirus...).

这篇关于如何在Matlab中利用并行处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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