Linux Platform cuda+opencv3.4 Configuration

Source: Internet
Author: User
Tags nvcc

Tagged with: c + + int ext does not update source Color-o GPO Lib

Some time ago, the OPENCV3.4,TX2 update source failed to install the TX2, OPENCV internal many functions have implemented GPU acceleration, but we manually write the function, want to through the GPU acceleration will need to manually call Cuda for acceleration. The following describes the environment configuration of the Linux platform and the hybrid compilation with OpenCV.

Linux Platform cuda+opencv3.4 configuration 1 environment installation

First need to install OPENCV and CUDA environment installation, there is TX2 platform under OPENCV and Cuda reference Baidu. Note that TX2 comes with OpenCV2.14, and if you need to install a high version of OPENCV, you need to be aware of multiple versioning issues. After the installation is complete, you can compile the OpenCV routine to determine if the OpenCV is complete, CUDA installation can be entered in the terminal

Nvcc

If the terminal prints the following information, it indicates that the installation is complete

2 Cuda Compilation process

Cuda program through the NVCC compiler compiled into an executable file, Cuda executable files are two, respectively, the CPU code executed on the host, and the other part is the GPU code executed on the device, NVCC compiled instructions and gcc/g++ compiler almost, The basic instructions are as follows

NVCC--gpu-architecture=compute_62--gpu-code=-i/usr/local/cuda/include/-  c kernels.cu-o KERNELS.O

which

--gpu-architecture and--gpu-code Specify the computational power of the GPU, depending on the ability of the GPU to be modified, the GPU computing power can be found here (the NVIDIA website hangs, will be in Wikipedia to see it),-- gpu-architecture specifies what version of PTX can be generated in the future,--gpu-code specifies the code version that is generated immediately,--gpu-architecture and--gpu-code can have multiple values, This NVCC will put multiple versions of the code together in the Fatbin file,

-i/usr/local/cuda/include/Specifies the directory where Cuda contains header files, and if there are other header files, include the paths to these header files (see 3)

-C means that only the source files are compiled, not linked, and the. cu file is compiled to generate a. cu file

-o Specifies the filename of the. o file to be generated

There are other compilation options that can be consulted in the Nvidia NVCC compilation manual, which can be found in the Cuda installation directory

3 Cuda mixed with OPENCV compilation

Cuda and OpenCV mixed compilation is actually talking about. cu files and. c/.cpp files mixed compilation, the method of compiling more, the Nvidia official website has introduced the use of CMake to compile the method, point here, the online introduction of more is also the use of cmake to compile, There are fewer tutorials to compile with makefile, here's how to mix Cuda and C + + with makefile.

In fact, Cuda and C + + compiler principle is very simple, cuda C is only an expanded C language set, so I use the NVCC compiler and g++ respectively to the. cu file and the. cpp file to compile, generate. o file, and then use the g++ to link, the measurement method is feasible. Because my Cuda program is all in the. cu file, and then in the. cpp file through the extern cuda function declaration, like this:

/*main.cpp*/externintBuildmaps (float*model,float*input,float*output,intHeightintwidth);intMain () {/*Initialize Code*/... buildmaps (float*) model, (float*) input, output, width, width); /* Other*/    ......}
/*kernel.cu*//*GPU Kernel functions*/__global__voidKernel_compute (float*model,float*input,float*output) {    ......}intBuildmaps (float*model,float*input,float*output,intHeightintwidth) {     /*Init*/... kernel_compute<<<grid, block>>>(Dev_m, Dev_i, dev_o); /* Other*/    ......}

So here's the OpenCV code like normal OPENCV code write makefile on line, writing Cuda code makefile note, if the. cu file has OpenCV code, you need to add the NVCC when compiling

-dopencv ' pkg-config--cflags opencv-3.4'

Used to specify the OPENCV header file path, without errors, the compilation can be done using the g++ link.

Here I am using a makefile template, write a relatively rough, but the usual compilation of basic enough, small and medium-sized project makefile can be modified with this template, the template is not perfect place also please correct me

#cpp源文件路径SRCS= $ (wildcard./src/*. cpp) #cu文件路径CU_SRC= $ (wildcard./src/*. cu) #.o file Objs=$ (Patsubst%.cpp,%.o,$ (SRCS)) Cu_obj=$ (Patsubst%.cu,%.o,$ (CU_SRC)) #g++and NVCC compiler cxx= g++NVCC=nvcc#c/c++ Compilation Options--OpenCV header file path cflags= ' Pkg-config--cflags opencv-3.4' #C/c++ compile Options--current project header file path and optimization options (-O3) CFLAGS+ =-i./include/-std=c++ One-O3 #开启OpenMPCFLAGS+= -fopenmp#cuda C compile option Nvflags=--gpu-architecture=compute_62--gpu-code=compute_62 nvflags+ =-dopencv ' Pkg-config--cflags opencv-3.4' #C/c++ linker Options--OpenCV Library and its path ldflags= ' Pkg-config--libs opencv-3.4` -O3ldflags+= -fopenmp#c/c++ linker Options--CUDA8.0 Library and its path nvldflags= ' Pkg-config--libs cuda-8.0' #最终目标EXE=VIDEO#OPENMP Options--Specify binding cpugomp_cpu_affinity="0 3 4 5 "#链接 $ (EXE): $ (OBJS) $ (cu_obj) @echo linking ... @$ (CXX)-O [email protected] $^$ (ldflags) $ (nvldflags) #C/c++compilation%.O:%.cpp @echo Compiling $<... @$ (CXX) $ (CFLAGS)-C $<    @-MV *.O src/#CUDA C compilation%.o:%.cu @echo NVCC compiling $<... @$ (NVCC) $ (nvflags)-C $<    @-MV *.O src/. Phony:cleanclean:-RM src/*. O-RM $ (EXE)

Linux Platform cuda+opencv3.4 Configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.