added support to build without cuda.

This commit is contained in:
Anatoly Baksheev 2010-07-15 13:36:00 +00:00
parent e1bd5aeadd
commit 2c84a66ec7
9 changed files with 169 additions and 138 deletions

View File

@ -282,6 +282,7 @@ endif()
set(WITH_TBB OFF CACHE BOOL "Include TBB support") set(WITH_TBB OFF CACHE BOOL "Include TBB support")
set(WITH_EIGEN2 ON CACHE BOOL "Include Eigen2 support") set(WITH_EIGEN2 ON CACHE BOOL "Include Eigen2 support")
set(WITH_CUDA OFF CACHE BOOL "Include NVidia Cuda Runtime support")
# =================================================== # ===================================================
# Macros that checks if module have been installed. # Macros that checks if module have been installed.
@ -608,6 +609,15 @@ if (WITH_TBB)
endif() endif()
endif() endif()
############################### TBB ################################
if (WITH_CUDA)
find_package(CUDA)
if (CUDA_FOUND)
message(STATUS "CUDA detected.")
set(HAVE_CUDA 1)
endif()
endif()
############################## Eigen2 ############################## ############################## Eigen2 ##############################
@ -1249,6 +1259,12 @@ else()
message(STATUS " Use TBB: NO") message(STATUS " Use TBB: NO")
endif() endif()
if (HAVE_CUDA)
message(STATUS " Uue Cuda: YES")
else()
message(STATUS " Use Cuda: No")
endif()
if(HAVE_EIGEN2) if(HAVE_EIGEN2)
message(STATUS " Use Eigen2: YES") message(STATUS " Use Eigen2: YES")
else() else()

View File

@ -165,3 +165,6 @@
/* Qt bindings use OpenGL */ /* Qt bindings use OpenGL */
#cmakedefine HAVE_HAVE_QT_OPENGL #cmakedefine HAVE_HAVE_QT_OPENGL
/* NVidia Cuda Runtime API*/
#cmakedefine HAVE_CUDA

View File

