Centos集成GTX-1080Ti显卡搭建深度学习环境全过程

来源:互联网 发布:沈阳202医院网络预约 编辑:程序博客网 时间:2024/04/27 14:10

Centos集成GTX-1080Ti显卡搭建深度学习环境全过程

在一个由N多台普通的不能再普通的机器攒凑起来的机箱中,搭载了最强核心——NVIDIA GeForce GTX 1080 Ti。我们的深度学习环境搭建之旅,将从如何攒凑这款独一无二的机箱开始,一点一点完成从简单电脑维修装配到逼格爆棚的Deep Learning的华丽丽转身。

一、安装系统 centos 7.3

使用 UltraISO 制作的U盘启动盘,镜像使用的是 CentOS-7-x86_64-DVD-1611.iso

安装过程略去。。。

二、准备显卡及相关驱动

硬件准备

由于GeForce GTX-1080Ti需要两个8pin接口供电,且比较耗能,功率为280w,公司没有闲置的能满足需求的大功率电源,即使有的话,8pin的供电的接口一般都不够用(小主板一般为4pin)。最终方案是显卡单独供电,从几个不用的电源上攒出来包含两个8pin(此8pin接口实为3根黄线和3根黑线,另外两根黑线为黑线接口引线串联起来,见下图)接口的电源(额定功率为270w),另外一个给机箱主板供电的电源(额定功率270w)除去本身的24pin接口和硬盘供电接口外,再拼接一条8接口(此8pin实为4根黄线和4根黑线组成),以弥补供电不足(PS: 电源功率足够大的话阔以忽略以上折腾活儿)。

显卡8pin接口

主板辅助电源8pin接口

最终机箱全貌

补充:目前主板供电的接口主要有24针与20针两种,在中高端的主板上,一般都采用24PIN的主板供电接口设计,低端的产品一般为20PIN。不论采用24PIN和20PIN,其插法都是一样的 。

另外随着CPU、显卡等功耗增大,主板上增加了 4pin或8pin 的辅助供电

软件准备

所需文件一览:

-rwxr-xr-x 1 root root   97546170 9月  20 10:24 cuda_8.0.61.2_linux-run            cuda补丁      -rwxr-xr-x 1 root root 1465528129 9月  20 10:27 cuda_8.0.61_375.26_linux-run       cuda8.0驱动  -rw-r--r-- 1 root root  201134139 9月  20 10:27 cudnn-8.0-linux-x64-v6.0.tgz       cudnn6.0(针对cuda8.0)-rwxr-xr-x 1 root root   80803084 9月  20 10:27 NVIDIA-Linux-x86_64-384.69.run     NVIDIA驱动

