主要内容

MATLAB语言功能支持GPU编码器万博1manbetx

GPU CODER™支万博1manbetx持许多MATLAB®language features supported byMATLABCoder™, seeMATLAB语言功能支持C/C ++代码生成万博1manbetx。但是,某些功能可以在受限模式下支持,而其他功能则不支持。万博1manbetx在以下各节中,我们突出显示了影响GPU代码生成的一些重要功能,然后列出了GPU编码器不支持的功能。万博1manbetx

一个常见和重要的考虑因素是可变大小的矩阵支持。万博1manbetx此功能确实会影响CUDA的方式®创建了内核,以下讨论描述了GPU代码生成的功能和注意事项。

可变大小数组的代码生成

对于代码生成,数组维度为固定尺寸或者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固定尺寸大批。在以下示例中,Z是一个固定尺寸的数组。

功能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 avariable-size大批。

可变大小的维度是bounded或者无限。有界的尺寸具有固定的上限。无界尺寸没有固定的上部尺寸。

在下面的示例中,第二维Z是有限的,可变大小的。它的上限为32。

功能s = myfcn(n)如果(n> 0)z =零(1,4);别的Z = zeros(1,32);ends = length(Z);

在下面的示例中,如果值的值n在编译时未知,然后是第二维Z是无限的。

功能s = myfcn(n)Z = rand(1,n); s = sum(Z);end

您可以通过以下方式定义可变大小数组

  • 使用构造函数,例如zeros或者那些,具有非稳定尺寸值

  • 在使用之前,将多个恒定大小分配给同一变量

  • Using loops to grow the dimensions of variables

  • 通过使用coder.typeof或者coder.varsize功能。例如,coder.typeof(1,[12,1],[true,false])coder.varsize(1, [Inf,1], [true, false])

有关更多信息,请参阅Define Variable-Size Data for Code Generation

Enabling and Disabling Support for Variable-Size Arrays

代码生成行为

对于有界的可变大小数组,GPU编码器将这些有界变量映射到GPU和CUDA内核。要指定可变大小数组的上限,请参见Specify Upper Bounds for Variable-Size Arrays

对你nbounded, variable-size arrays and variable-size arrays whose size is greater than or equal to a动态成员选择threshold, 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.

默认情况下,代码生成器设置为使用动态内存分配,用于可变大小的数组,其大小大于或等于阈值,阈值为2 GB。更改这些设置:

  • 在配置对象中,设置动态成员选择to临界点DynamicMemoryalcosionThrosh到非负整数。

  • 在GPU编码器应用中,在内存设置,设置Dynamic memory allocationto对于最大尺寸或以上阈值的数组和theDynamic memory allocation threshold到非负整数。

代码生成报告中的可变大小数组

您可以通过查看该数组是固定大小还是可变大小的大小column of the变量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结构功能

  • Index structure fields using dot notation

  • 将主要或入口点函数定义为结构

  • 将结构传递到本地功能

有关更多信息,请参阅Structure Definition for Code Generation

Note

GPU编码器generates more efficient code when you use struct of arrays instead of array of structs.

例子

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结构功能。

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

在MATLAB中,建立结构阵列时,通常会随身携带字段。代码生成不支持这种“动态”建筑结构风格。万博1manbetx原因之一是MATLAB中有可能对结构数组的两个不同元素具有不同的结构字段,这与更静态的类型推理方法冲突。因此,您必须先指定基本标量元素,然后从该完全指定的元素中生长一个结构数组。此方法确保结构数组的两个元素总是共享类型(字段)。

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

现在,您可以定义入口点功能mStructSupport那是AOFas input. The local functionarrayOp双打aofs.b和stores the result inaofs.a

功能[V] = mStructSupport(AofS) V = arrayOp(AofS);end功能aofs = arrayop(aofs)n = numel(aofs);fori = 1:n AofS(i).a = AofS(i).b * 2;endend

您可以使用中描述的任何方法使用GPU编码器应用程序生成代码to generate CUDA code for this example.

Unsupported Features

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

  • 内存完整性检查,请参阅Control Run-Time Checks

  • Array bound and dimension checks.

  • 休息语句。

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

  • 仅当在另一个函数中定义而不是作为入口点参数时,才万博1manbetx支持匿名函数。

  • MATLAB课程。