Hello everyone,
I'm trying to run a parfor loop to run the EnergyPlus toolbox co-simulation a bit faster (EnergyPlus Co-Simulation). I tried a lot but I'm getting an error that I'm unable to solve.
Below is my code:
ep=[];
tStart = tic;
i=0;
HVACEnergy = array2table(zeros(25,8));
Blg_Electricity = array2table(zeros(25,8));
Total_Energy = array2table(zeros(25,8));
parfor c=1:8
%%
t = getCurrentTask();
d = fullfile(tempdir, sprintf('worker_%d', t.ID));
if ~exist(d, 'dir'), mkdir(d); end
%%
ep = mlep;
ep.workDir = d;
% Building simulation file
ep.idfFile = 'C:\Users\mak19\Desktop\UBC_PhD\Trial_Two\6-Modeling\1-Trial_One\4-IHDX\DD_Winter_EE.idf';
% Weather file
ep.epwFile = 'C:\Users\mak19\Desktop\UBC_PhD\Trial_Two\6-Modeling\1-Trial_One\4-IHDX\CAN_BC_Kamloops.718870_CWEC.epw';
% Initialize
ep.initialize;
% Simulation length
endTime = 1*24*60*60; %[s]
% Logging
VN = ep.outputSigName;
VN{3} = 'HVAC_Energy';
VN{4} = 'Total_Building_Electricity';
VN{5} = 'Total_Electricity';
logTable = table('Size',[0, 1 + ep.nOut],...
'VariableTypes',repmat({'double'},1,1 + ep.nOut),...
'VariableNames',[{'Time'}; VN]);
% Start co-simulation
ep.start;
iLog = 1;
%%
% The simulation loop
t = 0;
while t < endTime
% Prepare inputs (possibly from last outputs)
u = [20 24 1 1 1 1 1 1 1];
% Get outputs from EnergyPlus
[y, t] = ep.read;
% Send inputs to EnergyPlus
ep.write(u,t);
% Log
logTable(iLog, :) = num2cell([t y(:)']);
iLog = iLog + 1;
i=i+1;
end
% Stop co-simulation process
%%
ep.stop;
%%
delete(ep)
%
HVACEnergy(:,c) = logTable(:,4);
Blg_Electricity(:,c)=logTable(:,5);
Total_Energy(:,c)=logTable(:,6);
end
TotalTime = toc(tStart);
And this is the error I'm getting:
Error using mlep/acceptSocket (line 1144)
Java exception occurred:
java.net.SocketTimeoutException: Accept timed out
at java.net.DualStackPlainSocketImpl.waitForNewConnection(Native Method)
at java.net.DualStackPlainSocketImpl.socketAccept(DualStackPlainSocketImpl.java:135)
at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:409)
at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:199)
at java.net.ServerSocket.implAccept(ServerSocket.java:545)
at java.net.ServerSocket.accept(ServerSocket.java:513)
Error in mlep/start (line 289)
obj.acceptSocket;
Many thanks in advance.
Mohamad

 채택된 답변

Raymond Norris
Raymond Norris 2021년 6월 18일

0 개 추천

I would suggest trying this first with a pool size of 1. If it works, then my guess is that mlep only allows one connection at a time.

댓글 수: 9

Mohamad Khalil
Mohamad Khalil 2021년 6월 18일
편집: Mohamad Khalil 2021년 6월 18일
Thanks, Raymond. With one pool, the code is working fine and wthout errors. However, when I try to increase to number of pools gradually, it sometimes actually work (therefore I think parfor could be applied to mlep), but other time not. I am really not sure what is happening there!
Is there a limit to the number of connections? For instance, does it always work with 2 workers, 3 workers, ... but always fail with 5, 6, 7?
If it sometimes passes with a higher number and sometimes fails with a smaller number of workers, my guess is that all the workers are trying to ping the server at the same time and connections fail quickly. Perhaps adding a random pause will help give the delay you need. I would suggest adjusting the pause also based on the number of workers running. For example:
% Add random delay to allow connection requests from each worker
p = gcp('nocreate');
pause(rand*p.NumWorkers*5)
ep = mlep;
Mohamad Khalil
Mohamad Khalil 2021년 6월 18일
편집: Mohamad Khalil 2021년 6월 18일
I implemented your suggestion, and it is working for 8 workers now, however, I can only perform 4 parallel runs, i.e. parfor c=1:4. I tried chaning the delay time, but still not working with 5+ loops! It seems as a concept the "mlep" accepts parallel computing, but there is something that prevent it from finishing the simulation. Any suggestion?
I'm missing something here. It works for 8 workers, but can only perform 4 parallel runs. It's not working with 5+ loops. How can it work for 8 workers, but only run 4 parallel runs? What happens on the 5 iteration? It hangs, it errors, etc.? And what do you mean that the sims are not finishing? Are you now no longer getting
java.net.SocketTimeoutException: Accept timed out
Yes, it only did up-to 4 parallel runs. When I'm doing 5+ parallel iterations, I am getting the same error again:
Error using mlep/acceptSocket (line 1144)
Java exception occurred:
java.net.SocketTimeoutException: Accept timed out
....
I would suggest increasing the pause for a larger number of workers
pause(rand*p.NumWorkers*15)
Mohamad Khalil
Mohamad Khalil 2021년 6월 20일
It actually did not work! I'm not sure what is going on. Something appears to be unstable as even with the pause code, the parallel loop crashes sometimes. I tried my code in another computer and I got a similar error.
Mohamad Khalil
Mohamad Khalil 2021년 6월 20일
It seems it has something to do with the "socket.cfg"! Although I'm doing as the answer in this post: https://www.mathworks.com/matlabcentral/answers/443270-parfor-energyplus-co-simulation-toolbox, I'm still getting an error. Your support means a lot for me. Thanks.
Mohamad Khalil
Mohamad Khalil 2021년 6월 21일
Is it possible to copy the "mlep" function to the multiple workers in a way that each worker has its own "mlep" function?. I feel might be better rather than all of the workers are using the same "mlep", which might not be supporting the "parfor" loop? Thanks.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 MATLAB Parallel Server에 대해 자세히 알아보기

질문:

2021년 6월 17일

댓글:

2021년 6월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by