|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
, L- q D% e# R7 H由于GPU近几年地迅速发展,GPU在多线程计算等方面逐渐超越CPU成为计算的主力军。而Matlab是常用的数学应用软件,现在讲解一下如何在Matlab中使用GPU加速计算- h4 @# D4 e! ]+ M7 }' {' O( n
T/ t( }( b5 c# W1 [5 j
文章目录6 H+ K8 Y1 E+ h3 p( R" b
0. 必要条件6 ~3 G) ]) a2 m: \9 t
1.给GPU传输数据1 ?9 M0 ` N+ m$ e7 v: q0 y: k
1.1 CPU的数据复制到GPU6 |0 o- ?) u# n0 e) P. V% w
1.2 直接在GPU上设置数据:6 p- j7 Q4 E3 c& H& s, d
2.数据在GPU上运算' ~- p1 z) S9 _9 o* n/ C6 |, q
3.GPU数据回传9 ^- @8 x- p( {; l" N
4.使用技巧
% S5 u. g q% J0 A) P, [; ] 4.1 如果没有并行计算不建议使用GPU4 z/ n9 X2 ]& k% p F3 I
4.2 如果没有Nvidia显卡或者显卡驱动
$ [, W, K: D9 T* Y- i7 l 4.3 双精度尽量转换为单精度
/ D' U3 a7 d. g/ t. K 附录
6 P- J4 R8 ~8 {7 P5 V, v% r8 H4 g0 H5 m& g4 X
8 @# h% s5 K: |( e
' p: ], ]- t! ]" k
0. 必要条件
7 a" @; b# O" @+ h( e' `) q& u1 n7 T4 t# O
要想在Matlab中使用GPU加速有两个必须的条件3 ?" r9 H- z( E
; U: B( D2 v/ M- 计算机上安装了NVIDIA显卡,目前AMD与Intel显示是暂不支持的;
- 安装NVIDIA显卡驱动。7 |/ Z) [% J: W3 E+ I
4 _; ]; a' M2 d g
+ a5 D U! L* V( U: Z, z6 B7 a# d1.给GPU传输数据
% g$ Y K# \/ c/ R6 \8 n( \( |: \ n1 |
1.1 CPU的数据复制到GPU
# r* T+ i9 A4 {& D" ~4 o$ C) _' V n
在使用GPU计算的时候,只需要将CPU的数据复制到GPU中即可。+ C/ ~6 V5 r5 ^" c
$ g: G' x6 W" ^ k* y9 E! f- G = gpuArray(M);
: o0 o" |+ n+ d* A7 l8 }0 o
9 }, n8 ^5 y" w6 u7 K
7 I/ W' v7 N* a8 }5 ]; S; K% ?/ Z5 d上边是对数据的名称做了修改,也可以直接进行重新赋值。
$ E& B! _# k9 a! ]
# c. a* d3 ~, D; z8 Q9 l \- M = gpuArray(M);
" ?5 ]0 U5 ^" h3 Y- g, ?$ ~$ ? ) x# H0 q% J9 I
- x0 D8 N* z- e1 @
1.2 直接在GPU上设置数据:
@: A/ S8 R- e+ h0 W. o1 e6 B, U' J
9 A9 A% b' I" A% i! I1 Z- A = zeros(10, 'gpuArray');
# H: z1 O# @) Z7 [
; m# {, Z% R$ P
5 b2 Q1 ]) c# ^/ ]& k可以对0矩阵以及1矩阵直接进行复制,但是在程序后边需要标注使用gpuArray。0 M( O! e( j+ B+ U/ p' C3 a
8 J0 i% y5 g& |" M4 [
- r = gpuArray.rand(1, 100) % 一行,一百列; M, `- i9 L7 `3 K" i
# J" c6 I2 V' ^ H5 {- ~4 ]7 X7 M
! N8 E, D8 H( @. ~随机矩阵的产生。
3 V! Q0 x7 v/ b, x4 P1 Y7 {7 `3 w! ^. y9 u1 N5 S0 H
2.数据在GPU上运算
1 A$ G6 A0 W1 i, _
W0 p+ D$ e' R# H% t/ H在GPU可以正常运行基本的运算,与正常矩阵计算方法相同
' L% B; L5 C$ c. m, e3 S; `0 d3 P( R
- A=abs(A);
; _/ h) Q6 @7 b/ |4 ?
( L9 }. d6 g0 A. Z5 a! T& l+ o7 I, K2 m9 f% k; K! N1 A- I# y
具体的可以运行的运算可以使用命令1 o" I; M8 s R4 ^4 z
4 t6 }8 |( f/ V9 X2 p2 @$ s" I7 _- methods(gpuArray)
. q' c, y' B4 l/ P( o2 x# c3 E 8 j. [ p7 Q% }7 V; ?4 f& K! Y: J) C
4 v9 i1 [( A/ Y
进行查看,Matlab可以在GPU运行的具体运算可以查看附录,附录中是Matlab给出的结果。
( g3 ?8 y3 L ^6 [2 V2 h D% K$ G2 ], R8 B& N; A6 {4 G
3.GPU数据回传+ N" ]. t9 ?+ e6 Z; ?8 {
- Y6 N5 a$ O# S6 |1 c6 g' a& r: J) u- B = gather (A);3 Y" c' x6 R% B
" v6 T6 o" Z1 T, l5 q; v$ w O+ ?" e1 J, `8 T& c. D
直接使用上边的命令就能够将GPU中的数据回传给CPU。
0 B! R% J2 y. z9 p0 I2 V M7 F, E8 F. R9 L! b' |6 z
4.使用技巧3 q/ \. x. I8 d. r& T5 i
! u* W4 i& U3 n F% P2 `4.1 如果没有并行计算不建议使用GPU7 _ {0 w6 y; z, d
' ]) \4 R& j2 x0 ~8 h8 e- index = 0;
- index = gpuArray(index);
- for i = 1 : 10000
- tic
- for j = 1 : 100000
- index = index + 1;
- end
- toc
- end
- disp(index)
1 O( U) W4 G) ?- G : ^3 C R) |6 D. \
0 j# N8 b4 u: Y1 s/ G
通过上边程序第二行程序就可以在GPU上运行,注释掉就会在CPU上运行。在我的电脑上运行时间如下表所示。! E6 W, Q S3 Y. o3 ~
6 z: X$ E* @8 B! |( J设备 CPU GPU
- r; o7 D8 O- B, |& N时间 0.00010 s 1.973017 s
/ S/ |. R) B7 p! @9 V
7 ]2 [9 F" e, C0 U* x4 z- s由此可以看出,单个线程运行程序还是建议在CPU上运行,CPU的主频还是高一点,GPU主要是支持多个线程同时运行。% b, J4 ?# l, c( g+ R: x
3 V n$ K1 `+ H1 t! U/ R
4.2 如果没有Nvidia显卡或者显卡驱动
6 K7 Q4 y7 z7 P7 y, o+ U6 _, B) y
2 O4 o4 ?. a, [; w, X4 C+ _( O如果没有Nvidia显卡或者显卡驱动,会显示下边的提示。
: h7 b# M3 } |/ h ]6 V; a8 F( E
, J& w! r3 b0 l T0 T H
2 Z0 b) ]. r. q0 Y v& b2 j$ U- T+ I
4.3 双精度尽量转换为单精度
/ Y) m8 J& `! S& l1 {! n+ w' {9 _2 K( f/ V7 L
在条件允许的情况下,尽量将计算过程中双精度转换为单精度。因为在GPU中单精度的计算速度明显优于双精度,在时间上会有很大的缩减。
% U: P1 I! A8 [* n* I; R附:单精度与上精度区别, e. H& K b7 \
1 @9 S8 h8 z: |/ g( F
数据类型 大小(字节) 取值范围 保留有效位数
4 @9 i! X" \7 g. d! D2 s& \+ p1 J( Y单精度 4个字节(32位) 3.4E-38~3.4E+38 7位
% O: A. @& k$ s" n! h5 O& `双精度 8个字节(64位) 1.7E-308~1.7E+308 16位) C' [3 k& G3 I' S" |2 o
3 M" X, Q/ O6 c5 E% q9 y/ r
$ d' f0 N1 x/ L( X9 M附录' t& {/ Z, m% q/ Y/ q9 }9 {
- c& B/ i5 m1 y2 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
) H- ^& N/ Z2 `( |7 \
8 D4 Q! }! C9 c; A
: u3 {1 J: g% E- q" u
" t1 t4 q. O% J1 P1 J M/ N |
|