directory

  • preface
  • Development Environment Overview
  • Graphics card driver installation
  • Download the driver
  • Disable the nouveau
  • Install the driver
  • Install CUDA8.0
  • First CUDA program
  • Say hello to the world
  • The last

preface

Installing a driver in Linux is really not a simple matter, especially the graphics card driver, a failure directly into the system is very likely. I finally put together a CUDA programming environment after going through a lot of torture. I really love Huang, but I still want to say “So, Nvidia: FUCK YOU!” (From the father of Linux)(Manual funny).

On June 23, 2007, NVIDIA released CUDA, a technology that allows users to compute with NVIDIA’s GeForce 8 and newer Quadro Gpus. It is also the first time that the GPU can be used as a c-compiler development environment. In recent years, one of the most successful applications of GPU is deep learning. Gpu-based parallel computing has become the standard configuration of training deep learning models.

GPU is not an independent computing platform, but needs to work together with CPU, which can be regarded as the CPU’s coprocessor. Therefore, when we talk about GPU parallel computing, we actually refer to the heterogeneous computing architecture based on CPU and GPU. In heterogeneous computing architecture, GPU and CPU are connected together by PCIe bus to work cooperatively. The location of CPU is called host, and the location of GPU is called device, as shown in the following figure:

CUDA technology has several advantages:

  • Scattered read – Code can be read from any address in memory
  • Unified Virtual Memory (CUDA 4)
  • Shared storage -CUDA exposes a fast shared storage area (48K per processor) that can be shared between multiple processes. As a user-managed cache, you can get more efficient bandwidth than using texture lookups.
  • Faster download and read back between gpus
  • Full support for integer and bit operations, including integer texture lookup

Development Environment Overview

  • GPU: NVIDIA GT 750M
  • OS: UBUNTU 14.04.1LTS 64-bit

Take a look at nvidia graphics cards with instructions:

lspci | grep -i nvidia
Copy the code

After you have built the environment, you can check the hardware information with code. Write your own or official example. The information about my NVIDIA GT 750M is shown in the picture below.


Graphics card driver installation

Don’t use the graphics driver that comes with UBUNTU add-on drivers!! Don’t use the graphics driver that comes with UBUNTU add-on drivers!! Don’t use the graphics driver that comes with UBUNTU add-on drivers!! In general, you will encounter some strange problems, of course, koi will not go wrong (manual funny). This is the first pit, which can be presented in three general ways:

  • After the installation, restart the system can not enter the system, stuck ubuntu loading page;
  • Unlimited login;
  • It’s installed, it’s in the system, and then you type in nvidia-SMI and nothing happens. Normally a table will pop up like this:


Download the driver

Let’s talk about my practical operation: first of all, download the graphics card driver to the official website, for example, I am GT 750M, the operating system is 64-bit Linux, I will find the corresponding version to download.

Delete the old drivers. Note that even if you’re not faking anything, this step is harmless.

sudo apt-get remove --purge nvidia*
Copy the code

Update and install the required libraries, first this many, then CUDA wave.

sudo apt-get update
sudo apt-get install dkms build-essential linux-headers-generic
Copy the code

Disable the nouveau

Open blacklist.conf and add the Settings to disable Nouveau at the end, which is an open source driver, as shown here:

sudo vim /etc/modprobe.d/blacklist.conf
Copy the code
blacklist nouveau
blacklist lbm-nouveau
options nouveau modeset=0
alias nouveau off
alias lbm-nouveau off
Copy the code

Disable the Nouveau kernel module

echo options nouveau modeset=0
sudo update-initramfs -u
Copy the code

Restart nouveau. If running the following command does not print anything, congratulations, nouveau has been disabled.

lsmod | grep nouveau
Copy the code

Install the driver

Go to tty1(shortcut key CTRL + Alt + f1, if there is no reaction to f1-F7 try one by one, different Linux, the keys will be slightly different). Run the following command to close the GUI.

sudo service lightdm stop
Copy the code

Install driver, pay attention to the pit, must add **-no-opengl-files**, do not add this even if the installation is successful, will also appear unlimited login problem.

Sudo chmod u+x nvidia-linux-x86_64-390.87. run sudo./ nvidia-linux-x86_64-390.87. run -- no-opengl-filesCopy the code

If you already installed it, but did not add **-no-opengl-files**, do the following to save it.

Run -- uninstall sudo./ nvidia-linux-x86_64-390.87. Run -- no-opengl-filesCopy the code

Try it with the Nvidia-SMI command, and if you see something like the following, congratulations, the driver is installed successfully. Or you can see the additional driver displayed to continue using the manually installed driver.


Install CUDA8.0

Let’s repair the library first.

sudo apt-get install freeglut3-dev libx11-dev libxmu-dev libxi-dev libgl1-mesa-glx libglu1-mesa libglu1-mesa-dev
Copy the code

