If you want to run a parallel task, you must remember that it is bad to have too many fast iterations and it is bad to have too few slow iterations. If you do a million iterations, each of which takes several milliseconds, the overhead from parallelization will destroy any possible gain. If you perform nine iterations that take an hour each and you run it on eight processors in parallel, seven processors will idle for an hour, waiting for iteration # 9 to finish.
So your example is pretty bad for checking the effects of parallelization, since both magic and rank are too fast.
function testParfor2 tic for i=1:4 pause(1); %# wait for 1 second end toc matlabpool open 4 tic parfor i=1:4 pause(1); %# wait for 1 second end toc Elapsed time is 4.050287 seconds. Elapsed time is 1.534534 seconds.
Please note that I performed the second parallel task at the same time, but roughly, the result should be reproducible: there is a bit of overhead (note that I did not take into account the time used by matlabpool !), But there is acceleration. If you increase the length of the pause, you should see the same amount of overhead. In addition, you should test your actual loops (try parallelizing the outermost loop, by the way).
To your second question:
matlabpool open
Creates as many workers as there are physical cores. Hyperthreading helps you ensure that your computer remains responsive when running a parallel job.
Finally, although magic and rank can be completely incompatible with each other, they can make calls for multi-threaded procedures.
Jonas source share