gpuArray

Description

A gpuArray object represents an array stored on the GPU. To
work with gpuArray objects, use any GPU-enabled MATLAB® function. You can use the array for direct calculations or in CUDA kernels that
execute on the GPU. 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.

Creation

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

Syntax

G = gpuArray(X)

Description

Input Arguments

X — Arraynumeric 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 outputs
X unchanged.

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

Create a row vector that repeats values from -15 to 15. To transfer it to the GPU and create a gpuArray, 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 4 gpuArray

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

If you need to transfer the data back from the GPU, use gather. Gathering 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

Perform a Monte Carlo Integration Using GPU-Enabled Functions

This example shows how to use MATLAB functions and operators with gpuArrays to compute the integral of a function, 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 = 2*rand(n,1,'gpuArray')-1;
y = 2*rand(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 GPU-enabled functions and operators on gpuArrays, the computations automatically run on the GPU. You can perform binary operations such as element-wise multiplication using the same syntax as MATLAB arrays use. To learn more about GPU-enabled functions, see Run MATLAB Functions on a GPU.

You can use the gpuArray function to transfer data to the GPU and create a gpuArray, or you can create an array directly on the GPU. gpuArray provides GPU versions of many functions that you can use to create data arrays, such as linspace. For more information, see Create GPU Arrays Directly.

Many MATLAB functions support gpuArrays. When you supply a gpuArray argument to any GPU-enabled function, the function runs automatically on the GPU. For more information, see Run MATLAB Functions on a GPU. Create a complex grid for the algorithm, and create the array count for the results. To create this array directly on the GPU, use the ones function, and specify 'gpuArray'.