Main Content

Use GPUs in Containers

To take advantage of the performance benefits offered by graphics processing units (GPUs), you can use the GPUs of the host system in a container. By default, a container does not have access to hardware resources of its host. To enable the container to access the NVIDIA® GPUs of the host system, you need to:

  • Run the container on a host system with the appropriate NVIDIA GPU drivers installed, for example use the NVIDIA GPU-Optimized AMI.

  • Make the GPUs of the host visible to the container by using the --gpus flag when you execute the docker run command. Set this flag to all if you want the container to have access to all the GPUs of the host machine. For example, run a MATLAB® container and give it access to the GPUs of the host by executing a command like the one below

    docker run --gpus all mathworks/matlab

Check Container Access to GPUs

To check if your container has access to the GPUs of the host, open MATLAB in the container and execute the following command

ans=2×5 table
    Index        Name         ComputeCapability    DeviceAvailable    DeviceSelected
    _____    _____________    _________________    _______________    ______________

      1      "TITAN RTX"            "7.5"               true              true      
      2      "Quadro K620"          "5.0"               true              false     

This command returns a table of properties of all GPU devices detected in your system. Alternatively, if you are using a MATLAB release earlier than R2021a, use the functions gpuDevice or gpuDeviceCount instead. For more information, on this command and on how to select a specific GPU, see Identify and Select a GPU Device (Parallel Computing Toolbox).

Use GPUs to Speed Up MATLAB Code

After giving the container access to the GPUs, you can speed up your MATLAB code, such as a training neural networks in parallel, by taking advantage of their computational power. To get started, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox). To learn more about using multiple GPUs to train a single neural network in parallel, see Deep Learning with MATLAB on Multiple GPUs (Deep Learning Toolbox).

See Also

(Parallel Computing Toolbox)

Related Topics