Go to the official CUDA website and download runfile(local) as shown below:

Md5 check, unqualified to download again. The following is my test result:

Md5sum cuda_8. 0.61 _375. 26 _linux. RunCopy the code

Close the graphical interface again

sudo service lightdm stop
Copy the code

Install **-no-opengl-files**, then default all the way.

Sudo sh cuda_8. 0.61 _375. 26 _linux. Run - no - opengl - filesCopy the code

You will then see three installeds.

Start the GRAPHICAL User Interface

sudo service lightdm start
Copy the code

There is a pit here, if it is 14.04.5, I will not be able to open it at this stage, then I will be able to switch it to 14.04.1.

Run the following command, if the three folders shown in the picture are displayed, congratulations to you.

ls /dev/nvidia*
Copy the code

If it’s missing or missing, there’s still some work to do. Use vim to create an xxx.sh(whatever the name is) and type the following:

#! /bin/bash
/sbin/modprobe nvidia

if [ "$?" -eq0];then
  # Count the number of NVIDIA controllers found.
  NVDEVS=`lspci | grep -i NVIDIA`
  N3D=`echo "$NVDEVS" | grep "3D controller" | wc -l`
  NVGA=`echo "$NVDEVS" | grep "VGA compatible controller" | wc -l`

  N=`expr $N3D + $NVGA1 `for i in `seq 0 $N`; do
    mknod -m 666 /dev/nvidia$i c 195 $i
  done

  mknod -m 666 /dev/nvidiactl c 195 255

else
  exit 1
fi

/sbin/modprobe nvidia-uvm

if [ "$?" -eq0];then
  # Find out the major device number used by the nvidia-uvm driver
  D=`grep nvidia-uvm /proc/devices | awk '{print $1}'`

  mknod -m 666 /dev/nvidia-uvm c $D 0
else
  exit 1
fi
Copy the code

When you run it, you’ll see three folders.

sudo chmod +x xxx.sh
sudo ./xxx.sh
ls /dev/nvidia*
/dev/nvidia0  /dev/nvidiactl  /dev/nvidia-uvm
Copy the code

Then write to rc.local as shown:

sudo vim /etc/rc.local
Copy the code

Open the environment configuration file

sudo vim /etc/profile
Copy the code

Finally write:

export PATH=/usr/local/ cuda - 8.0 / bin:$PATH
export LD_LIBRARY_PATH=/usr/local/ cuda - 8.0 / lib64:$LD_LIBRARY_PATH
Copy the code

Save and exit, and it takes effect.

source /etc/profile
Copy the code

And then you can see it.

Run some detection commands, and if they look like what I showed, congratulations, the environment is configured.

cat /proc/driver/nvidia/version
Copy the code
nvcc -V
Copy the code

Of course, you can compile the examples in NVIDIA Samples and run them. The most common is deviceQuery. Take two steps:


First CUDA program

I showed you a little bit of this in the development environment section, so let’s look at the code

vim Device.cu
Copy the code
#include <stdio.h>
int main() {
        int nDevices;

        cudaGetDeviceCount(&nDevices);
        for (int i = 0; i < nDevices; i++) {
                cudaDeviceProp prop;
                cudaGetDeviceProperties(&prop, i);
                printf("Device Num: %d\n", i);
                printf("Device name: %s\n", prop.name);
                printf("Device SM Num: %d\n", prop.multiProcessorCount);
                printf("Share Mem Per Block: %.2fKB\n", prop. SharedMemPerBlock / 1024.0);printf("Max Thread Per Block: %d\n", prop.maxThreadsPerBlock);
                printf("Memory Clock Rate (KHz): %d\n",
                   prop.memoryClockRate);
                printf("Memory Bus Width (bits): %d\n",
                   prop.memoryBusWidth);
                printf("Peak Memory Bandwidth (GB/s): %.2f\n\n",
                   2.0 * prop.memoryClockRate * (prop.memoryBusWidth / 8) / 1.0e6);
        }
        return 0;
}
Copy the code
nvcc Device.cu -o Device.o
Copy the code
./Device.o
Copy the code


Say hello to the world

There is no the Hello, world! How line? Specific instructions to the next period oh ~

#include <stdio.h>

__global__ void helloFromGPU ()
{
        printf("Hello, world! from GPU! \n");
}

int main()
{
        printf("Hello, world! from CPU! \n");

        helloFromGPU <<<2, 10>>>();

        cudaDeviceReset();

        return 0;
}
Copy the code


The last

In fact, since there is no terminal with GPU as its single core, GPU programming is actually based on CPU + GPU structure, with CPU responsible for complex serial and GPU responsible for parallel computing. Please remember to like it. If you have any comments or suggestions, see you in the comments section