MATLAB: How to perform parallel processing with many sessions of MATLAB

MATLAB

I would like to perform parallel processing with many sessions of MATLAB.

Best Answer

Here is a sketch of how you could do parallel MATLAB/UNIX with the Engine interface. It involves some fairly complicated UNIX programming. We do not support parallel processing with MATLAB
We have a product called Distributed Computing Toolbox which can run parallel MATLAB applications. You can find more information about this product on the following Web page:
As for using the MATLAB Engine interface on a UNIX machine - for simplicity, say that we are talking about running MATLAB in parallel on only two machines. The idea described here can be applied to any number of machines.
Start MATLAB Engine #1 on machine #1
Start MATLAB Engine #2 on machine #2
Start computation on Engine #1 with engEvalString()
Start computation on Engine #2 with engEvalString()
Wait until they are all done
Collect results from each of them with engGetMatrix()
The problem is that the engEvalString() call to Engine #1 will block until it is done, preventing you from starting up a computation on Engine #2 in parallel. Therefore, you need some way to get the effect of engEvalString() without blocking.
To make this more concrete, let's say that we want to compute c=a*b and f=e*d in parallel. The simple way to do c=a*b is
engEvalString('c=a*b');
c = engGetMatrix('c');
To achieve the same effect without blocking, you can write a MEX file (call it "Crunch") that goes with each MATLAB. When Crunch is called directly, it establishes a signal handler for the signal SIGUSR2. (Note that MEX files must restore the MATLAB signal handlers for all signals before exiting.) Here is how the program can get Engine #1 to compute c=a*b without blocking:
1. Call Engine #1 via engEvalString() to initialize the Crunch MEX file (sets up the signal handler)
2. Use engPutMatrix() to put the string 'c=a*b' into the workspace variable 'cmd'
3. Use engPutMatrix() to put the contents of matrices a & b into the workspace
4. Send the Engine a SIGUSR2 signal
5. The signal handler established by Crunch "wakes up," looks at 'cmd' and evaluates the string in that variable.
6. Do a similar sequence on Engine #2
7. Finally do engGetMatrix() on each Engine to get the results (it will block while they are busy, but that is OK because they are running in parallel)
Note: signals are not networked; you cannot send a UNIX signal to a remote process on another machine directly. From C code the way to do it is to build a string that says something like "rsh hostname kill -SIGUSR2 process-id", then execute the string using the system() routine. (You may have to use the actual numeric value of SIGUSR2 here.)