How to speed up simulation containing multiple for Loops?
古いコメントを表示
I have a script :
for (DBuck_On = 0.99:-0.09:0.01)
for (Mode_Control=0:1:3)
for(DT_Comp_Mode=0:1:3)
for(Fs=50e3:50e3:200e3)
for(DT_On = -500e-9:50e-9:500e-9)
for(DT_Off =-500e-9:50e-9:500e-9)
for (T_On_Min = 50e-9 : 50e-9 : 250e-9)
for (T_Off_Min = 50e-9 : 50e-9 : 250e-9)
for (Aux_Control = 0:1)
for (T_Aux_On_Min = [50e-9 60e-9] )
for (T_Aux_Off_Min = [50e-9 60e-9] )
for( DT_Aux_On =[50e-9 100e-9 200e-9])
for ( DT_Aux_Off =[50e-9 100e-9 200e-9])
Ts=1/Fs;
DBuck_Off = 1-DBuck_On;
if (Mode_Control==1)
DBuck_Off=0.5-DBuck_On/2;
end
D1=DBuck_On;
D2=DBuck_Off;
D3=1-DBuck_On-DBuck_Off;
sim('DPWMIgalSimulation_Comparison_RoiVersion_Vs_MyVersion');
MyRawData = csvread(strcat(pwd,'\MyTempTestVector.csv'));
RoiRawData = csvread(strcat(pwd,'\RoiTempTestVector.csv'));
if (~isequal(MyRawData,RoiRawData))
if(Flag)
MyData=MyRawData;
RoiData=RoiRawData;
Flag=false;
continue;
end
MyData=[MyData;MyRawData]
RoiData=[RoiData;RoiRawData]
end
end
end
end
end
end
end
end
end
end
end
end
end
end
I the simulation recieves the input(DbuckOn, Mode,DT...) and output single vector , into MyRawData/RoiRawData then i accumulate vectors that are not the same in two Databases :MyData and RoiData. This Data i will save in CSV. Is there a way to use all processors cores in order to execute the for loops faster? I tried parfor but it seems not to work(I use Matlab2016 trial). Any other suggestions for speed optimizations? Thank you
回答 (2 件)
Titus Edelhofer
2016 年 6 月 14 日
Hi,
In addition, I would suggest to replace your many loops by ndgrid, something like
[DBuck_OnVec, Mode_ControlVec, DT_Comp_ModeVec] = ndgrid(0.99:-0.09:0.01, 0:3, 0:3);
parfor cnt = 1:numel(DBuck_On)
DBuck_On = DBuck_OnVec(cnt);
...
sim(...)
end
Titus
3 件のコメント
Titus Edelhofer
2016 年 6 月 16 日
Hi,
that's right. The different workers would need to synchronize, what the current value of Ind is and therefore loop iterations would not be independent.
You could do the following
MyData = cell(1, length(DBuck_On));
RoiData = cell(1, length(DBuck_On));
parfor k=1:length(DBuck_On)
MyData{k} = {};
RoiData{k} = {};
...
if ~isequal(...)
MyData{k}{end+1} = MyRawData;
RoiData{k}{end+1} = RoiRawData;
end
end
This way you would end up with cell arrays of cell arrays, which you could combine again into one:
MyData = [MyData{:}];
Titus
Titus Edelhofer
2016 年 6 月 17 日
Hi Omer,
usually it's best to put the call to sim into a function. Pass the variables to the function as they are needed, something like
function y = simulate(k, Ts, Mode_Control, DT_Comp_Mode)
sim('DPWMIgalSimulation_Comparison_RoiVersion_Vs_MyVersion', 'SrcWorkspace', 'current');
end
Why k? You should make sure, that the filename includes the "k", otherwise parallel simulations would write into the same .csv file.
Read the csv files afterwards (i.e., after the call to "simulate") and delete them in order to not have hundreds of files.
Titus
6 件のコメント
omer
2016 年 6 月 19 日
omer
2016 年 6 月 19 日
Titus Edelhofer
2016 年 6 月 21 日
Hi Omer,
have you tried to use the output from Simulink?
simOut = sim('model', 'SrcWorkspace', 'current');
This should work, if you have output ports on top level and set the corresponding entry in simulation parameters (Ctrl-E, Data Import/Export), or by passing as parameter
simOut = sim('model', 'SrcWorkspace', 'current', 'SaveOutput', 'on');
Titus
omer
2016 年 6 月 21 日
Titus Edelhofer
2016 年 6 月 21 日
Hi Omer,
two comments: I guess the warning at the beginning is what you might have missed. Closing the parpool and opening it again might already help.
Second, why are you assigning the variables to base workspace? The parameter 'SrcWorkspace', 'Current' tells Simulink to use the variables from the current workspace anyway. No need to assign them all to base (only you need to be consistent in naming).
Titus
omer
2016 年 6 月 21 日
カテゴリ
ヘルプ センター および File Exchange で Whos についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


