Main Content

coder.fftw.StandaloneFFTW3Interface.lock

Class: coder.fftw.StandaloneFFTW3Interface
Namespace: coder.fftw

Lock access to FFTW planning

Syntax

coder.fftw.StandaloneFFTW3Interface.lock()

Description

coder.fftw.StandaloneFFTW3Interface.lock() locks access to the planning process for FFTW library calls in generated standalone code.

When multiple threads call an FFTW library, implement this method in an FFT library callback class that derives from coder.fftw.StandaloneFFTW3Interface.

Examples

expand all

In a class that derives from coder.fft.StandaloneFFTW3Interface, implement lock and unlock methods that call C code to manage a lock.

Write C functions that initialize, set, and unset a lock. To manage the lock, this example uses the OpenMP library. For a different library, modify the code accordingly.

  • Create a file mylock.c that contains this C code:

    #include "mylock.h"
    #include "omp.h"
    
    static omp_nest_lock_t lockVar;
    
    void mylock_initialize(void)
    {
        omp_init_nest_lock(&lockVar);
    }
    
    void mylock(void)
    {
        omp_set_nest_lock(&lockVar);
    }
    
    void myunlock(void)
    {
        omp_unset_nest_lock(&lockVar);
    }  

  • Create a header file mylock.h that contains:

    #ifndef MYLOCK_H
    #define MYLOCK_H
    
     void mylock_initialize(void);
     void mylock(void);
     void myunlock(void);
    
    #endif

Write an FFT callback class myfftcb that:

  • Specifies the FFTW library.

  • Implements lock and unlock methods that call the supporting C code to control access to the FFTW planning.

Use this class as a template. Replace fftwLocation with the location of your FFTW library installation.

classdef myfftcb < coder.fftw.StandaloneFFTW3Interface
    
    methods (Static)
        function th = getNumThreads
            coder.inline('always');
            th = int32(coder.const(1));
        end
        
        function lock()
            coder.cinclude('mylock.h', 'InAllSourceFiles', true);
            coder.inline('always');
            coder.ceval('mylock');
        end
        
        function unlock()
            coder.cinclude('mylock.h', 'InAllSourceFiles', true);
            coder.inline('always');
            coder.ceval('myunlock');
        end
        
        function updateBuildInfo(buildInfo, ctx)
            fftwLocation = '\usr\lib\fftw';
            includePath = fullfile(fftwLocation, 'include');
            buildInfo.addIncludePaths(includePath);
            libPath = fullfile(fftwLocation, 'lib');
            
            %Double
            libName1 = 'libfftw3-3';
            [~, libExt] = ctx.getStdLibInfo();
            libName1 = [libName1 libExt];
            addLinkObjects(buildInfo, libName1, libPath, 1000, true, true);
            
            %Single
            libName2 = 'libfftw3f-3';
            [~, libExt] = ctx.getStdLibInfo();
            libName2 = [libName2 libExt];
            addLinkObjects(buildInfo, libName2, libPath, 1000, true, true);
            
        end
    end
end

Set the code generation configuration parameters.

  • For code generation with the MATLAB® Coder™ codegen command, set:

    • CustomFFTCallback to 'myfftcb'.

    • CustomSource to 'mylock.c'.

    • CustomInitializer to 'mylock_initialize();'.

  • For code generation with the MATLAB Coder app, set:

    • Custom FFT library callback to myfftcb.

    • Additional source files to mylock.c.

    • Initialize function to mylock_initialize();.

  • For code generation from a MATLAB Function block by using Simulink® Coder, set these parameters:

    • Custom FFT library callback to myfftcb.

    • In Code Generation > Custom Code, under Additional build information, set Source files to mylock.c.

    • In Code Generation > Custom Code, under Insert custom C code in generated, set Initialize function to mylock_initialize();.

Generate code.

Version History

Introduced in R2017b