How to get the data from all the workers to a single worker inside SPMD loop

3 views (last 30 days)
Nc=3600;
spmd
n1=numlabs;
blocksize = ceil(Nc/n1);
start = (labindex-1)*blocksize + 1;
finish = min(Nc, start+blocksize-1);
for n=finish:-1:start
A_sample=rand(600, 53)
index_cpu = n-start+1;
B_sample (:,:,index_cpu)=A_sample; % each worker has its own copy of B_sample.
end
end
B_collect=cat(3,B_sample{:}); % B_sample is concatenated (collected from all workers) to a single worker outside the SPMD loop
In the above code, consider 4 workers (numlabs) are used inside parallel loop
  1. A_sample data is copied into B_sample inside SPMD.
  2. After this, inorder to collect the B_sample data from all the workers to a single variable b_collect, we concatenate the data from all the workers outside the SPMD in B_collect . This concatenation takes a lot of time as Nc value increases (Time expensive)
  3. In order to avoid the concatenation, I need to assign a variable say C_sample and make all the 4 workers to write to that variable C_sample inside SPMD (instead of writing in its own copy of variable). I think this might avoid the time expense.
I have two questions now :
  1. How should I do the 3RD point i.e collect the results in C_sample variable inside SPMD (i.e Say C_collect is a global variable and each worker updates in that variable, which will avoid the concatenation issue)
  2. As each worker (1,2,3,4) has its own copy of B_sample in a composite format, is there any faster way i can send the data of B_sample from workers(2,3,4) to worker 1 inside the SPMD?
I have attached the image and pdf of the same for a better understanding of the question.
mathworks_spmd_question.PNG
  2 Comments
Walter Roberson
Walter Roberson on 30 Jun 2019
labSend is the primary method to transmit data between workers.
If you are willing to work out the synchronization issues yourself then the File Exchange contribution sharedmatrix might be useful.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!