How to access the indexes of elements of a replicated matrix 'A' inside an spmd block?
2 views (last 30 days)
Show older comments
Suppose that I have a code as given below
spmd
% assume numlabs to be 4
nx = 3; ny = 3;
A = zeros(nx,ny);
end
We know that 'A' is a matrix of size 3x3 which is locally present on each worker. How to share the rightmost column of matrix 'A' present on worker 1 with the matrix 'A' present on the worker 2 and so on. Also, if (i,j) represents the index of an element of matrix 'A' on the border of worker 1. How to access the element next to the element (i,j) which is present on the worker 2?
I have tried building a codistributed array from the replicated array 'A' but the resulting code was running very slow. Hence it of no use to me.
1 Comment
Accepted Answer
Matt J
on 17 Dec 2019
Edited: Matt J
on 17 Dec 2019
How to share the rightmost column of matrix 'A' present on worker 1 with the matrix 'A' present on the worker 2 and so on.
One way is to use gcat, e.g.,
parpool('local',4);
spmd
A=labindex*eye(3);
column3=gcat(A(:,3));
k=mod(labindex,numlabs)+1;
B=[A,column3(:,k)*10];
end
>> C=cat(3,B{:})
C(:,:,1) =
1 0 0 0
0 1 0 0
0 0 1 20
C(:,:,2) =
2 0 0 0
0 2 0 0
0 0 2 30
C(:,:,3) =
3 0 0 0
0 3 0 0
0 0 3 40
C(:,:,4) =
4 0 0 0
0 4 0 0
0 0 4 10
Also, if (i,j) represents the index of an element of matrix 'A' on the border of worker 1. How to access the element next to the element (i,j) which is present on the worker 2?
Similarly,
i=1;j=2;
A0=10*reshape(1:9,3,3),
spmd
A=A0+labindex;
B=A;
B(:)=circshift(B(:),1-labindex);
ij=gcat(B(i,j));
next=mod(labindex,numlabs)+1;
ij=ij(next);
end
>> C=cat(3,A{:}), values=[ij{:}]
C(:,:,1) =
11 41 71
21 51 81
31 61 91
C(:,:,2) =
12 42 72
22 52 82
32 62 92
C(:,:,3) =
13 43 73
23 53 83
33 63 93
C(:,:,4) =
14 44 74
24 54 84
34 64 94
values =
52 63 74 41
0 Comments
More Answers (0)
See Also
Categories
Find more on Distributed Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!