Two different interacting programs on parallel computing toolbox
5 views (last 30 days)
Is it possible to run two different programs simultaneously using the parallel computing toolbox, where one function is dependent on constantly updating the result of the other?
I have implemented a TCP server receiving data from a micro-controller using the java libraries within MATLAB which works well. I now need to do some computations of varied length on the most recent data set which has arrived, and my proposed solution to this is a First In First Out buffer in my server function which is updating as fast as the data arrives and is readable by my main function.
I have looked at the documentation and examples for matlabpool, parfor, spmd, batch etc. but I am not sure if any of these allow me to run two different programs with shared data.
Any advice would be greatly appreciated, or indeed suggestions of other solutions to this problem. I have included my server code in case it helps.
if true function [DMat] = ServerInit()
%Declare host, port and retries
number_of_retries = 1; % set to -1 for infinite
current_retry = 0;
%Keep track of connection attempts
current_retry = current_retry + 1;
if ((number_of_retries > 0) && (current_retry > number_of_retries))
fprintf(1, 'Too many retries\n');
try % throws if unable to connect
fprintf(1, 'Retry %d connecting to %s:%d\n', ...
current_retry, host, port);
%Set-up and accept new connection
Server_socket = java.net.ServerSocket(port);
Client_sock = Server_socket.accept();
%Set up data input stream called Scan
%Read next line (delimeter \n)
%pause before retrying
Thomas Ibbotson on 15 Apr 2013
I think you want the labsend, labreceive functionality. You can use the labindex function to switch between server behaviour and client behaviour, for instance if you use spmd with 2 labs, one being the server and one being the client you could do:
if labindex == 1
% Read data from micro-controller
labSend(DMat, 2) % Send data to lab 2
data = labReceive(1); % Receive data from lab 1
% Do stuff with the data.
I think I should also point out that you're writing quite a bit of Java code in MATLAB, this is not necessarily the best idea. For instance, you're not closing your sockets after you read the data, you only close them in the catch block if there's an error. This is understandable because MATLAB does not have a finally statement (the equivalent functionality is provided by onCleanup).
You can call Java functions from MATLAB. In your situation it might be safer to write the code that reads from the micro-controller in Java and then call that code from MATLAB.