Borrar filtros
Borrar filtros

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

3 visualizaciones (últimos 30 días)
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 comentario
Matt J
Matt J el 17 de Dic. de 2019
Editada: Matt J el 17 de Dic. de 2019
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.

Iniciar sesión para comentar.

Respuesta aceptada

Matt J
Matt J el 17 de Dic. de 2019
Editada: Matt J el 17 de Dic. de 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

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by