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

Matlab应用之GPU加速

[复制链接]

该用户从未签到

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

EDA365欢迎您登录!

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

x
: b4 K8 V1 F3 R& D
由于GPU近几年地迅速发展,GPU在多线程计算等方面逐渐超越CPU成为计算的主力军。而Matlab是常用的数学应用软件,现在讲解一下如何在Matlab中使用GPU加速计算9 v0 S' W) h; n

# R3 _, R. u5 c8 k文章目录# z2 ]- Q0 t7 N, s
       0. 必要条件. |/ x) G* @8 f# V: y
       1.给GPU传输数据& B% K8 i) Q0 J+ Q7 D0 v! l
              1.1 CPU的数据复制到GPU
- j1 K# W' p% ?1 |- Z              1.2 直接在GPU上设置数据:* S) r/ u. G$ a8 H
       2.数据在GPU上运算. I5 I2 g* ]5 W2 u9 B" i
       3.GPU数据回传* L, T% B& @0 a5 {
       4.使用技巧
: F* J" m, z; A6 e% o9 {              4.1 如果没有并行计算不建议使用GPU; {$ p  |! D% C; @+ p
              4.2 如果没有Nvidia显卡或者显卡驱动
* f5 R. @3 ?' v) Z6 f              4.3 双精度尽量转换为单精度- Y6 S; X: X) E0 M) _" i4 e
       附录
4 D8 S- i7 C8 l/ X- m  k* q# d1 y8 T# P

* e- ^  J0 p9 S6 V0 ?  y& p
' l( U" l' Q8 m6 R8 f2 d  T0. 必要条件
! p; y7 Z% X. Z  P4 X' o$ q" [. I# G% d/ \7 c: ]' g# |# B0 ^
要想在Matlab中使用GPU加速有两个必须的条件
6 y& K0 f9 m. h/ U
7 j+ Q. D! p3 f, X' a5 O
  • 计算机上安装了NVIDIA显卡,目前AMD与Intel显示是暂不支持的;
  • 安装NVIDIA显卡驱动。2 Y6 E0 t, i3 ]

. o; z3 |9 e5 g: ?; \( F1 V( I3 E" l3 }6 P( x* T& y
1.给GPU传输数据: i0 E2 O/ ^  T/ |7 Z
- W7 \" v1 y  l5 b& }* H( N7 E' b' P' {
1.1 CPU的数据复制到GPU
2 p6 \4 M8 j, V4 L' T5 d
% G( L) B$ p; b# [. t4 o5 W在使用GPU计算的时候,只需要将CPU的数据复制到GPU中即可。
0 R! q8 m, r2 A+ [: ?) m+ V
$ z: D6 ]) X# a" }
  • G = gpuArray(M);! \+ T4 f3 c1 T: G
