Cuda Nhapmon

  • Published on
    10-Oct-2015

  • View
    14

  • Download
    0

Embed Size (px)

DESCRIPTION

Cuda nhap mon

Transcript

<ul><li><p>Hng Dn Cch Thit Lp D n CUDA Ngo Quoc Vinh </p><p>Kyoto Japan 2008 </p><p>1. Cu hnh phn cng v phn mm cn thit </p><p> nh ngha: </p><p>CUDA ngha l Compute Unified Device Architecture l 1 kin trc phn mm v </p><p>phn cng cho mc ch pht trin tnh ton trn GPU. Trong h thng a nhim </p><p>vic s dng GPU trong vic tnh ton (Lp trnh CUDA) v ha c th xy ra ng </p><p>thi. </p><p> Phn mm cn thit: </p><p>Cuda SDK version2.0 c th dng cho windows XP 32-bit hoc 64-bit. </p><p>Trong windows bn cn s dng chng trnh Microsoft Visual C++ 2005 vit 1 d </p><p>n CUDA </p><p> Phn cng cn thit: vit 1 chng trnh CUDA, ngoi cc phn mm h tr ta cn c phn cng </p><p>chng trnh hot ng (khng phi ch m phng) </p><p>Cc thit b phn cng c NVIDIA h tr trong lp trnh cuda, c th tham kho </p><p>tai site: http://www.nvidia.com/object/cuda_learn_products.html (trong trng hp </p><p>trn ch s dng 1 graphics card nu c bn c th s dng nhiu hn 1 graphics </p><p>card). </p><p> Tham kho: </p><p>Ti site ny c th tham kho cc ti liu lien quan n cuda s dng ting anh </p><p>http://forums.nvidia.com/index.php?showtopic=36286 </p><p>Ti site ny c th tham kho cc ti liu lien quan n cuda s dng ting nht </p><p>http://www.nvidia.co.jp/object/cuda_home_jp.html. </p></li><li><p>2. Cch ci t CUDA driver, CUDA tool kit v cuda SDK </p><p> 1 chng trnh cuda hot ng c trong mi trng windows xp. Bn cn phi c </p><p>cc th vin h tr. Cc th vin ny c cha trong b SDK do NVIDIA cung cp. </p><p> Cch download CUDA driver </p><p>Driver c download t http://www.nvidia.com/object/cuda_get.html#windows tng </p><p>ng vi s serial ca card. Nu ng ting nht bn c th download ti site: </p><p>http://www.nvidia.co.jp/object/cuda_get_jp.html#windows </p><p>Trong site ny bn chn vo mc NVIDIA driver for Microsoft Windows XP with CUDA </p><p>support (174.55). nu bn dng OS l Windows 32-bit thi chon vo x86 trong muc </p><p>Architecture download (Figure 1). </p><p>Figure 1 </p><p>1. Tip theo 1 site NVIDIA Driver Download s xut hin v bn click vo text click </p><p>here download.Figure 2. </p><p>Figure 2 </p><p> Hp thoi File Download xut hin v bn click Save. Figure 3. </p><p>Save</p><p>Figure 3 </p></li><li><p>2. hp thoi Save As hin th ra hi bn ni mun save file driver, lc ny bn </p><p>chn ng dn ni mun save file v click Save (Figure 4). Ch 1 thi gian </p><p>chng trnh t dng download file c hon tt. </p><p>Figure 4 </p><p> Cch ci t CUDA driver. </p><p>Sau khi download xong, bn double click vo file *.exe download ( v d trong </p><p>trng hp ny l file 169.21_forceware_winxp_32bit_english_whql dung cho </p><p> Geforce8800GT, Operation System Window XP, language English(US)). </p><p>Tip theo bn chn I accept the terms in the license agreement ri click Next. </p><p>Chng trnh s ni m bn mun ci chng trnh. Theo ti bn nn mc nh </p><p>c:\NVIDIA\Win2k\169.21\English (Figure 5) </p><p>Figure 5 </p><p>Click Next chng trnh load cc file cn ci t, </p><p>Click Next chng trnh ci t t ng (Figure 6). </p><p>Figure 6 </p><p>Ch trong giy lt, sau khi chng trnh ci t xong bn click Finish khi ng </p><p>my li. </p></li><li><p>Download file SDK v Toolkit. </p><p>Sau khi ci dat driver cho card ban cn phi ci b cng c h tr lp trnh cho </p><p>CUDA </p><p>Bn cn download 2 file NVIDIA_CUDA_Toolkit_1.0.exe v </p><p>NVIDIA_CUDA_SKD_1.0.exe ti site </p><p>http://www.nvidia.com/object/cuda_get.html#windows . ty thuc vo OS ca my bn </p><p>l 32 hay 64-bit (nu 32-bit bn chn Architecture l x86 v nu l 64-bit bn chon </p><p>x86-64) (hinh 4.1).Cch download 2 file ny hon ton ging nhau. </p><p>Sau khi click vo kiu Architecture, mt site mi s xut hin tip theo bn click vo </p><p>click here download file. thc h in download file bn thc hin cc bc 1,2,3 </p><p>ca mc 4.1.1 </p><p> Ci t CUDA Toolkit. </p><p>NVIDIA_CUDA_Toolkit_1.0.exe file ny cha cng c cc th vin h tr trong lp trnh </p><p>cuda v cc ti liu hng dn lp trnh. </p><p>Cch install file Toolkit. </p><p>Sau khi download file NVIDIA_CUDA_Toolkit_1.0.exe (hoc mi hn) bn double </p><p>click vo file ny ci t vo h thng. sau khi double click vo file ny th chng </p><p>trnh ci t t ng Install Shied Wizard s c kch hot. </p><p>Click button Next ci t chng trnh. Tip theo bn chn I accept the terms of </p><p>license Agrement v click Next (hnh 4.10), lc ny chng trinh s hi ni bn mun </p><p>ci t (theo ti bn nn ch default C:\CUDA) </p><p>Figure 7 </p><p>Click Next tip tc qua bc tip theo. Bn click Install ci t phn mm </p><p>sau khi qua cc bc m chng trnh Install Shied Wizard hng dn. ch 1 </p><p>vi pht sau khi chng trnh ci t xong bn click Finish kt thc vic ci t. </p><p> Ci t SDK NVIDIA_CUDA_SKD_1.0.exe y l b SDK ca NVIDIA. Trong file ny sau khi ci t </p><p>s cha cc d n mu. cc d n ny rt quan trng trong vic t nghin cu ca </p><p>ban. </p><p>1. Sau khi download file NVIDIA_CUDA_SDK_1.0.exe (ho mi hn) bn double </p><p>click vo file ny ci t vo h thng. sau khi double click vo file ny th chng </p><p>trnh ci t t ng Install Shied Wizard s c kch hot. </p></li><li><p>2. Click button Next ci t chng trnh. Tip theo bn chn I accept the </p><p>terms of license Agrement v click Next.bc ny c thc hin tng t bc 2 </p><p>ca cch install file Toolkit. </p><p>3. Chng trinh s hi mt s thng tin ca bn. Bn cn phi in tn vo </p><p>textbox Name. in tn cng ty hoc t chc vo Textbox Organization, v in </p><p>a ch Email ca bn vo Email(Optional).(Figure 8) </p><p>NameOrganization</p><p>Email</p><p>Next</p><p>Figure 8 </p><p>4. Click Next tip tc. chng trnh s yu cu bn ch r ni bn mun ci </p><p>t (theo ti bn nn ch default C:\Program Files\NVIDIA </p><p>Corporation\NVIDIA CUDA SDK) ri click Next tip tc qua bc tip theo. </p><p>5. Click Install ci t phn mm sau khi qua cc bc m chng trnh </p><p>Install Shied Wizard hng dn. ch 1 vi pht sau khi chng trnh ci t </p><p>xong bn click Finish kt thc vic ci t. </p><p>Hu ht cc chng trnh cuda mu c NVIDIA cung cp chy trn nn Visual C++. </p><p>V th bn cn phi c phn mm Microsoft Visual C++, c th dng bng Microsoft </p><p>Visual Studio C++ Express,c cung cp min ph. </p><p>Sau khi hon tt vic ci t, bn c th m d n mu deviceQuery ca NVIDIA </p><p>cung cp trong C:\Program Files\NVIDIA Corporation\NVIDIA CUDA </p><p>SDK\bin\win32\Release v chy th, nu thnh cng chng trnh s hin th cu </p><p>hnh card GPU ca bn v hin th thong bo TEST </p><p>3. Cch ci t chng trnh Visual profiler </p><p>Visual profiler c cung cp bi NVIDIA dng phn tch v nh gi 1 chng trnh </p><p>cuda. </p><p>Download visual profiler t site: http://www.nvidia.com/object/cuda_get.html#windows. </p><p>Trong site ny bn s tm thy dng text Cuda Visual Profiler trong bng Cuda for </p><p>Windows(hnh 4.9). </p><p>Vic download chng trnh ny tng t nh bc 7,8,9 ca mc 4.1.1. </p><p>Sau bn extract file CudaVisualProfiler_0.2_beta_windows.zip </p><p>Sau khi extract s xut hin folder CudaVisualProfiler trong cha 2 folders bin v </p><p>Projects. </p><p>Folder project s cha thng tin ca 1 d n cuda sau khi c phn tch. </p></li><li><p>Folder bin cha cc file *.dll v 1 file cudaprof.exe y l file chng trnh Cuda </p><p>Visual Profiler. </p><p>Chy chng trnh Cuda Visual Profiler bng cch double click vo file cudaprof.exe </p><p>(Figure 9). </p><p>Figure 9 </p><p>Chng trnh ny s hot ng m khng cn phi ci t. </p><p>4. To highlighting cho syntax ca 1 file cuda (*.cu) </p><p>1 file ngun cuda c m rng bng *.cu. nu bn dng Microsoft Visual C++ m </p><p>file ny th n s hin th dng file text (s khng trc quan bi v cc bin, tu kha u </p><p>l mu en.) tng kh nng trc quan cho chng rnh d quan st. NVIDIA cung </p><p>cp 1 file nhng vo Microsoft Visual C++ file *.cu hin th trc quan di Microsoft </p><p>Visual C++ nh 1 file *.cpp. </p><p>1. Bn vo ng dn C:\Program Files\NVIDIA CUDA </p><p>SDK\doc\syntax_highlighting\visual_studio_8 v copy file </p><p>usertype.dat vo C:\Program Files\Microsoft Visual Studio 8\Common7\IDE. </p><p>2. Tip theo bn vo menu tool -&gt;options trong hp thoi Options bn vo Text </p><p>Editor-&gt;File Extension (hnh 4.15). </p><p>Pha bn phi hp thoi trong Extension: text bn g vo cu (tn m rng ca </p><p>chng trnh cuda) </p><p>3. Tip theo trong listbox Editor: bn chn Microsoft Visual C++ (mi trng hot </p><p>ng ca cuda file). </p><p>Sau bn click button Apply and then click OK.khi ng li Microsoft Visual </p><p>studio hon tt (Figure 10). </p></li><li><p>(2)(3)</p><p>Figure 10 </p><p>By gi bn hon tt vic lm highlighting 1 file *.cu, lm chng trnh sang sa d </p><p>c hn </p><p>5. Cch thit lp 1 d n CUDA trn Microsoft Visual C++ 2005 </p><p>Nhng phn trc gii thiu cc ci t lien quan n 1 d n CUDA.trong mc 4.4 gii </p><p>thiu cch hot ng 1 chng trnh mu c NVIDIA SDK cung cp km theo. Tuy nhin </p><p>bn c th chy bt c chng trnh mu no ca NVIDIA SDK cung cp. </p><p>Trong phn ny s ch ra phng php to t mnh to ra 1 d n CUDA. </p><p> n gin ta s to 1 d n console </p><p> M Microsoft Visual C++, vo menu File-&gt;New-&gt;Project hp thoi New Project </p><p>hin th bn vo Visual C++-&gt;Win32 sau chn Win32 Console Application. </p><p>Bn c th t tn cho d n l CudaStep1 v solution CudaProgram. Sau </p><p>bn click OK-&gt;Next-&gt;Finish. Cho n thi im ny bn c 1 d n console </p><p>nhng cha phi l d n cuda (Figure 11). </p><p>Figure 11 </p><p> Vo ca s Solution Explorer click phi vo Header Files-&gt;Add-&gt;New Item </p><p>hp thoi Add new Item- CudaStep1 hin th. Tip theo bn vo Visual C++-</p><p>&gt;Code chn Header File(.h) v t tn CudaHeader.h ri click Add (Figure 12). </p><p>File ny s cha thng tin v cu hnh ca chng trnh cuda v prototype ca </p><p>cc hm kernel m bn s vit. </p></li><li><p>Name</p><p>Figure 12 </p><p> Tip theo cn to 1 file cha m ngun cho 1 chng trnh cuda, file ny s c </p><p>m rng bng .cu tng t nh bc 2 bn </p><p>vo ca s Solution Explorer click phi vo Header Files-&gt;Add-&gt;New Item hp </p><p>thoi Add new Item- CudaStep1 hin th. Tip theo bn vo Visual C++-&gt;Utility </p><p>chn Text File (.txt) v t tn l CudaFunction.cu ri click Add. </p><p>Cho n lc ny bn to ra 1 d n cuda, nhng chng trnh vn cha hot </p><p>ng c v bn cha vit code cho chng trnh. m t hot ng chng </p><p>trnh ta cn 1 chn trnh nh x l 1 matrix gm 32 phn t. hm cuda s c </p><p>nhim v tng gi tr 1 phn t ln 1 n v </p><p> M file CudaStep1.cpp v khi to 1 matrix dng lm d liu mu tnh ton </p><p>C th copy on chng trnh sau: </p><p>// cudastep1.cpp : Defines the entry point for the console application. </p><p>#include "stdafx.h" </p><p>#include </p><p>#include "CudaHeader.h" </p><p>#include </p><p>using std::cout; </p><p>using std::cin; </p><p>//prototype display function de hien thi len man hinh </p><p> void display(float *matrix, int col, int row); </p><p> //ham chnh </p><p>int _tmain(int argc, _TCHAR* argv[]) </p><p>{ </p><p> //khi to mng v gn gi tr ban u </p><p> float matrix[32]; </p><p> for (int I = 0; I &lt; 32; i++) { </p><p> matrix[i] = 9; </p><p> } </p><p> //hin th matran cha x l </p></li><li> cout</li><li><p> M file CudaFunction.cu vit hm tnh ton. Trong file ny ta s vit 2 hm, </p><p>hm CudaProcessing dng trong vic truyn d liu gia Host, Device v gi </p><p>hm kernel tnh ton. Hm cn li l CudaProcessingKernel dng tnh </p><p>ton. </p><p>#include "CudaHeader.h" </p><p>#include </p><p>#include </p><p>#include </p><p>#include </p><p>extern "C" </p><p>//prorotype hm kernel </p><p>__global__ void CudaProcessingKernel(float *data); </p><p>/*hm ny dng chuyn data t Host qua Device, gi hm kernel sau truyn d liu tnh ton v li cho Host*/ </p><p>void CudaProcessing( float *hostData) </p><p>{ </p><p>// chun b b nh trn Driver cha data nhn t Host </p><p>float *deviceData; </p><p>int size = sizeof(float)*MATRIXSIZE; </p><p>cudaMalloc((void**)&amp;deviceData, size); </p><p>//copy data t b nh Host vo b nh Device tnh ton </p><p>cudaMemcpy(deviceData, hostData, size, cudaMemcpyHostToDevice); </p><p>//khai bo s thread trn 1 block cn x l </p><p>dim3 dimBlock(XTHREADS, YTHREADS); </p><p>//khai bo s block trn 1 grid cn x l </p><p>dim3 dimGrid(XBLOCKS, YBLOCKS); </p><p>//gi chng trnh tnh ton kernel </p><p>CudaProcessingKernel(deviceData); </p><p>//sau khi tnh ton xong d liu c tr v li cho b nh Host </p><p>cudaMemcpy(hostData, deviceData, size, cudaMemcpyDeviceToHost); </p><p>//xa b nh tm thi trn Device </p><p>cudaFree(deviceData); </p><p>} </p></li><li><p>__global__ void CudaProcessingKernel(float *data) //kernel function </p><p>{ </p><p>//s th t block trn 1 grid </p><p>int bx = blockIdx.x; </p><p>//s th t thread trn 1 block </p><p>int tx = threadIdx.x; </p><p>//s th t thread trn 1 grid </p><p> int tid = bx * XTHREADS + tx; </p><p>//tnh ton data </p><p>data[tid] = data[tid]+1; </p><p> //ng b cc thread </p><p>__syncthreads(); </p><p>} </p><p> Bin dch chng trnh: bn c th bin dch chng trnh ny vi Win32 hoc </p><p>Win64, release, debug, emurelease, hoc emudebug Ty thuc vo cu hnh my </p><p>ca bn v ch bn mun build. Tuy nhin chng trnh s bo li v khng </p><p>compiler dc file CudaFunction.cu. </p><p>Bn cn download builrule t site </p><p>http://forums.nvidia.com/index.php?showtopic=30273 file cuda_build_rule.zip </p><p>vic download file ny tng t nh bc 8,9 ca mc 4.1.1.Vo Solution </p><p>Explorer click phi ln d n CudaStep1 chn Custom Build Rules hp </p><p>thoi Visual C++ Build Rule Files hin th click vo Find Existing bn chn </p><p>file cuda (file cuda build rules sau khi extract file cuda_build_rule.zip) v </p><p>click Open (Figure 13). </p><p>Figure 13 </p><p>Tr li hp thoi Visual C++ Build Rule Files bn check vo CUDA bo cho </p><p>compiler bit s dng build rule ny bin dch file cuda (*.cu). </p><p> Do trong chng trnh cn kt ni cc th vin nn bn vo Vo Solution </p><p>Explorer click phi ln d n CudaStep1 chn Properties hp thoi </p><p>CudaStep1 property pages xut hin. vo Configuration Properties-&gt;C/C++-</p><p>&gt;General trn ca s pha phi bn vo Additional Include Directories v nhp </p><p>ng dn $(CUDA_INC_PATH);./;../../common/inc;"C:/Program Files/NVIDIA </p></li><li><p>Corporation/NVIDIA CUDA SDK/common/inc" kt ni cc header ca </p><p>chng trnh Cuda (Figure 14). </p><p>Figure 14 </p><p> Vo Configuration Properties-&gt;linker-&gt;General trn ca s pha bn phi bn </p><p>vo Additional Library Directories v nhp ng dn cha dn cc file library </p><p>ca Cuda. $(CUDA_INC_PATH);./;../../common/lib;"C:/Program Files/NVIDIA </p><p>Corporation/NVIDIA CUDA SDK/common/lib";"C:/CUDA/lib" </p><p>vo Configuration Properties-&gt;linker-&gt;Input trn ca s pha bn phi bn vo </p><p>Additional Dependences v nhp tn cc th vin cn thit cho chng trnh. </p><p>Trong trng hp ny ta dng 2 th vin cudart.lib cutil32.lib.(Figure 15). </p><p>Figure 15 </p><p>By gi th bin dch v chy chng trnh bn s thy kt qu hin ln mng </p><p>hnh console. </p><p>Gii thch code: </p><p>Trong file CudaStep1.cpp cha hm Main() v hm hin th display(). Trong hm </p><p>main() 1 matrix s c khi to v gn gi tr ban u l 9. Sau hm main() </p><p>cho hin th gia tr ca matrix chua tnh ton nay ln man hnh. </p><p>Tip theo chng trnh chnh s gi hm tnh ton (CudaProcessing()) ca device </p><p>v truyn matrix ny device tnh ton. </p><p>Sau khi tnh ton xong th hm main() s cho hin th kt qu ln mn hnh. </p><p>Trong file CudaFunction.cu s tn ti 2 hm. </p></li><li><p>Hm CudaProcessing() dng copy data t b nh Host sang b nh device </p><p>sau gi hm tnh ton kernel, sau khi kt thc tnh ton d liu c tr v li </p><p>cho Host. </p><p>Hm CudaProcessingKernel() dng tnh ton. S th t ca mi phn t trong </p><p>matrix s tng ng vi s th t ca mi thread trong grid, iu ny c xc nh </p><p>thng qua ch s tid </p><p>6. C ch hot ng 1 chng trnh cuda </p><p>Ta s dng Cuda v mong mun chng trnh chy nhanh hn nh kh nng x l song </p><p>song. V th tt hn ht chng ta cn loi b cc nh hng lm 1 chng trnh chy </p><p>chm i. </p><p>1 chng trnh cuda hot ng theo m hnh SIMD (single instruction multiple data) v </p><p>th cc nh hng chnh n tc ca chng trnh l s khng thng nht v tranh </p><p>chp vng nh trong qu trnh c v lu data. iu ny buc trnh bin dch phi </p><p>chn gii php an ton trong truy cp d liu, iu ny bin 1 chng trnh song song </p><p>theo m hnh SIMD bin thnh m hnh ni tip. </p><p>Kch thc ca kiu d liu rt quan trng trong vic truy cp data 1 cch thng nht </p><p>(coalescing) kch thc data phi bng 4,8,16 bytes. </p><p>ngoi ra nu s lnh tnh ton l ln th ta nn copy data t global memory vo shared </p><p>memory hn ch vic truy cp thng xuyn vo global memory lm chm chng </p><p>trnh (do vic truy cp vo global memory mt rt nhiu thi gian hn truy cp vo </p><p>shared memory) </p><p>Pattern ca 1 chng trnh cuda thng s dng 2 hm (1 hm dnh cho vic truy cp </p><p>data v hm cn li thng c gi l hm kernel dng cho vic x l data) </p><p>//hm dng trong vic truy cp data </p><p>Void DataFunction( type hostData) </p><p>{ </p><p> //to 1 vng nh trn device lu data t host vo device </p><p>Type *deviceData; </p><p>Int size=sizeof(type)*(s phn t ca deviceData); </p><p>cudaMalloc((void**)&amp;deviceData,size); </p><p>//copy data t b nh Host vo b nh Device tnh ton </p><p>cudaMemcpy(deviceData, hostData, size, cudaMemcpyHostToDevice); </p><p>//to 1 vng nh lu data sau khi tnh toan </p><p>Type *resultData; </p><p>Int resultSize =sizeof(type)*(s phn t ca...</p></li></ul>