1.安装前准备工作

  • 更新系统(时间较长,耐心等待)

    # yum update# reboot
  • 安装kernel-devel Package

    # yum install kernel-devel-$(uname -r) gcc
  • 禁用 nouveau 驱动

    首先说明下什么是Nouveau,为什么有些系统安装N卡驱动的时候会提示“ERROR: The Nouveau kernel driver is currently in use by your system. This Nouveau是由第三方为NVIDIA显卡开发的一个开源3D驱动,也没能得到NVIDIA的认可与支持。虽然Nouveau Gallium3D在游戏速度上还远远无法和NVIDIA官方私有驱动相提并论,不过确让Linux更容易的应对各种复杂的NVIDIA显卡环境,让用户安装完系统即可进入桌面并且有不错的显示效果,所以,很多Linux发行版默认集成了Nouveau驱动,在遇到NVIDIA显卡时默认安装。企业版的Linux更是如此,几乎所有支持图形界面的企业Linux发行版都将Nouveau收入其中。

    # echo 'blacklist nouveau' >> /etc/modprobe.d/blacklist.conf

    验证是否成功禁用nouveau,重启后执行如下命令没有输出则没有启用nouveau。

    # lsmod | grep nouveau

    以上方法如果行不通的话,可能的原因是因为系统启动方式不太一样,具体不是特别了解,可采用如下方式禁用 nouveau 驱动:

    • Disable X Windows

      # sudo systemctl set-default multi-user.target
    • Remove Nouveau

      # sudo rpm -e xorg-x11-drivers xorg-x11-drv-nouveau
    • Blacklist Nouveau

      a) edit /etc/modprobe.d/blacklist.conf and add line: blacklist nouveau
      b) edit /etc/default/grub and append to GRUB_CMDLINE_LINUX: rdblacklist=nouveau
      (After step 5, if the nouveau driver is still running, try rd.driver.blacklist=nouveau here instead. Sometimes rdblacklist=nouveau fails.)
      **-If you have an encrypted root drive, remove “rhgb” from GRUB_CMDLINE_LINUX.**This will allow you to interact with the encryption passphrase prompt, since Plymouth doesn’t seem to run without a framebuffer friendly video driver loaded.
      c) Two options for booting now days are BIOS and EFI (Make sure that /boot and/or /boot/efi are mounted before executing these commands.)
      -If you chose BIOS boot run this command:
      grub2-mkconfig -o /boot/grub2/grub.cfg
      -If EFI boot on CentOS:
      grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg
      -If EFI boot on RHEL/Scientific Linux:
      grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg

    • Reboot and Install NVidia Driver (安装显卡驱动)

    • Enable X Windows (If Desired) (安装完显卡驱动后执行)

      # sudo systemctl set-default graphical.target# sudo reboot
  • 备份 initramfs 文件(系统内核镜像文件)

    initramfs文件初探

    Linux内核在初始化之后会执行init进程,而init进程会挂载我们的根文件系统,但由于init程序也是在根文件系统上的,所以这就有了悖论。Linux采用两步走的方法来解决这个问题。Linux2.6版以前的方法是:除了内核vmlinuz之外还有一个独立的initrd.img映像文件,其实它就是一个文件系统映像,linux内核在初始化后会mount initrd.img作为一个临时的根文件系统,而init进程就是在initrd.img里的,然后init进程会挂载真正的根文件系统,然后umount initrd.img。但Linux2.6内核的实现方式却不太一样,虽然完成的功能是一样的。Linux2.6采用initramfs。initramfs:init ram filesystem,它是一个cpio格式的内存文件系统,制作的方法有两个,一个是http://blog.csdn.net/htttw/article/details/7215858介绍的,但这样做出来的initramfs是和内核vmlinuz分开的,因此我们需要在grub里写上initramfs的路径。而另一种方法是把内核和initramfs制作在一起成为一个文件,方法是在linux源码make menuconfig,然后General setup–>选择Initial RAM filesystem and RAM disk (initramfs/initrd) support,然后在Initramfs source file(s)里输入我们的initramfs目录,然后make bzImage。这种方法做出来的内核就只有一个文件,不需要指定initramfs了。

    # sudo mv /boot/initramfs-$(uname -r).img /boot/initramfs-$(uname -r).img.bak
  • 重建 initramfs 文件(更新系统kernel后正常重启会在/boot目录下自动生成新内核镜像,可选)

    # sudo dracut -v /boot/initramfs-$(uname -r).img $(uname -r)
  • 关闭图形界面(如果有VNC服务, 请先关闭vnc服务),执行

    # sudo systemctl disable gdm# sudo reboot 

2.安装显卡驱动

下载对应型号的 NVidia 驱动,此处下载最新支持 GTX1080 Ti 的驱动 NVIDIA-Linux-x86_64-384.69.run。 点击 官方下载。

下载完后执行, 按提示选择进行安装:

# ./NVIDIA-Linux-x86_64-384.69.run

配置X11 (在安装驱动的时候有自动执行 nvidia-xconfig 选项, 如果选yes可以确定跳过此步骤)

# nvidia-xconfig WARNING: Unable to locate/open X configuration file.New X configuration file written to '/etc/X11/xorg.conf'

启动图形界面

# systemctl enable gdmCreated symlink from /etc/systemd/system/display-manager.service to /usr/lib/systemd/system/gdm.service.# reboot

