Two different interacting programs on parallel computing toolbox
2 views (last 30 days)
Show older comments
Hi Everyone,
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()
import java.net.Socket.*
import java.net.SocketChannel.*
import java.io.*
import java.nio.*
import java.io.DataInputStream.*
import java.nio.channels.*;
import java.net.*;
import java.util.Scanner;
%Declare host, port and retries
host='';
port=10001;
number_of_retries = 1; % set to -1 for infinite
current_retry = 0;
while true
%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');
break;
end
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
Scan=Scanner(Client_sock.getInputStream());
%Read next line (delimeter \n)
Data=Scan.nextLine();
remain=char(Data);
for x=1:1:95
[token,remain]=strtok(remain,',');
DMat(y,x)=str2double(token);
end
break;
catch
Server_socket.close()
Client_sock.close()
%pause before retrying
pause(1);
end
end
end
end
Accepted Answer
Thomas Ibbotson
on 15 Apr 2013
Hi Gav,
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:
spmd
if labindex == 1
% Read data from micro-controller
% ...
labSend(DMat, 2) % Send data to lab 2
else
data = labReceive(1); % Receive data from lab 1
% Do stuff with the data.
end
end
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.
HTH, Tom
More Answers (0)
See Also
Categories
Find more on Call Java from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!