这是在MATLAB中使用并行池的真正方法吗? [英] Is this a true way of using parallel pools in MATLAB?

查看:799
本文介绍了这是在MATLAB中使用并行池的真正方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用具有2个物理CPU内核的机器.而Arii_Modified函数是我写的.当我在不使用并行池的情况下按以下方式运行代码时,花费的时间为425.456

I am working with a machine having 2 physical CPU cores. and the Arii_Modified function is written by me. When I run the code as follows without using parallel pools, the time taken is 425.456 seconds

profile on
[Ps,Pd,Pv,ThetaMean,Variance,frobonius] = Arii2011_Modified(C11,C12_imag,C12_real,C13_imag,C13_real,C22,C23_imag,C23_real,C33);
profile off
profsave
span = C11+C22+C33;
total=Pd+Ps+Pv+frobonius;
save('results.mat');  

但是当我如下所示由两个工人启动并行池时:

But when I start a parallel pool with two workers as follows:

parpool('local',2)
profile on
[Ps,Pd,Pv,ThetaMean,Variance,frobonius] = Arii2011_Modified(C11,C12_imag,C12_real,C13_imag,C13_real,C22,C23_imag,C23_real,C33);
profile off
profsave
span = C11+C22+C33;
total=Pd+Ps+Pv+frobonius;
save('results.mat');
p = gcp;
delete(p)  

花费的时间是687.687秒.
我是否以一种真实的方式使用并行池?
函数Arii2011_modified中的代码完全是顺序代码.
我已经使用MEX解决方案来加速它,但是Arii2011_Modified.m代码或其中的mex函数的C ++源代码都没有使用并行编程代码吗?

The time taken is 687.687 seconds.
Am I using the parallel pool in a true way?
The code within the function Arii2011_modified is completely a sequential one.
I've used MEX solution to accelerate it but no parallel programming code has been used neither in Arii2011_Modified.m code nor in C++ source of the mex functions within it?

推荐答案

parpool本身并不能使任何代码并行运行并更快地执行.它只是创建了一个MATLAB实例池,可以用作工作程序来处理传递给它们的任务.您只是创建了一个并行池(使用parpool),但从未真正使用过它,因此,您不能指望代码执行得更快.执行时间增加仅仅是因为启动池所需的时间.

parpool alone doesn't magically make just any code run in parallel and execute faster. It simply creates a pool of MATLAB instances which can serve as workers which can process tasks you pass to them. You simply created a parallel pool (using parpool) but never actually use it therefore you can't expect your code to execute any faster. The increased execution time is simply because of the time required to start the pool.

如果您想真正使用创建的并行池,则需要将部分代码包装在parfor构造中,其中parfor中的每个迭代都是独立的,并且parfor循环的每次迭代都将移交给其中一个可用的工作器.

If you want to actually use the parallel pool you created, you'll need to wrap some part of your code within a parfor construct where each iteration within the parfor is independent and each iteration through the parfor loop will be handed off to one of the available workers.

如果您的代码实际上不是可并行化的,那么可能值得仔细研究一下Arii2011_Modified.m,看看是否可以对其中的某些部分进行矢量化处理以缩短执行时间.

If your code isn't actually parallelizable, it may be worth taking a closer look at Arii2011_Modified.m and seeing if you can perhaps vectorize some parts of it to improve execution times.

Mathworks有一些很棒的文档利用MATLAB中的并行计算优势.

The Mathworks has some great documentation on how to take advantage of parallel computing within MATLAB.

作为旁注,如果您想实际测量执行时间,则profile是执行此操作的一种不好方法,因为它会跟踪所有函数调用,因此会产生 lot 的开销.相反,您应该使用 timeit .

As a side note, if you want to actually measure execution times, profile is a bad way to do that since that introduces a lot of overhead since it's tracking all function calls. Instead you should use timeit.

这篇关于这是在MATLAB中使用并行池的真正方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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