Is it possible make parallel processing using MEX and OpenMP on Matlab?
Show older comments
Hello. I am making the neural recording and processing program with Matlab. the whole process are receiving from acquisition board - Data processing - Plot updating However, the process time for updating plot is too long and it dose not use all process(threads), so I want to run parallel processing of data processing and plot updating.
my plan is that make mex code which update plots using Matlab functions(plot or set) or other C library and add OpenMP code to run on parallel. by this, Matlab main thread simultaneously run both data processing code and several threads update plots.
is it possible? before starting, please check my thinking is possible!
and i add my code. please review that and give me some advices
thanks!
% I make 4 plot objects having 8 subplots.
% timeStamps and Amplifiers have 10080 samples.
for i = 1:32
subPlot(i) = subplot(8,4,i);
if 1 <= i && i <= 8
DataRawLine1(i) = plot(subPlot(i), timeStamps, Amplifiers(1,:));
elseif 9 <= i && i <= 16
DataRawLine2(i-8) = plot(subPlot(i), timeStamps, Amplifiers(1,:));
elseif 17 <= i && i <= 24
DataRawLine3(i-16) = plot(subPlot(i), timeStamps, Amplifiers(1,:));
elseif 25 <= i && i <= 32
DataRawLine4(i-24) = plot(subPlot(i), timeStamps, Amplifiers(1,:));
end
subPlot(i).XLim = [0 10080/20000];
subPlot(i).YLim = [-500 500];
subPlot(i).XTick = [];
subPlot(i).YTick = [];
end
% This code read data from FPGA USB interface board (data acquisition board)
% datablock.Amplifiers have 60 samples. for 12 times, newdata gets 720 samples.
for i = 1:12
datablock.read_next(board);
save_index = (i-1)*60 + 1;
newdata(indexChannels,save_index:save_index + 59) = datablock.Chips{1,1}.Amplifiers(indexChannels,:) * 1000000; % change unit V to uV
end
% this code update plots. we have too many plots, so updating whole plots every time spend
% too much time (we have only 36ms for each cycle, read, process and update). we divide 4
% plot objects and update each objects (8 subplots) in every cycle.
if refresh == 0
set(DataRawLine1, {'Ydata'}, num2cell(Amplifiers(1:8,:),2));
pause(0.000001); % wait until plots are updated
elseif refresh == 1
set(DataRawLine2, {'Ydata'}, num2cell(Amplifiers(9:16,:),2));
pause(0.000001); % wait until plots are updated
elseif refresh == 2
set(DataRawLine3, {'Ydata'}, num2cell(Amplifiers(17:24,:),2));
pause(0.000001); % wait until plots are updated
elseif refresh == 3
set(DataRawLine4, {'Ydata'}, num2cell(Amplifiers(25:32,:),2));
pause(0.000001); % wait until plots are updated
end
refresh = refresh + 1;
if refresh == 4
refresh = 0;
end

4 Comments
Jan
on 23 Oct 2017
Perhaps your code to update the plots can be improved substantially. There have been many threads in this forum concerning the acceleration of graphic updates. If you post the relevant part of the code, you might get a suggestion. This is even useful, if the data acquisition is solves in another process or thread.
sungwon min
on 24 Oct 2017
Walter Roberson
on 24 Oct 2017
10 ms to update is about 100 frames per second.
What output framerate do you need?
Accepted Answer
More Answers (1)
Jan
on 24 Oct 2017
Is newdata pre-allocated?
newdata = zeros(max(indexChannels, 720)); % A guess, adjust to the available info
for i = 1:12
datablock.read_next(board);
save_index = (i-1)*60 + 1;
newdata(indexChannels,save_index:save_index + 59) = ...
datablock.Chips{1,1}.Amplifiers(indexChannels,:) * 1000000;
end
Try to replace pause(0.000001) by drawnow with the "limitrate", "nocallbacks" or "update" flags.
I assume that set(DataRawLine1, {'Ydata'}, num2cell(... has the same speed as a loop:
for k = 1:8
set(DataRawLine1(k), 'YData', Amplifiers(k,:));
end
But in general your screen output looks efficient already.
In theory distributing the work over 2 threads seems to be useful: One for the data acquisition, one for the display. But the communication between the parts is not easy. The part for displaying should not use a partially filled data buffer. While it is easy to let a thread run in the background, while the main routine of a MEX returns to Matlab, it is not trivial to obtain the data from this thread efficiently in real-time.
1 Comment
sungwon min
on 25 Oct 2017
Edited: sungwon min
on 25 Oct 2017
Categories
Find more on Deep Learning 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!