@ -229,7 +229,9 @@ enum {
CV_StsParseError= -212, /* invalid syntax/structure of the parsed file */ CV_StsParseError= -212, /* invalid syntax/structure of the parsed file */
CV_StsNotImplemented= -213, /* the requested function/feature is not implemented */ CV_StsNotImplemented= -213, /* the requested function/feature is not implemented */
CV_StsBadMemBlock= -214, /* an allocated block has been corrupted */ CV_StsBadMemBlock= -214, /* an allocated block has been corrupted */
CV_StsAssert= -215 /* assertion failed */ CV_StsAssert= -215, /* assertion failed */
CV_GpuNotFound= -216,
CV_GpuApiCallError= -217
}; };
/****************************************************************************************\ /****************************************************************************************\

View File

@ -1,23 +1,12 @@
include(FindCUDA)
if (CUDA_FOUND)
include_directories(${CUDA_INCLUDE_DIRS})
link_directories(${CUDA_LIBRARIES})
#message ("CUDA_LIBRARIES = ${CUDA_LIBRARIES}")
#message ("CUDA_INCLUDE_DIRS = ${CUDA_INCLUDE_DIRS}")
#message ("CUDA_TARGET_LINK = ${CUDA_TARGET_LINK}")
#CUDA_GENERATED_OUTPUT_DIR (Default CMAKE_CURRENT_BINARY_DIR)
#====================================================================================
set(name "gpu") set(name "gpu")
set(DEPS "opencv_core") set(DEPS "opencv_core")
project(opencv_${name})
set(the_target "opencv_${name}")
project(${the_target})
add_definitions(-DCVAPI_EXPORTS) add_definitions(-DCVAPI_EXPORTS)
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include" include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include"
@ -42,16 +31,19 @@ if (CUDA_FOUND)
file(GLOB lib_hdrs "include/opencv2/${name}/*.h*") file(GLOB lib_hdrs "include/opencv2/${name}/*.h*")
source_group("Include" FILES ${lib_hdrs}) source_group("Include" FILES ${lib_hdrs})
if (HAVE_CUDA)
include_directories(${CUDA_INCLUDE_DIRS})
link_directories(${CUDA_LIBRARIES})
if (UNIX OR APPLE) if (UNIX OR APPLE)
set (CUDA_NVCC_FLAGS "-Xcompiler;-fPIC") set (CUDA_NVCC_FLAGS "-Xcompiler;-fPIC")
endif() endif()
CUDA_COMPILE(cuda_objs ${lib_cuda}) CUDA_COMPILE(cuda_objs ${lib_cuda})
#message ("lib cuda : ${cuda_objs}")
#CUDA_BUILD_CLEAN_TARGET() #CUDA_BUILD_CLEAN_TARGET()
endif()
set(the_target "opencv_${name}")
#message ("cuda_add_library : ${the_target} ${lib_srcs} ${lib_hdrs} ${lib_int_hdrs} ${lib_cuda} ${lib_cuda_hdrs}")
add_library(${the_target} ${lib_srcs} ${lib_hdrs} ${lib_int_hdrs} ${lib_cuda} ${lib_cuda_hdrs} ${cuda_objs}) add_library(${the_target} ${lib_srcs} ${lib_hdrs} ${lib_int_hdrs} ${lib_cuda} ${lib_cuda_hdrs} ${cuda_objs})
if(PCHSupport_FOUND) if(PCHSupport_FOUND)
@ -82,7 +74,6 @@ if (CUDA_FOUND)
) )
# Add the required libraries for linking: # Add the required libraries for linking:
#message (" ++++ target_link_libraries = ${the_target} ${OPENCV_LINKER_LIBS} ${IPP_LIBS} ${DEPS} ${CUDA_LIBRARIES}")
target_link_libraries(${the_target} ${OPENCV_LINKER_LIBS} ${IPP_LIBS} ${DEPS} ${CUDA_LIBRARIES}) target_link_libraries(${the_target} ${OPENCV_LINKER_LIBS} ${IPP_LIBS} ${DEPS} ${CUDA_LIBRARIES})
if(MSVC) if(MSVC)
@ -108,5 +99,4 @@ if (CUDA_FOUND)
DESTINATION include/opencv2/${name} DESTINATION include/opencv2/${name}
COMPONENT main) COMPONENT main)
endif()

View File

@ -216,6 +216,9 @@ namespace cv
class CudaStream class CudaStream
{ {
public: public:
static CudaStream empty();
CudaStream(); CudaStream();
~CudaStream(); ~CudaStream();
@ -236,9 +239,13 @@ namespace cv
// converts matrix type, ex from float to uchar depending on type // converts matrix type, ex from float to uchar depending on type
void enqueueConvert(const GpuMat& src, GpuMat& dst, int type); void enqueueConvert(const GpuMat& src, GpuMat& dst, int type);
//CUstream_st& getStream(); struct Impl;
const Impl& getImpl() const;
private: private:
void *impl;
Impl *impl;
CudaStream(const CudaStream&); CudaStream(const CudaStream&);
CudaStream& operator=(const CudaStream&); CudaStream& operator=(const CudaStream&);

View File

@ -41,33 +41,37 @@
//M*/ //M*/
#include "precomp.hpp" #include "precomp.hpp"
#include "opencv2/gpu/stream_access.hpp"
using namespace cv; using namespace cv;
using namespace cv::gpu; using namespace cv::gpu;
cv::gpu::CudaStream::CudaStream() : impl( fastMalloc(sizeof(cudaStream_t)) ) cv::gpu::CudaStream::CudaStream() : impl( (Impl*)fastMalloc(sizeof(Impl)) )
{ {
cudaSafeCall( cudaStreamCreate((cudaStream_t*)impl) ); //cudaSafeCall( cudaStreamCreate( &impl->stream) );
} }
cv::gpu::CudaStream::~CudaStream() cv::gpu::CudaStream::~CudaStream()
{
if (impl)
{ {
cudaSafeCall( cudaStreamDestroy( *(cudaStream_t*)impl ) ); cudaSafeCall( cudaStreamDestroy( *(cudaStream_t*)impl ) );
cv::fastFree( impl ); cv::fastFree( impl );
} }
}
bool cv::gpu::CudaStream::queryIfComplete() bool cv::gpu::CudaStream::queryIfComplete()
{ {
cudaError_t err = cudaStreamQuery( *(cudaStream_t*)impl ); //cudaError_t err = cudaStreamQuery( *(cudaStream_t*)impl );
if (err == cudaSuccess) //if (err == cudaSuccess)
return true; // return true;
if (err == cudaErrorNotReady) //if (err == cudaErrorNotReady)
return false; // return false;
//cudaErrorInvalidResourceHandle ////cudaErrorInvalidResourceHandle
cudaSafeCall( err ); //cudaSafeCall( err );
return true; return true;
} }
void cv::gpu::CudaStream::waitForCompletion() void cv::gpu::CudaStream::waitForCompletion()

