main content

array stored on gpu -凯发k8网页登录

array stored on gpu

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 any gpuarray-enabled matlab function such as fft, mtimes or mldivide. to find a full list of gpuarray-enabled functions in matlab and in other toolboxes, see . 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 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 .

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 .

description

example

g = gpuarray(x) copies the array x to the gpu and returns a gpuarray object.

input arguments

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.

example: g = gpuarray(magic(3));

data types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical
complex number support: yes

object functions

apply function to each element of array on gpu
transfer distributed array, composite array or gpuarray to local workspace
apply function to each page of distributed or gpu array

there are several methods for examining the characteristics of a gpuarray object. most behave like the matlab functions of the same name.

determine whether input is gpuarray
determine if gpuarray or cudakernel is available on gpu
determine whether input has specified underlying data type
number of array dimensions
array size
type of underlying data determining array behavior

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.

toolbox namelist of functions with gpuarray supportgpu-specific documentation
matlab 
statistics and machine learning toolbox™functions with gpuarray support (statistics and machine learning toolbox)analyze and model data on gpu (statistics and machine learning toolbox)
image processing toolbox™ (image processing toolbox) (image processing toolbox)
deep learning toolbox™

(deep learning toolbox)

*(see also deep learning with gpus)

(deep learning toolbox)

(deep learning toolbox)

computer vision toolbox™ (computer vision toolbox) (computer vision toolbox)
communications toolbox™ (communications toolbox) (communications toolbox)
signal processing toolbox™ (signal processing toolbox)code generation and gpu support (signal processing toolbox)
audio toolbox™ (audio toolbox)code generation and gpu support (audio toolbox)
wavelet toolbox™ (wavelet toolbox)code generation and gpu support (wavelet toolbox)
curve fitting toolbox™ (curve fitting toolbox) 

for a list of functions with gpuarray support in all mathworks® products, see . 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 the gpu arrays filter is not available.

examples

to transfer data from the cpu to the gpu, use the gpuarray function.

create an array x.

x = [1,2,3];

transfer x to the gpu.

g = gpuarray(x);

check that the data is on the gpu.

isgpuarray(g)
ans = logical
   1

calculate the element-wise square of the array g.

gsq = g.^2;

transfer the result gsq back to the cpu.

xsq = gather(gsq)
xsq = 1×3
     1     4     9

check that the data is not on the gpu.

isgpuarray(xsq)
ans = logical
   0

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.

g = rand(1,3,"gpuarray")
g =
    0.3640    0.5421    0.6543

check that the output is stored on the gpu.

isgpuarray(g)
ans = logical
   1

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.

gpudevice
ans = 
  cudadevice with properties:
                      name: 'quadro p620'
                     index: 2
         computecapability: '6.1'
            supportsdouble: 1
     graphicsdriverversion: '511.79'
               drivermodel: 'wddm'
            toolkitversion: 11.2000
        maxthreadsperblock: 1024
          maxshmemperblock: 49152 (49.15 kb)
        maxthreadblocksize: [1024 1024 64]
               maxgridsize: [2.1475e 09 65535 65535]
                 simdwidth: 32
               totalmemory: 2147287040 (2.15 gb)
           availablememory: 1615209678 (1.62 gb)
               cachepolicy: 'balanced'
       multiprocessorcount: 4
              clockratekhz: 1354000
               computemode: 'default'
      gpuoverlapstransfers: 1
    kernelexecutiontimeout: 1
          canmaphostmemory: 1
           devicesupported: 1
           deviceavailable: 1
            deviceselected: 1

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.

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 .

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 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*1/n*f*ones(n,1,"gpuarray")
result =
    3.1403

tips

  • if you need better performance, or if a function is not available on the gpu, gpuarray supports the following options:

    • to precompile and run purely element-wise code on gpuarray objects, use the function.

    • to run c code containing cuda® device code or library calls, use a mex function. for more information, see .

    • to run existing gpu kernels written in cuda c , use the matlab cudakernel interface. for more information, see .

    • to generate cuda code from matlab code, use gpu coder™. for more information, see get started with gpu coder (gpu coder).

  • you can control the random number stream on the gpu using .

  • none of the following can exceed intmax("int32"):

    • the number of elements of a dense array.

    • the number of nonzero elements of a sparse array.

    • the size in any given dimension. for example, zeros(0,3e9,"gpuarray") is not allowed.

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.

(___,"gpuarray")(___,"gpuarray")
(___,"gpuarray")(___,"gpuarray")
(___,"gpuarray")gpuarray.
(___,"gpuarray")gpuarray.
(___,"gpuarray")gpuarray.
(___,"gpuarray")gpuarray.
(___,"gpuarray")gpuarray.
(___,"gpuarray") 

extended capabilities

version history

introduced in r2010b

see also

| | | | | | | |

网站地图