Poor MEX performance when running filtfilt

Hi all,
I'm currently on the journey of converting my MATLAB data processing script into C. While testing some part of it (filtering data using filtfilt), I noticed that running the generated MEX file is MUCH slower than running the MATLAB function directly.
This is my MATLAB function:
function filter_test(data)
%$codegen
coder.inline('never');
% Initialise filtered data
f_data = data;
% Set the filter coeficients to remove the 50Hz and its harmonics
filter_coeficients = struct();
filter_coeficients.coef_50Hz = [...]; %1x4467 double
filter_coeficients.coef_100Hz = [...]; %1x4467 double
filter_coeficients.coef_150Hz = [...]; %1x4467 double
filter_coeficients.coef_200Hz = [...]; %1x4467 double
filter_coeficients.coef_250Hz = [...]; %1x4467 double
filter_coeficients.coef_300Hz = [...]; %1x4467 double
filter_coeficients.coef_350Hz = [...]; %1x4467 double
filter_coeficients.coef_400Hz = [...]; %1x4467 double
filter_coeficients.coef_450Hz = [...]; %1x4467 double
% Apply all the filters
filter_coeficients_id = fieldnames(filter_coeficients);
for c=1:numel(filter_coeficients_id)
f_data = filtfilt(filter_coeficients.(filter_coeficients_id{c}), 1, f_data);
end
end
I generated the MEX file (.mexw64) with the following command:
codegen('filter_test', '-args', {coder.typeof(double(0), [Inf Inf])})
This was one of the test I made:
data = rand(30000,1);
tic
filter_test(data); % Elapsed time is 1.914894 seconds.
toc
tic
filter_test_mex(data); % Elapsed time is 492.794533 seconds.
toc
The difference is abysmal! Forcing the code to coder.inline('always') made this even worse, as the run time reaches 997.7 seconds.
Am I doing something wrong? I guess the C performance will be similar to the MEX performance? Why is the MEX so slow when compared to MATLAB?

7 comentarios

Two suggestions :
  • Please make sure that you are running the MEX file (not the .m function). Typically codegen command on function 'foo' will produce a 'foo_mex' file. Similarly you will be having 'filter_test_mex' file.
  • Using rand() function for testing performance may not be a good idea, since it my produce different numbers for two different cases. Try to save the values from rand() in a mat file and use the same values for different experiments.
Diogo Tecelão
Diogo Tecelão el 28 de Nov. de 2020
Editada: Diogo Tecelão el 28 de Nov. de 2020
Thanks for your reply. I have corrected both the points that you mentioned. I am indeed running the filter_test_mex function (from filter_test_mex.mexw64), and I'm now comparing the performance with the exact same data. I've also made some further testing.
Turns out that the MEX function is 257 times slower than MATLAB! Any idea?
Diogo Tecelão
Diogo Tecelão el 28 de Nov. de 2020
I have converted the code into a DLL, and by running the DLL it takes 7 seconds to run. It is an improvement, but still much slower than MATLAB. I was expecting C to be faster. Is there any way I can optimize this?
Wilson A N
Wilson A N el 30 de Nov. de 2020
Editada: Wilson A N el 30 de Nov. de 2020
Hi Diogo,
Can you please share the complete reproduction steps along with the filter coefficients (seems to be large ~4467) ?
Additionally, which MATLAB release are you using?
I want to try to reproduce the issue and understand what is happening.
-Wilson
Diogo Tecelão
Diogo Tecelão el 30 de Nov. de 2020
Hi Wilson! Thanks for your reply and attention. How can I share the filter coeficients? I cannot paste them here, as there's a character limit.
Wilson A N
Wilson A N el 2 de Dic. de 2020
Hi Diogo,
You can save the filter coefficients as a .mat file and attach it here.
Wilson A N
Wilson A N el 11 de Dic. de 2020
Editada: Wilson A N el 11 de Dic. de 2020
Hi Diogo,
I tried using some random filter coefficients to try and reproduce the issue but I was not able to observe the slowdown you had reported. Below are my results:
MATLAB Simulation time is
1.4649
MEX Simulation time is
3.4633
SIL Simulation time is
4.3738
(SIL simulation is actually checking lib/dll time)
Please update the MATLAB Release in which you are facing the issue along with the filter coefficients. This will help us to look into the issue further.

Iniciar sesión para comentar.

Respuestas (1)

Jan
Jan el 11 de Dic. de 2020

0 votos

Just a remark: With FiltFiltM (https://www.mathworks.com/matlabcentral/fileexchange/32261-filterm ) my Matlab 2018b needs 6.0 instead of 8.0 seconds as for filtfilt.

Categorías

Más información sobre MATLAB Coder en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 28 de Nov. de 2020

Respondida:

Jan
el 11 de Dic. de 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by