View File

@ -45,6 +45,16 @@
using namespace cv; using namespace cv;
using namespace cv::gpu; using namespace cv::gpu;
#ifndef HAVE_CUDA
CV_EXPORTS int cv::gpu::getCudaEnabledDeviceCount() { return 0; }
CV_EXPORTS string cv::gpu::getDeviceName(int /*device*/) { cudaSafeCall(0); return 0; }
CV_EXPORTS void cv::gpu::setDevice(int /*device*/) { cudaSafeCall(0); }
CV_EXPORTS void cv::gpu::getComputeCapability(int /*device*/, int* /*major*/, int* /*minor*/) { cudaSafeCall(0); }
CV_EXPORTS int cv::gpu::getNumberOfSMs(int /*device*/) { cudaSafeCall(0); return 0; }
#else
CV_EXPORTS int cv::gpu::getCudaEnabledDeviceCount() CV_EXPORTS int cv::gpu::getCudaEnabledDeviceCount()
{ {
int count; int count;
@ -79,3 +89,5 @@ CV_EXPORTS int cv::gpu::getNumberOfSMs(int device)
cudaSafeCall( cudaGetDeviceProperties( &prop, device ) ); cudaSafeCall( cudaGetDeviceProperties( &prop, device ) );
return prop.multiProcessorCount; return prop.multiProcessorCount;
} }
#endif

View File

@ -57,41 +57,39 @@
#include "cuda_shared.hpp" #include "cuda_shared.hpp"
#ifndef HAVE_CUDA
#define cudaSafeCall(err) CV_Error(CV_GpuNotFound, "The library is compilled with no GPU support")
#define cudaCallerSafeCall(err) CV_Error(CV_GpuNotFound, "The library is compilled with no GPU support")
#else /* HAVE_CUDA */
#if _MSC_VER >= 1200 #if _MSC_VER >= 1200
#pragma warning (disable : 4100 4211 4201 4408) #pragma warning (disable : 4100 4211 4201 4408)
#endif #endif
#include "cuda_runtime_api.h" #include "cuda_runtime_api.h"
#define cudaCallerSafeCall(err) err;
#ifdef __GNUC__
#define cudaSafeCall(err) __cudaSafeCall(err, __FILE__, __LINE__, __func__)
#else
#define cudaSafeCall(err) __cudaSafeCall(err, __FILE__, __LINE__) #define cudaSafeCall(err) __cudaSafeCall(err, __FILE__, __LINE__)
#endif
//inline void __cudaSafeCall( cudaError err, const char *file, const int line )
//{
// if( cudaSuccess != err)
// CV_Error_(CV_StsAssert, ("%s(%i) : Runtime API error : %s.\n", cudaGetErrorString(err)));
//}
namespace cv namespace cv
{ {
namespace gpu namespace gpu
{ {
static inline void __cudaSafeCall( cudaError err, const char *file, const int line, const char *func = "")
inline void __cudaSafeCall( cudaError err, const char *file, const int line )
{ {
if( cudaSuccess != err) if( cudaSuccess != err)
{ cv::error( cv::Exception(CV_GpuApiCallError, cudaGetErrorString(err), func, file, line) );
std::cerr << file << "(" << line << ") : cudaSafeCall() Runtime API error : " << cudaGetErrorString(err) << "\n"; }
exit(-1);
} }
} }
template<class T> #endif /* HAVE_CUDA */
inline DevMem2D_<T> getDevMem(const GpuMat& mat)
{
return DevMem2D_<T>(mat.rows, mat.cols, mat.data, mat.step);
}
}
}
#endif #endif

View File

@ -41,7 +41,6 @@
//M*/ //M*/
#include "precomp.hpp" #include "precomp.hpp"
#include <limits>
using namespace cv; using namespace cv;
using namespace cv::gpu; using namespace cv::gpu;
@ -69,5 +68,5 @@ void StereoBM_GPU::operator() ( const GpuMat& left, const GpuMat& right, GpuMat&
DevMem2D disp = disparity; DevMem2D disp = disparity;
DevMem2D_<uint> mssd = minSSD; DevMem2D_<uint> mssd = minSSD;
impl::stereoBM_GPU(left, right, disp, ndisp, mssd); cudaCallerSafeCall( impl::stereoBM_GPU(left, right, disp, ndisp, mssd) );
} }