Filter löschen
Filter löschen

Several nested for-loops within parfor-loop variable indexing

18 Ansichten (letzte 30 Tage)
James Floyd
James Floyd am 22 Mär. 2023
Bearbeitet: Matt J am 23 Mär. 2023
I am trying to run a Simulink model for a sensitivity analysis. Since I need to run the system with 5 different variables all with 6 different values, there are 7776 total trials I need to run. So I'm trying to use a parfor-loop to speed up the process.
I have a set of 4 nested for-loops within a parfor-loop. The problem is that MATLAB says, "The PARFOR loop cannot run due to the way variable 'savedVar' is used." How do I go about redefining or reindexing the variables such that parfor can run?
The loop structure looks something like this:
jn = 1:length(var2);
kn = 1:length(var3);
ln = 1:length(var4);
nn = 1:length(var5); % This is to avoid calling a function within the for-loop index variables
parfor i = 1:length(var1)
savedVar_temp_var2 = zeros(length(var2),length(var3),length(var4),length(var5))
savedVar_temp_var3 = zeros(length(var3),length(var4),length(var5))
savedVar_temp_var4 = zeros(length(var4),length(var5))
savedVar_temp_var5 = zeros(length(var5)) % Preallocating for my sliced variables
for j = 1:jn
for k = 1:kn
for l = 1:ln
for n = 1:nn
parsim('mysim.slx')
savedVar_temp_var5(n) = Vars; % Vars is the stand-in here for the
% various variables I want to save during the simulation
end
savedVar_temp_var4(l,:) = savedVar_temp_var5; % Assigning the variables
% entire rows/columns at a time (recommended in "Sliced
% Variables" documentation
end
savedVar_temp_var3(k,:,:) = savedVar_temp_var4;
end
savedVar_temp_var2(j,:,:,:) = savedVar_temp_var3;
end
savedVar(i,:,:,:,:) = savedVar_temp_var2; % Allocating all temp variables to actual array
saveVariablesFunc(savedVar) % MATLAB's "save" function does not work in parfor -- adding
% a function to do it works
end
% locally defined saving function
function saveVariablesFunc(savedVar)
save('myfile.mat','savedVar','-mat')
end
  1 Kommentar
Paul
Paul am 23 Mär. 2023
Just want to point out that that use of parsim is undocumented.
I suspect there's probably a better way to accomplish the goal than using several loops. I suggest starting from this doc page for options and methods for running multiple simulations, if not read already.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt J
Matt J am 23 Mär. 2023
Bearbeitet: Matt J am 23 Mär. 2023
If you just remove the line,
saveVariablesFunc(savedVar)
form the loop it will work, after you also fix the defintion of jn...nn (they're supposed to be scalars).
I don't know what you're trying to accomplish with saveVariablesFunc. In a parallel loop, the order of the iterations is not predictable, so saving intermediate iterations to a file would not allow you to resume the loop from any well-defined starting point later on if your computer were to crash or something like that.
  1 Kommentar
Matt J
Matt J am 23 Mär. 2023
Bearbeitet: Matt J am 23 Mär. 2023
This might work, though
parfor i = 1:length(var1)
...
savedVar(i,:,:,:,:) = savedVar_temp_var2; % Allocating all temp variables to actual array
saveVariablesFunc(savedVar(i,:,:,:,:),i) % MATLAB's "save" function does not work in parfor -- adding
% a function to do it works
end
% locally defined saving function
function saveVariablesFunc(ithSlice,i)
save("myfile"+i,'ithSlice','i')
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Parallel for-Loops (parfor) finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by