找回密码
 注册
关于网站域名变更的通知
查看: 718|回复: 2
打印 上一主题 下一主题

Matlab应用之GPU加速

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2020-1-20 09:44 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式

EDA365欢迎您登录!

您需要 登录 才可以下载或查看,没有帐号?注册

x

: _* U, B; X: F8 ^: o由于GPU近几年地迅速发展,GPU在多线程计算等方面逐渐超越CPU成为计算的主力军。而Matlab是常用的数学应用软件,现在讲解一下如何在Matlab中使用GPU加速计算6 V+ ?5 y3 k0 {2 g/ N1 f
" ~1 p2 X' l6 w( Y8 p7 S
文章目录
) z" C2 |1 b- m) g       0. 必要条件$ P" }& T3 U9 A" W
       1.给GPU传输数据0 M3 t! j% D6 @9 g2 `
              1.1 CPU的数据复制到GPU# O- U- f3 K! ^" r# W3 a9 h
              1.2 直接在GPU上设置数据:
* E, }! y3 w! G* y- `# c- g       2.数据在GPU上运算6 s# ~7 M0 @# r6 X' A0 V
       3.GPU数据回传' q; `& e$ ]3 H. [7 R8 F+ M
       4.使用技巧
" k2 \5 [' h, m2 W' B7 |( o% P, ~              4.1 如果没有并行计算不建议使用GPU
% L* O& T" Q6 p              4.2 如果没有Nvidia显卡或者显卡驱动. h; x- \8 v! ^" I$ m9 N% J
              4.3 双精度尽量转换为单精度8 L: L5 e5 b4 w6 k! w  h
       附录8 z' @3 M8 G( D/ D9 K
/ S- ^0 N: f: z1 C2 e
) q8 z9 l7 v' v
! m3 i7 B4 n. d( u7 X  g6 ?$ }
0. 必要条件
+ H& k  d5 j1 ]9 i8 I1 f4 d( `- {& ^/ h0 ~6 X0 w# h
要想在Matlab中使用GPU加速有两个必须的条件
6 {7 s1 g5 C' _5 D, \
+ Q3 |/ P" I  q7 R2 X! ]" D1 F: g
  • 计算机上安装了NVIDIA显卡,目前AMD与Intel显示是暂不支持的;
  • 安装NVIDIA显卡驱动。4 W9 D" n1 k' A: g0 V

/ x/ z/ H: K% V/ L& K9 I
& [- `6 N+ ?" O6 O: ?+ C1.给GPU传输数据
3 C$ t7 @2 ]) S9 \+ e$ X  l( B  \% w# L, C6 n/ [
1.1 CPU的数据复制到GPU
5 A3 m* f8 ]; o- N
" Z: Y$ V1 n* p3 [# H在使用GPU计算的时候,只需要将CPU的数据复制到GPU中即可。, w9 |0 S* ], A0 N# N

4 z. d' k  K0 Z- Y. V
  • G = gpuArray(M);" l4 y2 K5 ?3 a" K
