How to synchronize parallel code execution while also sharing data?

6 次查看(过去 30 天)
myCluster = parcluster('local'); myCluster.NumWorkers=I; saveProfile(myCluster);
parpool(I)
spmd
i=labindex;
for j=0:100
%each worker in matlab is assigned a node and each worker indexes into
%the matrices accordingly
c_t=c(i,:); x_t=x(:,i); A_t=A{i}; a_t=a(i,:); X_t=X(:,i);
mem_num_t=mem_num(1,i); mem_den_t=mem_den(1,i);
[x_hat,v_hat,mem_num_t,mem_den_t] = node(x_t,c_t,beta,a_t,A_t,b,v,j,mem_num_t,mem_den_t,X_t);
v_hat
v(:,i) = v_hat;
labBarrier;
end
end
In the above code I have I workers (in my example let I be 5). So, each worker executes the function node (I am implementing a distributed algorithm). The variables c, x, A, a and X are all variables declared before the spmd section. I am indexing them according to the worker id. However, in the code I noticed that I always end up indexing the first row and columns of the matrices. The same happens in the end where I want to store the column vector into the matrix v (which was also declared before spmd) but the line
v(:,i) = v_hat;
only indexes the first column of my v matrix and I cannot understand how I can index the matrix v according to worker id. I have searched online and am unable to solve this query. Hopefully, someone experienced here can answer me. Thank you!
  1 个评论
Vineet Ahirkar
Vineet Ahirkar 2018-12-10
Try to print the value of "i" just before using it for indexing to verify its value.
I tried to create a similar script and was able to get the correct results on my end.
Try running this script and check the results -
I = 5;
if isempty(gcp('nocreate'))
parpool(I)
end
v = rand(3,5);
disp('value of v: ');
disp(v);
spmd
disp(labindex);
for j=0:4
disp(j)
v(:,labindex) = rand(3, 1);
disp('value of v: ');
disp(v);
end
labBarrier;
end
Also, have a look at composite objects, they might help you in this scenario.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by