Parallel computing with switch case function

조회 수: 3 (최근 30일)
B
B 2022년 2월 13일
편집: B 2022년 2월 16일
Hi all,
Please I have this code and want to run it in parallel ccomputing or cluster mode . How do i go by this?
clc; clear; tic
Sample_C = dir('*C*/Sampleution_*txt');
Sample_6_var_126_C = []; Sample_7_var_126_C = []; Sample_8_var_126_C = [];
Sample_9_var_126_C = []; Sample_10_var_126_C = []; Sample_11_var_126_C = [];
Sample_12_var_126_C = []; Sample_13_var_126_C = []; Sample_14_var_126_C = [];
Sample_15_var_126_C = []; Sample_16_var_126_C = [];
for i = 1:length(Sample_C)
A = dlmread(strcat(Sample_C(i).folder,'/',Sample_C(i).name),'',1,0);
n = max(A(:,2)); B(i,:) = n; C(i,:) = Sample_C(i).name; Ls(i,:) = [num2cell(B(i,:)) cellstr(C(i,:))];
switch n
case 6
Sample_6_var_126_C = [Sample_6_var_126_C A];
Sample6 = 'E:\Kotty\Sampleution\Sampleution_sort';
directorypath = fullfile(Sample6,"Sample_6_var_126_C.mat");
save(directorypath,'Sample_6_var_126_C','-v7.3');
case 7
Sample_7_var_126_C = [Sample_7_var_126_C A];
Sample7 = 'E:\Kotty\Sampleution\Sampleution_sort';
directorypath = fullfile(Sample7,"Sample_7_var_126_C.mat");
save(directorypath,'Sample_7_var_126_C','-v7.3');
case 8
Sample_8_var_126_C = [Sample_8_var_126_C A];
Sample8 = 'E:\Kotty\Sampleution\Sampleution_sort';
directorypath = fullfile(Sample8,"Sample_8_var_126_C.mat");
save(directorypath,'Sample_8_var_126_C','-v7.3');
case 9
Sample_9_var_126_C = [Sample_9_var_126_C A];
Sample9 = 'E:\Kotty\Sampleution\Sampleution_sort';
directorypath = fullfile(Sample9,"Sample_9_var_126_C.mat");
save(directorypath,'Sample_9_var_126_C','-v7.3');
case 10
Sample_10_var_126_C = [Sample_10_var_126_C A];
Sample10 = 'E:\Kotty\Sampleution\Sampleution_sort';
directorypath = fullfile(Sample10,"Sample_10_var_126_C.mat");
save(directorypath,'Sample_10_var_126_C','-v7.3');
case 11
Sample_11_var_126_C = [Sample_11_var_126_C A];
Sample11 = 'E:\Kotty\Sampleution\Sampleution_sort';
directorypath = fullfile(Sample11,"Sample_11_var_126_C.mat");
save(directorypath,'Sample_11_var_126_C','-v7.3');
case 12
Sample_12_var_126_C = [Sample_12_var_126_C A];
Sample12 = 'E:\Kotty\Sampleution\Sampleution_sort';
directorypath = fullfile(Sample12,"Sample_12_var_126_C.mat");
save(directorypath,'Sample_12_var_126_C','-v7.3');
case 13
Sample_13_var_126_C = [Sample_13_var_126_C A];
Sample13 = 'E:\Kotty\Sampleution\Sampleution_sort';
directorypath = fullfile(Sample13,"Sample_13_var_126_C.mat");
save(directorypath,'Sample_13_var_126_C','-v7.3');
case 14
Sample_14_var_126_C = [Sample_14_var_126_C A];
Sample14 = 'E:\Kotty\Sampleution\Sampleution_sort';
directorypath = fullfile(Sample14,"Sample_14_var_126_C.mat");
save(directorypath,'Sample_14_var_126_C','-v7.3');
case 15
Sample_15_var_126_C = [Sample_15_var_126_C A];
Sample15 = 'E:\Kotty\Sampleution\Sampleution_sort';
directorypath = fullfile(Sample15,"Sample_15_var_126_C.mat");
save(directorypath,'Sample_15_var_126_C','-v7.3');
case 16
Sample_16_var_126_C = [Sample_16_var_126_C A];
Sample16 = 'E:\Kotty\Sampleution\Sampleution_sort';
directorypath = fullfile(Sample16,"Sample_16_var_126_C.mat");
save(directorypath,'Sample_16_var_126_C','-v7.3');
end
end
toc
Ls = 'E:\Kotty';
directorypath = fullfile(Ls,"Ls.mat");
save(directorypath,'Ls','-v7.3');
Thank you,
Kitty

