Main Content

canUseGPU

Verify supported GPU is available for computation

    Description

    tf = canUseGPU returns a logical value indicating if there is a GPU available for computation. The function returns logical 1 (true) if there is a supported GPU available, a recent GPU driver is present, and Parallel Computing Toolbox™ is installed and licensed for use. Otherwise, the function returns logical 0 (false).

    Use canUseGPU to avoid executing code that requires a GPU if one is not available. Using a GPU requires Parallel Computing Toolbox and a supported GPU device. For information on supported GPUs, see GPU Computing Requirements (Parallel Computing Toolbox). To diagnose issues with your GPU, for example if canUseGPU unexpectedly returns 0 (false), use the validateGPU (Parallel Computing Toolbox).

    example

    Examples

    collapse all

    Solve a system of linear equations. Perform the calculation using a GPU if one is available; otherwise, use the CPU.

    Create the data on the CPU.

    N = 1000;
    A = rand(N);
    b = rand(N,1);

    Transfer the matrix A to the GPU, if there is one available

    tf = canUseGPU
    tf = 
       1
    
    if tf
        A = gpuArray(A);
    end

    Solve the system of linear equations.

    x = A\b;

    Since A exists on the GPU, the solution is calculated on the GPU.

    Version History

    Introduced in R2019b

    See Also

    (Parallel Computing Toolbox) | (Parallel Computing Toolbox) | (Parallel Computing Toolbox) | | (Parallel Computing Toolbox)

    Topics