You can't do this within parfor
, where the iterations must be independent. Instead, take a look at the spmd
block, and the commands labSend
, labReceive
and labBroadcast
.
Can I send and receive data between workers while on a parfor (MATLAB)?
-
07-12-2021 - |
题
I would like to know if I could trade (send and receive) data between nodes while executing a parfor. What I would like to accomplish is something like this:
parfor i = 1:4
%Perform a computation on each column of a 4x4 matrix on 4 different workers (each worker takes one column).
%Make Worker #1 send its data to Worker #3, Worker #3 to Worker #1, Worker #2 ro Worker #4 and Worker #4 to Worker #2.
%Make other computations on the data received from the other workers
end
I would like to perform all these computations without leaving the parfor. Are there any MPI commands I can use for this? Is this achievable? If so, how could I achieve this? Thanks!
解决方案
其他提示
Data cannot be exchanged between parfor
instances. That's part of the use of parfor
, to have individual tasks, that are truly independent. There is no guarentee how many of them will be running at the same time, for instance. On some computers, parfor
will only allow 1 instance, on others it may allow 4 running at the same time, still others only 2 at a time.
Matlab as a whole doesn't do threading really, but it does have a few constructs that allow for thread-like applications. In general, however, they are quite complex, and not really useful.