Reputation: 185
I have a very simple script that calls the built-in genetic algorithm function:
function test1(gen)
options = gaoptimset('UseParallel', 'always', 'Vectorized', 'off');
tic;
x = ga(@dejong5fcn, 2, [], [], [], [], [], [], [], options);
toc
end
First, I ran test1 without starting matlabpool. As expected, it runs fine but uses only one CPU core as observed with Windows Resource Monitor. It takes 4.2 seconds to run 20020 fitness evaluations. Then, I started the parallel engine with: "start matlabpool local 4" and then performed an otherwise identical run of test1. It runs and uses all four CPU cores, but takes about 90.7 seconds to perform 20020 fitness evaluations.
What am I not understanding about parallelism in Matlab R2012a (on Windows 7 64 bit)? Thanks for any help.
Upvotes: 1
Views: 290
Reputation: 24127
When you parallelize an algorithm, there is an overhead involved in communicating between the several parallel operations, and passing data back and forth between them. In this case you have a fairly large number of fairly small operations, and the overhead is swamping any speedup from parallelization.
Try parallelizing a smaller number of larger operations, and you should see a much better speedup.
By the way, this is the reason that the UseParallel
option is not on by default. When you set it to true, you are telling the Parallel Computing Toolbox that you know that the problem will benefit from parallelization (not all problems do), and that you are giving it permission to parallelize the algorithm.
Upvotes: 1