Ubuntu CUDA cuDNN TensorFlow Caffe

1 Installing NVIDIA Graphics Drivers by apt or run

1.1 Using apt install

  • 1 [Option]Add Graphic Drivers PPA
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt-get update
  • 2 Check Graphic Drivers Version
ubuntu-drivers devices
  • 3 Installing
sudo apt install nvidia-version
  • 4 Reboot and check
sudo reboot
sudo nvidia-smi
sudo nvidia-setting

1.2 Using run install

  • 2.1 Before you Begin

Disable Nouveau driver

lsmod | grep nouveau

If yes:
Create a new file or edit an existing one

sudo gedit/etc/modprobe.d/blacklist-nouveau.conf

The following two lines will need to be added:

blacklist nouveau
options nouveau modeset=0
sudo update-initramfs -u

Reboot your system and check

  • 2.2 Starting the Installer
cd yourdirectory
sudo sh NVIDIA-Linux-x86_64-390.77.run
  • 2.3 Restart your system to ensure the graphics driver takes effect.
sudo reboot
sudo nvidia-smi
sudo nvidia-settings

2 Installing CUDA

2.1 Pre-installation Actions

  • 1 Verify You Have a CUDA-Capable GPU
lspci | grep -i nvidia
  • 2 Verify You Have a Supported Version of Linux
