Receiving I/O error 32 when opening another program (EnergyPlan) inside parfor

2 views (last 30 days)
Hello everyone,
I am trying to use parralel computing to run the same program (EnergyPlan) simultaneously. EnergyPlan is a energy system tool that computes whether a given energy system is able to supply the given demand. I would like to run different energy system configurations (e.g. different amounts of wind and solar power) and use an optimization algorithm to find the best setup for the given demand.
The program is set up that it opens EnergyPlan using the "system" command to execute EnergyPlan. Afterwards the results of each run are retrieved compared to see, which system scores highest. When I run the program in a simple for-loop, it works without problems. However, when I change the for-loop to a parfor-loop, the opened EnergyPlan displays the error "I/O error 32". According to sources I found this error indicates a sharing violation. The first EnergyPlan program is executed (unfortunately the program pops up so I can always see when a run is performed) just fine and it closes again. However, there is a second EnergyPlan opened while the first one is still open and this one shows the beforementioned error. According to EnergyPlan.eu the tool can be run in parallel and other researchers have done so succesfully using python or java. Therefore, I don't know why this error occurs with Matlab.
Does anyone know how to circumvent this issue? Any help would be much appreciated.
Regards,
Markus
Minimum working example (provided EnergyPlan and toolbox for Matlab are installed):
%%Paths and Folder definitions
%Path where it is located EnergyPLAN executable.
dir.energyPlanPath = 'energyPLAN.exe';
%Path of reference file.
dir.inputFilePath = 'energyPlan Data\Data\Denmark2030Alternative.txt';
%Folder where will be output resulf of EnergyPLAN.
dir.outputFolder = 'Outputs\';
REvalue = 1e4*rand(10,2);
result = zeros(size(REvalue,1),1);
parfor i = 1:length(REvalue)
% Call EnergyPlan
annualData = energyPlan(dir.energyPlanPath,...
dir.inputFilePath,dir.outputFolder,...
'input_RES1_capacity=', REvalue(i,1),... % 1st variable to be changed
'input_RES2_capacity=', REvalue(i,2)); % 2nd variable to be changed
result(i) = annualData(55); % random value taken out
end
  19 Comments
MD
MD on 13 Aug 2018
@OCDER: It worked!!! The strange thing is that the loop doesn't wait for the system command to finish but continues with the readOutputEnergyPlan. Then it opens the EnergyPlan instances afterwards while Matlab has already given me an error (which makes sense since the output cannot be read when it is not there). It actually opens even more instances of EnergyPlan than there are logical cores at the same time. Furthermore, it even opens more than 4 instances in your example. I guess, I will still have to investigate a bit more.
However, it does not have problems finding the file anymore. The computation is run and it obtains an output. Thank you very much for solving the issue! I would have never found it without you.
OCDER
OCDER on 13 Aug 2018
Nice! If that works, can you go ahead an accept the answer below? That way we know the question is answered with a working solution, and I get some reputation points :) Play around with removing start so that it waits for it to finish per parallel worker. I think the file naming and having multiple input file copies was the real issue, not start. If you do need start, then you might need another step to wait for the output file to be generated before continuing.

Sign in to comment.

Accepted Answer

OCDER
OCDER on 13 Aug 2018
Edited: OCDER on 13 Aug 2018
energyPlanPath = 'C:\Users\HumptyDumpty\Desktop\Programme etc\Studium\4o Semestre\Master Thesis\EnergyPlan\energyPLAN.exe';
origInput = 'C:\Users\HumptyDumpty\Desktop\Programme etc\Studium\4o Semestre\Master Thesis\EnergyPlan\energyPlan Data\Data\Denmark2030Alternative_tmp.txt';
inputPre = 'C:\Users\HumptyDumpty\Desktop\Programme etc\Studium\4o Semestre\Master Thesis\EnergyPlan\energyPlan Data\Data\Denmark2030Alternative_tmp';
outputPre = 'C:\Users\HumptyDumpty\Desktop\Programme etc\Studium\4o Semestre\Master Thesis\EnergyPlan\Outputs\out_Denmark2030Alternative';
%
assert(~isempty(dir(energyPlanPath)), 'energyPlanPath is not valid. Check file name.');
assert(~isempty(dir(origInput)), 'origInput file is not valid. Check file name.');
%
%To prevent parfor from accessing and writing to same file, make copies and specify separate outputs
Inputs = cell(4, 1);
Outputs = cell(4, 1);
for j = 1:4
Inputs{j} = [inputPre num2str(j) '.txt'];
[Success, Msg] = copyfile(origInput, Inputs{j});
assert(Success, Msg);
Outputs{j} = [outputPre num2str(j) '.txt'];
end
%Attempt to run in parallel
parfor j = 1:4
executionString = sprintf('start "title" "%s" -i "%s" -ascii "%s"', energyPlanPath, Inputs{j}, Outputs{j});
system(executionString); %execution of EnergyPLAN.
%to wait for output file to be generated
while isempty(dir(Outputs{j}))
pause(0.1); %check again in 0.1 sec
end
... rest of your codes
end
  1 Comment
MD
MD on 13 Aug 2018
I managed to run it smoothly without any problems now. The reason for the other problems was actually the use of "start". This forces the program to open regardless of whether Matlab is ready or not. With this executionString it works perfectly since a new instance of EnergyPlan is only opened when the old one was computed:
executionString = sprintf('"%s" -i "%s" -ascii "%s"', energyPlanPath, Inputs{j}, Outputs{j});
Now I only need to find a way to force the program to run in the background and not pop up every single time it runs.
Thanks again!

Sign in to comment.

More Answers (0)

Categories

Find more on Entering Commands in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!