How can I launch a parallel function?

I'm creating an application which controls an Ar Drone Parrot. To do this I use a main function. In addition, I want to launch a parallel function to the main program which contains a while loop, and they will be executed at once. That is to say, the main function doesn't stop when the while loop is being executed. When the secondary function is called, the main function continues and it doesn't wait for the secondary function.
I'm using Matlab 2015b and I suppose that I must use the Parallel Computing Toolbox, but I don't know what commands I can use or how I can do this.
I have been looking for a solution and I think that I can use the batch command to launch the parallel function. I am right? Have anybody a better solution?
Thanks.
Victoria.

Réponses (1)

Walter Roberson
Walter Roberson le 31 Mai 2016

0 votes

If neither function uses graphics, then use spmd .
If one of the functions uses graphics, then you can use batch() or parfeval() for the other one.
It is not possible to have both functions use graphics.
If you need to update graphics with the results of the second function, then you will need to use TCP or UDP to send data back to the first function and have the first function do the graphics. This is the same method you must use if the second function needs to communicate anything back to the first function (or the first function needs to send anything to the second function.) The exception to this rule is that if you are using spmd you can use labSend() and labReceive()

7 commentaires

In one of these functions I'm receiving stream video, I take images from this video and I show them using imshow. Then, I only can use graphics in this function, do not?
Can I not use global variables to use the images in the other function?
Excuse my english.
Thanks.
Victoria
Walter Roberson
Walter Roberson le 31 Mai 2016
global variables are not shared between workers.
Your imshow() would have to be in your main routine, not the one being run by batch() or parfeval().
Your streaming video reception could be in either of the locations, but if it is in the batch() or parfeval() then you need to transfer the data to the main routine before it can be displayed.
I mentioned TCP or UDP earlier. Another approach is to use a shared memory mapped file with memmapfile(), and a third approach is to use a shared array; see http://www.mathworks.com/matlabcentral/fileexchange/28572-sharedmatrix . The shared file / shared array solutions are trickier to use in some ways, because you have to invent a protocol so that the other side knows when it is safe to read the data, that the data is not in the middle of being written to.
Could I use this method with four functions?
Where one of them is the main function (with imshow) and the other three functions haven't got graphics. And I would use TCP, UDP or memmapfile() to share data among functions.
Walter Roberson
Walter Roberson le 1 Juin 2016
Yes. When you use more than one function the complexity increases because you need to keep the context straight about which source you are getting the data from and what it "knows", but it can certainly be done.
But, I can't have a function with a GUI and other with imshow, really?
Walter Roberson
Walter Roberson le 1 Juin 2016
Unless it has changed in R2016a (and I see some hints it might have), MATLAB uses a single Java thread to do all graphics rendering. The data to be rendered needs to get transferred into the address space of that Java thread. Parallel workers (parfor, spmd, batch, parfeval) all operate in individual processes, not as threads so the data has to cross process boundaries.
Do not think of the parallel architecture as being multiple threads: look at it as being inherently a distributed architecture where the workers might be on different continents from each other.
I have solved the problem with the GUI and the imshow command putting both in the same script and using this like main. The imshow command is in a periodic function within the main.
But now, I have three functions I want to work simutaneously. The main function will call the second function when a condition is true, but the main function must continue running at the same time. In addition, the second function contains a while loop. I need that this while loop was running at the same time that the main function is running. The same goes when the second function call the third function.
Is possible do this with batch or parfeval?
On the other hand, could I read data from the second function with UDP or TCP altough it has not finished?
I would be very grateful if you or anybody have any idea to do this. Thanks in advance.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Parallel Computing Toolbox dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by