Main Content

gpuArray

一种rray stored on GPU

Description

一种gpuArray对象表示一个数组存储在GPU内存。一种large number of functions in MATLAB®and in other toolboxes supportgpuArrayobjects, allowing you to run your code on GPUs with minimal changes to the code. To work withgpuArray对象,使用任何gpuArray-enabled MATLAB function such asfft,,,,mtimesormldivide。找到完整的列表gpuArray- 在MATLAB和其他工具箱中启用的功能,请参阅GPU-supported functions。有关更多信息,请参阅在GPU上运行MATLAB功能

如果要从GPU检索数组,例如使用不支持的函数万博1manbetxgpuArrayobjects, use the收集功能。

笔记

You can load MAT files containinggpuArraydata as in-memory arrays when a GPU is not available. AgpuArray无GPU加载的对象是有限的,您不能将其用于计算。使用一个gpuArrayobject loaded without a GPU, retrieve the contents using收集

Creation

采用gpuArray将MATLAB工作区中的数组转换为gpuArrayobject. Some MATLAB functions also allow you to creategpuArray直接对象。有关更多信息,请参阅Establish Arrays on a GPU

Description

例子

G= gpuArray(X复制数组Xto the GPU and returns agpuArrayobject.

输入参数

expand all

一种rray to transfer to the GPU, specified as a numeric or logical array. The GPU device must have sufficient free memory to store the data. IfXis already agpuArrayobject,gpuArrayoutputsXunchanged.

您也可以将稀疏阵列传输到GPU。gpuArraysupports only sparse arrays of double-precision.

Example:g = gpuarray(魔术(3));

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|逻辑
复杂的数字支持:万博1manbetxYes

对象功能

arrayfun 一种pply function to each element of array on GPU
收集 转移distributed array or gpuArray to local workspace
pagefun 将功能应用于分布式或GPU数组的每个页面

有几种检查A的特征的方法gpuArrayobject. Most behave like the MATLAB functions of the same name.

isgpuarray Determine whether input isgpuArray
存在 Determine if gpuArray or CUDAKernel is available on GPU
isUnderlyingType Determine whether input has specified underlying data type
ndims Number of array dimensions
尺寸 一种rray size
underlyingType 确定数组行为的基础数据类型

几个MATLAB工具箱包括带有内置的功能gpuArraysupport. To view lists of all functions in these toolboxes that supportgpuArray对象,使用下表中的链接。带有信息指示器的列表中的功能具有限制或使用指示说明特定于在GPU上运行该功能的情况。您可以在功能参考页面的“扩展功能”部分中检查使用说明和限制。有关个人更新的信息gpuArray-enabled functions, see the release notes.

工具箱名称 List of Functions withgpuArray万博1manbetx GPU-Specific Documentation
MATLAB 职能withgpuArray万博1manbetx
Statistics and Machine Learning Toolbox™ 职能withgpuArray万博1manbetx(统计和机器学习工具箱) 分析和建模GPU(统计和机器学习工具箱)
图像处理工具箱™ 职能withgpuArray万博1manbetx(Image Processing Toolbox) GPU Computing(Image Processing Toolbox)
Deep Learning Toolbox™

职能withgpuArray万博1manbetx(Deep Learning Toolbox)

*(也可以看看Deep Learning with GPUs

Scale Up Deep Learning in Parallel, on GPUs, and in the Cloud(Deep Learning Toolbox)

在多个GPU上使用MATLAB进行深度学习(Deep Learning Toolbox)

计算机视觉工具箱™ 职能withgpuArray万博1manbetx(计算机视觉工具箱) GPU Code Generation and Acceleration(计算机视觉工具箱)
Communications Toolbox™ 职能withgpuArray万博1manbetx(Communications Toolbox) 代码生成和加速支持万博1manbetx(Communications Toolbox)
Signal Processing Toolbox™ 职能withgpuArray万博1manbetx(Signal Processing Toolbox) Code Generation and GPU Support(Signal Processing Toolbox)
一种udio Toolbox™ 职能withgpuArray万博1manbetx(Audio Toolbox) Code Generation and GPU Support(Audio Toolbox)
Wavelet Toolbox™ 职能withgpuArray万博1manbetx(Wavelet Toolbox) Code Generation and GPU Support(Wavelet Toolbox)
曲线拟合工具箱™ 职能withgpuArray万博1manbetx(曲线拟合工具箱)

You can browsegpuArray-万博1manbetx 支持功能from all MathWorks®products at the following link:gpuArray-万博1manbetx 支持功能。一种lternatively, you can filter by product. On theHelp酒吧,单击职能。In the function list, browse the left pane to select a product, for example, MATLAB. At the bottom of the left pane, selectGPU Arrays。如果您选择没有的产品gpuArray- 启用功能,然后GPU Arraysfilter is not available.

例子

全部收缩

要将数据从CPU传输到GPU,请使用gpuArray功能。

Create an arrayX

X= [1,2,3];

转移Xto the GPU.

g = gpuarray(x);

Check that the data is on the GPU.

isgpuarray(G)
ans =逻辑1

计算数组的元素正方形G

gsq =g。^2;

转移the resultGSQback to the CPU.

XSq = gather(GSq)
XSq =1×31 4 9

检查数据是否不在GPU上。

isgpuarray(XSq)
ans =逻辑0

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

直接在GPU上创建一个随机数数组。

g = rand(1,3,“ gpuarray”
G= 0.3640 0.5421 0.6543

Check that the output is stored on the GPU.

isgpuarray(G)
ans =逻辑1

This example shows how to usegpuArray-enabled MATLAB functions to operate withgpuArrayobjects. You can check the properties of your GPU using thegpuDevice功能。

gpuDevice
ans = CUDADevice with properties: Name: 'TITAN RTX' Index: 1 ComputeCapability: '7.5' 万博1manbetxSupportsDouble: 1 DriverVersion: 11.2000 ToolkitVersion: 11 MaxThreadsPerBlock: 1024 MaxShmemPerBlock: 49152 MaxThreadBlockSize: [1024 1024 64] MaxGridSize: [2.1475e+09 6553565535] SIMDWidth: 32 TotalMemory: 2.5770e+10 AvailableMemory: 2.4177e+10 MultiprocessorCount: 72 ClockRateKHz: 1770000 ComputeMode: 'Default' GPUOverlapsTransfers: 1 KernelExecutionTimeout: 1 CanMapHostMemory: 1 Device万博1manbetxSupported: 1 DeviceAvailable: 1 DeviceSelected: 1

创建一个从-15到15重复值的行矢量。将其传输到GPU并创建一个gpuArrayobject, use thegpuArray功能。

x = [-15:15 0 -15:15 0 -15:15];gpux = gpuarray(x);谁是GPUX
Name Size Bytes Class Attributes gpuX 1x95 760 gpuArray

To operate withgpuArray对象,使用任何gpuArray-enabled MATLAB function. MATLAB automatically runs calculations on the GPU. For more information, see在GPU上运行MATLAB功能。例如,使用diag,,,,expm,,,,mod,,,,round,,,,腹肌,,,,andfliplr一起。

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));

如果您需要从GPU转移数据,请使用收集。传输数据to the CPU can be costly, and is generally not necessary unless you need to use your result with functions that do not supportgpuArray

结果= gather(gpuF); whos结果
名称大小字节类属性结果96x96 73728 double

通常,由于GPU和CPU之间的数值精度和算法差异,在CPU和GPU上运行代码可以产生不同的结果。CPU和GPU的答案都是对真实分析结果的浮点近似值,并且在计算过程中遭受了不同的循环行为。在此示例中,结果是整数,round消除了圆形错误。

This example shows how to use MATLAB functions and operators withgpuArrayobjects 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 bothX和y坐标,通过与兰德功能。To create a random array directly on the GPU, use the兰德function and specify "gpuarray”。有关更多信息,请参阅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 π 通过在单位圆中采样点。因为代码使用gpuArray-enabled functions and operators ongpuArrayobjects, 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 aboutgpuArray-enabled functions, see在GPU上运行MATLAB功能

f = x.^2 + y.^2 <= 1; result = 4*1/n*f*ones(n,1,“ gpuarray”
结果= 3.1403

Tips

  • If you need better performance, or if a function is not available on the GPU,gpuArraysupports the following options:

    • 预编译和运行纯粹的元素代码gpuArrayobjects, use thearrayfun功能。

    • To run C++ code containing CUDA®device code or library calls, use a MEX function. For more information, seeRun MEX-Functions Containing CUDA Code

    • 要运行用CUDA C ++编写的现有GPU内核,请使用MATLAB CUDAKERNEL接口。有关更多信息,请参阅Run CUDA or PTX Code on GPU

    • To generate CUDA code from MATLAB code, use GPU Coder™. For more information, seeGet Started with GPU Coder(GPU Coder)

  • You can control the random number stream on the GPU usinggpurng

  • None of the following can exceedintmax(“int32”)

    • The number of elements of a dense array.

    • 稀疏阵列的非零元素的数量。

    • The size in any given dimension. For example,零(0,3e9,"gpuArray")不允许。

备择方案

You can also create agpuArrayobject using some MATLAB functions by specifying agpuArrayoutput. The following table lists the MATLAB functions that enable you to creategpuArray直接对象。有关更多信息,请参见功能参考页面的扩展功能部分。

eye(___,“ gpuarray”) true(___,“ gpuarray”)
错误的(___,“ gpuarray”) (___,“ gpuarray”)
Inf(___,“ gpuarray”) gpuarray。colon
(___,“ gpuarray”) gpuarray。freqspace
ones(___,“ gpuarray”) gpuarray。linspace
兰德(___,“ gpuarray”) gpuarray。logspace
兰德i(___,“ gpuarray”) gpuarray。斯皮尔
兰德n(___,“ gpuarray”)

版本历史记录

在R2010b中引入