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

Matlab应用之GPU加速

[复制链接]

该用户从未签到

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

EDA365欢迎您登录!

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

x

& X9 x1 @! B! _. a- \由于GPU近几年地迅速发展,GPU在多线程计算等方面逐渐超越CPU成为计算的主力军。而Matlab是常用的数学应用软件,现在讲解一下如何在Matlab中使用GPU加速计算
" u9 T; K0 {  M7 V) g# Q
; P/ z7 v  s$ [/ w  i% E- q8 t9 v文章目录
. ]2 v% |; k+ ?) Y$ @8 F$ }       0. 必要条件% t% u* L8 c1 M6 L( U9 t3 A
       1.给GPU传输数据9 d/ h4 w1 i; l
              1.1 CPU的数据复制到GPU
7 x/ a$ O0 w2 H& [/ i              1.2 直接在GPU上设置数据:% @, o3 m! p4 m" I( l8 g  R( C9 {
       2.数据在GPU上运算7 U0 j0 R1 {2 Q
       3.GPU数据回传
8 R) z* C; L% {) N0 O       4.使用技巧
. k  O; l, l  U2 z              4.1 如果没有并行计算不建议使用GPU
% i1 [  h$ q/ C# A7 @7 U& q1 Z              4.2 如果没有Nvidia显卡或者显卡驱动2 Z$ N$ l/ u8 |& K, N0 I* U
              4.3 双精度尽量转换为单精度( l8 T& O4 J$ k) s4 H) Y6 {4 I6 N
       附录0 e) `% x# Q& N
7 P' [/ ~: Q3 i- _9 ^" `
/ b' @: [) ?8 m9 l$ D/ g

! M1 ]. w. P, {) Y; R1 q9 T0. 必要条件. k, b2 p9 k) ~/ A: ?$ x# w7 k

/ B+ e, F: w. l& ~$ U( d要想在Matlab中使用GPU加速有两个必须的条件7 c' g8 \  P6 P: e. V4 f

) V$ i0 C' ]( O6 t0 L7 M# t! c
  • 计算机上安装了NVIDIA显卡,目前AMD与Intel显示是暂不支持的;
  • 安装NVIDIA显卡驱动。
    ; @2 h8 H3 p. N; y* X' h( v7 c

+ r- S( l. @0 W8 ?+ Z
( Y! c5 B7 ?: p  Q$ }6 u1.给GPU传输数据* K% x$ ~' n9 P1 u1 R- I7 d1 U
6 o4 r" d1 h1 B- g/ Y8 ]
1.1 CPU的数据复制到GPU
9 y* h6 F+ n- n! t+ s
: Y% h" o8 O4 B在使用GPU计算的时候,只需要将CPU的数据复制到GPU中即可。, e4 ]2 w* E1 H( U2 A, l/ X7 Z

9 p) C4 w0 Q  ^* g- n
  • G = gpuArray(M);
    , v/ v4 ^  x+ ^2 Q! s) ]/ \
4 }1 K4 f% C; g8 x' Q/ M( d' ~
* Y+ x( ]6 G% m  ^. r4 O3 Z
上边是对数据的名称做了修改,也可以直接进行重新赋值。! E9 K- k, {% j, h- Z2 b( }
: o3 @/ q  q  T
  • M = gpuArray(M);
    ) T+ i, {. G% [3 N( m

# x6 [: A9 A) ~/ l- I. }6 ~+ p; e6 W/ a, I" O8 D
1.2 直接在GPU上设置数据:
, p/ W( u% r/ r6 a1 @
: \# ]& B, t; q' p/ y
  • A = zeros(10, 'gpuArray');
    / a& i% J5 h. ^6 P8 a8 p

2 K, T& B- @; ]6 k, d, K
% Z7 q. E. l' u, A- Z& R可以对0矩阵以及1矩阵直接进行复制,但是在程序后边需要标注使用gpuArray。
) o  |; {. O: d  {: ?: B: k% f: D3 a" }5 i, ^7 E' e) U
  • r = gpuArray.rand(1, 100) % 一行,一百列$ K  M2 _6 }" j5 |- ^2 M9 a
4 D7 f0 u( _- }; F9 ~# f/ ^3 v
6 f4 M- H  n: \+ o2 d4 ~  g$ h$ {
随机矩阵的产生。2 F* P- X: p3 P" I+ V
! Q0 ?0 \) }  V/ W7 X
2.数据在GPU上运算
' x- x4 @  }, d; {" @. x( X3 N0 E7 K0 O' g' Z2 _4 l
在GPU可以正常运行基本的运算,与正常矩阵计算方法相同6 Z4 T: d7 V  Y. x  L
3 u, o  V6 I3 o; ~7 O/ ~
  • A=abs(A);$ n2 F2 b. r/ Z# ^

9 D: a0 O' b$ @9 S" U; D
& {8 k' O) D4 K3 }& ]( l+ ?$ Q具体的可以运行的运算可以使用命令
( Z( T0 X+ w- E5 O2 a0 B+ A* s) m) G
  • methods(gpuArray)
    1 ^: i& p+ S) N6 h7 z3 s6 S$ C

6 ]. @. q/ A( L9 [
. ~! p" x. B& {- {  h进行查看,Matlab可以在GPU运行的具体运算可以查看附录,附录中是Matlab给出的结果。$ Q: v/ m. l! M+ l+ L1 u, U

% ]+ l$ ]" d; }% }3.GPU数据回传
" }' i0 I! b0 v$ E# ^! K% O2 x( s# c( c: H, o& l
  • B = gather (A);3 ^1 U. Y8 I  Z) O) @* G
