主要内容

在GPU上建立数组

AgpuArrayin MATLAB®represents an array that is stored on the GPU. For a complete list of functions that support arrays on the GPU, seeRun MATLAB Functions on a GPU.

Create GPU Arrays from Existing Data

Send Arrays to the GPU

GPU arrays can be created by transferring existing arrays from the workspace to the GPU. Use thegpuArrayfunction to transfer an array from MATLAB to the GPU:

N = 6; M = magic(N); G = gpuArray(M);

您可以用一行代码来完成此操作:

g = gpuarray(魔术(n));

Gis now a MATLABgpuArray代表存储在GPU上的魔法广场的对象。提供给的输入gpuArray必须是数字(例如:single,double,int8, etc.) or logical. (See alsoWork with Complex Numbers on a GPU

从GPU检索阵列

Use thegatherfunction to retrieve arrays from the GPU to the MATLAB workspace. This takes an array that is on the GPU represented by agpuArrayobject, and transfers it to the MATLAB workspace as a regular MATLAB array. You can useisequalto verify that you get the correct values back:

G = gpuArray(ones(100,'uint32')); D = gather(G); OK = isequal(D,ones(100,'uint32'))

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 supportgpuArray.

Example: Transfer Array to the GPU

Create a 1000-by-1000 random matrix in MATLAB, and then transfer it to the GPU:

X = rand(1000); G = gpuArray(X);

示例:指定精度的传输阵列

在MATLAB中创建一个双精度随机值的矩阵,然后将矩阵作为单精量从MATLAB传输到GPU:

X = rand(1000); G = gpuArray(single(X));

Create GPU Arrays Directly

A number of functions allow you to directly construct arrays on the GPU by specifying the'gpuarray'键入输入参数。这些功能仅需要数组大小和数据类信息,因此它们可以构造数组,而无需从MATLAB工作区传输任何元素。有关更多信息,请参阅gpuArray.

Example: Construct an Identity Matrix on the GPU

To create a 1024-by-1024 identity matrix of typeINT32on the GPU, type

II = eye(1024,'int32','gpuarray');size(II)
1024 1024

使用一个数值参数,您可以创建一个二维矩阵。

示例:在GPU上构造多维阵列

用数据类创建一个3维数组doubleon the GPU, type

G = ones(100,100,50,'gpuarray');尺寸(g)
100 100 50
基础类型(G)
double

数据的默认类是double, so you do not have to specify it.

Example: Construct a Vector on the GPU

To create a 8192-element column vector of zeros on the GPU, type

Z = zeros(8192,1,'gpuarray');尺寸(z)
8192 1

For a column vector, the size of the second dimension is 1.

ExaminegpuArray特征

有几个功能可用于检查gpuArray目的:

功能 Description
基础类型 Class of the underlying data in the array
existsOnGPU Indication if array exists on the GPU and is accessible
isreal Indication if array data is real
Isunderlyingtype

Determine if underlying array data is of specified class, such asdouble

isequal Determine if two or more arrays are equal
isnumeric Determine if an array is of a numeric data type
issparse Determine if an array is sparse
length Length of vector or largest array dimension
mustBeUnderlyingType Validate that array has specified underlying type, such as double
ndims 数组中的尺寸数量
size Size of array dimensions

For example, to examine the size of thegpuArrayobjectG, 类型:

g = rand(100,'gpuarray');s = size(g)
100 100

Save and LoadgpuArrayObjects

您可以保存gpuArrayvariables as MAT files for later use. When you save agpuArrayfrom the MATLAB workspace, the data is saved as agpuArrayvariable in a MAT file. When you load a MAT file containing agpuArrayvariable, the data is loaded onto the GPU as agpuArray.

Note

您可以加载包含的垫子文件gpuArraydata as in-memory arrays when a GPU is not available. AgpuArrayloaded without a GPU is limited and you cannot use it for computations. To use agpuArrayloaded without a GPU, retrieve the contents usinggather.

For more information about how to save and load variables in the MATLAB workspace, seeSave and Load Workspace Variables.

See Also

相关话题