云GPU租用及CUDA profile工具入門教程
有一篇很好的CUDA入門文章《An Even Easier Introduction to CUDA》,這篇文章中用到的nvprof在CUDA高版本已經(jīng)不支持,需要換成nsys。所以本文中仍然沿用它的代碼,附加nsys的安裝方式以及簡單應(yīng)用。
Part1云服務(wù)器租用使用云服務(wù)器可以避免一部分復(fù)雜的環(huán)境安裝工作,這里我以gpushare.com為例,其他云服務(wù)廠商的操作流程應(yīng)該都差不多。
首先從云市場選一張卡
選擇框架版本,創(chuàng)建實例
等待片刻,實例啟動后即可登錄
復(fù)制ssh命令到terminal中
nvidia-smi查看顯卡環(huán)境
NsightSystems是CUDA高版本的profile工具,低版本CUDA用的nvprof工具已經(jīng)不支持了。
首先將安裝包上傳到服務(wù)器上(安裝包下載比較麻煩,要到nvidia官網(wǎng)注冊) 我用這家云GPU廠商提供的工具是個ftp客戶端:FileZilla。
填寫主機地址,端口和用戶密碼
密碼在登錄命令的下面
連接成功后只要兩邊互相拖動就可以傳文件了
把下載到的NsightSystems-linux-cli-public-2022.2.1.31-5fe97ab.deb文件傳到服務(wù)器上,用命令安裝
dpkg?-i?NsightSystems-linux-cli-public-2022.2.1.31-5fe97ab.deb
nvprof -V驗證安裝成功
將以下代碼命名為add.cu
#include?<iostream>
#include?<math.h>
//?Kernel?function?to?add?the?elements?of?two?arrays
__global__
void?add(int?n,?float?*x,?float?*y)
{
??for?(int?i?=?0;?i?<?n;?i++)
????y[i]?=?x[i]?+?y[i];
}
int?main(void)
{
??int?N?=?1<<20;
??float?*x,?*y;
??//?Allocate?Unified?Memory?–?accessible?from?CPU?or?GPU
??cudaMallocManaged(&x,?N*sizeof(float));
??cudaMallocManaged(&y,?N*sizeof(float));
??//?initialize?x?and?y?arrays?on?the?host
??for?(int?i?=?0;?i?<?N;?i++)?{
????x[i]?=?1.0f;
????y[i]?=?2.0f;
??}
??//?Run?kernel?on?1M?elements?on?the?GPU
??add<<<1,?1>>>(N,?x,?y);
??//?Wait?for?GPU?to?finish?before?accessing?on?host
??cudaDeviceSynchronize();
??//?Check?for?errors?(all?values?should?be?3.0f)
??float?maxError?=?0.0f;
??for?(int?i?=?0;?i?<?N;?i++)
????maxError?=?fmax(maxError,?fabs(y[i]-3.0f));
??std::cout?<<?"Max?error:?"?<<?maxError?<<?std::endl;
??//?Free?memory
??cudaFree(x);
??cudaFree(y);
??return?0;
}
用nvcc命令編譯并執(zhí)行
nvcc?add.cu?-o?add_cuda?-run
用nvprof進行性能分析,
nsys?profile?--stats=true?./add_cuda
可以得到一系列指標(biāo)數(shù)據(jù),以及兩個結(jié)果文件report1.nsys-rep和report1.sqlite

大家還可以從Even Easier教程中找到add_block和add_grid的代碼,同樣用nvcc和nvprof來比較幾種不同的并行方式的性能差異。
如果大家想自己動手,配置實驗環(huán)境的話,可以點擊閱讀原文租用gpushare的GPU。新人任務(wù)只要充值30元即可獲得100元禮券,其中60元都可以按小時租用。白嫖15個小時(3.9一小時的3090)夠做很多入門級的實驗了~


Part4RefEven Easier教程:https://developer.nvidia.com/blog/even-easier-introduction-cuda/
filezilla使用教程:https://gpushare.com/docs/data/upload/#filezilla
