필터 지우기
필터 지우기

Several nested for-loops within parfor-loop variable indexing

조회 수: 16 (최근 30일)
James Floyd
James Floyd 2023년 3월 22일
편집: Matt J 2023년 3월 23일
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
Paul
Paul 2023년 3월 23일
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.

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

채택된 답변

Matt J
Matt J 2023년 3월 23일
편집: Matt J 2023년 3월 23일
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
Matt J
Matt J 2023년 3월 23일
편집: Matt J 2023년 3월 23일
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

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by