Opencv CUDA编译用法介绍

本文将从多个方面对Opencv CUDA编译进行详细的阐述和解读。通过以下小标题,我们将详细介绍如何进行编译。

一、环境搭建

在使用CUDA进行加速之前,需要进行CUDA的环境搭建。在这里以Ubuntu操作系统为例,具体操作如下:

sudo apt-get install linux-headers-`uname -r` -y
sudo sh cuda_10.1.243_418.87.00_linux.run
vim ~/.bashrc
export PATH=$PATH:/usr/local/cuda-10.1/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-10.1/lib64
source ~/.bashrc

以上操作创建了CUDA运行所需的依赖项、安装了CUDA并将其添加到环境变量中。

二、Opencv编译

接下来,我们需要下载Opencv源码并进行编译。具体操作如下:

sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
git clone https://github.com/opencv/opencv.git
cd opencv
git checkout 3.4.5
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
make -j8
sudo make install

以上操作下载了Opencv源码、切换到3.4.5版本、创建了build文件夹并进行编译安装。

三、CUDA编译

接下来,我们需要编译CUDA。具体操作如下:

cd ~/NVIDIA_CUDA-10.1_Samples/
make -j8

以上操作进入CUDA Samples文件夹,并进行编译。如果编译成功,将生成许多样例可执行文件。

四、Opencv CUDA编译

在上述步骤顺利完成后,我们可以开始进行Opencv CUDA编译。具体操作如下:

cd ~/opencv/
mkdir build-cuda && cd build-cuda
cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
      -D WITH_CUDA=ON \
      -D CUDA_ARCH_BIN=6.1 \
      -D CUDA_ARCH_PTX=6.1 \
      -D WITH_CUBLAS=ON \
      -D BUILD_EXAMPLES=ON \
      -D OPENCV_GENERATE_PKGCONFIG=ON \
      ..
make -j8
sudo make install

以上操作创建了build-cuda文件夹并进行了Opencv CUDA编译。其中CUDA_ARCH_BIN和CUDA_ARCH_PTX可根据自己的显卡和CUDA版本进行调整。

五、Opencv CUDA测试

最后,我们可以进行Opencv CUDA测试以验证CUDA是否正常使用。具体操作如下:

cd ~/opencv/samples/gpu
./gpu-template

以上操作进入Opencv GPU示例文件夹并运行gpu-template,如果输出如下内容,则可以证明CUDA正常使用:

Device 0: "GeForce GTX 1080 Ti"
  CUDA Driver Version / Runtime Version          10.1 / 10.1
  CUDA Capability Major/Minor version number:    6.1
  Total amount of global memory:                 11175 MBytes (11720104960 bytes)
  (28) Multiprocessors, (128) CUDA Cores/MP:     3584 CUDA Cores
  GPU Max Clock rate:                            1582 MHz (1.58 GHz)
  Memory Clock rate:                             5505 Mhz
  Memory Bus Width:                              352-bit
  L2 Cache Size:                                 2883584 bytes
  Maximum Texture Dimension Size (x,y,z)         1D=(131072), 2D=(131072, 65536), 3D=(16384, 16384, 16384)
  Maximum Layered 1D Texture Size, (num) layers  1D=(32768), 2048 layers
  Maximum Layered 2D Texture Size, (num) layers  2D=(32768, 32768), 2048 layers
  Total amount of constant memory:               65536 bytes
  Total amount of shared memory per block:       49152 bytes
  Total number of registers available per block: 65536
  Warp size:                                     32
  Maximum number of threads per multiprocessor:  2048
  Maximum number of threads per block:           1024
  Max dimension size of a thread block (x,y,z): (1024, 1024, 64)
  Max dimension size of a grid size    (x,y,z): (2147483647, 65535, 65535)
  Maximum memory pitch:                          2147483647 bytes
  Texture alignment:                             512 bytes
  Concurrent copy and kernel execution:          Yes with 2 copy engine(s)
  Run time limit on kernels:                     Yes
  Integrated GPU sharing Host Memory:            No
  Support host page-locked memory mapping:       Yes
  Alignment requirement for Surfaces:            Yes
  Device has ECC support:                        Disabled
  CUDA Device Driver Mode (TCC or WDDM):         WDDM (Windows Display Driver Model)
  Device supports Unified Addressing (UVA):      Yes
  Device supports Compute Preemption:            No
  Supports Cooperative Kernel Launch:            No
  Supports MultiDevice Co-op Kernel Launch:      No
  Device PCI Domain ID / Bus ID / location ID:   0 / 131 / 0
  Compute Mode:
     < Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >

至此,Opencv CUDA编译完成并且可以正常使用。

本文链接:https://my.lmcjl.com/post/5674.html

展开阅读全文

4 评论

留下您的评论.