Main Content

coder.specifyAsGPU

Specify that value is a GPU input to an entry-point function for GPU code generation

Since R2023b

Description

coder.specifyAsGPU(value,className) is primarily used to specify that value is a GPU input with an underlying class of className for code generation. To learn more about input-type specification using arguments blocks, see Use Function Argument Validation to Specify Entry-Point Input Types.

The coder.specifyAsGPU validator has three distinct behaviors:

  • In MATLAB® execution, coder.specifyAsGPU throws an error if value is a GPU array with an underlying type that does not match className. Alternatively, if value is not a GPU array, coder.specifyAsGPU throws an error if the type of value does not match className.

  • During input-type specification using function argument validation, coder.specifyAsGPU specifies that value is a GPU array with an underlying type of className.

  • During code generation, coder.specifyAsGPU throws an error if the type of value does not match className.

example

Examples

collapse all

Use function argument validation to specify that an entry-point input can represent GPU data for GPU code generation using coder.specifyAsGPU.

Define an entry-point function multiplyByThreeInGPU, which accepts a double-precision 16-by-16 matrix input that represents GPU data.

function out = multiplyByThreeInGPU(in)
arguments
    in (16,16) {coder.specifyAsGPU(in, 'double')}
end
out = in*3;
end

Generate a static library for multiplyByThreeInGPU using the codegen command. Because the arguments block fully specifies the type and size of the input argument in, you do not need to use the -args option.

cfg = coder.gpuConfig('lib');
codegen -config cfg -c multiplyByThreeInGPU -report
Code generation successful: View report

Open the code generation report. Observe that the expression out = in*3 is executed on the GPU in the generated code.

Screenshot of code generation report showing that the expression out = in*3 is executed on GPU.

Input Arguments

collapse all

Value to validate, specified as a numeric scalar, numeric array, logical scalar, or logical array.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

Name of a class, specified as a character vector or string scalar.

Data Types: char | string

Extended Capabilities

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

Version History

Introduced in R2023b