Running Functions in Parallel

Hi,
I'm working on a project where I need to call a function from within a script. The problem is that the function takes ~2 sec to execute. The main script is also controlling a quadcopter, and when run independently(without calling the said function), runs at ~50Hz. Is there a way in which I do not have to wait for the function output, and keep executing the main script. You can refer to the relevant code snippet below:
% Main Script
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%-------------------- Main Loop for Simulation -----------------------%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%set up the domain
x_coord=[0:.1:4.0]; %meters
y_coord=[0:.1:4.0]; %meters
z_trash=[0:.1:2.5]; %meters
z_coord(1,1,:)=z_trash;
clear z_trash;
Q=zeros(length(y_coord),length(x_coord),length(z_coord));
x_mat = permute(repmat(x_coord',1,length(y_coord),length(z_coord)),[2 1 3]);
y_mat = permute(repmat(y_coord,length(x_coord),1,length(z_coord)),[2 1 3]);
z_mat = repmat(z_coord,length(x_coord),length(y_coord),1);
R=10;
%Define the human head state x_h y_h z_h pitch_h roll_h yaw_h
x_h=3; y_h=2; z_h=1.8; pitch_h=0; roll_h=0; yaw_h=pi/2;
points=[x_mat(:),y_mat(:),z_mat(:)];
while (true)
tic
phi=10;
c1=1;
a=-60; %deg
b=60; %deg
sigma=8; %deg
c2 = 5;
B=(1/max(0,c1))+(1/max(0,c2));
Si=1./B;
Si=Si./max(max(max(Si)));
Q=Q+Si;
output = resample([points,Q(:)]);
save('results.mat','output','-v6');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%--------------- problematic part - runs at ~ 0.5Hz ------------------%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
code = gmm();
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ctrl_msg(1).Translation.X = vx;
ctrl_msg(1).Translation.Y = vy;
ctrl_msg(1).Translation.Z = vz;
ctrl_msg(1).Rotation.X = roll;
ctrl_msg(1).Rotation.Y = pitch;
ctrl_msg(1).Rotation.Z = yaw;
send(ctrl_pub(1),ctrl_msg(1));
drawnow
toc
end
The secondary script (gmm.m) calls a python function which computes EM using a CUDA implementation and takes around 2 sec on my GPU. The code for gmm.m can be found below. inp and out are .mat files which I write to communicate between python and matlab. If I use '&' at the end of commandStr in the script below, the main scripts initializes multiple instances of Python causing the CPU to get overloaded. If I don't use it, the function waits till the command is executed and returns a returncode of 1.
function returncode = gmm()
global inp out thresh num_iter verbose K;
if exist(fullfile(cd, inp), 'file') == 2
commandStr = ['python emgmm.py ' inp ' ' out ' ' int2str(num_iter) ' ' int2str(thresh) ' ' int2str(verbose) ' ' int2str(K) '&'];
returncode = system(commandStr);
end
end
Is there a way to keep executing the gmm function in the background till it exits with returncode 1 (after ~2sec) and let the main loop keep on executing. Any suggestions would really help me out alot!

Answers (1)

Walter Roberson
Walter Roberson on 30 Aug 2018
parfeval with a pool size of 1 (so that the gpu is not overloaded) and NOT using & on the command (so that it waits for the results)
You can queue multiple parfeval and they will execute in sequence. But of course if you issue them faster than an average of two seconds intervals then you risk developing a growing backlog.

Categories

Asked:

on 30 Aug 2018

Answered:

on 30 Aug 2018

Community Treasure Hunt

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

Start Hunting!