Filter löschen
Filter löschen

Sliced variables in parfor loop (Fixed Index Listing)

1 Ansicht (letzte 30 Tage)
kira
kira am 20 Okt. 2015
Beantwortet: Walter Roberson am 16 Dez. 2015
Hi,
I'm trying to parallelize the following code (you can see commented the original two nested for loops), but i have an issue with Variable Data. What the code does is basically to iterate N times certain functions with the same parameter n (to obtain some statistics) and I'm saving all the interesting output in variable Data. Since I have 14 different functions to test (i just add 5 in the code bellow), I had to call Data{1,..., Data{2,..., Data{3,..., etc. I've been trying to figure out a way to use some temporal variables but with no luck.
Your help is very much appreciated and feel free to ask for any further details.
clear all;
init=3;
fin=4;
nn=init:fin;
cuantos=length(nn);
p=0.5;
r=1;
e=1;
N=100;
maxiter=20000;
plot=false;
Data=cell(14,2);
Data{1,1}='RU';
Data{2,1}='RMU';
Data{3,1}='RUIH';
Data{4,1}='RMUIH';
Data{5,1}='RUIHS';
Data{1,2}=zeros(2,100,cuantos);
Data{2,2}=zeros(2,100,cuantos);
Data{3,2}=zeros(2,100,cuantos);
Data{4,2}=zeros(2,100,cuantos);
Data{5,2}=zeros(2,100,cuantos);
% progressbar;
% k=1;
% iter=cuantos*N;
% for n=1:cuantos
parfor k=1:cuantos*N
[i,n] = ind2sub([N,cuantos], k);
x=GenerateAltConf(nn(1),nn(1),e);
E=GenerateLine(length(x),r);
% for i=1:N
% progressbar(k/iter);
[~,~,Data{1,2}(1,i,n),Data{1,2}(2,i,n)]=Automata1DCG_RU(x,E,r,maxiter,plot);
[~,~,Data{2,2}(1,i,n),Data{2,2}(2,i,n)]=Automata1DCG_RMU(x,E,r,maxiter,plot);
[~,~,Data{3,2}(1,i,n),Data{3,2}(2,i,n)]=Automata1DCG_RUIH(x,E,r,maxiter,plot);
[~,~,Data{4,2}(1,i,n),Data{4,2}(2,i,n)]=Automata1DCG_RMUIH(x,E,r,maxiter,plot);
[~,~,Data{5,2}(1,i,n),Data{5,2}(2,i,n)]=Automata1DCG_RUIHS(x,E,r,maxiter,plot);
%k=k+1;
%end
end

Antworten (1)

Walter Roberson
Walter Roberson am 16 Dez. 2015
parfor k=1:cuantos*N
x=GenerateAltConf(nn(1),nn(1),e);
E=GenerateLine(length(x),r);
[~,~,D11(k),D12(k)] = Automata1DCG_RU(x,E,r,maxiter,plot);
[~,~,D21(k),D22(k)] = Automata1DCG_RMU(x,E,r,maxiter,plot);
[~,~,D31(k),D32(k)] = Automata1DCG_RUIH(x,E,r,maxiter,plot);
[~,~,D41(k),D42(k)] = Automata1DCG_RMUIH(x,E,r,maxiter,plot);
[~,~,D51(k),D52(k)] = Automata1DCG_RUIHS(x,E,r,maxiter,plot);
end
Data{1,2}(1,:,:) = reshape(D11, cuantos, N);
Data{1,2}(2,:) = reshape(D12, cuantos, N);
Data{2,2}(1,:) = reshape(D21, cuantos, N);
Data{2,2}(2,:) = reshape(D22, cuantos, N);
Data{3,2}(1,:) = reshape(D31, cuantos, N);
Data{3,2}(2,:) = reshape(D32, cuantos, N);
...
I am not sure if you would be able to write parts of it into indexed cell arrays; I do not have the toolbox to test with. The way you used ind2sub to create two indices from the loop variable is definitely not allowed: the only portion that can change is the loop index.
My recollection at the moment is that you would not be able to index two different locations of the same cell array for output, but I am not positive of that.
In your sample it appears that you call all the different functions with the same parameter. You might want to consider
Afuns = {@Automata1DCG_RU, @Automata1DCG_RMU, @Automata1DCG_RUIH ...};
parfor k = 1 : cuantos*N
x = GenerateAltConf(nn(1),nn(1),e);
E = GenerateLine(length(x),r);
for J = 1 : length(Afuns)
thisfun = Afuns{J};
[~, ~, v(J,1), v(J,2)] = thisfun(x,E,r,maxiter,plot);
end
temp_data(:,:,k) = v;
end
and then after the parfor, loop moving the information in temp_data to its final destination, which might be something like a mat2cell()

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by