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

Matlab应用之GPU加速

[复制链接]

该用户从未签到

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

EDA365欢迎您登录!

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

x

* I) N* h. b1 u. ~! D$ q由于GPU近几年地迅速发展,GPU在多线程计算等方面逐渐超越CPU成为计算的主力军。而Matlab是常用的数学应用软件,现在讲解一下如何在Matlab中使用GPU加速计算
& ?$ J7 P9 o& e5 n+ k; d
* x) I4 S; D- K" u+ l1 a文章目录' v2 y. r* Y# W, ?2 N( ], ~( z( Q& `* T) l% q
       0. 必要条件
  j* m# e. \! C3 V       1.给GPU传输数据
6 u4 d9 K# o! x, f7 e              1.1 CPU的数据复制到GPU
* ~; I6 E* N& n/ f. R2 o; ]              1.2 直接在GPU上设置数据:4 R3 }% W0 ^  ?1 w
       2.数据在GPU上运算
; S/ A; l  U# }& O# b       3.GPU数据回传  A! P" ^3 c3 u
       4.使用技巧
; a& d: |- w- F; J+ l              4.1 如果没有并行计算不建议使用GPU
/ h/ X& I: M4 o* I5 ~( R, `              4.2 如果没有Nvidia显卡或者显卡驱动
* A+ `9 r, v' i3 b0 f9 i              4.3 双精度尽量转换为单精度. F" z$ |( v- Z3 M3 a" N+ B! g
       附录6 C+ K4 G; z- G7 c5 g& k
2 {# L( K$ t2 O2 V

% s* {6 z. P& `; H% H1 a* |+ ]; A' K1 T/ |; E' \
0. 必要条件1 s: u1 E* w3 I- j7 p" A
- G' g% E/ i& N6 F
要想在Matlab中使用GPU加速有两个必须的条件9 x  I7 |5 o% I% O

# c0 c# f2 D( t, M0 V& q
  • 计算机上安装了NVIDIA显卡,目前AMD与Intel显示是暂不支持的;
  • 安装NVIDIA显卡驱动。/ k6 ^! S% C# B7 L. z5 `% p
* m0 S& [6 _* V) S1 R5 E
. j1 ?$ t$ G. |* k' j8 d
1.给GPU传输数据& t, G% y  v" \  G3 `  j: f
) `  N  D9 N( t* l* h
1.1 CPU的数据复制到GPU# X5 B8 k/ l% X( ^

; c8 M0 x8 X6 T8 Q: n$ |1 c6 {; k在使用GPU计算的时候,只需要将CPU的数据复制到GPU中即可。0 q& w7 }6 S0 e6 ^7 u
+ }# f8 c, n* ]/ d
  • G = gpuArray(M);- J5 `. C2 u0 Y0 h. a& M2 H4 p  @% j

( o; J6 m/ i) `7 F3 i9 `+ O9 n4 m/ W( d- T
上边是对数据的名称做了修改,也可以直接进行重新赋值。
/ v; M$ F: T" h$ H* @
+ v9 }/ n8 P! G
  • M = gpuArray(M);
    + [3 v- J  L& h" c% L0 I- ~

& G+ ~% L. g! v5 m& ~4 m) D- N% X5 @- `- d- N, a; d: u
1.2 直接在GPU上设置数据:
' k  A& _  H! K* U+ e% W* M6 p8 i$ ?* g0 s  a1 m" o) S
  • A = zeros(10, 'gpuArray');
    7 O& F9 V+ M' d1 k; d7 a; Y

  r" x1 F0 W' g/ f) ?* S8 n
, P2 l" P- ]3 ]/ t- R  Y可以对0矩阵以及1矩阵直接进行复制,但是在程序后边需要标注使用gpuArray。
. G. T) r6 k$ F4 S) j3 R2 U
9 z% Z7 a4 S; L$ K7 ?9 B" B" d# ?
  • r = gpuArray.rand(1, 100) % 一行,一百列
    : \( H. n5 J( ^

) z2 K2 @( C/ r/ _5 a* y- j- D& a* J+ t2 R* ~" T7 b
随机矩阵的产生。
/ Z: s& i, v0 Q  f+ g/ B& E
( P& G5 k; d5 W9 g( B) ~2.数据在GPU上运算0 g' B$ D1 [; y/ U

  n; B* B9 g8 |在GPU可以正常运行基本的运算,与正常矩阵计算方法相同/ K; T7 v6 n6 y9 R7 u7 l) z

" m; {0 S4 q+ n9 @1 v  c
  • A=abs(A);
    ; w$ M5 ~7 h, T3 N

2 c! X5 V& ^* a4 G: r
8 l9 X2 b) P) J, X( K具体的可以运行的运算可以使用命令+ j( t4 e1 i! O3 a$ f. D  y* A

4 v: P2 L" n8 ~
  • methods(gpuArray)" R* A9 ?! L# i$ I& n1 _5 \* N) R
) Z7 f- ]4 N% C* l
- u) g# Z; q" o, J& r1 u! f
进行查看,Matlab可以在GPU运行的具体运算可以查看附录,附录中是Matlab给出的结果。
" `8 T# j( M, F' `% C3 L6 p% s0 j( z& z2 a: b: S, U
3.GPU数据回传) Q' `! `: ~6 d8 \8 y3 I' f

, n- G! `% l. c7 s" n- F: I) s
  • B = gather (A);
    8 F  r2 A6 J. I) w

& j% ^& Z2 g! r% A3 T$ V* Q
7 E2 g  L; K" r) J直接使用上边的命令就能够将GPU中的数据回传给CPU。. N2 i! A& @5 B0 c2 b
6 q6 C. D$ c, A0 x
4.使用技巧# A: u% A0 G  s" N5 c5 i
& i! n0 v4 {0 p) `
4.1 如果没有并行计算不建议使用GPU
; c# r) m9 Z  [) ]4 j$ ?, q. G* i3 U6 Q
  • index = 0;
  • index = gpuArray(index);
  • for i = 1 : 10000
  •     tic
  •     for j = 1 : 100000
  •         index = index + 1;
  •     end
  •     toc
  • end
  • disp(index)
    2 C, J  f4 m/ ]6 A1 [7 c
- Y* H& c0 H3 {6 s
% m$ I5 Y; v8 [2 t8 v) B
通过上边程序第二行程序就可以在GPU上运行,注释掉就会在CPU上运行。在我的电脑上运行时间如下表所示。1 R. H- S0 `& |% E/ f9 x) N6 X

6 \0 N6 X- A) {8 g8 m2 P2 A设备                 CPU                      GPU; J% P8 M% j0 X& x# v3 O& z
时间              0.00010 s            1.973017 s
3 l) A7 M5 i! E1 G* F. B7 ?. b; i2 Y: R2 W( k3 ~
由此可以看出,单个线程运行程序还是建议在CPU上运行,CPU的主频还是高一点,GPU主要是支持多个线程同时运行。
% F0 A5 S% x" R, \5 U6 d' Y* W; a3 B! M# B6 `
4.2 如果没有Nvidia显卡或者显卡驱动
% x$ {& {+ k5 S7 |6 g$ ]$ ~; m( _% U$ J; t5 Q3 z7 a
如果没有Nvidia显卡或者显卡驱动,会显示下边的提示。& F5 R8 j' a8 e

; ^* j  j0 v# e0 J/ b ' f) o; g0 Y6 V: M; L* [

) d  _# e3 t1 a- \- T& Y: r* Q/ z4.3 双精度尽量转换为单精度& Y2 o( Q5 O$ A/ k( J

8 s7 v, L9 {8 s在条件允许的情况下,尽量将计算过程中双精度转换为单精度。因为在GPU中单精度的计算速度明显优于双精度,在时间上会有很大的缩减。
" P- s1 B8 }0 Y2 w1 o3 l附:单精度与上精度区别
& _' V! v$ l3 q+ M9 I
+ C0 K2 c1 I) ]. O  T+ n8 y! i$ g数据类型                    大小(字节)                     取值范围                  保留有效位数* i+ h  `. r1 _" N2 n( \
单精度                   4个字节(32位)         3.4E-38~3.4E+38               7位7 V5 |! g$ q7 u+ v( H# S1 t: q
双精度                   8个字节(64位)         1.7E-308~1.7E+308           16位: \# L0 ?4 X# b, y
& B: z. b6 p5 |, P# N
! m4 Z/ v4 Z0 U# ^) v2 a% `
附录
  i: Y+ [7 A0 t* X, _' j, R& x, d: ]  [
  • >> 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
      f, L6 j, ^- q( t" z- B# \

' B9 r; K& Q8 e$ N% {! d" D1 U: f( e, W0 g- @

2 h5 R( |* t6 b! f0 c  U" Q! W

该用户从未签到

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-10-6 00:52 , Processed in 0.140625 second(s), 27 queries , Gzip On.

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

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

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