How to create a parallel loop?

You think this will be a simple question, but I cannot find a solution. Take the following loop:

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

This only works on one core on my computer, although it can be easily parallelized (or at least should be). I am using Matlab 2012b, and I tried to find documentation on how to create parallel loops but cannot find them (the matlab docs provide examples of how to create these loops, and not how to run them in parallel).

I tried to figure out how to change the parameters of the parallel computing toolbar, but none of them work, since they are all designed for Matlab 2013 (I use 2012b). If someone could provide an example of a trivial parallelizable loop that actually works in parallel, I would be very grateful!

Note. I checked and installed a set of parallel computing tools, although I don’t know if it is enabled or how to enable it, since the documentation does not seem to give an answer for this for my version (I typed preferences on the command line, but did not see it there).

EDITOR: I got this by doing this:

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

... but I really don’t know why this works, regardless of whether I close each pool, what the pool is (I read the documnentation , still not get it) and how matlabpool differs from parpool ...

+7
parallel-processing matlab
source share
3 answers

As I said in my comment, you need to run MATLAB employees:

 matlabpool open N 

The parpool team replaced the matlabpool command in version R2013b. The command creates several local workers (assuming that your cluster is the local profile by default), which are just MATLAB.exe processes that run without a GUI that execute portions of the parallelized code, for example, your parfor .

+2
source share

There is no need to close the pool. In some cases, you can leave it open for later reuse (since opening also takes some time). Testing a zero pool size can be useful for deciding if a new matball should be open:

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

Since the change is from matlabpool to parpool , there is an even simpler way to create a pool. Unlike parpool , it does not parpool error if the pool already exists. Just call gcp (which means "get the 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; it just ensures that it will open when you need it.

+1
source share

All Articles