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 MATLAB gpuArray inputs to the generated MEX function.

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

  • Use coder.typeof to represent the gpuArray type of an entry-point function input. For example:


  • Use the gpuArray function. For example:

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


  • GPU Coder supports all data type supported by gpuArray.

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

  • For 'lib', 'dll', and 'exe' targets, you must pass the corresponding 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.

  • The MemoryMode (memory allocation mode) property of the code configuration object must be set to 'discrete'. For 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 by gpuArray. For example. if the entry-point function returns a struct and because struct is not supported by gpuArray, the generated code returns a CPU output. However, if a supported matrix type is returned, then the generated code returns a GPU output.


    Passing gpuArray inputs does not guarantee the outputs to also be gpuArray.


  • GPU Coder does not support the following types:

    • char

    • half

    • 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