, q! c. b. ~+ K* Y8 f- @) [
- h0 T' E' a/ u3 v' C& n
上边是对数据的名称做了修改,也可以直接进行重新赋值。
# l. o1 l& D7 t3 D6 C: H+ X+ m4 k9 \
  • M = gpuArray(M);" U, L) z* h/ J, m
( v# p, I. V- y6 M

, E1 P. ~) V% \0 M1.2 直接在GPU上设置数据:
6 `1 `( w  G% y3 |4 k4 D0 h1 H  S* l" I& p9 E$ X3 W5 f3 j% _
  • A = zeros(10, 'gpuArray');
    3 u% X- ^5 b+ @9 I; x% U4 M
* F2 e: \' `1 h' W

0 C- I4 D, R- z可以对0矩阵以及1矩阵直接进行复制,但是在程序后边需要标注使用gpuArray。/ d: k0 S8 ?7 q& X

, H, ~: K6 @: c2 {! c
  • r = gpuArray.rand(1, 100) % 一行,一百列" `' p2 l& K8 M$ j! L
9 Z3 R6 V( g: L7 {6 I1 S5 G

0 Q# J; K7 R& ~0 v1 U/ |随机矩阵的产生。9 K! U+ j2 r4 g  b6 F

( h7 C1 E1 A$ B. F- P: p# E! [2.数据在GPU上运算
0 H8 m2 C5 `0 e# I# B( D; R# V. X
在GPU可以正常运行基本的运算,与正常矩阵计算方法相同; G" _( A, X! h7 ]& x- w& ?1 w

( j" t, J, L3 G# J5 k
  • A=abs(A);
    # Y+ e% y4 V. |
7 f. y7 t" I& |; T

% E# {1 e0 G9 q! g/ L# v具体的可以运行的运算可以使用命令! |) O, G* [5 ?! v. z" O
  S1 M0 j5 s7 e" c# Y' r, ~# G7 m$ D
  • methods(gpuArray)& h6 G9 A+ w: f# X

, y! m/ y& `: a+ b* i& s0 |/ z
' X/ v2 x$ l: {) e& P进行查看,Matlab可以在GPU运行的具体运算可以查看附录,附录中是Matlab给出的结果。, ]$ Y5 B! \. t- D0 d
( N% c, y0 r! p& A" U/ G9 D
3.GPU数据回传- W) h# J6 S  y6 t' g
+ h( D/ e' V& n8 Z$ J8 v" j, b
  • B = gather (A);0 D7 B  {6 r8 ?  a6 z( {# h, C9 _2 C8 ~6 Q

5 W# v9 @- p9 X$ E$ h! {+ X" a3 V1 w5 I5 x
直接使用上边的命令就能够将GPU中的数据回传给CPU。
/ R* |0 C3 c# f5 d, f7 W% V( ^: p  a8 k* C4 n$ g% k% I6 V
4.使用技巧
- j! \: s4 c+ ~* y9 N8 H$ R- }) A" i" H# ~3 r
4.1 如果没有并行计算不建议使用GPU: x0 [0 ^$ X& Q( a  _! g' z; L. Z+ G

$ B( [+ f4 s: C* o
  • index = 0;
  • index = gpuArray(index);
  • for i = 1 : 10000
  •     tic
  •     for j = 1 : 100000
  •         index = index + 1;
  •     end
  •     toc
  • end
  • disp(index)8 I2 k; ?, \) P" `! L4 g8 k) ]

3 v' j' N' R0 Q& d, w. m2 y5 z
0 c- }. [  v# Y, J通过上边程序第二行程序就可以在GPU上运行,注释掉就会在CPU上运行。在我的电脑上运行时间如下表所示。
, N* Y* F) y- F, A4 {0 e+ \6 Y
% w  k' R3 t, V: `5 N! {$ M* u设备                 CPU                      GPU
3 X% [8 @* e" _+ J0 T# y# A7 j时间              0.00010 s            1.973017 s
7 p# j# I9 c9 k" ?; Q
" a$ V2 A; t7 ^3 Z/ b8 N7 r6 F由此可以看出,单个线程运行程序还是建议在CPU上运行,CPU的主频还是高一点,GPU主要是支持多个线程同时运行。% U1 o! a0 ^2 V  t$ {

  q3 }4 w  C' s' y4.2 如果没有Nvidia显卡或者显卡驱动
- `" v1 F* }, V* d9 g1 @) k
* s7 v2 W# m( Q) \  G! y/ B如果没有Nvidia显卡或者显卡驱动,会显示下边的提示。
, W0 h! y) I! f) e  z+ ^
* c; y  x* P0 P+ Z" s- J3 B
2 [# `- N0 K4 o1 S' t# Q. j4 @$ s# o4 O* `
4.3 双精度尽量转换为单精度( y4 ]' k* G" I/ Y8 a
* P, K$ z: K4 a+ Y) {; g3 X6 p
在条件允许的情况下,尽量将计算过程中双精度转换为单精度。因为在GPU中单精度的计算速度明显优于双精度,在时间上会有很大的缩减。2 a- I  C$ y2 K* Q& ~! t
附:单精度与上精度区别
9 I3 D# C6 [7 G1 X0 t6 F0 S( ]4 S( z4 g; }; F3 S5 `
数据类型                    大小(字节)                     取值范围                  保留有效位数1 I- F  A% `8 ?2 q9 D9 A4 M
单精度                   4个字节(32位)         3.4E-38~3.4E+38               7位+ ^2 s- R3 ~5 s; _  G7 v4 t' l, `
双精度                   8个字节(64位)         1.7E-308~1.7E+308           16位
' K  N1 o  j2 u) D. d- b9 h+ M! I: R/ V1 @: e$ G
: i% d4 h* w3 l% ~  l8 \% G8 P
附录
# E) e8 Z" x, r7 n$ B0 @( }# `5 ^% Q2 M0 ?6 @1 S9 b8 }
  • >> methods(gpuArray)
  • Methods for class gpuArray:
  • abs                   eq                    ipermute              quiver3
  • accumarray            eRF                   iradon                rad2deg
  • acos                  erfc                  isaUnderlying         radon
  • acosd                 erfcinv               isbanded              rdivide
  • acosh                 erfcx                 isdiag                real
  • acot                  erfinv                isempty               reallog
  • acotd                 errorbar              isequal               realpow
  • acoth                 existsOnGPU           isequaln              realsqrt
  • acsc                  exp                   isequalwithequalnans  reducepatch
  • acscd                 expint                isfinite              reducevolume
  • acsch                 expm                  isfloat               regionprops
  • all                   expm1                 ishermitian           rem
  • and                   eye                   isinf                 repelem
  • angle                 ezcontour             isinteger             repmat
  • any                   ezcontourf            islogical             reshape
  • applylut              ezgraph3              ismember              rgb2gray
  • area                  ezmesh                ismembertol           rgb2hsv
  • arrayfun              ezmeshc               isnan                 rgb2ycbcr
  • asec                  ezplot                isnumeric             ribbon
  • asecd                 ezplot3               isocaps               roots
  • asech                 ezpolar               isocolors             rose
  • asin                  ezsurf                isonormals            rot90
  • asind                 ezsurfc               isosurface            round
  • asinh                 factorial             isreal                scatter
  • assert                false                 issorted              scatter3
  • atan                  feather               issparse              sec
  • atan2                 fft                   issymmetric           secd
  • atan2d                fft2                  istril                sech
  • atand                 fftfilt               istriu                semilogx
  • atanh                 fftn                  kmeans                semilogy
  • bandwidth             fill                  knnsearch             setdiff
  • bar                   fill3                 ldivide               setxor
  • bar3                  filter                le                    shiftdim
  • bar3h                 filter2               legendre              shrinkfaces
  • barh                  find                  length                sign
  • besselj               fix                   line                  sin
  • bessely               flip                  linspace              sind
  • beta                  flipdim               log                   single
  • betainc               fliplr                log10                 sinh
  • betaincinv            flipud                log1p                 size
  • betaln                floor                 log2                  slice
  • bicg                  fplot                 logical               smooth3
  • bicgstab              fprintf               loglog                sort
  • bicgstabl             full                  logspace              sortrows
  • bitand                gamma                 lsqr                  sparse
  • bitcmp                gammainc              lt                    spfun
  • bitget                gammaincinv           lu                    spones
  • bitor                 gammaln               mat2gray              sprand
  • bitset                gather                mat2str               sprandn
  • bitshift              ge                    max                   sprandsym
  • bitxor                gmres                 mean                  sprintf
  • bsxfun                gop                   medfilt2              spy
  • bwdist                gpuArray              mesh                  sqrt
  • bwlabel               gradient              meshc                 stairs
  • bwlookup              gt                    meshgrid              std2
  • bwmorph               head                  meshz                 stdfilt
  • cast                  hist                  min                   stem
  • cat                   histc                 minres                stem3
  • cconv                 histcounts            minus                 stream2
  • cdf2rdf               histeq                mldivide              stream3
  • ceil                  histogram             mod                   streamline
  • cgs                   horzcat               mode                  streamparticles
  • chol                  hsv2rgb               movmean               streamribbon
  • circshift             hypot                 movstd                streamslice
  • clabel                idivide               movsum                streamtube
  • classUnderlying       ifft                  movvar                stretchlim
  • comet                 ifft2                 mpower                sub2ind
  • comet3                ifftn                 mrdivide              subsasgn
  • compass               im2double             mtimes                subsindex
  • complex               im2int16              nan                   subspace
  • cond                  im2single             ndgrid                subsref
  • coneplot              im2uint16             ndims                 subvolume
  • conj                  im2uint8              ne                    sum
  • contour               imabsdiff             nextpow2              superiorfloat
  • contour3              imadjust              nnz                   surf
  • contourc              imag                  nonzeros              surfc
  • contourf              image                 norm                  surfl
  • contourslice          imagesc               normest               svd
  • conv                  imbothat              normxcorr2            svds
  • conv2                 imclose               not                   swapbytes
  • convn                 imcomplement          nthroot               symmlq
  • corr2                 imdilate              null                  tail
  • corrcoef              imerode               num2str               tan
  • cos                   imfill                numel                 tand
  • cosd                  imfilter              nzmax                 tanh
  • cosh                  imgaussfilt           ones                  tfqmr
  • cot                   imgaussfilt3          or                    times
  • cotd                  imgradient            padarray              transpose
  • coth                  imgradientxy          pagefun               trapz
  • cov                   imhist                pareto                tril
  • csc                   imlincomb             patch                 trimesh
  • cscd                  imnoise               pcg                   trisurf
  • csch                  imopen                pcolor                triu
  • ctranspose            imreconstruct         pdist                 true
  • cummax                imregdemons           pdist2                typecast
  • cummin                imregionalmax         permute               uint16
  • cumprod               imregionalmin         pie                   uint32
  • cumsum                imresize              pie3                  uint64
  • curl                  imrotate              planerot              uint8
  • deg2rad               imrotate_old          plot                  uminus
  • del2                  imshow                plot3                 union
  • det                   imtophat              plotmatrix            unique
  • detectFASTFeatures    ind2sub               plotyy                uniquetol
  • detectHarrisFeatures  inf                   plus                  unwrap
  • detrend               inpolygon             polar                 uplus
  • diag                  int16                 poly                  var
  • diff                  int2str               polyder               vertcat
  • discretize            int32                 polyfit               vissuite
  • disp                  int64                 polyval               volumebounds
  • display               int8                  polyvalm              voronoi
  • divergence            interp1               pow2                  waterfall
  • dot                   interp2               power                 xcorr
  • double                interp3               prod                  xor
  • edge                  interpn               psi                   ycbcr2rgb
  • eig                   interpstreamspeed     qmr                   zeros
  • end                   intersect             qr
  • eps                   inv                   quiver
  • Static methods:
  • colon                 rand                  randperm
  • freqspace             randi                 speye
  • loadobj               randn. ]7 L; T! y8 N. e+ n% v

& i/ z, Y4 R/ h0 Y1 \0 f. @1 Z' i5 j. c$ S0 g0 x

3 W6 v  }; E4 X) v- f( O6 u' x  a

该用户从未签到

2#
发表于 2020-1-20 18:00 | 只看该作者
Matlab应用      GPU加速

该用户从未签到

3#
发表于 2020-1-21 17:51 | 只看该作者
Matlab应用之GPU加速
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

推荐内容上一条 /1 下一条

EDA365公众号

关于我们|手机版|EDA365电子论坛网 ( 粤ICP备18020198号-1 )

GMT+8, 2025-8-11 21:09 , Processed in 0.125000 second(s), 26 queries , Gzip On.

深圳市墨知创新科技有限公司

地址:深圳市南山区科技生态园2栋A座805 电话:19926409050

快速回复 返回顶部 返回列表