Main Content

Generate Code for Functions with Multiple Signatures

An entry-point function is a top-level MATLAB® function from which you generate code. If your entry-point function has inputs, you must specify the properties of the inputs to generate code. In this case, the generated code works only with the signature of the entry-point function that you specify during code generation.

If your entry-point function supports multiple signatures, you can generate a single MEX function instead of generating a separate MEX function for each signature. Additionally, you can also generate corresponding C/C++ code for each signature. The generated code works with the multiple signatures provided during code generation.

By using multisignature functionality, you can:

  • Generate code that supports the multiple signatures that you specify in the entry-point function.

  • Reduce the overhead involved in generating and using separate MEX functions for each signature of your entry-point function.

  • Achieve MATLAB function-like behavior in the generated MEX function.

Generate Multisignature MEX Function for a Single Entry-Point Function

To generate a multisignature MEX (polymorphic MEX) function, consider this function myAdd:

function y = myAdd(a,b)
%#codegen
y = a+b;
end
Suppose that you want to generate a MEX function from myAdd that works with three different data types: double, int8, and vector of doubles. Specify the three arguments as: {1,2}, {int8(2), int8(3)}, and {1:10, 1:10}. You specify the entry-point function followed by a -args for each signature of the entry-point function.

To generate code for myAdd function, at the MATLAB command prompt, run this codegen command:

codegen -config:mex myAdd.m -args {1,2} -args {int8(2),int8(3)} -args {1:10,1:10} -report
This syntax generates a single MEX function myAdd_mex for the signatures specified in the codegen command.

At the command prompt, call the generated MEX function myAdd_mex. Make sure that the values you pass to myAdd_mex match the input properties that you specified in the codegen command.

myAdd_mex(3,4)
ans =

     7
myAdd_mex(int8(5),int8(6))
ans =

  int8

   11
myAdd_mex(1:10,2:11)
ans =

     3     5     7     9    11    13    15    17    19    21

Running the MATLAB function myAdd with these input values produces the same output. These test cases verify that myAdd and myAdd_mex have the same behavior.

Generate Multisignature MEX Function for Multiple Entry-Point Functions

During code generation, you can also generate one MEX function for multiple entry-point functions containing multiple signatures.

Suppose that you have two entry-point functions myAdd and myMul. The first entry-point function, myAdd returns the sum of two values:

function y = myAdd(a,b)
%#codegen
y = a+b;
end

The second entry-point function, myMul returns the multiplication of two values:

function y = myMul(a,b)
%#codegen
y = a*b;
end

You specify the entry-point function followed by a -args for each signature of the entry-point function. Consider that the function myAdd supports the input types double and int8. Specify these arguments as: {1,2} and {int8(1), int8(2)}. Similarly, if the function myMul supports the input types double and int16, specify these arguments as: {1,2} and {int16(1), int16(2)}. Now, you can generate a MEX function from your entry-point functions.

To generate code for myAdd and myMul functions, at the MATLAB command prompt, run this codegen command:

codegen -config:mex myAdd.m -args {1,2} -args {int8(1),int8(2)} myMul.m -args {1,2} -args {int16(1),int16(2)} -o 'myMath' -report

This syntax generates one MEX function myMath for all the signatures that you specified in the codegen command.

You can verify the output values by using the generated MEX function myMath at the command prompt. Make sure that the values you pass to myMath match the input properties that you specified before code generation.

myMath("myAdd",3,4)
ans =

     7
myMath("myAdd",int8(5),int8(6))
ans =

   int8 
   11
myMath("myMul",3,4)
ans =

     12
myMath("myMul",int16(5),int16(6))
ans =

    int16
    30

Running the MATLAB function myAdd and myMul with these input values produces the same output. These test cases verify that myAdd, myMul, and the generated MEX function myMath have the same behavior.

Limitations

Multisignature MEX (polymorphic MEX) generation does not support:

See Also

|

Related Topics