Fill an array with spmd in Matlab

163 views Asked by At

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 ?

1

There are 1 answers

0
Edric On BEST ANSWER

Firstly, if your function evaluations are independent, you might well be better off using parfor, like so:

R=zeros(p,1);
parfor i=1:p
    R(i)=myfunction(parameters(i));
end

spmd is generally only useful when you need communication between iterations. In any case, you can run this sort of thing inside spmd using the for-drange construct like so:

spmd
    R = codistributed.zeros(p, 1)
    for i = drange(1:p)
        R(i) = myfunction(parameters(i));
    end
end

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" both R and parameters)