您好,登錄后才能下訂單哦!
這篇文章主要介紹了Ubuntu怎么安裝和卸載CUDA和CUDNN的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇Ubuntu怎么安裝和卸載CUDA和CUDNN文章都會(huì)有所收獲,下面我們一起來看看吧。
禁用nouveau驅(qū)動(dòng)
sudo vim /etc/modprobe.d/blacklist.conf
在文本最后添加:
blacklist nouveau options nouveau modeset=0
然后執(zhí)行:
sudo update-initramfs -u
重啟后,執(zhí)行以下命令,如果沒有屏幕輸出,說明禁用nouveau成功:
lsmod | grep nouveau
下載驅(qū)動(dòng)
根據(jù)自己顯卡的情況下載對(duì)應(yīng)版本的顯卡驅(qū)動(dòng),比如筆者的顯卡是rtx2070:
下載完成之后會(huì)得到一個(gè)安裝包,不同版本文件名可能不一樣:
nvidia-linux-x86_64-410.93.run
卸載舊驅(qū)動(dòng)
以下操作都需要在命令界面操作,執(zhí)行以下快捷鍵進(jìn)入命令界面,并登錄:
ctrl-alt+f1
執(zhí)行以下命令禁用x-window服務(wù),否則無法安裝顯卡驅(qū)動(dòng):
sudo service lightdm stop
執(zhí)行以下三條命令卸載原有顯卡驅(qū)動(dòng):
sudo apt-get remove --purge nvidia* sudo chmod +x nvidia-linux-x86_64-410.93.run sudo ./nvidia-linux-x86_64-410.93.run --uninstall
安裝新驅(qū)動(dòng)
直接執(zhí)行驅(qū)動(dòng)文件即可安裝新驅(qū)動(dòng),一直默認(rèn)即可:
sudo ./nvidia-linux-x86_64-410.93.run
執(zhí)行以下命令啟動(dòng)x-window服務(wù)
sudo service lightdm start
最后執(zhí)行重啟命令,重啟系統(tǒng)即可:
reboot
注意: 如果系統(tǒng)重啟之后出現(xiàn)重復(fù)登錄的情況,多數(shù)情況下都是安裝了錯(cuò)誤版本的顯卡驅(qū)動(dòng)。需要下載對(duì)應(yīng)本身機(jī)器安裝的顯卡版本。
為什么一開始我就要卸載cuda呢,這是因?yàn)楣P者是換了顯卡rtx2070,原本就安裝了cuda 8.0 和 cudnn 7.0.5不能夠正常使用,筆者需要安裝cuda 10.0 和 cudnn 7.4.2,所以要先卸載原來的cuda。注意以下的命令都是在root用戶下操作的。
卸載cuda很簡(jiǎn)單,一條命令就可以了,主要執(zhí)行的是cuda自帶的卸載腳本,讀者要根據(jù)自己的cuda版本找到卸載腳本:
sudo /usr/local/cuda-8.0/bin/uninstall_cuda_8.0.pl
卸載之后,還有一些殘留的文件夾,之前安裝的是cuda 8.0??梢砸徊h除:
sudo rm -rf /usr/local/cuda-8.0/
這樣就算卸載完了cuda。
安裝的cuda和cudnn版本:
cuda 10.0
cudnn 7.4.2
接下來的安裝步驟都是在root用戶下操作的。
下載和安裝cuda
我們可以在官網(wǎng):cuda10下載頁(yè)面,
下載符合自己系統(tǒng)版本的cuda。頁(yè)面如下:
下載完成之后,給文件賦予執(zhí)行權(quán)限:
chmod +x cuda_10.0.130_410.48_linux.run
執(zhí)行安裝包,開始安裝:
./cuda_10.0.130_410.48_linux.run
開始安裝之后,需要閱讀說明,可以使用ctrl + c
直接閱讀完成,或者使用空格鍵
慢慢閱讀。然后進(jìn)行配置,我這里說明一下:
(是否同意條款,必須同意才能繼續(xù)安裝) accept/decline/quit: accept (這里不要安裝驅(qū)動(dòng),因?yàn)橐呀?jīng)安裝最新的驅(qū)動(dòng)了,否則可能會(huì)安裝舊版本的顯卡驅(qū)動(dòng),導(dǎo)致重復(fù)登錄的情況) install nvidia accelerated graphics driver for linux-x86_64 410.48? (y)es/(n)o/(q)uit: n install the cuda 10.0 toolkit?(是否安裝cuda 10 ,這里必須要安裝) (y)es/(n)o/(q)uit: y enter toolkit location(安裝路徑,使用默認(rèn),直接回車就行) [ default is /usr/local/cuda-10.0 ]: do you want to install a symbolic link at /usr/local/cuda?(同意創(chuàng)建軟鏈接) (y)es/(n)o/(q)uit: y install the cuda 10.0 samples?(不用安裝測(cè)試,本身就有了) (y)es/(n)o/(q)uit: n installing the cuda toolkit in /usr/local/cuda-10.0 ...(開始安裝)
安裝完成之后,可以配置他們的環(huán)境變量,在vim ~/.bashrc
的最后加上以下配置信息:
export cuda_home=/usr/local/cuda-10.0 export ld_library_path=${cuda_home}/lib64 export path=${cuda_home}/bin:${path}
最后使用命令source ~/.bashrc
使它生效。
可以使用命令nvcc -v
查看安裝的版本信息:
test@test:~$ nvcc -v nvcc: nvidia (r) cuda compiler driver copyright (c) 2005-2018 nvidia corporation built on sat_aug_25_21:08:01_cdt_2018 cuda compilation tools, release 10.0, v10.0.130
執(zhí)行以下幾條命令:
cd /usr/local/cuda-10.0/samples/1_utilities/devicequery make ./devicequery
正常情況下輸出:
./devicequery starting... cuda device query (runtime api) version (cudart static linking) detected 1 cuda capable device(s) device 0: "geforce rtx 2070" cuda driver version / runtime version 10.0 / 10.0 cuda capability major/minor version number: 7.5 total amount of global memory: 7950 mbytes (8335982592 bytes) (36) multiprocessors, ( 64) cuda cores/mp: 2304 cuda cores gpu max clock rate: 1620 mhz (1.62 ghz) memory clock rate: 7001 mhz memory bus width: 256-bit l2 cache size: 4194304 bytes maximum texture dimension size (x,y,z) 1d=(131072), 2d=(131072, 65536), 3d=(16384, 16384, 16384) maximum layered 1d texture size, (num) layers 1d=(32768), 2048 layers maximum layered 2d texture size, (num) layers 2d=(32768, 32768), 2048 layers total amount of constant memory: 65536 bytes total amount of shared memory per block: 49152 bytes total number of registers available per block: 65536 warp size: 32 maximum number of threads per multiprocessor: 1024 maximum number of threads per block: 1024 max dimension size of a thread block (x,y,z): (1024, 1024, 64) max dimension size of a grid size (x,y,z): (2147483647, 65535, 65535) maximum memory pitch: 2147483647 bytes texture alignment: 512 bytes concurrent copy and kernel execution: yes with 3 copy engine(s) run time limit on kernels: yes integrated gpu sharing host memory: no support host page-locked memory mapping: yes alignment requirement for surfaces: yes device has ecc support: disabled device supports unified addressing (uva): yes device supports compute preemption: yes supports cooperative kernel launch: yes supports multidevice co-op kernel launch: yes device pci domain id / bus id / location id: 0 / 1 / 0 compute mode: < default (multiple host threads can use ::cudasetdevice() with device simultaneously) > devicequery, cuda driver = cudart, cuda driver version = 10.0, cuda runtime version = 10.0, numdevs = 1 result = pass
下載和安裝cudnn
進(jìn)入到cudnn的下載官網(wǎng):,然點(diǎn)擊download開始選擇下載版本,當(dāng)然在下載之前還有登錄,選擇版本界面如下,我們選擇cudnn library for linux
:
下載之后是一個(gè)壓縮包,如下:
cudnn-10.0-linux-x64-v7.4.2.24.tgz
然后對(duì)它進(jìn)行解壓,命令如下:
tar -zxvf cudnn-10.0-linux-x64-v7.4.2.24.tgz
解壓之后可以得到以下文件:
cuda/include/cudnn.h cuda/nvidia_sla_cudnn_support.txt cuda/lib64/libcudnn.so cuda/lib64/libcudnn.so.7 cuda/lib64/libcudnn.so.7.4.2 cuda/lib64/libcudnn_static.a
使用以下兩條命令復(fù)制這些文件到cuda目錄下:
cp cuda/lib64/* /usr/local/cuda-10.0/lib64/ cp cuda/include/* /usr/local/cuda-10.0/include/
拷貝完成之后,可以使用以下命令查看cudnn的版本信息:
cat /usr/local/cuda/include/cudnn.h | grep cudnn_major -a 2
測(cè)試安裝結(jié)果
到這里就已經(jīng)完成了cuda 10 和 cudnn 7.4.2 的安裝??梢园惭b對(duì)應(yīng)的pytorch的gpu版本測(cè)試是否可以正常使用了。安裝如下:
pip3 install https://download.pytorch.org/whl/cu100/torch-1.0.0-cp35-cp35m-linux_x86_64.whl pip3 install torchvision
然后使用以下的程序測(cè)試安裝情況:
import torch import torch.nn as nn import torch.nn.functional as f import torch.optim as optim import torch.backends.cudnn as cudnn from torchvision import datasets, transforms class net(nn.module): def __init__(self): super(net, self).__init__() self.conv1 = nn.conv2d(1, 10, kernel_size=5) self.conv2 = nn.conv2d(10, 20, kernel_size=5) self.conv2_drop = nn.dropout2d() self.fc1 = nn.linear(320, 50) self.fc2 = nn.linear(50, 10) def forward(self, x): x = f.relu(f.max_pool2d(self.conv1(x), 2)) x = f.relu(f.max_pool2d(self.conv2_drop(self.conv2(x)), 2)) x = x.view(-1, 320) x = f.relu(self.fc1(x)) x = f.dropout(x, training=self.training) x = self.fc2(x) return f.log_softmax(x, dim=1) def train(model, device, train_loader, optimizer, epoch): model.train() for batch_idx, (data, target) in enumerate(train_loader): data, target = data.to(device), target.to(device) optimizer.zero_grad() output = model(data) loss = f.nll_loss(output, target) loss.backward() optimizer.step() if batch_idx % 10 == 0: print('train epoch: {} [{}/{} ({:.0f}%)]\tloss: {:.6f}'.format( epoch, batch_idx * len(data), len(train_loader.dataset), 100. * batch_idx / len(train_loader), loss.item())) def main(): cudnn.benchmark = true torch.manual_seed(1) device = torch.device("cuda") kwargs = {'num_workers': 1, 'pin_memory': true} train_loader = torch.utils.data.dataloader( datasets.mnist('../data', train=true, download=true, transform=transforms.compose([ transforms.totensor(), transforms.normalize((0.1307,), (0.3081,)) ])), batch_size=64, shuffle=true, **kwargs) model = net().to(device) optimizer = optim.sgd(model.parameters(), lr=0.01, momentum=0.5) for epoch in range(1, 11): train(model, device, train_loader, optimizer, epoch) if __name__ == '__main__': main()
如果正常輸出一下以下信息,證明已經(jīng)安裝成了:
train epoch: 1 [0/60000 (0%)] loss: 2.365850
train epoch: 1 [640/60000 (1%)] loss: 2.305295
train epoch: 1 [1280/60000 (2%)] loss: 2.301407
train epoch: 1 [1920/60000 (3%)] loss: 2.316538
train epoch: 1 [2560/60000 (4%)] loss: 2.255809
train epoch: 1 [3200/60000 (5%)] loss: 2.224511
train epoch: 1 [3840/60000 (6%)] loss: 2.216569
train epoch: 1 [4480/60000 (7%)] loss: 2.181396
關(guān)于“Ubuntu怎么安裝和卸載CUDA和CUDNN”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“Ubuntu怎么安裝和卸載CUDA和CUDNN”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。