coder.rref - Indicate read-only data to pass by reference - MATLAB (original) (raw)

Indicate read-only data to pass by reference

Syntax

Description

coder.rref([arg](#bvms4qb-arg)) indicates thatarg is a read-only expression or variable to pass by reference to an external C/C++ function. Use coder.rref only inside acoder.ceval call.

The coder.rref function can enable the code generator to optimize the generated code. Because the external function is assumed to not write tocoder.rref(arg), the code generator can perform optimizations such as expression folding on assignments to arg that occur before and after thecoder.ceval call. Expression folding is the combining of multiple operations into one statement to avoid the use of temporary variables and improve code performance.

Note

The code generator assumes that the memory that you pass withcoder.rref(arg) is read-only. To avoid unpredictable results, the C/C++ function must not write to this variable.

See also coder.ref and coder.wref.

example

coder.rref([arg](#bvms4qb-arg),'gpu') indicates thatarg is a GPU argument. This option requires a valid GPU Coder™ license. If the coder.ceval calls a CUDA® GPU __device__ function, the code generator ignores the'gpu' specification.

Examples

Pass Scalar Variable as a Read-Only Reference

Consider the C function addone that returns the value of a constant input plus one:

double addone(const double* p) { return *p + 1; }

The C function defines the input variable p as a pointer to a constant double.

Pass the input by reference to addone:

... y = 0; u = 42; y = coder.ceval('addone', coder.rref(u)); ...

Pass Multiple Arguments as a Read-Only Reference

... u = 1; v = 2; y = coder.ceval('my_fcn', coder.rref(u), coder.rref(v)); ...

Pass Class Property as a Read-Only Reference

... x = myClass; x.prop = 1; y = coder.ceval('foo', coder.rref(x.prop)); ...

Pass Structure as a Read-Only Reference

To indicate that the structure type is defined in a C header file, use coder.cstructname.

Suppose that you have the C function use_struct. This function reads from the input argument but does not write to it.

#include "MyStruct.h"

double use_struct(const struct MyStruct *my_struct) { return my_struct->f1 + my_struct->f2; }

The C header file, MyStruct.h, defines a structure type namedMyStruct:

#ifndef MYSTRUCT #define MYSTRUCT

typedef struct MyStruct { double f1; double f2; } MyStruct;

double use_struct(const struct MyStruct *my_struct);

#endif

In your MATLAB® function, pass a structure as a read-only reference touse_struct. To indicate that the structure type fors has the name MyStruct that is defined in the C header file MyStruct.h, usecoder.cstructname.

function y = foo %#codegen y = 0; coder.updateBuildInfo('addSourceFiles','use_struct.c');

s = struct('f1',1,'f2',2); coder.cstructname(s,'MyStruct','extern','HeaderFile','MyStruct.h'); y = coder.ceval('use_struct', coder.rref(s));

To generate standalone library code, enter:

codegen -config:lib foo -report

Pass Structure Field as a Read-Only Reference

... s = struct('s1', struct('a', [0 1])); y = coder.ceval('foo', coder.rref(s.s1.a)); ...

You can also pass an element of an array of structures:

... c = repmat(struct('u',magic(2)),1,10); b = repmat(struct('c',c),3,6); a = struct('b',b); coder.ceval('foo', coder.rref(a.b(3,4).c(2).u)); ...

Input Arguments

collapse all

arg — Argument to pass by reference

scalar variable | array | element of an array | structure | structure field | object property

Argument to pass by reference to an external C/C++ function. The argument cannot be a class, a System object™, a cell array, or an index into a cell array.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | struct
Complex Number Support: Yes

Limitations

Tips

Extended Capabilities

C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

GPU Code Generation

Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

HDL Code Generation

Generate VHDL, Verilog and SystemVerilog code for FPGA and ASIC designs using HDL Coder™.

The coder.rref function support only MATLAB to High-Level Synthesis (HLS) workflow in HDL Coder™.

Version History

Introduced in R2011a