# r* ]+ }1 H2 I# ]
! _% Y+ d: x( H
直接使用上边的命令就能够将GPU中的数据回传给CPU。7 d. T6 L2 B" a* D! D* A

& v7 e" J7 b* S1 O( B4.使用技巧7 X! Z" {5 j, _. u; c$ ^0 p! P: @4 [

! [7 [0 M! W; [4.1 如果没有并行计算不建议使用GPU- N2 |6 G& |6 A9 _
4 ~& V" X* O: I3 h
  • index = 0;
  • index = gpuArray(index);
  • for i = 1 : 10000
  •     tic
  •     for j = 1 : 100000
  •         index = index + 1;
  •     end
  •     toc
  • end
  • disp(index)0 `* c3 D8 f4 p

8 o" {4 i% L4 ?4 v0 R6 c* J0 g9 @% L. Y! y/ G7 Q
通过上边程序第二行程序就可以在GPU上运行,注释掉就会在CPU上运行。在我的电脑上运行时间如下表所示。! V4 v" z. q) w0 D3 k, |

5 z& g% q+ \: F; i: u) m2 ?设备                 CPU                      GPU4 g' J7 R, n( b9 |
时间              0.00010 s            1.973017 s9 M* t3 u, l$ C+ J, c0 s

+ U: M1 y( X  S3 r由此可以看出,单个线程运行程序还是建议在CPU上运行,CPU的主频还是高一点,GPU主要是支持多个线程同时运行。
) _0 i) t8 u# P6 [, M4 s; S$ _3 |6 t9 c2 K
4.2 如果没有Nvidia显卡或者显卡驱动
) Q: j9 P! k6 ~3 ^! \2 o" q4 |3 @4 x7 {
如果没有Nvidia显卡或者显卡驱动,会显示下边的提示。, X/ Y0 K( L6 R
7 n5 ^9 t; J' [& p  G7 u4 c
. }+ k6 |/ V7 |6 W& Y. g
% t4 r/ T' q8 s9 h8 `( ?
4.3 双精度尽量转换为单精度
8 l; Q4 l+ `! o9 L- @) p% ~' Q  g& D. ^. c4 N  ?
在条件允许的情况下,尽量将计算过程中双精度转换为单精度。因为在GPU中单精度的计算速度明显优于双精度,在时间上会有很大的缩减。
# G8 W1 }" W* b% w) N) H; e附:单精度与上精度区别0 N" y) @+ D9 n; k0 g% v6 V1 v

$ r' ^2 R$ F- \. m数据类型                    大小(字节)                     取值范围                  保留有效位数
1 Z: j$ N; t9 n% B5 f$ z单精度                   4个字节(32位)         3.4E-38~3.4E+38               7位. J. E9 x% D# z/ j
双精度                   8个字节(64位)         1.7E-308~1.7E+308           16位4 E3 y# j' V; F/ F
. w/ c$ I2 l. W5 _7 h
. U- u$ V) }0 J1 p2 p! u
附录
# N& ^- Y# V/ o5 w) v& A1 x) U% Q6 X% J; m* B. J! h
  • >> 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
    & J! |. g* P  O7 y6 z6 @

: ~3 }- E3 z0 R5 d( |! n  f, v3 B" x, m7 y0 ?

; M  c4 i! {1 v

该用户从未签到

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-11-23 21:45 , Processed in 0.187500 second(s), 27 queries , Gzip On.

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

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

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