Using Parallel computing with external software

13 次查看(过去 30 天)
Hi. Im trying to use Parallel computing with a function that calls an external software (Ansys). Im using optimization with Genetic Algorithm. But i keep receiving an error. I start the pool with 3 cores.
main code:
clc; clear;
termino=1; % just a parameter
funcao = @(x) Eval_v1_3(x,termino); % objective function with 3 variables
LB = [0.7 1.0 0.5]; % lower bounds
UB = [3.0 3.0 0.7]; % upper bounds
options = optimoptions('ga','MaxGenerations',20,'PopulationSize',10,'UseParallel',true); % sets PARALLEL
x = ga(funcao,3,[],[],[],[],LB,UB,[],options);
objective function:
function val = Eval_v1_3(x,termino)
dlmwrite('param_otim.txt',[x(1) x(2) x(3) termino],'precision','%.5f'); % writes the individual to be read in the middle of Ansys execution
!C:\"Program Files"\"ANSYS Inc"\v150\ansys\bin\winx64\ANSYS150 -b -i "code.txt" -o "output.txt" -np 1 % call Ansys and execute an algorithm that reads param_otim.txt
T_A=load('temps_monit_A.txt','-ascii'); T_B=load('temps_monit_B.txt','-ascii'); % reads the values generated by the simulation
rp = 1; t_sol1 = 1467; t_sol2 = 840; % just parameters
val = rp*sqrt(((t_sol1 -T_A(1))^2 + (t_sol1 -T_A(2))^2 + (t_sol2-T_B(1))^2 + (t_sol2-T_B(2))^2 )); % objective function
dlmwrite('monitoramento.txt',[x T_A T_B -val],'delimiter','\t','-append'); % writes every individual and result for monitoring purpose
end
error that appears:
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
Can i discover which file could not be access? The problem is in Ansys or Matlab? Is there a better way to code the problem (still using GA)?
It works without the parallel, but i would like to speed up with parallel computing.
Thanks!

采纳的回答

Mechrod
Mechrod 2018-7-27
编辑:Mechrod 2019-5-1
This week i came back to work with this again, and after some research and tests, this is the answer for the main file:
clc; clear;
spmd
mkdir(sprintf('worker%d', labindex));
copyfile('file1.db',sprintf('worker%d/',labindex));
copyfile('file2.mp',sprintf('worker%d/',labindex));
copyfile('file3.txt',sprintf('worker%d/',labindex));
cd(sprintf('worker%d', labindex));
end
funcao = @(x) ZF_Eval_v1_3_new(x,termino);
LB = [0.7 1.0 0.5];
UB = [3.0 3.0 0.7];
options = optimoptions('ga','MaxGenerations',20,'PopulationSize',10,'UseParallel',true); % sets PARALLEL
x = ga(funcao,3,[],[],[],[],LB,UB,[],options);
and the function file remains the same:
function val = ZF_Eval_v1_3_new(x,termino)
dlmwrite('param_otim.txt',[x(1) x(2) x(3) termino],'precision','%.5f'); % writes the individual to be read in the middle of Ansys execution
!C:\"Program Files"\"ANSYS Inc"\v150\ansys\bin\winx64\ANSYS150 -b -i "file3.txt" -o "output.txt" -np 1
T_A=load('temps_monit_ZF.txt','-ascii');
rp = 1; t_sol1 = 1467; t_sol2 = 840; % just parameters
val = rp*sqrt(((t_sol1 -T_A(1))^2 + (t_sol1 -T_A(2))^2 + (t_sol2-T_A(3))^2 + (t_sol2-T_A(4))^2 )); % objective function
end
The execution time was reduced in 40%. As Walter said, the key was to mkdir and copy the files to the workers folders. I used 3 workers for testing, so it created 3 folders, copying all the files i needed on each folder.
Hope its usefull for someone!

更多回答(2 个)

Walter Roberson
Walter Roberson 2017-11-22
You need to use a different input and output file names for the different workers. With the code you are using now, all of the workers are trying to write to the same file at the same time.
  13 个评论
Walter Roberson
Walter Roberson 2019-6-5
load(file.path\Model_copy1\result.mat)
That involves two invocations of the mldivide (\) operator.
Nothing in your invocations appears to tell MotorCAD where to place the output.
Jason Xu
Jason Xu 2019-6-5
编辑:Jason Xu 2019-6-5
Hi Walter:
After the simulaton is run with the code below:
% run the model
invoke(mcad1,'BuildModel_Lab');
and by the time simulation is finished, MotorCAD automatically generated a folder with the same name as the model (eg. Model_copy1), and within the folder Model_copy a file with fixed name result.mat is also generated by MotorCAD automatically. So all i need to do after the simulation is to locate the result.mat with respected to the relative model (eg. Model_copy1) and load this .mat file and then ill have my calculation output.
whats your suggestion for my scenario? Thank you!

请先登录,再进行评论。


Jason Xu
Jason Xu 2019-5-1
Hi Mechrod, nice work! Still I’m wondering what are those file1 file2 file3 that you have copied to your workers directories? In my understanding is it necessary to copy your ansys model and your calculation results text file as well? How do u make sure different ansys models are being called regarding to different workers? Thank you!
  3 个评论
Jason Xu
Jason Xu 2019-5-10
Hi Mechrod, thank you for your detailed answer. I think my model is working now! Still sometimes after several hundreds of interations when the matlab code ask the model to be saved and quit then the model just got frozen and crashed. So hows Ansys's stability? Did it happen to you ? Thank you!
Mechrod
Mechrod 2019-5-10
Hi!
My Ansys works fine. When the individual has a bad combination of parameters, my simulation may not converge and simply exit. To avoid that, i found 2 different solutions:
1 - put all the objective function inside a if to check if the result file exists, like this:
if exist('results.txt', 'file') == 2 % File does exist
%make the calculations
else
val = 9999999;
end
If the results file does not exist, assign a very high value, to tell that this is a bad individual.
2 - In the beginning of your Ansys algorithm, write a dummy results file, that make a very bad result for the objective function. If the simulations converge, overwrite this file with the real results.
Hope it helps.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Create Model Web Views 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by