ubuntu 16.04安装 nvidia driver + cuda

前端之家收集整理的这篇文章主要介绍了ubuntu 16.04安装 nvidia driver + cuda前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1. 选择安装nvidia driver最新版本

sudo apt purge nvidia-*
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt update
sudo apt install nvidia-381
sudo reboot

2. 安装nvidia cuda最新版本

wget http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_9.1.85-1_amd64.deb
sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub
sudo dpkg -i cuda-repo-ubuntu1604_9.1.85-1_amd64.deb
sudo apt-get update
sudo apt-get install -y cuda nvidia-cuda-toolkit

3. 测试

#include "cuda_runtime.h" 
#include "device_launch_parameters.h" 
#include <stdio.h>

#define N 10


__global__ void add(int *a,int *b,int *c){
    int tid = blockIdx.x;

    if(tid < N){
        c[tid] = a[tid] + b[tid];   
    }
}


int main(){
    int a[N],b[N],c[N];
    int *dev_a,*dev_b,*dev_c;

    cudaMalloc((void**)&dev_a,N*sizeof(int));
    cudaMalloc((void**)&dev_b,N*sizeof(int));
    cudaMalloc((void**)&dev_c,N*sizeof(int));

    for(int i=0; i<N; i++){
        a[i] = i;
        b[i] = i+1;
    }

    cudaMemcpy(dev_a,a,N*sizeof(int),cudaMemcpyHostToDevice);
    cudaMemcpy(dev_b,b,cudaMemcpyHostToDevice);

    add<<<N,1>>>(dev_a,dev_b,dev_c);

    cudaMemcpy(c,dev_c,cudaMemcpyDeviceToHost);

    for(int i=0; i<N; i++){
        printf("%d + %d = %d\n",a[i],b[i],c[i]);
    }   

    return 0;
}

参考文献
https://askubuntu.com/questions/902636/nvidia-smi-command-not-found-ubuntu-16-04
https://cryptoandcoffee.com/mining-gems/cuda-9-1-install-ubuntu-16-04-apt-get/

猜你在找的Ubuntu相关文章