MATLAB: Parfor inefficiency for large matrix operations

parfor

Good day all
I have two large matrices which I need to compute in a for loop. I know the index of the for loop is small, but the computations take long due to the size of the matrices. Could anyone tell me why there is no speed boost using parfor? I also tried different matrix sizes and parfor never works. I have a quad core PC that makes 4 parallel workers. Here is the code:
if true
parpool(4)
N = 5000;P=2000;
mean = zeros(N,10);var = zeros(N,10);
t1 = ones(P,N);t2 = eye(P);
tic
for jj = 1:4
diag(t1.'*(t2\(t2.'\t1)));
end
toc
t1 = repmat(t1,1,1,4);t2 = repmat(t2,1,1,4);
tic
for jj = 1:4
diag(t1(:,:,jj).'*(t2(:,:,jj)\(t2(:,:,jj).'\t1(:,:,jj))));
end
toc
end
I duplicate the code and add par in front of the for to get the parfor result.
The result I got with for was:
Elapsed time is 6.292219 seconds.
Elapsed time is 6.280422 seconds.
And with parfor:
Elapsed time is 7.617721 seconds.
Elapsed time is 6.583254 seconds.
I have also tried using this code:
to first send the data to each worker. There was no improvement.
Many thanks in advance for any insights.

Best Answer

This appears to be because MATLAB's intrinsic multi-threading is already doing a decent job of running your code in parallel. When this is the case, parfor using local workers tends to be no faster - because you have no additional hardware to apply to the problem. ( parfor is often slower in these sort of cases because of the overhead of transferring the data to the workers).
If you have MDCS workers available, then it's possible you might see speedup - because that way you're using more hardware than just your local machine.