How to execute parallel functions ?

Hi everyones,
I have a three buffer system and It takes 3 steps : - I need to collect data from a .txt file - I need to filter these data - I need to plot the filtered data
I want to do these three steps simultaneously ! (even if I have a delay between current data and plotted data) Should I use a parfor loop, matlabpool or multi-thread programming ?
Thanks a lot !

1 Comment

Notice I need to have all the computed data on the same workspace, and I have to use these at each moment.

Sign in to comment.

 Accepted Answer

Friedrich
Friedrich on 24 Apr 2013
Edited: Friedrich on 24 Apr 2013
Hi,
look at createTask and createJob. You create a Task for each file, let the worker process is and pass the data back to MATLAB. In MATLAB you get that data and plot it and restart the process:
c = parcluster; %or maybe c = findResource('scheduler','Configuration','local');
job = createJob(c);
for i=1:3
createTask(job,@rand,1,{i});
%create a function handle to the function you use to process the file, pass in the filename as argument to the function to be able to use the same function for all files
end
submit(job);
wait(job);
results = job.fetchOutputs %maybe you need getAllOutputArguments depends on the version you are using
job.delete
Or use SPMD (this should give a better peformance as CreateJob/CreateTask because te workers need to be started only once)
matlabpool open 3
result = cell(3,1);
spmd
switch labindex
case 1
result{1} = rand(1); %process file 1
case 2
result{2} = rand(2); %process file 2
case 3
result{3} = rand(3); %process file 3
end
end
%each worker holds its own result because result is a Composite object
result
result{:}

More Answers (4)

Paul-Henri Michel
Paul-Henri Michel on 23 Apr 2013
No answers ?
Thanks !

11 Comments

Friedrich
Friedrich on 23 Apr 2013
Edited: Friedrich on 23 Apr 2013
I dont see if this can parallelized at all. Each steps requieres that the previous step is done. So how can you start plotting while you are still reading the file? Is it really one file you read or multiple? How big is the file? How long does you code run now to accomplish these there steps?
I have three files, and I consider reading data from a file with Matlab is very fast, so while other files are read, I just plot data from the third (a bit faster than the duration of each step). Notice that all files are filled by a C-program, I just need to read these files (and computed data or plotted it) in Matlab in real time and I need this to be synchronized (how to control the start and stop of each worker ?)
Friedrich
Friedrich on 23 Apr 2013
Edited: Friedrich on 23 Apr 2013
The first problem will be that you can't plot from a worker. The second problem is that MATLAB itself doesn't operate in Real-Time (only xPC Target and Real-Time-Windows Target do). For the worker communication you can use functions like labsend, labreceive, labbarrier etc.
I think what might work is to start 3 workers where each worker operates on his own file and does all 3 steps. You would need to save the .fig file from the worker so that you can open and display the figure later on.
But you won't be able to see a "live" graphic from the worker.
Ok, so in Matlab there are no means to compute data and to plot data in "real-time" ????
Yes, no real-time. Mostly related to the Operating Systems are not real time at all. There are two toolboxes which work in real time (mentioned above) but there you can't plot and they are Simulink based.
Are you sure you really need real time? So far it sounds more like as fast as possible (a.k.a. "live") is enough.
Hum, indeed I don't need true real-time, all I want is to display my data in "live" while I do other things in Matlab. But, for example, If my data refers to a 10 secondes acquisition I can plot it in a 5 secondes.
Friedrich
Friedrich on 23 Apr 2013
Edited: Friedrich on 23 Apr 2013
Mhh, the main problem here is Parallel Computing Toolbox because you can't display/update a figure. That plotting really needs to be done in MATLAB itself, which means some timer/task is needed. However when you start a long calculation in MATLAB the timer callback isn't executed as long your code doesn't use any drawnow/pause or other commands which let the event queue update. Which means the figure isn't updated as long your other code is running.
Maybe I can launch two process of Matlab and establish a communication by UDP protocol between the two Matlab ?? I just use a hold on command for each step of plotting ? Seems to be a good idea ?
When starting 2 MATLABs why communicate anyway? Simple use one MATLAB to run the code and display the results and use the other one for your normal work. I don't see a need for the two MATLABs to exchange any data/information.
No I mean one Matlab for computing and one Matlab for plotting. Once the first Matlab has finished to filter data, I send the data to the second Matlab, which display these on a "hold on" figure and I restart the operation !
But maybe the UDP protocol is too slow to do this, because my file data contain more than 4 000 characters.
Plotting doesnt take much time. Using a MATLAB for Plotting only seems a bit too much. I would do all that in one MATLAB. You can use PCT to read in the 3files and post process it. You then use these results and plot it and then start over again. This can run in 1 MATLAB.
You use the second MATLAB for your own.

Sign in to comment.

Paul-Henri Michel
Paul-Henri Michel on 23 Apr 2013
Ok, now I don't consider plotting data, I just want read data and filter data (other data obviously) simultaneously. In fact, I need two workers on the same workspace ?
Should I use parfor loop, matlabpool ?
Thanks !

2 Comments

Why do you need the same workspace? You can put the data together later on.
What do you mean by later ? On each end of cycle (end of each worker) I need data and I restart a new loop.

Sign in to comment.

Paul-Henri Michel
Paul-Henri Michel on 23 Apr 2013
No answers for : how to give work to two workers on the same Matlab session ?
Thanks !

Categories

Find more on Parallel Computing Toolbox 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!