gpuArray - Array stored on GPU - MATLAB (original) (raw)

Description

A gpuArray object represents an array stored in GPU memory. A large number of functions in MATLAB® and in other toolboxes support gpuArray objects, allowing you to run your code on GPUs with minimal changes to the code. To work with gpuArray objects, use anygpuArray-enabled MATLAB function such as fft, mtimes ormldivide. To find a full list of gpuArray-enabled functions in MATLAB and in other toolboxes, see GPU-supported functions. For more information, see Run MATLAB Functions on a GPU.

If you want to retrieve the array from the GPU, for example when using a function that does not support gpuArray objects, use the gather function.

Note

You can load MAT files containing gpuArray data as in-memory arrays when a GPU is not available. A gpuArray object loaded without a GPU is limited and you cannot use it for computations. To use a gpuArray object loaded without a GPU, retrieve the contents using gather.

Creation

Use gpuArray to convert an array in the MATLAB workspace into a gpuArray object. Some MATLAB functions also allow you to create gpuArray objects directly. For more information, see Establish Arrays on a GPU.

Syntax

Description

G = gpuArray([X](#mw%5F5f2193f8-b8ac-42db-b682-3a5458265bfd)) copies the arrayX to the GPU and returns a gpuArray object.

example

Input Arguments

expand all

X — Array

numeric array | logical array

Array to transfer to the GPU, specified as a numeric or logical array. The GPU device must have sufficient free memory to store the data. If X is already a gpuArray object, gpuArray outputsX unchanged.

You can also transfer sparse arrays to the GPU. gpuArray supports only sparse arrays of double-precision.

Example: G = gpuArray(magic(3));

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical
Complex Number Support: Yes

Object Functions

arrayfun Apply function to each element of array on GPU
gather Transfer distributed array, Composite object, orgpuArray object to local workspace
pagefun Apply function to each page of distributed or GPU array

There are several methods for examining the characteristics of agpuArray object. Most behave like the MATLAB functions of the same name.

Several MATLAB toolboxes include functions with gpuArray support. To view lists of all functions in these toolboxes that support gpuArray objects, use the links in the following table. Functions in the lists with information indicators have limitations or usage notes specific to running the function on a GPU. You can check the usage notes and limitations in the Extended Capabilities section of the function reference page. For information about updates to individual gpuArray-enabled functions, see the release notes.

For a list of functions with gpuArray support in all MathWorks® products, see gpuArray-supported functions. Alternatively, you can filter by product. On the Help bar, click Functions. In the function list, browse the left pane to select a product, for example, MATLAB. At the bottom of the left pane, select GPU Arrays. If you select a product that does not have gpuArray-enabled functions, then theGPU Arrays filter is not available.

Examples

collapse all

Transfer Data to and from the GPU

To transfer data from the CPU to the GPU, use the gpuArray function.

Create an array X.

Transfer X to the GPU.

Check that the data is on the GPU.

Calculate the element-wise square of the array G.

Transfer the result GSq back to the CPU.

Check that the data is not on the GPU.

Create Data on the GPU Directly

You can create data directly on the GPU directly by using some MATLAB functions and specifying the option "gpuArray".

Create an array of random numbers directly on the GPU.

Check that the output is stored on the GPU.

Use MATLAB Functions with the GPU

This example shows how to use gpuArray-enabled MATLAB functions to operate with gpuArray objects. You can check the properties of your GPU using the gpuDevice function.

ans = CUDADevice with properties:

             Name: 'NVIDIA RTX A5000'
            Index: 1 (of 2)
ComputeCapability: '8.6'
      DriverModel: 'TCC'
      TotalMemory: 25544294400 (25.54 GB)
  AvailableMemory: 24734105600 (24.73 GB)
  DeviceAvailable: true
   DeviceSelected: true

Show all properties.

Create a row vector that repeats values from -15 to 15. To transfer it to the GPU and create a gpuArray object, use the gpuArray function.

X = [-15:15 0 -15:15 0 -15:15]; gpuX = gpuArray(X); whos gpuX

Name Size Bytes Class Attributes

gpuX 1x95 760 gpuArray

To operate with gpuArray objects, use any gpuArray-enabled MATLAB function. MATLAB automatically runs calculations on the GPU. For more information, see Run MATLAB Functions on a GPU. For example, use diag, expm, mod, round, abs, and fliplr together.

gpuE = expm(diag(gpuX,-1)) * expm(diag(gpuX,1)); gpuM = mod(round(abs(gpuE)),2); gpuF = gpuM + fliplr(gpuM);

Plot the results.

imagesc(gpuF); colormap(flip(gray));

If you need to transfer the data back from the GPU, use gather. Transferring data back to the CPU can be costly, and is generally not necessary unless you need to use your result with functions that do not support gpuArray.

result = gather(gpuF); whos result

Name Size Bytes Class Attributes

result 96x96 73728 double

In general, running code on the CPU and the GPU can produce different results due to numerical precision and algorithmic differences between the GPU and CPU. Answers from the CPU and GPU are both equally valid floating point approximations to the true analytical result, having been subjected to different roundoff behavior during computation. In this example, the results are integers and round eliminates the roundoff errors.

Perform Monte Carlo Integration Using gpuArray-Enabled Functions

This example shows how to use MATLAB functions and operators with gpuArray objects to compute the integral of a function by using the Monte Carlo integration method.

Define the number of points to sample. Sample points in the domain of the function, the interval [-1,1] in both x and y coordinates, by creating random points with the rand function. To create a random array directly on the GPU, use the rand function and specify "gpuArray". For more information, see Establish Arrays on a GPU.

n = 1e6; x = 2rand(n,1,"gpuArray")-1; y = 2rand(n,1,"gpuArray")-1;

Define the function to integrate, and use the Monte Carlo integration formula on it. This function approximates the value of π by sampling points within the unit circle. Because the code uses gpuArray-enabled functions and operators on gpuArray objects, the computations automatically run on the GPU. You can perform binary operations such as element-wise multiplication using the same syntax that you use for MATLAB arrays. For more information about gpuArray-enabled functions, see Run MATLAB Functions on a GPU.

f = x.^2 + y.^2 <= 1; result = 4*nnz(f)/n

Limitations

Tips

Alternatives

You can also create a gpuArray object using some MATLAB functions by specifying a gpuArray output. The following table lists the MATLAB functions that enable you to create gpuArray objects directly. For more information, see the Extended Capabilities section of the function reference page.

eye(___,"gpuArray") true(___,"gpuArray")
false(___,"gpuArray") zeros(___,"gpuArray")
Inf(___,"gpuArray") createArray(___,"gpuArray") (since R2024a)
NaN(___,"gpuArray") gpuArray.colon
ones(___,"gpuArray") gpuArray.freqspace
rand(___,"gpuArray") gpuArray.linspace
randi(___,"gpuArray") gpuArray.logspace
randn(___,"gpuArray") gpuArray.speye

Extended Capabilities

Thread-Based Environment

Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.

Version History

Introduced in R2010b