Trying to parallel compute a function

5 visualizaciones (últimos 30 días)
Peter Kupec
Peter Kupec el 4 de Ag. de 2022
Comentada: Peter Kupec el 5 de Ag. de 2022
I have a function in a program that does calcuations I believe in serial. It gets an array of values as inputs and then performs these calculations. The outputs are not based on previous outputs so I believe this function can be done in parallel with itself. I should mention the AT08gui function is the one I want to perform in parallel. This for loop should only really run 1 time unless multiple files are used. I apologize if I'm not using the proper nomenclature as I'm not extremely familiar with parallel computing and just want to share the load of a 7000 point program between the 4 cpu cores I have to hopefully speed the process up, so that each core would be only doing 1750 and hopefully would be speed the process up. Essentially the same thing as me running the program in 4 seperate instances with the data split up appropriately and then having the outputs return to a single excel file.
I hope this makes sense and hopefully there is maybe a simple matlab function that implements what I'm talking about? or maybe MATLAB automatically does this and my function is already running in parallel?
for x=1:length(FLIST)
v=FLIST(x);
if firstpass==1||get(handles.Filetype_selector,'value')==4
Fdat(1)={v};
[D, Oh, Ogh, Osh, Ov, Ogv, Osv, ~, ~, ~, ~, AntF, AntB, AntFX, AntBX, ux, uy, ~]=AT08gui(Filename,Filetype,v,Tsurf,Psurf,pMSL,er,ho,ha,AOIs,R,model,gm,MBD,MBDr,det,resolution,Numfiles,Fdat);
[E]=WRITETA(FNXLS,E,D,v,Tsurf,Psurf,pMSL,er,ho,ha,AOIs,model,Ov,Oh,Osh,Ogv,Ogh);
firstpass=0;
else
[D, Oh, Ogh, Osh, Ov, Ogv, Osv]=Get_Ta(AntF,AntB,AntFX,AntBX,ux,uy,v,Tsurf,Psurf,pMSL,er,ho,ha,R,AOIs,model,gm,us);
[E]=WRITETA(FNXLS,E,D,v,Tsurf,Psurf,pMSL,er,ho,ha,AOIs,model,Ov,Oh,Osh,Ogv,Ogh);
end
end
  2 comentarios
Matt J
Matt J el 4 de Ag. de 2022
The outputs are not based on previous outputs.
It doesn't look that way. The line,
[E]=WRITETA(FNXLS,E,D,v,Tsurf,Psurf,pMSL,er,ho,ha,AOIs,model,Ov,Oh,Osh,Ogv,Ogh);
refers to the previous value of E.
In any case, do you even have the Parallel Computing Toolbox?
Peter Kupec
Peter Kupec el 4 de Ag. de 2022
I do have the parallel computing toolbox. And I think that E value is irrelevent because it is set to 1 a couple lines above and the WRITETA function looks like this in the functions file.
Appreciate any help.
[E]=WRITETA(FNXLS,os,D,v,Tsurf,Psurf,pMSL,er,ho,ha,AOIs,model,Ov,Oh,Osh,Ogv,Ogh)

Iniciar sesión para comentar.

Respuestas (1)

Bruno Luong
Bruno Luong el 4 de Ag. de 2022
"or maybe MATLAB automatically does this and my function is already running in parallel?"
No.
But if you vectorize your function AT08gui , MATLAB might use low-level function on vector that are implemented in multi-thread.
Writing in parallel data to a file is not straigthforward, see this threadcurently discussed.
  1 comentario
Peter Kupec
Peter Kupec el 5 de Ag. de 2022
So I tried to write a basic script to do what I was refering to but it seems that the parfor loop to run these basic functions is getting exponentially slower the larger I make my arrays. I read in another forum that what I might be refering to is distributive computing which again I'm not familiar with. I tried utilizing the spmd function and distributed arrays but they don't seem to be immediately making the computations faster. I know my original function is much more complex than these smaller tests ones so maybe I would see results if I implemented it.
As for your advice on vectorization. I'm not sure how much of the nested functions are vectorized but I believe it makes substantial use of this. My main goal though is to leverage the cores in my computer to dramatically increase the run time of the original posts program.
Here is my test file which I believe is splitting the load of this array across 2 cores but isn't making the program run any faster, in fact much slower.
%% inputs
inp1=linspace(1 ,10,70000000);
inp2=linspace(20,30,70000000);
inp3=2;
%% functions
funcs1 = {@testfunc1, @testfunc1} ; % let fun1, fun2 be two functions
%% function inputs
arguments = {inp1,inp3 ;inp2,inp3} ; % write the inputs of each function
%outputs={out1, out2};
%% preallocation of space for solutions
solutions = cell(2,2); % initialize the solution
solutions2 = cell(2,2); % initialize the solution
%solutions2 = cell(1,2); % initialize the solution
% use of parfor
%% function parfor loop
tic
parfor ii = 1:2
[solutions{ii,:}]=funcs1{ii}(arguments{ii,:});
%[solutions{ii,:},solutions{ii,:}]=funcs1{ii}(arguments{ii,:});
end
toc
%% linear for loop
tic
[solutions2{1,:}]=testfunc1(inp1,inp3);
[solutions2{2,:}]=testfunc1(inp2,inp3);
toc
function [y,kk]=testfunc1(x,b)
y=x+b;
kk=x;
end

Iniciar sesión para comentar.

Categorías

Más información sobre Performance and Memory en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by