uname -m && cat /etc/*release

You should see output similar to the following:

x86_64
  • 3 Verify the System Has gcc Installed
gcc --version

If not:

sudo apt install gcc

Or:

sudo apt install build-essential
  • 4 Verify the System has the Correct Kernel Headers and Development Packages Installed
    The version of the kernel your system is running can be found by running the following command:
uname -r

The kernel headers and development packages for the currently running kernel can be installed with:

sudo apt install linux-headers-$(uname -r)
  • 5 Choose an Installation Method
    The CUDA Toolkit can be installed using either of two different installation mechanisms: distribution-specific packages (RPM and Deb packages), or a distribution-independent package (runfile packages).
    The distribution-independent package has the advantage of working across a wider set of Linux distributions, but does not update the distribution’s native package management system. The distribution-specific packages interface with the distribution’s native package management system.
    It is recommended to use the distribution-specific packages, where possible.
  • 6 Download the NVIDIA CUDA Toolkit
  • 7 Handle Conflicting Installation Methods
  • Use the following command to uninstall a Toolkit runfile installation:
sudo /usr/local/cuda-X.Y/bin/uninstall_cuda_X.Y.pl
  • Use the following command to uninstall a Driver runfile installation:
sudo /usr/bin/nvidia-uninstall
  • Use the following commands to uninstall a RPM/Deb installation:
sudo apt --purge remove <package_name>

2.2 Package Manager Installation(deb)

Ubuntu 16.04 x86_64 deb(local) Download

  • Install repository meta-data
sudo dpkg -i cuda-repo-ubuntu1604-9-2-local_9.2.148-1_amd64.deb
  • Installing the CUDA public GPG key
sudo apt-key add /var/cuda-repo-<version>/7fa2af80.pub
  • Update the Apt repository cache
sudo apt update
  • Install CUDA
sudo apt install cuda

2.3 Runfile Installation

  • 1 Disable the Nouveau drivers
lsmod | grep nouveau

if yes, turn 1.2

  • 2 Reboot into text mode
    Ctrl+Alt+F1
    Stop the graphical interface
sudo service lightdm stop
  • 3 Run the installer
sudo sh cuda_<version>_linux.run
  • 4 Reboot the system to reload the graphical interface.

2.4 Post-installation Actions

  • 1 Mandatory Actions
    Environment Setup
    The PATH variable needs to include /usr/local/cuda-9.2/bin
    To add this path to the PATH variable:
sudo gedit /etc/profile		# System variable

type:

export PATH=/usr/local/cuda-9.2/bin${PATH:+:${PATH}}		# have changed, check document

source:

source /etc/profile

In addition, when using the runfile installation method, the LD_LIBRARY_PATH variable needs to contain /usr/local/cuda-9.2/lib64 on a 64-bit system, or /usr/local/cuda-9.2/lib on a 32-bit system

export LD_LIBRARY_PATH=/usr/local/cuda-9.2/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}		# have changed, check document

Tips:

  • Temporary variable
export PATH=/usr/local/cuda-9.2/bin${PATH:+:${PATH}}
  • User variable
sudo vim ./bashrc
source ./bashrc
  • System variable
sudo vim /etc/profile
source profile
  • 2 Recommended Actions
  • 1 Verify the Installation
  • 1 Verify the Driver Version
cat /proc/driver/nvidia/version
  • 2 Compiling the Examples
    The version of the CUDA Toolkit can be checked
nvcc -V

compile:

cd ~/NVIDIA_CUDA-9.2_Samples
make

The resulting binaries will be placed under ~/NVIDIA_CUDA-9.2_Samples/bin.

  • 3 Running the Binaries
cd bin/x86_64/linux/release
./deviceQuery

ensures that the system and the CUDA-capable device are able to communicate correctly.

./bandwidthTest

Delete make result:

make clean

3 cuDNN

3.1 Prerequisites

Ensure you meet the following requirements before you install cuDNN.

  • A GPU of compute capability 3.0 or higher. To understand the compute capability of the GPU on your system, see: CUDA GPUs.
  • If you are using cuDNN with a Volta GPU, version 7 or later is required.
  • One of the following supported Architecture - OS combinations:
  • On x86_64 (for installing cuDNN with debian files) - Ubuntu 14.04 or Ubuntu 16.04
  • On x86_64 (for installing tgz files) - Any Linux distribution
  • On POWER8/POWER9 - RHEL7.4
  • One of the following supported CUDA versions and NVIDIA graphics driver:
  • NVIDIA graphics driver R375 or newer for CUDA 8
  • NVIDIA graphics driver R384 or newer for CUDA 9
  • NVIDIA graphics driver R390 or newer for CUDA 9.2
  • 1 Installing NVIDIA Graphics Drivers
  • 2 Installing CUDA

3.2 Downloading cuDNN

3.3 Installing cuDNN on Linux from a Tar File or Debian File

  • 1 Installing from a Tar File
  • 1.Unzip the cuDNN package.
tar -xzvf cudnn-<version>-linux-x64-v7.tgz
  • 2.Copy the following files into the CUDA Toolkit directory.
sudo cp cuda/include/cudnn.h /usr/local/cuda/include
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
  • 2 Installing from a Debian File
  • 1.Install the runtime library, for example:
sudo dpkg -i libcudnn<version>amd64.deb
  • 2.Install the developer library, for example:
sudo dpkg -i libcudnn7-dev_7.0.3.11-1+cuda9.0_amd64.deb
  • 3.Install the code samples and the cuDNN Library User Guide, for example:
sudo dpkg -i libcudnn7-doc_7.0.3.11-1+cuda9.0_amd64.deb

3.4 Verifying

To verify that cuDNN is installed and is running properly, compile the mnistCUDNN sample located in the /usr/src/cudnn_samples_v7 directory in the debian file.

  • 1.Copy the cuDNN sample to a writable path.
cp -r /usr/src/cudnn_samples_v7/ $HOME
  • 2.Go to the writable path.
cd  $HOME/cudnn_samples_v7/mnistCUDNN
  • 3.Compile the mnistCUDNN sample.
make clean && make
  • 4.Run the mnistCUDNN sample.
./mnistCUDNN

If cuDNN is properly installed and running on your Linux system, you will see a message similar to the following:

Test passed!

4 Tensorflow

pip3 install tensorflow-gpu

5 Caffe

5.1 Prerequisites

Caffe has several dependencies:

sudo apt install build-essential
  • CUDA is required for GPU mode.
  • library version 7+ and the latest driver version are recommended, but 6.* is fine too. 5.5, and 5.0 are compatible but considered legacy
  • BLAS via ATLAS, MKL, or OpenBLAS.
  • Boost >= 1.55
  • protobuf, glog, gflags, hdf5
sudo apt install libboost-all-dev
sudo apt install libprotobuf-dev libprotobuf-all-dev libprotobuf-compiler libgoogle-glog-dev libgflags-dev libhdf5-serial-dev

Optional dependencies:

  • OpenCV >= 2.4 including 3.0
  • IO libraries: lmdb, leveldb (note: leveldb requires snappy)
  • cuDNN for GPU acceleration (v6)
sudo apt install libopencv-dev
sudo apt install libsnappy-dev
sudo apt install liblmdb-dev libleveldb-dev

Pycaffe interfaces have their own natural needs.

  • For Python Caffe: Python 2.7 or Python 3.3+, numpy (>= 1.7), boost-provided boost.python

cuDNN Caffe: for fastest operation Caffe is accelerated by drop-in integration of NVIDIA cuDNN. To speed up your Caffe models, install cuDNN then uncomment the USE_CUDNN := 1 flag in Makefile.config when installing Caffe. Acceleration is automatic. The current version is cuDNN v6; older versions are supported in older Caffe.

CPU-only Caffe: for cold-brewed CPU-only Caffe uncomment the CPU_ONLY := 1 flag in Makefile.config to configure and build Caffe without CUDA. This is helpful for cloud or cluster deployment.

5.2 CUDA and BLAS

Caffe requires the CUDA nvcc compiler to compile its GPU code and CUDA driver for GPU operation. To install CUDA, go to the NVIDIA CUDA website and follow installation instructions there. Install the library and the latest standalone driver separately; the driver bundled with the library is usually out-of-date. Warning! The 331.* CUDA driver series has a critical performance issue: do not use it.

For best performance, Caffe can be accelerated by NVIDIA cuDNN. Register for free at the cuDNN site, install it, then continue with these installation instructions. To compile with cuDNN set the USE_CUDNN := 1 flag set in your Makefile.config.

Caffe requires BLAS as the backend of its matrix and vector computations. There are several implementations of this library. The choice is yours:

  • ATLAS: free, open source, and so the default for Caffe.
  • Intel MKL: commercial and optimized for Intel CPUs, with free licenses.

1.Install MKL.
2.Set up MKL environment (Details: Linux, OS X). Example: source /opt/intel/mkl/bin/mklvars.sh intel64
3.Set BLAS := mkl in Makefile.config

  • OpenBLAS: free and open source; this optimized and parallel BLAS could require more effort to install, although it might offer a speedup.

1.Install OpenBLAS
2.Set BLAS := open in Makefile.config

sudo apt install libopenblas-dev

5.3 Python Caffe (optional)

The main requirements are numpy and boost.python (provided by boost). pandas is useful too and needed for some examples. You can install the dependencies with

for req in $(cat requirements.txt); do pip install $req; done

but we suggest first installing the Anaconda Python distribution, which provides most of the necessary packages, as well as the hdf5 library dependency.

To import the caffe Python module after completing the installation, add the module directory to you $PYTHONPATH by export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH or the like. You should not import the module in the caffe/python/caffe directory!

sudo vim /etc/profile
export PYTHONPATH=/home/user_name/caffe/python:$PYTHONPATH
source /etc/profile

Caffe’s Python interface works with Python 2.7. Python 3.3+ should work out of the box without protobuf support. For protobuf support please install protobuf 3.0 alpha (https://developers.google.com/protocol-buffers/). Earlier Pythons are your own adventure.

5.4 Compilation

Caffe can be compiled with either Make or CMake. Make is officially supported while CMake is supported by the community.

  • 1 Compilation with Make
    Configure the build by copying and modifying the example Makefile.config for your setup. The defaults should work, but uncomment the relevant lines if using Anaconda Python.
cp Makefile.config.example Makefile.config
# Adjust Makefile.config (for example, if using Anaconda Python, or if cuDNN is desired)
make all
make test
make runtest
  • For CPU & GPU accelerated Caffe, no changes are needed.
  • For cuDNN acceleration using NVIDIA’s proprietary cuDNN software, uncomment the USE_CUDNN := 1 switch in Makefile.config. cuDNN is sometimes but not always faster than Caffe’s GPU acceleration.
  • For CPU-only Caffe, uncomment CPU_ONLY := 1 in Makefile.config.

To compile the Python and MATLAB wrappers do make pycaffe and make matcaffe respectively. Be sure to set your MATLAB and Python paths in Makefile.config first!

Distribution: run make distribute to create a distribute directory with all the Caffe headers, compiled libraries, binaries, etc. needed for distribution to other machines.

Speed: for a faster build, compile in parallel by doing make all -j8 where 8 is the number of parallel threads for compilation (a good choice for the number of threads is the number of cores in your machine).

Now that you have installed Caffe, check out the MNIST tutorial and the reference ImageNet model tutorial.

  • 2 CMake Build
    In lieu of manually editing Makefile.config to configure the build, Caffe offers an unofficial CMake build thanks to @Nerei, @akosiorek, and other members of the community. It requires CMake version >= 2.8.7. The basic steps are as follows:
mkdir build
cd build
cmake ..
make all
make install
make runtest
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tres_Lu

您的鼓励将是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值