Parallel computing with switch case function

3 views (last 30 days)
B
B on 13 Feb 2022
Edited: B on 16 Feb 2022
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

Accepted Answer

Voss
Voss on 13 Feb 2022
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 Comment
B
B on 16 Feb 2022
Edited: B on 16 Feb 2022
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

Sign in to comment.

More Answers (0)

Categories

Find more on Parallel for-Loops (parfor) in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!