Convert a for loop to parfor loop

조회 수: 4 (최근 30일)
Amita Giri
Amita Giri 2020년 2월 9일
댓글: Amita Giri 2020년 2월 10일
Hi,
In order to make my code faster, I am trying to convert for loop to parfor loop.
The original subpart of my code is (subject to error)
SNR_ind = -5:2.5:10;
run = 100;
count = 1;
for SNR_ind = SNR
error_NP = [];
error_SPVP = [];
parfor i = 1:run
%% Measurement
[S_IC,S_IT,W_T,W_C] = noise_gen(SNR_ind,SIR,A_I,S_IC1,S_IT1,I,N_I,Avg_eeg_S.avg,t_index);
[Avg_eeg_IC,~] = source_modeling(vol,elec,d_I_loc,t_index,S_IC); % With new S_IC,S_IT
[Avg_eeg_IT,dip_posm] = source_modeling(vol,elec,d_I_loc,t_index,S_IT);
X_T = Avg_eeg_S.avg + Avg_eeg_IT.avg + W_T;
X_C = Avg_eeg_IC.avg + W_C;
%% Inverse localization
[error] = Null_proj(leadfield_eeg_scan,N_S,N_I,X_C,X_T,zplane,mri_resliced,d_S_loc);
error_NP = [error_NP error];
[error] = SPVP_proj(leadfield_eeg_scan,N_S,N_I,X_C,X_T,zplane,mri_resliced,d_S_loc);
error_SPVP = [error_SPVP error];
end
Eerror_NP(count,:) = error_NP;
Eerror_SPVP(count,:) = error_SPVP;
count = count+1;
end
I have used parfor in inner loop as the inner loop will run more times than the outer loop.
I am getting error - " Error: The temporary variable 'S_IC' must be set before it is used. For more information, see Parallel for Loops in MATLAB, "Uninitialized Temporaries"."
I do understand the problem that variable S_IC need to be used in the very next statement it is calculated. But I don't know how to work around. I've read the documentation, but i can't figured out how to convert into a parfor loop. How should I use parfor in this context?
  댓글 수: 2
Walter Roberson
Walter Roberson 2020년 2월 9일
error_NP = [error_NP error];
Do not do that. Assign to a row or column instead. Or if there is a different output size then assign into a cell array and convert to numeric vector afterwards.
Amita Giri
Amita Giri 2020년 2월 10일
Thanks Walter, it gives me a whole insight of how to write a series code to parallel.

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

채택된 답변

Amita Giri
Amita Giri 2020년 2월 10일
편집: Amita Giri 2020년 2월 10일
I figured out, how to solve serial for to parfor. The variable who are causing the dependency in the loop. I calculated them in a prior loop and called them afterwards. Also, it's better to use cells instead of arrays in context to parallel computing. Parfor decreased my computation time by 1/4. This is really nice to learn. The revised code of my earlier code is as follows.
SNR = -5:2.5:10;
run = 100;
count = 1;
for SNR_ind = SNR
parfor i = 1:run
[S_IC,S_IT,W_T,W_C] = noise_gen(SNR_ind,SIR,A_I,S_IC_initial,S_IT_initial,I,N_I,Avg_eeg_S.avg,t_index);
S_IC1{1,i} = S_IC;
S_IT1{1,i} = S_IT;
W_T1{1,i} = W_T;
W_C1{1,i} = W_C;
end
S_IC2{count,1} = S_IC1;
S_IT2{count,1} = S_IT1;
W_T2{count,1} = W_T1;
W_C2{count,1} = W_C1;
count = count+1;
end
count = 1;
for SNR_ind = SNR
parfor i = 1:run
[Avg_eeg_IC,~] = source_modeling(vol,elec,d_I_loc,t_index,S_IC2{count,1}{1,i}); % With new S_IC,S_IT
Avg_eeg_IC1{1,i} = Avg_eeg_IC.avg;
[Avg_eeg_IT,~] = source_modeling(vol,elec,d_I_loc,t_index,S_IT2{count,1}{1,i}); % With new S_IC,S_IT
Avg_eeg_IT1{1,i} = Avg_eeg_IT.avg;
end
Avg_eeg_IC2{count,1} = Avg_eeg_IC1;
Avg_eeg_IT2{count,1} = Avg_eeg_IT1;
count = count+1;
end
count = 1;
for SNR_ind = SNR
for i = 1:run
X_T1{1,i} = Avg_eeg_S.avg + Avg_eeg_IT2{count,1}{1,i}+ W_T2{count,1}{1,i};
X_C1{1,i} = Avg_eeg_IC2{count,1}{1,i}+ W_C2{count,1}{1,i};
end
X_T{count,1} = X_T1;
X_C{count,1} = X_C1;
count = count+1;
end
count = 1;
for SNR_ind = SNR
parfor i = 1:run
i
close all
%% Inverse localization
[error_NP{1,i}] = Null_proj(leadfield_eeg_scan,N_S,N_I,X_C{count,1}{1,i},X_T{count,1}{1,i},zplane,mri_resliced,d_S_loc);
[error_SPVP{1,i}] = SPVP_proj(leadfield_eeg_scan,N_S,N_I,X_C{count,1}{1,i},X_T{count,1}{1,i},zplane,mri_resliced,d_S_loc);
end
Eerror_NP{count,:} = cell2mat(error_NP);
Eerror_SPVP{count,:} = cell2mat(error_SPVP);
count = count+1;
end
  댓글 수: 2
Walter Roberson
Walter Roberson 2020년 2월 10일
The first line should assign to SNR instead of SNR_ind
Amita Giri
Amita Giri 2020년 2월 10일
Yeah, alright. I have incorporated the changes. Thanks

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by