|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
4 T8 [! d3 s! }- {# m& w由于GPU近几年地迅速发展,GPU在多线程计算等方面逐渐超越CPU成为计算的主力军。而Matlab是常用的数学应用软件,现在讲解一下如何在Matlab中使用GPU加速计算
% a! v& m8 T8 U% p. J
4 p3 S" t, J8 q, }# n8 J5 K# a文章目录: v/ b8 L) Z; T7 ^$ [5 d8 X
0. 必要条件7 Q) e U% Z- r' v9 I. {
1.给GPU传输数据8 r C3 n* q+ M0 J2 |+ e# h' E- P
1.1 CPU的数据复制到GPU) I, a4 S% r& k! C2 {( @
1.2 直接在GPU上设置数据:- r I7 [4 J) o1 c( k) A/ x6 @* Z
2.数据在GPU上运算5 s7 O8 C) m. W
3.GPU数据回传+ Y0 D& m7 T0 S: c$ K* a
4.使用技巧7 p* H% B0 [# I4 {% e5 M7 X
4.1 如果没有并行计算不建议使用GPU
% D8 i6 [7 Q) b8 U) k* \+ L6 K0 t 4.2 如果没有Nvidia显卡或者显卡驱动8 c n8 h" Y2 Z- s# e
4.3 双精度尽量转换为单精度
& `( F- M% ~/ ^+ @7 a& d6 } 附录6 [2 M7 Y; K; s# ~
: J" h3 T) u+ L# I: p4 u9 E0 v% z- ~/ o- \# d+ N
3 P4 }3 I3 @' U2 Y/ Q3 r
0. 必要条件/ G. F" }1 |3 D5 E5 o
M T9 j; o7 M/ S) S' A
要想在Matlab中使用GPU加速有两个必须的条件
2 b; V% D2 X2 c% c8 @4 G j& ^; G% w0 E* A! ^5 p, I* d" f
- 计算机上安装了NVIDIA显卡,目前AMD与Intel显示是暂不支持的;
- 安装NVIDIA显卡驱动。
/ z _; I( r( A* \% c$ ] $ K2 } A6 ^6 N: C
# \/ r: _4 D0 \, ^3 g @1.给GPU传输数据* T0 j) a4 n- y! ?! w! W" \
. D" o6 t" q) g7 l r7 }1.1 CPU的数据复制到GPU
! H) r) ~! B% j ]( i7 S, i' S6 Q, p+ Y3 P7 M
在使用GPU计算的时候,只需要将CPU的数据复制到GPU中即可。
/ P) H7 m+ {3 h9 ~3 B# Q3 X8 g7 L# Z L# t4 I8 B) b
- G = gpuArray(M);2 B# S. h& ?- H9 \6 L
V7 q7 V Y5 @0 |/ p( t5 [$ c$ ~2 J6 v, S
上边是对数据的名称做了修改,也可以直接进行重新赋值。
: ~# K2 V' y+ S) M; w( d1 ?7 b7 c. K7 x; u. D+ z
- M = gpuArray(M);' [/ v: T1 Q" y6 U' g7 Q
. X+ O7 b, A7 z( Y4 |4 j% r, F
3 v" l4 S/ v" K X1.2 直接在GPU上设置数据:
) h9 s& ?9 E, \/ {* o: _, |4 |' G" A$ S0 j1 ~
- A = zeros(10, 'gpuArray');. M7 q T$ h z6 U7 W: C
7 o" W B6 L( X+ a$ |" ^1 U1 m, n4 n: r
7 m6 e0 e% v3 E% K
可以对0矩阵以及1矩阵直接进行复制,但是在程序后边需要标注使用gpuArray。
6 [4 ?+ [0 {3 \. l5 P5 X6 |& |8 ^+ h0 f a& B/ u% k
- r = gpuArray.rand(1, 100) % 一行,一百列# B' @* l$ q' A# m$ c
( S" ?$ @4 G' E5 H0 s; V. ^5 H- h
Q& V) U7 X G, S5 b( d3 Y7 e1 F/ Y1 W
随机矩阵的产生。& R1 Z$ y$ g9 C, l* l
# Q. G2 y, D3 c
2.数据在GPU上运算
! I+ n2 s" H7 D9 f
3 {$ W' i2 ]* j+ L6 k在GPU可以正常运行基本的运算,与正常矩阵计算方法相同
4 j( k. r9 Z1 h/ b. A/ C* r$ c% D5 R8 b0 L% j" R# z. e
' ~ z( j) I% \. r" S* K: a& j* `- I' ~1 P$ {( N! Q
具体的可以运行的运算可以使用命令
) w" E7 Z# e' ]5 E6 x+ i+ l
+ L3 q+ _1 b0 F6 W0 C& f0 Z( x- methods(gpuArray)
- ]3 z5 p8 P) `% d7 d
% h) O9 Z7 v8 [6 R9 M
" F$ f5 y5 D$ |7 |进行查看,Matlab可以在GPU运行的具体运算可以查看附录,附录中是Matlab给出的结果。+ P% C$ J, ]6 v9 f* ~# t+ p8 t1 C
6 K# h9 \5 {1 z! x P* }: L- q8 M7 O3.GPU数据回传
+ M' V% ^0 [% U1 j: }" @) O$ O2 I1 z1 A7 i/ Q# V0 V( w$ ^
- B = gather (A);8 A3 _( y2 ~8 I5 a: J7 h3 j0 ~
# u# A; [2 o! j) F, F0 f
4 b. @3 F r* V4 ~% k9 m1 G1 U直接使用上边的命令就能够将GPU中的数据回传给CPU。: ~. I) n. _! O2 T8 d
% Q% ]" x. V) O
4.使用技巧3 M' n# O- R- q5 k7 H$ l' h
a5 P1 `- y# S4 f9 [ ~
4.1 如果没有并行计算不建议使用GPU; s4 V8 _: K+ N/ ^& l! T* B
. _* _ `& P# {+ X2 `- index = 0;
- index = gpuArray(index);
- for i = 1 : 10000
- tic
- for j = 1 : 100000
- index = index + 1;
- end
- toc
- end
- disp(index)
|+ k: J) Y' r5 m* s : t0 n; d t. O9 Y" s4 N9 D. d
0 T; T6 k* e" @5 @1 D0 u- A( ?& h通过上边程序第二行程序就可以在GPU上运行,注释掉就会在CPU上运行。在我的电脑上运行时间如下表所示。4 D/ i* j7 l0 E+ H0 `
/ \) a6 K+ K- R8 O
设备 CPU GPU0 ^( V* a& X: ]3 c6 z
时间 0.00010 s 1.973017 s7 h4 W0 v+ f$ V, z" Z3 V1 W5 M
1 b# O; U% n7 d) ^由此可以看出,单个线程运行程序还是建议在CPU上运行,CPU的主频还是高一点,GPU主要是支持多个线程同时运行。; p- q% n4 r7 O' E
, x) d0 B* V( U( w2 z4.2 如果没有Nvidia显卡或者显卡驱动
1 Y b6 [* |# M' F
9 {# g/ H$ g" J$ A7 t8 ?! u. n) J如果没有Nvidia显卡或者显卡驱动,会显示下边的提示。
& i* e* h/ G% A7 t- i
3 o. ^; @) @0 f
/ E& ]* k+ W/ H' ]6 u2 X! Q! Z1 E' R
4.3 双精度尽量转换为单精度
$ w8 N- E( i9 D+ \, Y7 v1 `% Z* M& \
在条件允许的情况下,尽量将计算过程中双精度转换为单精度。因为在GPU中单精度的计算速度明显优于双精度,在时间上会有很大的缩减。
7 E4 Y, _, R! P- Y附:单精度与上精度区别
- W! m; q* T! {4 G: }( h" T: m
, r9 q% `1 G% m" d& B* R数据类型 大小(字节) 取值范围 保留有效位数/ W% U7 x6 V, Z: C0 G
单精度 4个字节(32位) 3.4E-38~3.4E+38 7位
% q5 u: F; \+ e- T双精度 8个字节(64位) 1.7E-308~1.7E+308 16位" z- c% U4 ~2 [! L4 j
. c, @ N, q9 j3 ^. i
9 w f n, Z$ @( n# f5 n1 h) p
附录
( t" T1 o0 s0 f- \9 r3 Y9 m9 @8 U' b0 f3 C" Q3 j2 I
- >> 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" h3 f7 A: M! R" a2 V
9 U4 ^+ R+ `8 E% @8 k- K+ H: u3 i
4 Q3 }6 e( E* Q. T
$ Z! d4 y6 G4 ~# p0 m( B4 Z9 \0 }! D |
|