2-3 使用GPU來????

GPU (graphic processing units) X{MΡAiO~Ӭǭp⪺̤jܭAѩ GPU 㦳jqBzOAҥHY@ǾAXB⪺ΡAGPU iO̬OAXLFC

O GPU {]pAYH C C++ ӶiA|@{]p@ǡAAnA GPU ]pHεw鵲cA~Ro GPU pOCYnb MATLAB Өϥ GPU [tUعBAhO۷eA]Ӹ`wgQ]tb MATLAB ²檺OFC

MATLAB P GPU ̬۬Ӱ򥻫OpUGէAXi

ҦpAHUdҥiHܧAWXi GPU dAHιw] GPU dTG

Example 1: 02-{XPO餧̨Τ/gpuDevice01.md = gpuDeviceCount g = gpuDevice d = 1 g = <a href="matlab:helpPopup parallel.gpu.CUDADevice" style="font-weight:bold">CUDADevice</a> with properties: Name: 'GeForce GTX 970M' Index: 1 ComputeCapability: '5.2' SupportsDouble: 1 DriverVersion: 7 ToolkitVersion: 6.5000 MaxThreadsPerBlock: 1024 MaxShmemPerBlock: 49152 MaxThreadBlockSize: [1024 1024 64] MaxGridSize: [2.1475e+09 65535 65535] SIMDWidth: 32 TotalMemory: 3.2212e+09 AvailableMemory: 2.9349e+09 MultiprocessorCount: 10 ClockRateKHz: 1038000 ComputeMode: 'Default' GPUOverlapsTransfers: 1 KernelExecutionTimeout: 1 CanMapHostMemory: 1 DeviceSupported: 1 DeviceSelected: 1

bWzdҤAܤFڪu@idAܦdUجʽC

bϥdiBɡAڭ̳q``UC򥻨BJG

  1. ϥ gpuArray OAN MATLAB u@ŶܼƷh GPU O餤C
  2. ϥ GPU O餤ܼƨӰUئb GPU BC
  3. ϥ gather OANsb GPU ܼƷh MATLAB u@ŶC

bHUdҤAڭ̥H²檺x}ۭӻpާ@HWoXӨBJG

Example 2: 02-{XPO餧̨Τ/gpuStep01.ma=rand(100, 10000); b=rand(100, 10000)'; tic c=a*b; fprintf('CPU time = %g sec\n', toc); A=gpuArray(a); % Put a to GPU's memory B=gpuArray(b); % Put b to GPU's memory tic C=A*B; % Multiplication via GPU fprintf('GPU time = %g sec\n', toc); c2=gather(C); % Put C to MATLAB's workspace fprintf('isequal(c, c2) = %g\n', isequal(c, c2)); fprintf('Mean deviation = %g\n', mean(mean(abs(c-c2))));CPU time = 0.00463387 sec GPU time = 0.000350486 sec isequal(c, c2) = 0 Mean deviation = 5.55428e-13

bWzdҤAڭ̥iH[UC{HG
  1. GPU pɶ]]tƷhɶ^ju CPU pɶ 1/20C
  2. GPU p⵲GM CPU ۦPA̪tʷpC

SOn`NOAWz GPU pɶAä]tƷhɶC@ӨAڭӺɶqָƷhAúɶqb GPU i業BA_hϦӷ|ovC

be@ӽdҤAp[tTשMx}צܤjYAUoӽdұNQoYG

Example 3: 02-{XPO餧̨Τ/gpuSpeedup01.mfprintf('computer = %s\n', computer); fprintf('version = %s\n', version); % Speed test step=10000; colCounts=step*(1:1:20); for i=1:length(colCounts) fprintf('%d/%d\n', i, length(colCounts)); n=colCounts(i); a=rand(100, n); b=rand(100, n)'; myTic=tic; c=a*b; cpuTime(i)=toc(myTic); A=gpuArray(a); B=gpuArray(b); myTic=tic; C=A*B; gpuTime(i)=toc(myTic); end subplot(211); plot(colCounts, cpuTime, '.-', colCounts, gpuTime, '.-'); legend('CPU time', 'GPU time', 'location', 'northwest'); title('CPU & GPU time'); ylabel('Time (sec)'); subplot(212); plot(colCounts, cpuTime./gpuTime, 'o-'); title('GPU speedup ratio'); ylabel('Ratios'); xlabel('No. of columns');computer = PCWIN64 version = 8.5.0.197613 (R2015a) 1/20 2/20 3/20 4/20 5/20 6/20 7/20 8/20 9/20 10/20 11/20 12/20 13/20 14/20 15/20 16/20 17/20 18/20 19/20 20/20

bWzdҤA[tT׫ܤjAGPU tץiF CPU tת 500 HWCЪ`NAWzdҪpä]tƷhһݪɶC

Hint
ЭקWzdҡANƷhɶ]J`pɶAݬݷ||X{uovvpC

]^


MATLAB{]pGig