필터 지우기
필터 지우기

How to access the indexes of elements of a replicated matrix 'A' inside an spmd block?

조회 수: 3 (최근 30일)
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
Matt J
Matt J 2019년 12월 17일
편집: Matt J 2019년 12월 17일
I have tried building a codistributed array from the replicated array 'A' but the resulting code was running very slow
The sharing that you describe forces the workers to synchronize. Doing this too often can definitely slow things down.

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2019년 12월 17일
편집: Matt J 2019년 12월 17일
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개)

카테고리

Help CenterFile Exchange에서 Distributed Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by