Sepideh Abadpour
Sepideh Abadpour

Reputation: 2598

Is this a true way of using parallel pools in MATLAB?

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)  

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?

Upvotes: 0

Views: 246

Answers (1)

Suever
Suever

Reputation: 65430

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.

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.

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.

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

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.

Upvotes: 1

Related Questions