Using Parallel computing with external software

13 visualizaciones (últimos 30 días)
Mechrod
Mechrod el 22 de Nov. de 2017
Editada: Jason Xu el 5 de Jun. de 2019
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!

Respuesta aceptada

Mechrod
Mechrod el 27 de Jul. de 2018
Editada: Mechrod el 1 de Mayo de 2019
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!

Más respuestas (2)

Walter Roberson
Walter Roberson el 22 de Nov. de 2017
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 comentarios
Walter Roberson
Walter Roberson el 5 de Jun. de 2019
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 el 5 de Jun. de 2019
Editada: Jason Xu el 5 de Jun. de 2019
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!

Iniciar sesión para comentar.


Jason Xu
Jason Xu el 1 de Mayo de 2019
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 comentarios
Jason Xu
Jason Xu el 10 de Mayo de 2019
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 el 10 de Mayo de 2019
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Create Model Web Views en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by