Contenu principal

Generate C++ Classes in HLS code for MATLAB Classes

When you generate HLS code, the code generator produces C++ classes for the MATLAB® classes in your MATLAB code These include all MATLAB classes such as value classes, handle classes, and system objects.

By default, all the class methods are inlined into the calling functions. If you want to generate C++ class methods from MATLAB class methods, use apply coder.inline('never') on the MATLAB class methods or set the InstantiateFunctions property of the coder.HDLconfig object to true.

Generate HLS Code and Map MATLAB Classes to C++ Classes

The code generator follows certain rules when mapping MATLAB classes to C++ classes. This example illustrates how the code generator applies these rules to map MATLAB classes to C++ classes.

Generate HLS Code for a Handle Class with Private and Public Members

Define a MATLAB handle class MyClass:

classdef MyClass < handle
    properties
        publicProp = 1;
    end
    properties(Access = private)
        privateProp
    end
    methods
        function obj = MyClass(value)
            obj.privateProp = value;
        end
        function publicMethod(obj,value)
            obj.privateMethod(value);
        end
        function res = calculateSomeValue(obj)
            res = obj.publicProp*obj.privateProp;
        end
    end
    methods (Access = private)
        function privateMethod(obj,value)
            obj.publicProp = obj.publicProp + value;
            obj.privateProp = obj.privateProp + obj.doubleThisValue(value);
        end
    end
    methods(Static)
        function res = doubleThisValue(val)
            res = 2 * val;
        end
    end
end

Define a MATLAB function useMyClass that uses MyClass:

function out = useMyClass(x,y)
obj = MyClass(x);
obj.publicMethod(y);
out = obj.calculateSomeValue;
end

Generate HLS code for useMyClass. Specify the input argument to be a double scalar. Set the code generation configuration property InstantiateFunctions to true.

cfg = coder.config("hdl");
cfg.Workflow = "High Level Synthesis";
cfg.InstantiateFunctions = true;
codegen -config cfg useMyClass -args {0,0} -report
Code generation successful: View report

Open the code generation report and inspect the generated code. The file MyClass.hpp contains the definition of the generated C++ class MyClass:

class MyClass final
{
 public:
  real_T publicProp;
 private:
  real_T privateProp;
 public:
  MyClass *init(real_T value);
  void MyClass_publicMethod(real_T value_0);
 private:
  void MyClass_privateMethod(real_T value_1);
 public:
  static real_T MyClass_doubleThisValue(real_T val);
  real_T MyClass_calculateSomeValue();
};

Here is the code generated for the function useMyClass:

class useMyClassClass
{
 public:
  real_T useMyClass(real_T x, real_T y)
  {
    MyClass obj;
    obj.init(x);
    obj.MyClass_publicMethod(y);
    return obj.MyClass_calculateSomeValue();
  }
};

Explore Mapping of MATLAB Classes to C++ Classes

This table lists some of the rules that the code generator follows when generating C++ classes and the corresponding snippets from the code generated for MyClass.

RuleCode Snippet

The class constructor in the MATLAB code is mapped onto an init method. When an instance of the class is created, the generated code explicitly calls the init method.

The file MyClass.cpp contains the definition of init.

MyClass *MyClass::init(real_T value)
{
  MyClass *obj_0;
  obj_0 = this;
  obj_0->publicProp = 1.0;
  obj_0->privateProp = value;
  return obj_0;
}

If a class member is set as private in MATLAB, it is also set as private in the generated HLS code.

Inlining a public method in the generated HLS code changes a private property in your MATLAB code to a public property in the generated code and breaks data encapsulation. For example, a public method myMethod that uses a private property prop of the object is called by an entry-point function. If myMethod is inlined in the generated code, the property prop must be visible from outside the object and changed to a public property.

The same inlining rules apply to both ordinary functions and public methods:

  • If the function or method is called using coder.inlineCall or coder.nonInlineCall, then this call overrides coder.inline directives in the body of the function, as well as global inlining settings.

  • If the body of the function or the method contains an explicit coder.inline("always") or coder.inline("never") directive, then this directive overrides global inlining settings.

The definition of the generated C++ class MyClass is:

class MyClass final
{
 public:
  real_T publicProp;
 private:
  real_T privateProp;
 public:
  MyClass *init(real_T value);
  void MyClass_publicMethod(real_T value_0);
 private:
  void MyClass_privateMethod(real_T value_1);
 public:
  static real_T MyClass_doubleThisValue(real_T val);
  real_T MyClass_calculateSomeValue();
};

The visibility of all data and member functions is preserved between MATLAB and the generated code.

Static methods in MATLAB are mapped onto static C++ methods.

The generated code for the static method doubleThisValue has this signature:

static real_T MyClass_doubleThisValue(real_T val);

Additional Usage Notes and Limitations

These additional usage notes and limitations apply when generating C++ classes from MATLAB classes:

  • The class prototype for the generated class MyClass is in the header file MyClass.hpp. The implementations of the methods of the class are in the file MyClass.cpp.

  • In the generated code, class hierarchies are flattened. For example, if one MATLAB class inherits from another, then the corresponding generated classes have no inheritance relationship between them. In the generated code, all properties and methods of the inherited class are reproduced in the inheritor class.

  • When a MATLAB class uses different types for its properties, the code generator produces a separate C++ class for each type usage.

  • If a MATLAB class member has different GetAccess and SetAccess attributes, the corresponding member of the generated class has the more permissive of the two attributes. For example, if a property prop has the attributes (GetAccess = public, SetAccess = private), the member prop is defined to be a public property in the generated code.

See Also

| | | | |

Topics