MATLAB: Log() much slower in parallel

logMATLABparallelParallel Computing Toolboxslow

I run a MATLAB parallel job using the local scheduler to increase performance of the calculation. However, my function is much slower in the parallel job compared to its serial execution. By profiling I found out that some operations take much more time in parallel tasks (e.g. calling the built-in function log() about 6 times more). Any idea why it is slower?
A simplified example, let us have the following three functions:
function par_test()
Scheduler = findResource('scheduler', 'type', 'local');
Job = createJob(Scheduler);
createTask(Job, @par_func, 1, {});
submit(Job);
waitForState(Job, 'finished')
OutputArgCell = getAllOutputArguments(Job);
pInfoVector = [OutputArgCell{:, 1}];
mpiprofile('viewer', pInfoVector);
destroy(Job);
end
function pInfo = par_func()
mpiInit;
mpiprofile on
par_calc()
pInfo = mpiprofile('info');
end
function par_calc()
U = unifrnd(0, 1, 10000, 10000);
R = log(U);
end
Run
>> par_test
to execute par_calc in one parallel task and generate a profile report.
Run
>> profile on
>> par_calc
>> profile viewer
to execute par_calc serially and generate a profile report. Compare the execution time of log().

Best Answer

log() is, I believe, normally automatically parallized over the number of available cores, even without the Parallel Computing Toolbox. If you have 6 cores then 1/6 of the work (approximately) would go to each core. But then when you run in parallel, each session only has access to a single core and cannot distribute the session's work.