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
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 Center および File Exchange で Resizing and Reshaping Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!