Parallel Computing / Run Two Function Simultaneously

19 次查看(过去 30 天)
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

回答(1 个)

Adam Filion
Adam Filion 2012-6-24
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 个评论
Adam Filion
Adam Filion 2012-6-25
Workers are started with a number of restrictions on them. One of them is -nojvm, which disables java. Some others are -singlecompthread, which limits it to a single computational thread, and -nodesktop, which turns off the MATLAB desktop. I think there may be a few others I'm forgetting, but the error you are seeing is expected. If you want to view these images, save them to a file at each loop iteration.
Raymundo Dominguez
Raymundo Dominguez 2013-7-25
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by