matlab language features support for gpu coder -凯发k8网页登录

main content

matlab language features support for gpu coder

gpu coder™ supports many of the matlab® language features supported by matlab coder™, see . however, some features may be supported in a restricted mode and others not supported. in the following sections, we highlight some of the important features that affect gpu code generation and then list the features that not supported by gpu coder.

a common and important consideration is variable-size matrices support. this feature can really affect the way cuda® kernels are created and the following discussion describes the feature and considerations for gpu code generation.

code generation for variable-size arrays

for code generation, an array dimension is fixed-size or variable-size. if the code generator can determine the size of an array and that the size of the array does not change at run time, then the dimension is fixed-size. when all dimensions of an array are fixed-size, the array is a fixed-size array. in the following example, z is a fixed-size array.

function z = myfcn()
z = zeros(1,4);
end

if the code generator cannot determine the size of an array or the code generator determines that the size changes, then the dimension is variable-size. when at least one of its dimensions is variable-size, an array is a variable-size array.

a variable-size dimension is either bounded or unbounded. a bounded dimension has a fixed upper size. an unbounded dimension does not have a fixed upper size.

in the following example, the second dimension of z is bounded, variable-size. it has an upper bound of 32.

function s = myfcn(n)
if (n > 0)
    z = zeros(1,4);
else
    z = zeros(1,32);
end
s = length(z);

in the following example, if the value of n is unknown at compile time, then the second dimension of z is unbounded.

function s = myfcn(n)
z = rand(1,n);
s = sum(z);
end

you can define variable-size arrays by:

  • using constructors, such as zeros or ones, with a nonconstant size value

  • assigning multiple, constant sizes to the same variable before using it

  • using loops to grow the dimensions of variables

  • declaring all instances of a variable to be variable-size by using coder.typeof or coder.varsize functions. for example, coder.typeof(1, [12,1],[true, false]) and coder.varsize(1, [inf,1], [true, false]).

for more information, see .

enabling and disabling support for variable-size arrays

code generation behavior

for variable-size arrays that are bounded, gpu coder maps these bounded variables to the gpu and cuda kernels are created. to specify upper bounds for variable-size arrays, see .

for unbounded, variable-size arrays and variable-size arrays whose size is greater than or equal to a dynamicmemoryallocationthreshold, gpu coder does not map these variables to the gpu and kernels are not created. the code generator allocates memory dynamically on the cpu heap. gpu coder issues a warning for unbounded variables in the build log and code generation report.

by default, the code generator is set to use dynamic memory allocation for variable-size arrays whose size is greater than or equal to the threshold with a threshold value of 2 gb. to change these settings:

  • in the configuration object, set the enabledynamicmemoryallocation to true and dynamicmemoryallocationthreshold to a non-negative integer.

  • in the gpu coder app, in the memory settings, select enable dynamic memory allocation and set the dynamic memory allocation threshold to a non-negative integer.

variable-size arrays in a code generation report

you can tell whether an array is fixed-size or variable-size by looking at the size column of the variables tab in a code generation report.

a colon (:) indicates that a dimension is variable-size. a question mark (?) indicates that the size is unbounded. for example, a size of 1-by-:? indicates that the size of the first dimension is fixed-size 1 and the size of the second dimension is unbounded, variable-size. an asterisk (*) indicates that the code generator produced a variable-size array, but the size of the array does not change during execution.

structure definition for code generation

to generate efficient standalone code for structures, you must define and use structures differently than you normally would when running your code in the matlab environment. for code generation, you must first create a scalar template version of the structure before growing it into an array. the code generation inference engine uses the type of this scalar value as the base type of the array. to generate standalone code for matlab structures, you are restricted to the following operations:

  • define structures as local and persistent variables by assignment and using the struct function

  • index structure fields using dot notation

  • define primary or entry-point function inputs as structures

  • pass structures to local functions

for more information, see .

note

gpu coder generates more efficient code when you use struct of arrays instead of array of structs.

example

this example shows how to write a matlab function that uses structure arrays so that it is suitable for code generation. first, you must specify the base element using the struct function.

temps = struct('a',0,'b',0);
nume = 2000;
aofs = repmat(temps,nume,1);

in matlab, when building up a structure array, you would typically add fields as you go. this "dynamic" style of building structures is not supported for code generation. one reason is that it is possible in matlab to have different structure fields for two different elements of a structure array, which conflicts with the more static approach of type inference. therefore, you must specify the base scalar element first, and then grow a structure array from this fully specified element. this method guarantees that two elements of a structure array always share type (fields).

for ind = 1:nume
 aofs(ind).a = rand;
 aofs(ind).b = rand;
end

now, you can define an entry-point function mstructsupport that takes aofs as input. the local function arrayop doubles aofs.b and stores the result in aofs.a.

function [v] = mstructsupport(aofs)
 v = arrayop(aofs);
end
function aofs = arrayop(aofs)
 n = numel(aofs);
 for i = 1:n
  aofs(i).a  = aofs(i).b * 2;    
 end  
end

you can use any of the methods described in code generation by using the gpu coder app to generate cuda code for this example.

unsupported features

the following list contains the features that are not currently supported.

  • memory integrity checks, see .

  • array bound and dimension checks.

  • break statements.

  • function handles are supported only when defined within another function and not as entry-point parameter.

  • anonymous functions are supported only when defined within another function and not as an entry-point parameter.

  • matlab classes.

网站地图