Main Content

Support for GPU Arrays

You can use GPU arrays as input and output arguments to an entry-point function when generating CUDA®MEX, source code, static libraries, dynamic libraries, and executables. GPU Coder™ automatically takes care of CPU-GPU copies based on the input type and the usage of the variable in your MATLAB®design. The GPU array functionality is useful in minimizing CPU-GPU copies when you are trying to:

  • Integrate the generated code with an existing implementation that has its outputs on the GPU memory.

  • Pass MATLABgpuArrayinputs to the generated MEX function.

To mark an input to the entry-point function as a GPU type, use one of the following approaches:

  • Usecoder.typeofto represent thegpuArraytype of an entry-point function input. For example:

    coder.typeof(rand(20),'Gpu',true);

  • Use thegpuArrayfunction. For example:

    in = gpuArray(rand(1,10)); codegen-configcfg-args{in}test

Considerations

  • GPU Coder supports all data type supported bygpuArray

  • For using variable dimension arrays, only the bounded types are supported.

  • For'lib','dll', and'exe'targets, you must pass the correct pointers to the entry-point function in the example main function. For example, if an input is marked as'Gpu', a GPU pointer should be passed when the entry-point is called from the main function.

  • TheMemoryMode(memory allocation mode) property of the code configuration object must be set to'discrete'。对于example,

    cfg.GpuConfig.MallocMode ='discrete';

  • During code generation, if one input to entry-point function is of the GPU array, then GPU Coder attempts to make all the output variables GPU array types, provided they are supported bygpuArray。对于example. if the entry-point function returns astructand becausestructis not supported bygpuArray, the generated code returns a CPU output. However, if a supported matrix type is returned, then the generated code returns a GPU output.

    Note

    PassinggpuArrayinputs does not guarantee the outputs to also begpuArray

Limitations

  • GPU Coder does not support the following types:

    • char

    • 一半

    • Scalar GPU arrays

    • Structures

    • Cell arrays

    • Classes

    • Enumerated types

    • Fixed-point data types

  • GPU Coder does not support the'unified'memory mode for GPU arrays.

See Also

||||

Related Topics