验证

  • 执行命令查看显卡状态

    # nvidia-smi
  • 在页面右上角的 应用程序-> 其他 -> Nvidia 选项中,有如下 GPU 0 选项,说明安装成功(找的样图,型号和驱动不同

【注】: 如果中途有问题, 可以执行 ./NVIDIA-Linux-x86_64-384.69.run --uninstall 进行卸载.

3.安装cuda

【cuda-8.0驱动下载】 【cuda-8.0补丁下载】 【其他版本驱动下载】

科普下cuda,简单来说就是能使用GPU并行计算的平台和编程模型,能极大提升计算性能。

Q: What is CUDA?
CUDA® is a parallel computing platform and programming model that enables dramatic increases in computing performance by harnessing the power of the graphics processing unit (GPU).
Since its introduction in 2006, CUDA has been widely deployed through thousands of applications and published research papers, and supported by an installed base of hundreds of millions of CUDA-enabled GPUs in notebooks, workstations, compute clusters and supercomputers. Applications used in astronomy, biology, chemistry, physics, data mining, manufacturing, finance, and other computationally intense fields are increasing using CUDA to deliver the benefits of GPU acceleration.

  • 再次进入text mode

    # init 3
  • 安装依赖库:

    # yum -y install gcc-c++# yum -y install epel-release# yum -y install --enablerepo=epel dkms# yum -y install kernel-devel-$(uname -r) kernel-headers-$(uname -r)
  • 安装cuda-8.0驱动(一定注意下图安装过程中的红框部分,跳过安装NVIDIA驱动的步骤(输入n)):

    # ./cuda_8.0.61_375.26_linux-run 

  • 安装cuda补丁

    # ./cuda_8.0.61.2_linux-run  
  • 设置环境变量

    # vim /etc/profile

    最后一行添加

    export PATH="/usr/local/cuda-8.0/bin:$PATH"export LD_LIBRARY_PATH="/usr/local/cuda-8.0/lib64"export CUDA_HOME="/usr/local/cuda"   

    使之生效

    # source /etc/profile      

4.安装cuDNN

科普下cudnn,简单点说cuDNN是NVIDIA深度学习中的深层神经网络库,并通过cuda,我们就可以直接利用Nvidia GPU的并行计算能力进行深度学习训练任务了。

The NVIDIA CUDA® Deep Neural Network library (cuDNN) is a GPU-accelerated library of primitives for deep neural networks. cuDNN provides highly tuned implementations for standard routines such as forward and backward convolution, pooling, normalization, and activation layers. cuDNN is part of the NVIDIA Deep Learning SDK.

  • 下载cudnn-8.0-linux-x64-v6.0.tgz

    下载cuDNN,需要创建一个NVIDIA账号,cuDNN Home Page.

    不想注册的话,可以通过如下方式获取

    wget http://developer.download.nvidia.com/compute/redist/cudnn/v6.0/cudnn-8.0-linux-x64-v6.0.tgz
  • 解压并拷贝相关文件,完成安装

    # tar -xzvf cudnn-8.0-linux-x64-v6.0.tgz # 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*

三、python 3.6.2编译安装及配置

1.安装依赖库

yum install gcc-c++ sqlite-devel gcc zlib zlib-devel bzip2-devel  openssl-devel ncurses-devel tcl tcl-devel tk tk-devel

2.安装Python3

【python-3.6.2 源码下载】

将python 源码放置到一个无害的目录,执行命令:

# sudo mkdir /usr/local/python3# tar -xzvf Python-3.6.2.tgz# cd Python-3.6.2 # 进入解压目录# sudo ./configure --prefix=/usr/local/python3  --enable-optimizations # 指定创建的目录

修改Setup文件

vim Modules/Setup

修改结果如下(将行开头的注释“#”去掉):

# Socket module helper for socket(2)_socket socketmodule.c# Socket module helper for SSL support; you must comment out the other# socket line above, and possibly edit the SSL variable:#SSL=/usr/local/ssl_ssl _ssl.c \-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \-L$(SSL)/lib -lssl -lcrypto

编译安装(时间较长,耐心等待)

sudo makesudo make install

3.与Python 2.7.5 共存

创建链接python3,这样就可以通过 python 命令使用 Python 2,python3 来使用 Python 3。

sudo ln -s /usr/local/python3/bin/python3 /usr/bin/python3

安装 pip

sudo ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

pip升级

pip3 install --upgrade pip

四、python机器学习和深度学习库安装

1.安装机器学习相关库

pip3 install -U wheelpip3 install -U numpy pip3 install -U scipypip3 install -U matplotlibpip3 install -U pandaspip3 install -U scikit-learnpip3 install -U ipython jupyter notebookpip3 install -U spyderpip3 install -U dill

2.安装深度学习相关库

yum install -y python-numpy python-dev cmake zlib1g-dev libjpeg-dev xvfb libav-tools xorg-dev python-opengl libboost-all-dev libsdl2-dev swigpip3 install -U h5pypip3 install -U tensorflow-gpupip3 install -U scikit-imagepip3 install -U keraspip3 install -U keras-rl  # 强化学习(可选)pip3 install -U keras-vis # 可视化(可选)pip3 install -U gym[all] (可选,安装时稍微有些麻烦,暂时没用到,未安装)

五、jupyter配置

1.生成默认配置

# cd /usr/local/python3/bin# ./jupyter notebook --generate-configWriting default config to: /home/user/.jupyter/jupyter_notebook_config.py

2.编辑配置文件

# vim /home/user/.jupyter/jupyter_notebook_config.py  c.NotebookApp.ip = '*'               #所有绑定服务器的IP都能访问c.NotebookApp.port = 8899            #将端口设置为自己喜欢的吧,默认是8888c.NotebookApp.open_browser = False   #我们并不想在服务器上直接打开Jupyter Notebook,所以设置成Falsec.NotebookApp.notebook_dir = '/data/bigdata/notebooks/'   #设置notebook工作目录

3. 设置Jupyter Notebook密码

在python控制台 ,输入

In [1]: from notebook.auth import passwdIn [2]: passwd()Enter password: *******Verify password:*******Out[2]: 'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'   #密码密文

修改配置密码信息

# vim /home/you/.jupyter/jupyter_notebook_config.py  c.NotebookApp.password = u'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'  #注意前面的u

4.启动

设置为开机自启动服务

# cd ~# mkdir services# vim jupyter.service

在 jupyter.service 中输入

[Unit]Description=Jupyter notebook env[Service]User=rootGroup=rootEnvironment=LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64 #添加此环境变量,避免启动tensorFlow时找不到某些类库ExecStart=/usr/local/python3/bin/jupyter notebook --allow-root[Install]WantedBy=multi-user.target 

保存退出

# cp ./jupyter.service /usr/lib/systemd/system/jupyter.service# ln -s /usr/lib/systemd/system/jupyter.service jupyter.service  #同时修改此两个文件

开机自启动

# systemctl enable jupyter.service

启动服务

# systemctl start jupyter.service

PS:单独启动一次可通过如下方式(必须在用户目录下, 如/root目录下执行)

nohup /usr/local/python3/bin/jupyter notebook --allow-root  >/dev/null 2>&1 &

六、测试下环境

至此,深度学习所用到的环境搭建完成,下面我们就拿训练人脸识别的模型做个测试吧。

bingo,这一天的工作,值了。

问题

1.运行 init 3 报错

注意: 如果报如下错误,可能是因为/boot下空间不足了

[root@localhost GPU]# init 3Broadcast message from systemd-journald@localhost.localdomain (Wed 2017-09-20 11:26:40 CST):dracut[11820]: dracut: creation of /boot/initramfs-3.10.0-693.2.2.el7.x86_64kdump.img failedMessage from syslogd@localhost at Sep 20 11:26:40 ... dracut:dracut: creation of /boot/initramfs-3.10.0-693.2.2.el7.x86_64kdump.img failed

查看/boot 空间

[root@localhost boot]# df /boot文件系统        1K-块   已用  可用 已用% 挂载点/dev/sdb1      255724 253488  2236  100% /boot

查看老版本的内核版本

[root@localhost boot]# rpm -qa|grep kernelkernel-3.10.0-514.26.2.el7.x86_64kernel-tools-libs-3.10.0-693.2.2.el7.x86_64kernel-headers-3.10.0-693.2.2.el7.x86_64kernel-tools-3.10.0-693.2.2.el7.x86_64kernel-devel-3.10.0-693.2.2.el7.x86_64kernel-3.10.0-693.2.2.el7.x86_64kernel-3.10.0-514.el7.x86_64abrt-addon-kerneloops-2.1.11-48.el7.centos.x86_64

卸载老版本内核

[root@localhost boot]# rpm -e kernel-3.10.0-514.el7.x86_64 kernel-3.10.0-514.26.2.el7.x86_64

参考

https://linuxconfig.org/nvidia-geforce-driver-installation-on-centos-7-linux-64-bit

http://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#redhat-installation

http://www.pyimagesearch.com/2016/07/04/how-to-install-cuda-toolkit-and-cudnn-for-deep-learning

https://www.linkedin.com/pulse/20140808222919-219659043-rhel-centos-7-and-nvidia-drivers

阅读全文
0 0
原创粉丝点击