채택된 답변

Voss
Voss 2022년 2월 13일
Well, you can run it in parallel by using a parfor loop instead of a for loop (if you have the Parallel Computing Toolbox).
But I would also like to make a suggestion regarding the practice of having several very similarly named variables leading to highly redundant code like you have here. You can replace all those variables named *_6_* through *_16_* with a single cell array with 11 elements, each element of which contains the contents of one of your old numbered variables. This will simplify your code significantly (but won't make much difference in terms of speed probably).
However, you can avoid saving to mat file each time through the loop and just save once at the end. This may likely significantly decrease the time it takes your code to run, so perhaps you won't need to use parallel computing at all.
Something like this:
clc; clear; tic
Sample_C = dir('*C*/Sampleution_*txt');
Sample_var_126_C = cell(1,11);
Ls = cell(numel(Sample_C),2);
for i = 1:numel(Sample_C)
A = dlmread(strcat(Sample_C(i).folder,'/',Sample_C(i).name),'',1,0);
n = max(A(:,2));
Ls(i,:) = {n Sample_C(i).name};
% don't do anything to Sample_var_126_C if n is out of bounds
if n < 6 || n > 16
continue
end
% n = 6 goes to index 1 in cell array Sample_var_126_C
% n = 7 goes to index 2 in cell array Sample_var_126_C
% ...
% n = 16 goes to index 11 in cell array Sample_var_126_C
% append A to the appropriate cell:
Sample_var_126_C{n-5} = [Sample_var_126_C{n-5} A];
end
% now write the mat-files:
my_dir = 'E:\Kotty\Sampleution\Sampleution_sort';
% three different options here (select an option or try different ones):
option = 3;
if option == 1
% (1) keep the separate mat-files like you had it:
for n = 6:16
S = struct(sprintf('Sample_%d_var_126_C',n),Sample_var_126_C{n-5});
directorypath = fullfile(my_dir,sprintf('Sample_%d_var_126_C.mat',n));
save(directorypath,'-struct','S','-v7.3');
end
elseif option == 2
% (2) or maybe it's a good idea to write everything in one mat-file as a cell array:
directorypath = fullfile(my_dir,'Sample_var_126_C.mat');
save(directorypath,'Sample_var_126_C','-v7.3');
else
% (2) or write to one mat-file all the different numbered variables you had before:
S = struct();
for n = 6:16
S.(sprintf('Sample_%d_var_126_C',n)) = Sample_var_126_C{n-5};
end
save(directorypath,'-struct','S','-v7.3');
end
toc
% now save the Ls.mat file
% (I think there was a mistake here because you were overwriting the
% variable Ls with this directory ('E:\Kotty') rather than writing the Ls
% that had been built up in the for loop, so I changed it to write the Ls
% from the for loop.)
% Ls = 'E:\Kotty';
directorypath = fullfile('E:\Kotty','Ls.mat');
save(directorypath,'Ls','-v7.3');
  댓글 수: 1
B
B 2022년 2월 16일
편집: B 2022년 2월 16일
Thanks Level6, this works well. Much appreciated. Still couldn't run it with many workers though. It takes about an hour to run with one worker, but I have twelve workers I could use and produce the result faster.
Kitty

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by