Use Intel AVX2 Code Replacement Library to Generate SIMD Code from MATLAB Algorithms - MATLAB & Simulink (original) (raw)
Main Content
Note
This workflow requires a MATLAB® Coder™ license and an Embedded Coder® license.
To generate SIMD code from MATLAB System objects, create a coder.config (MATLAB Coder) object, set the CodeReplacementLibrary
property to 'DSP Intel AVX2-FMA (Windows)'
, 'DSP Intel AVX2-FMA (Linux)'
, or 'DSP Intel AVX2-FMA (Mac)'
, and use the object with the codegen (MATLAB Coder) command.
Consider this MATLAB function that filters a random multichannel signal using the dsp.FIRFilter System object™.
function y = firsingle()
persistent fir
if isempty(fir)
b = fir1(250,.4);
fir = dsp.FIRFilter(b);
end
frameSize = 512; numChannels = 8; numFrames = 1e3;
for k = 1:numFrames x = randn(frameSize,numChannels,'single'); y = fir(x); end
Generate plain C code executable of this function using thecodegen
command.
cfg = coder.config('exe');
% provides starter main.c
cfg.GenerateExampleMain = 'GenerateCodeAndCompile';
switch computer('arch')
case 'glnxa64'
codegen firsingle -config cfg -report -o firsingle_std
case 'win64'
codegen firsingle -config cfg -report -o firsingle_std.exe
case 'maci64'
codegen firsingle -config cfg -report -o firsingle_std
case 'maca64'
codegen firsingle -config cfg -report -o firsingle_std
end
Measure the time it takes to run the generated executable.
tic; system('firsingle_std'); tplain = toc
Generate AVX2 C code executable by setting theCodeReplacementLibrary
parameter to either 'DSP Intel AVX2-FMA (Windows)'
, 'DSP Intel AVX2-FMA (Linux)'
, or'DSP Intel AVX2-FMA (Mac)'
, and calling thecodegen
command on the coder.config
object.
cfg = coder.config('exe');
switch computer('arch')
case 'glnxa64'
cfg.CodeReplacementLibrary = 'DSP Intel AVX2-FMA (Linux)';
case 'win64'
cfg.CodeReplacementLibrary = 'DSP Intel AVX2-FMA (Windows)';
case 'maci64'
cfg.CodeReplacementLibrary = 'DSP Intel AVX2-FMA (Mac)';
case 'maca64'
cfg.CodeReplacementLibrary = 'DSP Intel AVX2-FMA (Mac)';
end
cfg.GenerateExampleMain = 'GenerateCodeAndCompile'; % provides starter main.c
switch computer('arch')
case 'glnxa64'
codegen firsingle -config cfg -report -o firsingle_avx2
case 'win64'
codegen firsingle -config cfg -report -o firsingle_avx2.exe
case 'maci64'
codegen firsingle -config cfg -report -o firsingle_avx2
case 'maca64'
codegen firsingle -config cfg -report -o firsingle_avx2
end
Measure the time it takes to run the generated executable.
tic; system('firsingle_avx2'); tavx2 = toc
The generated SIMD code is around 4.5x faster compared to the plain C code on a Windows® 10 machine.
You can also generate a static library and a dynamic library by specifying the build type as 'lib'
and 'dll'
, respectively.
cfg = coder.config('lib'); cfg.CodeReplacementLibrary = 'DSP Intel AVX2-FMA (Windows)'; codegen MATLABfunctionName -config cfg
cfg = coder.config('dll'); cfg.CodeReplacementLibrary = 'DSP Intel AVX2-FMA (Windows)'; codegen MATLABfunctionName -config cfg
MATLABfunctionName
is the MATLAB function that calls the System object you are trying to generate SIMD code from. For a list of System objects that support SIMD code generation, see System objects in DSP System Toolbox that Support SIMD Code Generation.