How to synchronize parallel code execution while also sharing data?

조회 수: 6 (최근 30일)
Muhammad Sarim Mehdi
Muhammad Sarim Mehdi 2018년 12월 5일
댓글: Vineet Ahirkar 2018년 12월 10일
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