CUDA Toolkit
下载:https://developer.nvidia.com/cuda-downloads
安装:
- 方法一:使用distribution-specific packages 安装 CUDA,ubuntu对应deb安装包
- 方法二:使用distribution-independent package 安装 CUDA,即run安装包
推荐使用方法一安装,比较简单快键,只要以下几条命令即可。方法二见参考。
1.Install repository meta-data
sudo apt-get install build-essential
sudo dpkg -i cuda-repo-<distro>_<version>_<architecture>.deb
2.Update the Apt repository cache
sudo apt-get update
3.Install CUDA
sudo apt-get install cuda
4.Environment Setup
修改etc/profile或~/.bashrc,source /etc/profile(对应profile)或sudo ldconfig(对应bashrc)使之生效
export PATH=/usr/local/cuda-7.0/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-7.0/lib64:$LD_LIBRARY_PATH
5.Install Writable Samples
cuda-install-samples-7.0.sh <dir>
6.Reboot
重启系统
参考:
- Getting Started Linux :: CUDA Toolkit Documentation
- Ubuntu12.04配置NVIDIA cuda5.5经验帖 - Rachel Zhang的专栏 - 博客频道 - CSDN.NET
- Caffe + Ubuntu 15.04 + CUDA 7.0 新手安装配置指南
- fbcunn/INSTALL.md at master · facebook/fbcunn · GitHub
OpenCV
1.Install required packages
sudo apt-get update
sudo apt-get install build-essential checkinstall cmake pkg-config yasm libtiff4-dev libjpeg-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev python-dev python-numpy libtbb-dev libqt4-dev libgtk2.0-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils libeigen3-dev
sudo add-apt-repository ppa:jon-severinsson/ffmpeg
sudo apt-get update
sudo apt-get install ffmpeg
sudo apt-get install frei0r-plugins
2.Build and install OpenCV
mkdir release
cd release
cmake -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=OFF -D INSTALL_PYTHON_EXAMPLES=OFF -D BUILD_EXAMPLES=OFF -D WITH_QT=ON -D WITH_OPENGL=ON -D ENABLE_FAST_MATH=1 -D WITH_CUDA=ON -D CUDA_FAST_MATH=1 -D WITH_CUBLAS=1 -D CUDA_GENERATION=Auto -D WITH_GSTREAMER_0_10=OFF ..
检查cmake的输出,确认CUDA和CUBLAS将被安装
-- Use Cuda: YES (ver 6.5)
-- Use OpenCL: YES
--
-- NVIDIA CUDA
-- Use CUFFT: YES
-- Use CUBLAS: YES
-- USE NVCUVID: NO
-- NVIDIA GPU arch: 20
-- NVIDIA PTX archs:
-- Use fast math: YES
If everything is correct you can install OpenCV:
make
sudo make install
如果使用pkgconfig,需要修改环境变量:
echo '/usr/local/lib' | sudo tee -a /etc/ld.so.conf.d/opencv.conf
sudo ldconfig
printf '# OpenCV\nPKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig\nexport PKG_CONFIG_PATH\n' >> ~/.bashrc
source ~/.bashrc
注意:
- 其中有几个example没有编译,改为
OFF
-D WITH_GSTREAMER_0_10=OFF
是为了防止出现error: ‘GstMiniObjectClass’ does not name a type
-D CUDA_GENERATION=Auto
是为了防止出现nvcc fatal : Unsupported gpu architecture 'compute_11'
参考:
- Ubuntu 14.04: install OpenCV with CUDA
- Build OpenCV with CUDA support - Stack Overflow
- Ubuntu 编译opencv出错:‘GstMiniObjectClass’ does not name a type 解决方法 - jdpshq的专栏 - 博客频道 - CSDN.NET
Test
测试一下OpenCV的功能,如opencv-2.4.10/samples/gpu/中的surf_keypoint_matcher.cpp
这里提供两种方法:
- 使用cmake建立工程
新建CMakeLists.txt
,内容如下:
PROJECT(test)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
FIND_PACKAGE( OpenCV REQUIRED )
ADD_EXECUTABLE(surf surf_keypoint_matcher.cpp)
TARGET_LINK_LIBRARIES(surf ${OpenCV_LIBS})
mkdir build
cd build
cmake ..
./surf --left ../tsucuba_left.png --right ../tsucuba_right.png
- 使用命令行g++编译
g++ surf_keypoint_matcher.cpp -o surf `pkg-config --cflags --libs opencv` -L/usr/local/cuda/lib64
./surf --left tsucuba_left.png --right tsucuba_right.png
注:
不加上-L/usr/local/cuda/lib64
会出错,因为在/usr/local/lib/pkgconfig/opencv.pc
中并没有CUDA library的信息。
/usr/bin/ld: cannot find -lcufft
/usr/bin/ld: cannot find -lcublas
/usr/bin/ld: cannot find -lnpps
/usr/bin/ld: cannot find -lnppi
/usr/bin/ld: cannot find -lnppc
/usr/bin/ld: cannot find -lcudart