|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
3 Q9 I/ r1 f! @# K- }# J( b由于GPU近几年地迅速发展,GPU在多线程计算等方面逐渐超越CPU成为计算的主力军。而Matlab是常用的数学应用软件,现在讲解一下如何在Matlab中使用GPU加速计算/ N: l* ?7 u$ P6 n3 L! L
4 n; x3 X3 |; R# \6 g文章目录
' @) G1 y5 D$ j' O( f 0. 必要条件
+ I+ Q; X3 N& b: V: H 1.给GPU传输数据/ n$ C( U- t& ~( ^! i5 I
1.1 CPU的数据复制到GPU
& d3 n7 e+ l- N( {8 x9 \ 1.2 直接在GPU上设置数据:7 K: i: c$ ` s7 i* U- m0 o, Z
2.数据在GPU上运算4 G1 k0 }% W, A- I9 q9 {8 `
3.GPU数据回传
- U, v7 d6 ^& T$ m! d0 l( T2 p 4.使用技巧
* A7 J2 U- H8 x! z 4.1 如果没有并行计算不建议使用GPU
! S5 q* s( ]% ?) K! C 4.2 如果没有Nvidia显卡或者显卡驱动' O" y( l# e+ I3 y: T3 _3 p
4.3 双精度尽量转换为单精度$ f) ~% k0 _- p3 @
附录. C; E2 q6 V/ @' ^ ~/ \: e5 ]
8 i) ^' `# @/ K' L% z0 A3 \8 R6 f; j6 M, G
% W; U0 h7 j7 C9 i9 D2 u0. 必要条件" X% m8 @. T! H% Q) q- d! m
8 i6 T$ E) \3 S, Q- E, o$ ], M. v要想在Matlab中使用GPU加速有两个必须的条件
# R% c) {( J& P1 f, X1 m7 {! T Y
- N+ ^5 Y8 d1 Y4 c, n' X8 u- 计算机上安装了NVIDIA显卡,目前AMD与Intel显示是暂不支持的;
- 安装NVIDIA显卡驱动。
' c$ r+ X! e7 t0 u& l, ]; P ' C. h- r& D% a8 o; @: d1 f
- f$ v; ?/ {& k2 E/ K& y( {1.给GPU传输数据2 b- J" V6 f w1 ]" @
" X' |1 V; X1 `1.1 CPU的数据复制到GPU
" M6 L8 l2 _2 e0 K/ @
2 J$ ]: Z8 K( n% Q; U! F; L3 h在使用GPU计算的时候,只需要将CPU的数据复制到GPU中即可。
: p$ g2 q( E( E+ t3 ]1 ]9 h: h0 E( b9 n6 o: r
- G = gpuArray(M);
6 D7 H& Q3 P3 ] V \9 C) w, }$ z2 ~
( j: y6 D' j: H# T
上边是对数据的名称做了修改,也可以直接进行重新赋值。% T- x! ^! s" t' x
0 G S1 N8 ~3 t V2 s" g' C$ `
- M = gpuArray(M);
2 N6 E# E2 @/ u+ t
& I/ a8 ^( N7 }, x7 t1 H# V D
$ i% F2 t- ^% G4 n1.2 直接在GPU上设置数据:
. S; f7 Y5 z3 w1 w1 m
5 P# \) E" {$ u8 i( i. }. Z- A = zeros(10, 'gpuArray');, [; {) H+ m+ t+ r5 M
: a1 B3 v X& D; i o& d4 \. `
* }# l+ B w) m
可以对0矩阵以及1矩阵直接进行复制,但是在程序后边需要标注使用gpuArray。
4 O9 e; ?% b" J6 M% @; q$ V$ I
0 g9 `% j3 \5 p% X0 ~- g- r = gpuArray.rand(1, 100) % 一行,一百列
8 w6 Z+ ?4 C, y( B2 h Q0 X8 g ( K6 \' I/ H" u$ _1 A; k8 i
" g4 t; b+ }! q; p* |随机矩阵的产生。' ^2 O7 X' h, ~6 p# _! Q+ ~% U9 m
; w4 z, e* `0 p& C2.数据在GPU上运算
; r' R# U4 [% a" r, D1 q
% G! k: ^* U: ~2 D% ]; _ [在GPU可以正常运行基本的运算,与正常矩阵计算方法相同
. K9 w! C! n$ Y( |% e
& F; \) p) }7 W! }- A=abs(A);& ~, B8 U/ g! s; F+ G2 v
9 Z! J; P( P$ H$ Y0 ^9 G- f( k( G8 h
- j, U( O9 S6 k$ T: H# F' ]
具体的可以运行的运算可以使用命令: y/ Y3 r9 q0 b# y8 S7 U9 y' c
- w5 x4 \; Q0 C- methods(gpuArray)
, q: K2 F' A9 H/ k) I; T! x' a / l2 u# M7 D8 B
+ ]3 i3 Q8 I4 R进行查看,Matlab可以在GPU运行的具体运算可以查看附录,附录中是Matlab给出的结果。
: Z; y: ?0 K9 p+ P) m$ G2 m% I( a8 B% u) Q: i5 g* l
3.GPU数据回传; a5 i7 F* y, d' o$ c
; ^8 }( ?) s/ X& ^
- B = gather (A);: u1 ^& v' ]8 U; v T+ ]# [. W
2 y F, `: n/ |1 n$ P, J
$ N6 X+ n( m4 \; o* X% ?: x
直接使用上边的命令就能够将GPU中的数据回传给CPU。
, y# Z9 \1 r$ q# A" b( W# ?6 m8 m2 ]% ^. B. J8 Y% N% k/ D
4.使用技巧
7 W' i, b2 ]& z% F' I" o
& J' _5 m; d9 m. |! V p% F4.1 如果没有并行计算不建议使用GPU
& ^% c t- S9 I. v% I# W7 L/ M! b5 m& a1 t
- index = 0;
- index = gpuArray(index);
- for i = 1 : 10000
- tic
- for j = 1 : 100000
- index = index + 1;
- end
- toc
- end
- disp(index)
+ I; W/ k! a. v5 ]1 X ' h0 r0 X+ U8 O" m" Q
/ g2 m7 h S) r6 b& y/ H9 p
通过上边程序第二行程序就可以在GPU上运行,注释掉就会在CPU上运行。在我的电脑上运行时间如下表所示。
/ z. U. N: h2 N6 f x
7 p; H, q1 E: O设备 CPU GPU
6 c( L. Z+ w4 S时间 0.00010 s 1.973017 s
& a- R& y) I+ [0 y3 d, Z7 ]4 l0 y( ^. T, ^0 [8 C P
由此可以看出,单个线程运行程序还是建议在CPU上运行,CPU的主频还是高一点,GPU主要是支持多个线程同时运行。2 x3 }# b' O* c
+ R2 Z! ?3 O7 L# N) g% k
4.2 如果没有Nvidia显卡或者显卡驱动1 i6 m! X( w& z% `- V6 ^% n
; f0 {3 G8 M& m! `! G1 k1 T
如果没有Nvidia显卡或者显卡驱动,会显示下边的提示。( r0 ]% e, ~+ {4 L5 n0 H
$ b5 }( k4 x% a0 D* v' y
0 O. x- [' `9 L- D+ _% y: k: |# R9 f+ T. |+ A7 j
4.3 双精度尽量转换为单精度
- E, w% Z' ^+ r3 P% h& \# n
' F+ u6 X/ K2 b在条件允许的情况下,尽量将计算过程中双精度转换为单精度。因为在GPU中单精度的计算速度明显优于双精度,在时间上会有很大的缩减。
0 B2 Q& D+ V2 b9 i* U8 m% o5 p( v, ~附:单精度与上精度区别
# o5 v0 ]5 h% ?6 p
$ w7 U- h; l$ O, a+ a数据类型 大小(字节) 取值范围 保留有效位数 v9 u Z) E' L/ G) y$ K
单精度 4个字节(32位) 3.4E-38~3.4E+38 7位
u3 g0 A3 M8 w# V双精度 8个字节(64位) 1.7E-308~1.7E+308 16位 f: J3 ]: K! {2 y% X7 j6 @7 M# p( v
1 q! g5 S2 [' G6 ], W6 m% e3 Y2 @: X ~% ^( X1 Q8 \; N
附录
0 }( P/ s" N' n7 I% G% W0 S
4 M# g; k. L! z0 f3 F+ 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 randn2 S* y) m( x6 S3 G$ h' \
0 J1 ?4 ^+ B/ y+ T$ \% a3 Z+ C' u9 W$ x1 R! S" ?
: e+ }' T* ~2 s8 k8 v# y |
|