How to perform the inverse operation of "spmdCat" in parallel computing

6 views (last 30 days)
In the parallel computing toolbox, the function "spmdCat" is responsible for concatenating arrays on spmd workers, for example:
B = spmdCat(A) horizontally concatenates the array A defined on each worker running an spmd block or communicating job.
Edit and run the following code:"spmdCat" in Matlab help center
parpool(4);
spmd
B = spmdCat(spmdIndex);
end
B{1}
The results are as follows:
ans =
1 2 3 4
However, there is no function in the parallel computing toolbox that performs the spmdCat inverse operation (that is, the "scatter" operation which can split arrays into spmd workers).
To implement the scatter function, I wrote the following code: "spmdsendreceive"
r_part=spmdSendReceive(spmdIndex,spmdIndex,A(i));
% The arrays A(a replicable varible on spmdworkers) to be scattered is divided into
% n(n=spmdSize) parts according to i(i is a row vector),and then A(i) is sent and
% received (in paralled?) using spmdSendReceive.
So, my question is whether the above code can be implemented in a parallel way for the "scatter" operation, and if there are other more efficient implementation methods?
Sincerely looking forward for your answers.

Answers (1)

Edric Ellis
Edric Ellis on 27 Mar 2025
A "scatter" method isn't exposed directly, but you can achieve the same thing using the codistributed constructor, like this:
spmd
if spmdIndex == 1
% Set up "x" to have the value we want on worker 1
x = rand(20);
else
% This value will be ignored
x = [];
end
% This will scatter the contents of "x" to all workers
d = codistributed(x, 1)
end

Products


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!