I have a 1-by-p array R in Matlab (with p large). I initialize this array with all its entries equal to 0 and the i-th element of the array R shall receive the output from myfunction
, applied to parameters(i)
. In other words :
R=zeros(p,1);
for i=1:p
R(i)=myfunction(parameters(i));
end
The same function myfunction
is applied multiple times with different input. And because p might become large, I recognized a spmd
problem (single program, multiple data) and thought that using the spmd
construct would help the previous code run faster.
If I run matlabpool
, I obtain n_workers different labs. My idea is to break the array R into n_workers different parts and ask each available worker to fill a part of the array. I would like to do something like this :
q=((p-1)-mod(p-1,n_workers-1))/(n_workers-1);
lab 1:
for j=1:q
R(j) = myfunction(parameters(j));
end
lab 2:
for j=(q+1):(2*q+1)
R(j) = myfunction(parameters(j));
end
...
lab n_workers:
for j=( q*(n_workers-1)+1 ):p
R(j) = myfunction(parameters(j));
end
However, since I'm new to the parallel programming, I don't know how to write this properly in Matlab. Instead of subdividing myself the array R, could I use a coditributed
array instead ?
Firstly, if your function evaluations are independent, you might well be better off using
parfor
, like so:spmd
is generally only useful when you need communication between iterations. In any case, you can run this sort of thing insidespmd
using thefor-drange
construct like so:In that case, you'd probably also want to make
parameters
be a distributed array too to avoid having multiple copies in memory. (parfor
automatically avoids that problem by "slicing" bothR
andparameters
)