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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by