Parallel Computing / Run Two Function Simultaneously

103 views (last 30 days)
Kosai
Kosai on 24 Jun 2012
I have written a programm in matlab, which have three Functions :
function F1()
imURL = 'http://192.168.0.1/snap.jpg';
while(1)
y = imread(imURL); figure(1); imshow(y);
end
end
//////////////////////////////////////////////////////////////
function F2()
A = magic(15);
while(true)
// Do some thing on "A"
end
end
///////////////////////////////////////////////////////////////
function F3()
F1();
F2();
end
And my question is : How can I let the "F1" running on a Thread and the "F2" on another Thread in order to get "F1" and "F2" running simultaneously.
I know that there is no Treading in Matlab, but maybe someone know how to parallel my Programm and get the "F1" and "F2" running simultaneously.
I tried "Timer" but it cuaseb too many Interuptions and Problems .
Thanks

Answers (1)

Adam Filion
Adam Filion on 24 Jun 2012
As long as F1 and F2 do not interact, and it looks like they don't, then you can do this through a simple PARFOR loop. Something like the following would work.
matlabpool open 2
parfor i = 1:2
if i == 1
F1()
else
F2()
end
end
You can of course expand this with more loop iterations and other mechanisms like switch/case if you want the scripts to run more than once or add additional scripts into the loop.
However you need to keep in mind that with PARFOR you have no control over which loop iteration get run when and in what order. The above code opens 2 workers and has 2 loop iterations, so each iteration will be run at the same time on its own worker and will be run once. If you expand this to multiple runs of these functions or adding in more functions, then you may want to go the route of jobs and tasks to exercise more control.
  5 Comments
Raymundo Dominguez
Raymundo Dominguez on 25 Jul 2013
Dear Adam:
I am working on digital image processing and recently I have the need to make my programs run in parallel way and I need your advice. The first problem that I have is that, as you said, workers in Matlab have some restrictions (cannot handle images due to the -nojvm command).
So, I was thinking follow tour advice and save the images at each loop iteration, the next question is how can I do that? Take into account that with PARFOR we don't have control over which loop iteration get run when and in what order, how I can save the images in order that I can have the control of a iterate variable that can be added to the name of the result file?
I am trying to use a global iteration variable in order to construct the file name in this way:
str = '';
str = strcat(str,'tmp-');
str = strcat(str, int2str(obj.counter));
str = strcat(str, '.tif')
where counter is the iterate variable. But this does not work and at the end of the process I have the saved file with the name 'tmp-1 (it depends a number that I gave at the beginning and normally is greater than 2) . What I am doing wrong?
I will appreciate any help.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!