Skip to Main Content Skip to Search
Product Documentation

Generating Reentrant C Code from MATLAB Code

About This Tutorial

Learning Objectives

This tutorial shows you how to:

Prerequisites

To complete this tutorial, install the required products and set up your C compiler as described in Prerequisites

Required Files

TypeNameDescription
Function codematrix_exp.mMATLAB Function that computes matrix exponential of the input matrix using Taylor series and returns the computed output.
C main functionmain.c Calls the reentrant code.

To run the tutorial, copy these files to a local folder. For instructions, see Copying Files Locally.

Copying Files Locally

Copy the tutorial files to a local working folder.

  1. Create a local working folder, for example, c:\ecoder\work.

  2. Change to the matlabroot\help\toolbox\ecoder\examples folder. At the MATLAB command line, enter:

    cd(fullfile(docroot, 'toolbox', 'ecoder', 'examples'))
    
  3. Copy the reentrant_win folder to your local working folder.

    Your work folder now contains the files you need to complete this tutorial.

  4. Set your MATLAB current folder to the work folder that contains your files for this tutorial. At the MATLAB command line, enter:

    cd work

    where work is the full path of the work folder containing your files.

About the Example

This example requires libraries that are specific to the Microsoft Windows operating system and, therefore, runs only on Windows platforms. It is a simple, multithreaded example that uses no persistent or global data. Two threads call the MATLAB function matrix_exp with different sets of input data.

 Contents of matrix_exp.m

When you generate reusable, reentrant code, codegen supports dynamic allocation of function variables that are too large for the stack, as well as persistent and global variables. codegen generates a header file, primary_function_name_types.h, which you must include when using the generated code. This header file contains the following structures:

Providing a main Function

To call the reentrant code, you must provide a main function that:

 Contents of main.c

Configuring Build Parameters

You enable generation of reentrant code using a code generation configuration object.

  1. Create a configuration object.

    e = coder.config('exe', 'ecoder', true);

    This command creates a coder.EmbeddedCodeConfig object which contains all the configuration parameters that the codegen function needs to generate standalone C/C++ static libraries and executables for an embedded target.

  2. Enable reentrant code generation.

    e.MultiInstanceCode = true;

Generating the C Code

Call the codegen function to generate C code, with the following options:

codegen -config e main.c -report matrix_exp.m -args ones(160,160)

codegen generates a C executable, matrix_exp.exe, in the current folder and C code in the /codegen/exe/matrix_exp subfolder. Because you selected report generation, codegen provides a link to the report.

Viewing the Generated C Code

codegen generates a header file matrix_exp_types.h, which defines the matrix_expStackData global structure. This structure contains local variables that are too large to fit on the stack.

To view this header file:

  1. Click the View report link to open the code generation report.

  2. In the report, click the C code tab.

  3. On this tab, click the link to matrix_exp_types.h.

/*
 * matrix_exp_types.h
 *
 * MATLAB Coder code generation for function 'matrix_exp'
 */
#ifndef __MATRIX_EXP_TYPES_H__
#define __MATRIX_EXP_TYPES_H__

/* Type Definitions */
typedef struct {
    struct {
        real_T F[25600];
        real_T Y[25600];
    } f0;
 } matrix_expStackData;
 
#endif
/* End of code generation (matrix_exp_types.h) */

Running the Code

Call the code, first verifying that the example is running on Windows platforms.

% This example can only be run on Windows platforms
if ~ispc
 error('This example requires Windows-specific libraries and can only be run on 
 Windows.');
end
system('matrix_exp.exe')

The executable runs and reports completion.

Key Points to Remember

Learn More

To...See...

Learn more about the generated code API

Generated Code API

Call reentrant code with no persistent or global data on UNIX

Call Reentrant Code with No Persistent or Global Data (UNIX Only)

Call reentrant code with persistent data on Windows

Call Reentrant Code — Multithreaded with Persistent Data (Windows Only)

Call reentrant code with persistent data on UNIX

Call Reentrant Code — Multithreaded with Persistent Data (UNIX Only)

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS