Parallel running two function and Exchange data between them

12 views (last 30 days)
Hello, I'm getting stuck in this situation.
I have two functions called A and B. Function A takes 1 min to run and return variable [xx]. Function B may run in some hours. During running time, function B use variable [xx], which is updated every minute by function A. I don't want to interrupt function B, because I plan for it to run in real time.
How can I do it in Matlab?
I have one laptop 4 cores with graphic card GeForce GT 635M supporting CUDA. Can I run function A in GPU and function B in CPU and exchange data between them? How to do it?
Thanks in advance,
  2 Comments
KUMAR GAURAV
KUMAR GAURAV on 2 Nov 2017
I am working on a similar problem. In pursuit of finding answers I found that it can be done with two timers also. Please help with probable solutions.
KUMAR GAURAV
KUMAR GAURAV on 4 Nov 2017
I will appreciate the help. Somebody please suggest solutions.

Sign in to comment.

Answers (1)

Vishal_R
Vishal_R on 2 Jul 2014
These are the 3 options that came to my mind.
1. Using a single process: If you use a single MATLAB process to implement your methods then Function A and B will use only one process and share memory. Since, both functions use only one process there will be minimal memory transfer since function B use a variable which is updated by function A. However, when function A needs to run then function B has to context switch out. I am not really sure what do you mean by real time implementation here though.
2. Using Parallel computing toolbox: If you use PCT (Parallel Computing Toolbox) then you can spawn two processes (by doing parpool(2)) and then schedule these two functions on these processes. Now, both functions will have their own memory and CPU. The only drawback here is, that these processes will pass their results to client and client will act as a bridge between them. This involves Inter process communication which is expensive (time intensive). This implementation will make sure that you have a real time implementation of function B.
3. Using GPU: You can run function A on GPU by creating GPU arrays and using your computations on them. But, now if you want A’s variables from GPU to CPU then you can simply use “gather(gpuArray)” to transfer data from GPu to CPU. However, this data transfer is very expensive since the GPU puts its data on PCI Express bus and CPU will collect the same from it. If function B needs data from function A very frequently then data transfer will have latency that will make function B. I would suggest you to try option 1 & 3 and observe which one best suits your use case.
  3 Comments
KUMAR GAURAV
KUMAR GAURAV on 4 Nov 2017
I am very new to PCT and don't know about all the tools and techniques. Simply I was trying with two timers. And then I found this one
https://in.mathworks.com/matlabcentral/answers/281487-how-to-run-two-different-scripts-simultaneously-in-a-single-matlab-window
Trying with http://in.mathworks.com/matlabcentral/fileexchange/28572-sharedmatrix but don't know how to use this one.
Walter Roberson
Walter Roberson on 4 Nov 2017
I saw a report within the last couple of weeks that shared matrix does not work with R2016b (I think it was) and possibly later, that detaching the shared memory segment fails.
In the situation where one short computation needs to run less frequently, the path would be to use one timer to update a shared variable that the second longer routine knew to examine for updates (you do not want to suddenly zap a variable because the long computation might be in the middle of something that does not expect values to change between one look and the next.)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!