2

I am still somewhat new to parallel computing in Matlab. I have used OpenMP in C successfully, but could not get better performance in Matlab.

First, since I'm machine at a university that I am new to, I verified that the machine I am on has the Parallel Computing Toolbox by typing ver in the command prompt and it displayed: Parallel Computing Toolbox Version 5.2 (R2011b). Note that the machine has 4 cores

I tried simple examples of using parfor vs. for, but for always won, though this might be because of the overhead cost. I was doing simple things like the example here: MATLAB parfor is slower than for -- what is wrong?

Before trying to apply parfor to my bigger more complicated program (I need to compute 500 evaluations of a function and each evaluation takes about a minute, so parallelizing will help here), I would very much like to see a concrete example where parfor beats for. . Examples are abundant for OpenMP, but did not find a simple example that I can copy and paste that shows parfor is better than for

Community
  • 1
  • 1
db1234
  • 797
  • 2
  • 11
  • 28
  • 1
    have you tried taking functions that take a long time to calculate? you can just make one that has a `pause(5)` in it... – Gunther Struyf Aug 13 '12 at 14:12
  • 1
    did you set matlabpool before using `parfor`? For example: `matlabpool open 4` – Serg Aug 13 '12 at 15:16
  • @Serg: No. Does `matlabpool open 4` simply mean I want to use 4 cores? If so, that would be very helpful to know. Also, would, for instance, `matlabpool open 3` mean I only use 3 out of the 4 cores? – db1234 Aug 13 '12 at 15:41
  • @GuntherStruyf: Yes, I have, though the example was complicated, which is why I wanted to check things with a simple example. I am now away from my work computer...I'll check your and Serg's suggestion tomorrow and get back – db1234 Aug 13 '12 at 16:22
  • Well..Serg seems right since here: http://www.mathworks.ch/help/toolbox/distcomp/brb2x2l-1.html it says that "Note If matlabpool is not running, a parfor-loop runs serially on the client without regard for iteration sequence." and I did not open matlabpool... – db1234 Aug 13 '12 at 16:35

3 Answers3

7

I use the following code (once per Matlab session) in order to use parfor:

pools = matlabpool('size');
cpus = feature('numCores');
if pools ~= (cpus - 1)
    if pools > 0
        matlabpool('close');
    end
    matlabpool('open', cpus - 1);
end

This leaves 1 core for other processes. Note, the feature() command is undocumented.

Serg
  • 13,470
  • 8
  • 36
  • 47
  • Great, thanks! Tried remote login and it worked. Thanks for helping me get started with `parfor` in Matlab. – db1234 Aug 13 '12 at 18:19
  • @dblazevski You are welcome. What do you mean by remote login? Is it relevant to my question here? http://stackoverflow.com/questions/11726156/matlab-parallel-processing-using-a-network-computer – Serg Aug 13 '12 at 19:03
  • Ah no, sorry...Before in one of my comments above I mentioned that I was away from a work computer. I'm at a new job and I didn't know what the actual server name was so I figured I'd try again tomorrow... Then after digging and guessing, I remotely logged in to test what you said. So, no, not relevant, but was me to some extent thinking out loud. – db1234 Aug 13 '12 at 19:20
4

There is an example of improved performance from parfor on Loren Shure's MATLAB blog.

Her example is simply computing the rank of a magic square matrix:

function ranks = parMagic(n)

ranks = zeros(1,n);
parfor (ind = 1:n)
    ranks(ind) = rank(magic(ind));  % last index could be ind,not n-ind+1
end
cjh
  • 866
  • 4
  • 16
0

Serg describes how to "enable" parallel functionality. Here is a very simple cut and paste example to test it with as requested. Simply copy and paste the follwing into an mfile and run it.

function parfortest()
enable_parallel;
pause on
tic;
N=500;
for i=1:N
    sequential_answer=slow_fun(i);
end
sequential_time=toc
tic;
parfor i=1:N
   sequential_answer=slow_fun(i);
end
parallel_time=toc
end
function result=slow_fun(x)
    pause(0.001);
    result=x;
end

If you have run the code to enable parallel as shown in the answer by Serg you should get a pretty obvious improvement in performance.

Mr Purple
  • 2,325
  • 1
  • 18
  • 15