Effacer les filtres
Effacer les filtres

Parallel Computing / Run Two Function Simultaneously

29 vues (au cours des 30 derniers jours)
Kosai le 24 Juin 2012
I have written a programm in matlab, which have three Functions :
function F1()
imURL = '';
y = imread(imURL); figure(1); imshow(y);
function F2()
A = magic(15);
// Do some thing on "A"
function F3()
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 .

Réponses (1)

Adam Filion
Adam Filion le 24 Juin 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
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 commentaires
Adam Filion
Adam Filion le 25 Juin 2012
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 le 25 Juil 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.

Connectez-vous pour commenter.


En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by