caffe,这是是数据组需要做一些大数据模型的训练(深度学习), 要求 服务器+显卡(运算卡), 刚开始老板让买的牌子是泰坦的(这是2年前的事情了). 后来买不到这个牌子的,(jd,tb)看过丽台的,看过gtx系列的哪个型号来着, 也不合适,后来买的特斯拉显卡
[查了下一些知名的显卡牌子](https://www.zhihu.com/question/42110806)
![](https://ws1.sinaimg.cn/large/9e792b8fgy1fo7vm1f8zoj214t0mdgz1.jpg)
最终敲定买特斯拉系列Tesla K20Xm.
![](https://ws1.sinaimg.cn/large/9e792b8fgy1fo7vx49iscj20hg0dc449.jpg)
第一次买的这个玩意可以识别,也可以跑,就是跑着跑着发烧服务器就自动重启了. 后来联系厂家后寄回,他们发了个有风扇的给我, 之后跑起来就没啥问题了.
```
Graphics Card Interface: PCI-E
Graphics Coprocessor: Tesla K20
Graphics Card Ram: 5 GB
Graphics Ram Type: GDDR5
```
这个卡好像是2-32w左右吧. 忘记了.总之上万了.
caffe安装过程相当的麻烦,步骤较多. 后台发现[docker可以轻易的安装](https://hub.docker.com/r/bvlc/caffe/tags/),免费了很多烦恼
![](https://ws1.sinaimg.cn/large/9e792b8fgy1fo7w3i1pwvj20nx0azq3e.jpg)
不过经历了这些麻烦后,发现老当益壮, 无论什么linux新的服务都不惧了(因为当初接触linux时间不是很长)
# 1. 环境及安装前检查
```
- 服务器硬件:
戴尔T630,类似台式机一样,立式的. CPU32核/64G内存/4G硬盘
- 显卡:
root@shendu:~# lspci |grep -i nvidia
02:00.0 3D controller: NVIDIA Corporation GK110GL [Tesla K20Xm] (rev a1)
- 软件:
Ubuntu14.04 server64bit版本:ubuntu-14.04.5-server-amd64.iso
Python 2.7.12
root@shendu:~# uname -a
Linux shendu.pp100.net 4.4.0-31-generic #50~14.04.1-Ubuntu SMP Wed Jul 13 01:07:32 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
说明:购买服务器和显卡就费了不少时间,蛋疼点是两者兼容性,不太好买,买回来后有些主板可能不兼容导致lspci|grep –i nvidia没结果,就是主板识别不了显卡。折腾了好久返回厂家换了一次,搞好了。
- 安装说明:
依赖+cudnn+ Anaconda2 +caffe
安装过程统一用root账号,统一安装在 /root/caffe目录下。
- 安装前检查:
*首先验证你是否有nvidia的显卡(http://developer.nvidia.com/cuda-gpus这个网站查看你是否有支持gpu的显卡):
$ lspci | grep -i nvidia
*查看linux发行版本
uname -m && cat /etc/*release*看gcc版本
$ gcc --version
说明:是cuda 32位尚未找到合适的安装包,所以这里的linux要64位的)
```
# 2. 安装依赖
```
apt-get install build-essential # basic requirement
apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler #required by caffe
apt-get install libatlas-base-dev -y
apt-get install libprotobuf-dev -y
apt-get install libleveldb-dev -y
apt-get install libsnappy-dev -y
apt-get install libopencv-dev -y
apt-get install libboost-all-dev -y
apt-get install libhdf5-serial-dev -y
apt-get install libgflags-dev -y
apt-get install libgoogle-glog-dev -y
apt-get install liblmdb-dev -y
apt-get install protobuf-compiler –y
apt-get install libatlas-base-dev #安装Intel MKL 或Atlas,默认我们用显卡来运算,软件默认就是Atlas,保持不变
```
# 3. 安装cuda7.5
*在cuda官网(https://developer.nvidia.com/cuda-downloads#linux )下载对应版本
![](https://ws1.sinaimg.cn/large/9e792b8fgy1fo7uf0imv8j20bq04t74z.jpg)
下载并安装
![](https://ws1.sinaimg.cn/large/9e792b8fgy1fo7ufavvyzj207700jjra.jpg)
```
dpkg -i cuda-repo-ubuntu1404-7-5-local_7.5-18_amd64.deb
```
安装cuda(安装cuda的同时就会把显卡驱动也全部安装好)
```
apt-get update
apt-get install cuda –y
```
# 4. 安装cudnn
```
tar xf cudnn-7.5-linux-x64-v5.0-ga.tgz #自行下载
cd cuda
cp lib64/* /usr/local/cuda/lib64/
cp include/cudnn.h /usr/local/cuda/include/
- 更新软连接
cd /usr/local/cuda/lib64/
sudo chmod +r libcudnn.so.5.0.5
sudo ln -sf libcudnn.so.5.0.5 libcudnn.so.5
sudo ln -sf libcudnn.so.5 libcudnn.so
sudo ldconfig
echo '/usr/local/cuda/lib64' >> /etc/ld.so.conf.d/cuda.conf
echo '/root/anaconda2/lib' >> /etc/ld.so.conf
- 统一设置环境变量
export PATH="/usr/local/cuda/bin:$PATH"
export LD_LIBRARY_PATH="/root/anaconda2/lib:$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH=/usr/local/cuda-7.5/lib64:$LD_LIBRARY_PATH
export PYTHONPATH=/root/caffe/caffe-master/python
```
# 5. 安装CUDA SAMPLE
```
cd /usr/local/cuda/samples
make all –j12 #此处会报错,看后文解决.
reboot
cd /usr/local/cuda/samples/bin/x86_64/linux/release
./deviceQuery #如果出现显卡信息, 则驱动及显卡安装成功
```
# 6. 安装opencv
```
https://github.com/bearpaw/Install-OpenCV #下载zip包,拖进Ubuntu解压到/root/caffe
下载opencv-2.4.9.zip 到/root/caffe下 #自行网上下载
root@shendu:~/caffe# ls
caffe-master caffe-master.zip Install-OpenCV Install-OpenCV-master.zip opencv-2.4.9 opencv-2.4.9.zip
root@shendu:~/caffe# pwd
/root/caffe
#安装依赖
/root/caffe/Install-OpenCV-master/Ubuntu/dependencies.sh
#安装脚本修改
root@shendu:~/caffe/Install-OpenCV/Ubuntu/2.4# cat opencv2_4_9.sh
arch=$(uname -m)
if [ "$arch" == "i686" -o "$arch" == "i386" -o "$arch" == "i486" -o "$arch" == "i586" ]; then
flag=1
else
flag=0
fi
echo "Installing OpenCV 2.4.9"
mkdir OpenCV
cd OpenCV
echo "Removing any pre-installed ffmpeg and x264"
sudo apt-get -y remove ffmpeg x264 libx264-dev
echo "Installing Dependenices"
sudo apt-get -y install libopencv-dev
sudo apt-get -y install build-essential checkinstall cmake pkg-config yasm
sudo apt-get -y install libtiff4-dev libjpeg-dev libjasper-dev
sudo apt-get -y install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev
sudo apt-get -y install python-dev python-numpy
sudo apt-get -y install libtbb-dev
sudo apt-get -y install libqt4-dev libgtk2.0-dev
sudo apt-get -y install libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev
sudo apt-get -y install x264 v4l-utils ffmpeg
# sudo apt-get -y install libgtk2.0-dev # duplicate
echo "Downloading OpenCV 2.4.9"
#wget -O OpenCV-2.4.9.zip http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.9/opencv-2.4.9.zip/download #注释掉
echo "Installing OpenCV 2.4.9"
unzip OpenCV-2.4.9.zip
cd opencv-2.4.9
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D WITH_CUBLAS=OFF -D WITH_CUDA=OFF -D WITH_CUFFT=OFF .. #添加这三项
make -j12
sudo make install
sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig
cd ../../..
rm -rf OpenCV
echo "OpenCV 2.4.9 ready to be used"
mkdir –p /root/caffe/Install-OpenCV/Ubuntu/2.4/
cp /root/caffe/opencv-2.4.9.zip /root/caffe/Install-OpenCV/Ubuntu/2.4/OpenCV/OpenCV-2.4.9.zip
/root/caffe/Install-OpenCV/Ubuntu/2.4/opencv2_4_9.sh
```
# 7. 安装Caffe所需要的Python环境
```
https://www.continuum.io/downloads#all
bash /root/Anaconda2-4.2.0-Linux-x86_64.sh #一路yes即可。
reboot
cd /root/caffe/caffe-master/python
for req in $(cat requirements.txt); do pip install $req; done #安装前先看看你的echo $PATH正常不。有问题重启下机器。
```
# 8. 安装caffe(操作前重启下机器,确保$PATH正常)
```
cd /root/caffe/caffe-master
cp Makefile.config.example Makefile.config
ldconfig
conda remove libtiff
make all –j12
make test
make runtest
make pycaffe
```
# 9. 测试验证
```
1.
sudo sh ./examples/mnist/create_mnist.sh
生成mnist-train-leveldb/ 和 mnist-test-leveldb/文件夹,这里包含了LDB格式的数据集
注:新版caffe都需要从根目录上执行,如果使用下列命令执行:
# cd examples/mnist
# sudo sh ./create_mnist.sh
可能会遇到这个错误:./create_mnist.sh: 16: ./create_mnist.sh: build/examples/mnist/convert_mnist_data.bin: not found,全路径试试。
2,验证python
import caffe #此处不要用ipython,可以用bpython。
3,查看显卡驱动版本
查看显卡的驱动版本
cat /proc/driver/nvidia/version
4) Verify the driver version:
$ cat /proc/driver/nvidia/version
5) Check CUDA driver version:
$ nvcc -V
cd /usr/local/cuda/samples/bin/x86_64/linux/release
./deviceQuery
./bandwidthTest
nvidia-settings –v
```
```
查看gpu温度等:
nvidia-smi
nvidia-smi -a|grep "GPU Current Temp"|awk -F" " '{print $5}'
nvidia-smi -a|grep -A 4 "Utilization"|sed -n '2p'|awk -F':' '{print $2}'|awk -F' ' '{print $1}'
nvidia-smi -a|grep -A 4 "Utilization"|sed -n '3p'|awk -F':' '{print $2}'|awk -F' ' '{print $1}'
gpu当前温度
UserParameter=gpu_temper,nvidia-smi -a|grep "GPU Current Temp"|awk -F" " '{print $5}'
gpu使用率:
UserParameter=gpu_ulim,nvidia-smi -a|grep -A 4 "Utilization"|sed -n '2p'|awk -F':' '{print $2}'|awk -F' ' '{print $1}'
UserParameter=gpu_mem_ulim,nvidia-smi -a|grep -A 4 "Utilization"|sed -n '3p'|awk -F':' '{print $2}'|awk -F' ' '{print $1}'
```
# 10. 第5步报错解决
![](https://ws1.sinaimg.cn/large/9e792b8fgy1fo7uibmuowj20id01i3z7.jpg)
```
原因: /usr/local/cuda/samples下是nvidia-352,而我的NVIDIA是367,需要批量替换掉sample下的不正确的文件(即将sample下所有文件包含nvidia-352关键字替换为nvidia-367),我写了个脚本处理这事.
cd /usr/local/cuda/samples
grep -r "nvidia-352" . > aaa.txt
root@shendu:/usr/local/cuda/samples# cat t.sh
for i in `cat aaa.txt`;do
sed -i 's#nvidia-352#nvidia-367#g' $i
done
sh t.sh
参考:https://devtalk.nvidia.com/default/topic/769578/cuda-6-5-cannot-find-lnvcuvid/
```
# 11. 监控gpu使用率
```
先决条件: 你需要安装好CUDA
echo $PATH 含有类似/usr/local/cuda/bin
Linux系统,在程序运行的时候,如何实时监控GPU卡的使用率呢?
在终端窗口输入如下命令:
# nvidia-smi -l
nvidia-smi(NVIDIA System Management Interface)可以实时监控GPU卡的使用情况,-l是其参数,用于循环显示。
```
下图是插有1片K20卡的机器运行nvidia-smi-l后的显示示意图:
![](https://ws1.sinaimg.cn/large/9e792b8fgy1fo7uip23n5j20h807jtar.jpg)
可以看到,这时没有调用GPU的程序在运行,GPU卡的使用率为0%。
有程序运行时,显示示意如下图。下面的大框显示程序名,GPU卡的使用率为59%。
![](https://ws1.sinaimg.cn/large/9e792b8fgy1fo7uixqhoij20hj07omxs.jpg)
K80是双核心卡,1片K80即2个GPU设备。没有使用率时的显示示意图如下:
![](https://ws1.sinaimg.cn/large/9e792b8fgy1fo7uj79y9cj20ht090mxt.jpg)
如果程序调用1个GPU设备,那么nvidia-smi –l 的执行结果如下图所示:
![](https://ws1.sinaimg.cn/large/9e792b8fgy1fo7ujda5mxj20hn08r3z6.jpg)
如果程序调用2个GPU设备,那么nvidia-smi –l 的执行结果如下图所示:
![](https://ws1.sinaimg.cn/large/9e792b8fgy1fo7ujlv1q8j20f507ftar.jpg)