3 `( A. k2 r7 w# [9 c. ]
- i3 f8 }0 _- L  x+ Y7 \8 q. F
上边是对数据的名称做了修改,也可以直接进行重新赋值。" A+ R! Q9 g5 ~! n! H( K; R
/ T" ^- f' v9 T# J- j
  • M = gpuArray(M);* n0 u- J8 e$ t1 H1 J

  u% h- p6 Z5 X2 ]2 N; `  U$ `7 C0 G0 X% k% @$ s9 o
1.2 直接在GPU上设置数据:
4 c% `' z" F  N/ u# v& \5 h" I" \) H0 g0 X: y( _: T% l9 P7 I  ~
  • A = zeros(10, 'gpuArray');0 }7 [. E3 n* |0 T
* d4 `. h5 V/ ~( ~$ W" J
! p6 v2 c, `+ S
可以对0矩阵以及1矩阵直接进行复制,但是在程序后边需要标注使用gpuArray。
8 w* e- u' R& U$ Y# U3 k# t" @9 y" C
  • r = gpuArray.rand(1, 100) % 一行,一百列# l2 D( a" s# s- @& K: j2 \5 h1 ^
( v3 o# V. o( C0 `8 |

3 ~$ X7 X) W7 G+ Z* m随机矩阵的产生。& [3 V+ G5 [$ W) F

7 @! {6 f/ s, A6 l2.数据在GPU上运算
7 J3 b, V8 k5 K" X1 ^; X7 n* R1 w% I) T; q
在GPU可以正常运行基本的运算,与正常矩阵计算方法相同
' H" _4 J) Z4 T" T
) t8 m2 k1 U( H4 p/ C
  • A=abs(A);0 X$ r" k2 z, d$ g
" e" J1 Z. e) E* t0 w, i$ J8 Z- o
- K+ k( U( [* Y# V$ ~
具体的可以运行的运算可以使用命令) y! F7 }4 j: N1 v; R2 n3 a4 F% d1 `

! w. k% R0 a; X
  • methods(gpuArray)* W: m2 ]$ t' a% w/ `
: c. y9 L: ?5 D' p- H

+ ?% R0 q" g# f4 B6 a4 @进行查看,Matlab可以在GPU运行的具体运算可以查看附录,附录中是Matlab给出的结果。
  Y/ i8 h5 h( f7 S" a( I7 F* A# C2 @3 A9 b& x! C1 Q8 n1 D
3.GPU数据回传8 C* \. v$ ~2 N% A4 y
& g7 u, s0 w% V5 F* w) q
  • B = gather (A);
    4 ?' D0 P* r3 ?! T$ [
( h3 O4 v8 O& R

2 [' X1 B3 f$ s" k* a+ p" d直接使用上边的命令就能够将GPU中的数据回传给CPU。
* T% g$ t- u7 }+ H' z/ H
5 S: r0 k+ @3 P/ j2 A6 y2 T7 u9 W4.使用技巧# o# z- b! q) h! [! {9 T" a4 b6 K1 ]

3 z1 ?! G% e' C, A/ O5 ?4.1 如果没有并行计算不建议使用GPU+ l2 A' ^3 \/ B" h4 L( a: v0 @
* p9 C) G, K6 D( w& _; G7 W$ s
  • index = 0;
  • index = gpuArray(index);
  • for i = 1 : 10000
  •     tic
  •     for j = 1 : 100000
  •         index = index + 1;
  •     end
  •     toc
  • end
  • disp(index)
    : I1 N5 T  E9 r) @8 O: K1 Q, |

+ w; Y9 `3 p6 C
: B4 i: C' z1 D& {- z+ c9 M7 l通过上边程序第二行程序就可以在GPU上运行,注释掉就会在CPU上运行。在我的电脑上运行时间如下表所示。, ^* S- a9 u3 z
, v3 Y4 {6 t9 i- }  z
设备                 CPU                      GPU5 ?: M- v7 b0 A0 P. d- m
时间              0.00010 s            1.973017 s! }  t0 n: w3 }7 l. ?8 _
$ ]1 ~5 b% m1 d1 Q' m
由此可以看出,单个线程运行程序还是建议在CPU上运行,CPU的主频还是高一点,GPU主要是支持多个线程同时运行。- D* K5 [+ K3 U  p
) s! \7 D4 n1 C1 Q
4.2 如果没有Nvidia显卡或者显卡驱动
4 n( Y( l3 ]  h$ l, j, j
, |# M& n0 F& U+ O1 c! X如果没有Nvidia显卡或者显卡驱动,会显示下边的提示。
7 c+ c8 S0 P  n5 U/ f) t; S0 d  P- a# c# |" A

* s7 B: e* @* r
" j/ {" R+ _8 r4.3 双精度尽量转换为单精度3 [( X3 o4 O' u
3 L2 S7 w. ~! W, w$ T- h- ~
在条件允许的情况下,尽量将计算过程中双精度转换为单精度。因为在GPU中单精度的计算速度明显优于双精度,在时间上会有很大的缩减。& g0 J% Z, Y/ O3 \
附:单精度与上精度区别
! X8 g1 `$ M/ o: q5 q) s4 @1 F* m5 s& Z9 d2 n
数据类型                    大小(字节)                     取值范围                  保留有效位数
0 M+ K; G& ^2 @( n7 F8 T单精度                   4个字节(32位)         3.4E-38~3.4E+38               7位
7 j: T1 {8 \' J6 y! C双精度                   8个字节(64位)         1.7E-308~1.7E+308           16位
) D) ?7 a& |5 A  c' Q
7 u4 F' m0 ^5 i" i/ O/ j7 M; R
/ Q' c$ _$ T5 f- a9 g7 b附录1 j3 H- f4 q! r

* d( y+ `0 A3 G. S
  • >> 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# n7 q0 X  B) [8 j) H1 Y
8 X* I! c: V1 y  J) s3 V6 j

' F! \3 L& C) P% Y. L: F( M3 S  O7 r

该用户从未签到

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-1 02:38 , Processed in 0.171875 second(s), 26 queries , Gzip On.

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

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

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