How do I create a parallel loop?

16,183

Solution 1

Like I said in my comment, you need to launch the MATLAB workers:

matlabpool open N

The parpool command replaced the matlabpool command in version R2013b. The command creates a number of local workers (assuming your default cluster is the local profile), which are simply MATLAB.exe processes running without a GUI, that execute parts of parallelized code, like your parfor loop.

Solution 2

It is not necessary needed to close the pool. In some cases you may wish to keep it open for later reuse (as opening also takes some time). Testing for a zero pool size can be helpful to decide, if a new matlabpool needs to be open:

A = zeros(1,10000000);
if matlabpool('size') == 0 
   matlabpool('open',4) ;
end
parfor i = 1:length(A)
    A(i) = i;
end

Solution 3

Since the change from matlabpool to parpool, there is an even easier way to create the pool. Unlike parpool, it doesn't throw an error if the pool already exists. Just call gcp (which stands for "get current pool").

gcp();
A = zeros(1,10000000);
parfor i = 1:length(A)
    A(i) = i;
end

It is good practice to always leave the pool open; this just ensures that it's open when you need it.

Share:
16,183
quant
Author by

quant

I'm here to learn programming and *nix mostly, and am immensely indebted to this community for always being there when I get stuck. The help and support I've received on SE sites has allowed me to develop my understanding in a range of disciplines much faster than I could ever have hoped for. I try to give back once in a while as well; it's the least I could do. I'm the owner and maintainer of www.portnovel.com

Updated on June 24, 2022

Comments

  • quant
    quant almost 2 years

    You'd think this would be simple question, but I can't find the solution. Take the following loop:

    A = zeros(1,10000000);
    parfor i = 1:length(A)
        A(i) = i;
    end
    

    This only runs on a single core on my computer, although it's readily parallelisable (or at least it should be). I am using Matlab 2012b, and I've tried looking for documentation on how to create parallel loops but can't find any (the matlab docs just show examples of how to create these loops, not how to actually run them in parallel).

    I've tried looking up how to modify parallel computing toolbox settings, but none of them work since they're all for Matlab 2013 (I'm using 2012b). If someone could provide an example of a trivial, parallelisable loop that actually runs in parallel I would be very grateful!

    Note: I have checked and the parallel computing toolbox is installed, although I have no way of knowing if it is enabled, or how to enable it, since the documentation doesn't seem to provide an answer to this for my version (I typed preferences into the command prompt but didn't see it there).

    EDIT: I got it working by doing this:

    matlabpool('open',4);
    A = zeros(1,10000000);
    parfor i = 1:length(A)
        A(i) = i;
    end
    matlabpool('close');
    

    ... but I don't really know why this works, whether I have close the pool every time, what a pool actually is (I've read the documnentation, still don't get it), and how matlabpool differs from parpool...

  • Edric
    Edric over 10 years
    Nit-pick: whether PARPOOL opens local workers or not depends on where your default configuration points. If you have only PCT, then it's true you only have access to local workers. If you have MDCS too, then you can run workers on a cluster.