From 97c9bd44344e57c5c5c343d0104859828f0f440b Mon Sep 17 00:00:00 2001 From: DepthDeluxe Date: Sun, 6 Dec 2015 11:02:02 -0500 Subject: [PATCH] modified build environment to build CUDA programs --- CMakeLists.txt | 1 + openbr/CMakeLists.txt | 11 +++++++++++ openbr/cuda-plugins/cglg/copyfrom.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ openbr/cuda-plugins/cglg/copyto.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ openbr/cuda-plugins/cglg/customthreshold.cu | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ openbr/cuda-plugins/cglg/passthrough.cpp | 23 +++++++++++++++++++++++ openbr/cuda-plugins/cglg/threshold.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ openbr/cuda-plugins/cuda-plugins.cmake | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ openbr/plugins/cuda/copyfrom.cpp | 43 ------------------------------------------- openbr/plugins/cuda/copyto.cpp | 54 ------------------------------------------------------ openbr/plugins/cuda/passthrough.cpp | 23 ----------------------- openbr/plugins/cuda/threshold.cpp | 44 -------------------------------------------- 12 files changed, 298 insertions(+), 164 deletions(-) create mode 100644 openbr/cuda-plugins/cglg/copyfrom.cpp create mode 100644 openbr/cuda-plugins/cglg/copyto.cpp create mode 100644 openbr/cuda-plugins/cglg/customthreshold.cu create mode 100644 openbr/cuda-plugins/cglg/passthrough.cpp create mode 100644 openbr/cuda-plugins/cglg/threshold.cpp create mode 100644 openbr/cuda-plugins/cuda-plugins.cmake delete mode 100644 openbr/plugins/cuda/copyfrom.cpp delete mode 100644 openbr/plugins/cuda/copyto.cpp delete mode 100644 openbr/plugins/cuda/passthrough.cpp delete mode 100644 openbr/plugins/cuda/threshold.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d8047ea..1f41dc3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -108,6 +108,7 @@ endif() if(UNIX) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wno-strict-overflow -Wno-comment -Wno-unknown-pragmas -fvisibility=hidden -fno-omit-frame-pointer") set(CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib ${_qt5Core_install_prefix}/lib) + set(CUDA_NVCC_FLAGS "") # put NVCC compiler flags here if(NOT APPLE) if(${CMAKE_CXX_COMPILER} STREQUAL "/opt/intel/bin/icpc") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-intel -wd2196") diff --git a/openbr/CMakeLists.txt b/openbr/CMakeLists.txt index 6e9677c..c09d502 100644 --- a/openbr/CMakeLists.txt +++ b/openbr/CMakeLists.txt @@ -8,6 +8,7 @@ set(SRC openbr.cpp universal_template.cpp) aux_source_directory(core BR_CORE) include(plugins/plugins.cmake) +include(cuda-plugins/cuda-plugins.cmake) # Janus API option(BR_WITH_JANUS "Build IARPA Janus related applications." ON) @@ -28,6 +29,7 @@ if(NOT BR_EMBEDDED) install(FILES ${HEADERS} DESTINATION include/openbr/gui) endif() +# normal BR library declaration add_library(openbr SHARED ${SRC} ${BR_CORE} ${BR_JANUS} ${BR_GUI} ${BR_ICONS} ${BR_THIRDPARTY_SRC} ${BR_RESOURCES} ${NATURALSTRINGCOMPARE_SRC}) qt5_use_modules(openbr ${QT_DEPENDENCIES}) set_target_properties(openbr PROPERTIES @@ -37,6 +39,15 @@ set_target_properties(openbr PROPERTIES target_link_libraries(openbr ${BR_THIRDPARTY_LIBS}) add_cppcheck(openbr) +# CUDA portion of BR + +cuda_add_library(openbr-cuda SHARED ${BR_THIRDPARTY_CUDA_SRC}) +qt5_use_modules(openbr-cuda ${QT_DEPENDENCIES}) +set_target_properties(openbr-cuda PROPERTIES + DEFINE_SYMBOL BR_CUDA_LIBRARY + VERSION ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH} + SOVERSION ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}) + # Janus implementation if(BR_WITH_JANUS) set(JANUS_BUILD_PP5_WRAPPER ${BR_WITH_PP5} CACHE BOOL "Build Janus implementation using PittPatt 5") diff --git a/openbr/cuda-plugins/cglg/copyfrom.cpp b/openbr/cuda-plugins/cglg/copyfrom.cpp new file mode 100644 index 0000000..e624aae --- /dev/null +++ b/openbr/cuda-plugins/cglg/copyfrom.cpp @@ -0,0 +1,43 @@ +#include + +#include +#include + +#include + +using namespace std; + +using namespace cv; +using namespace cv::gpu; + + +namespace br +{ + class CUDACopyFrom : public UntrainableTransform + { + Q_OBJECT + +private: + void project(const Template &src, Template &dst) const + { + // reassemble the integer and then build pointer to it + uint64_t gpuMatInt = (((uint64_t)src.m().at(1,0)) << (uint64_t)32) + ((uint64_t)src.m().at(0,0)); + GpuMat* gpuMat = (GpuMat*)gpuMatInt; + + printf("gpuMatInt: %li\n", gpuMatInt); + printf("m.at(0,0): %i\nm.at(1,0): %i\n", src.m().at(0,0), src.m().at(1,0)); + + // download the data back into the destination + Size size = gpuMat->size(); + Mat out = Mat(size.height, size.width, gpuMat->depth()); + + gpuMat->download(out); + + dst = out; + } + }; + + BR_REGISTER(Transform, CUDACopyFrom); +} + +#include "cuda/copyfrom.moc" diff --git a/openbr/cuda-plugins/cglg/copyto.cpp b/openbr/cuda-plugins/cglg/copyto.cpp new file mode 100644 index 0000000..36f683f --- /dev/null +++ b/openbr/cuda-plugins/cglg/copyto.cpp @@ -0,0 +1,54 @@ +#include + +#include +#include + +#include + +using namespace std; + +using namespace cv; +using namespace cv::gpu; + +namespace br +{ + class CUDACopyTo : public UntrainableTransform + { + Q_OBJECT + +private: + void project(const Template &src, Template &dst) const + { + // get the mat to send to the GPU + GpuMat* gpuMat = new GpuMat; + + try + { + // copy the contents to the GPU + gpuMat->upload(src.m()); + } + catch(const cv::Exception& ex) + { + cout << "Error: " << ex.what() << endl; + } + + // now create a new Mat that contains the 64-bit pointer + Mat m = Mat(2, 1, CV_32S); + + // pointer magic + uint64_t gpuMatInt = (uint64_t)gpuMat; + m.at(0,0) = (int32_t)(gpuMatInt & 0x00000000FFFFFFFF); + m.at(1,0) = (int32_t)((gpuMatInt & 0xFFFFFFFF00000000) >> (uint64_t)32); + + printf("gpuMatInt: %li\n", gpuMatInt); + printf("m.at(0,0): %i\nm.at(1,0): %i\n", m.at(0,0), m.at(1,0)); + + // save away in the destination mat + dst += m; + } + }; + + BR_REGISTER(Transform, CUDACopyTo); +} + +#include "cuda/copyto.moc" diff --git a/openbr/cuda-plugins/cglg/customthreshold.cu b/openbr/cuda-plugins/cglg/customthreshold.cu new file mode 100644 index 0000000..b414b57 --- /dev/null +++ b/openbr/cuda-plugins/cglg/customthreshold.cu @@ -0,0 +1,72 @@ +/* +#include + +// external opencv CUDA interface +#include +#include + +// internal CUDA stuff +#include +#include +#include +#include +#include + +#include + +using namespace std; + +using namespace cv; +using namespace cv::gpu; +using namespace cv::cuda; +using namespace cv::cuda::device; + +namespace br +{ + class CUDACustomThresholdTransform : public UntrainableTransform + { + Q_OBJECT + +private: + void project(const Template &src, Template &dst) const + { + // get the mat to send to the GPU + GpuMat gpuMat_src, gpuMat_dst; + + try + { + // copy the contents to the GPU + gpuMat_src.upload(src.m()); + + threshold(gpuMat_src, gpuMat_dst, 128.0, 255.0, CV_THRESH_BINARY); + + gpuMat_dst.download(dst.m()); + } + catch(const cv::Exception& ex) + { + cout << "Error: " << ex.what() << endl; + } + } + }; + + BR_REGISTER(Transform, CUDACustomThresholdTransform); + + namespace cuda { namespace customthreshold { + texture tex_src(false, cudaFilterModePoint, cudaAddressModeClamp); + struct SrcTex { + __host__ SrcTex(int _xoff, int _yoff) : xoff(_xoff), yoff(_yoff) {} + __device__ __forceinline__ int operator ()(int y, int x) const { + return tex2D(tex_src, x + xoff, y + yoff); + } + } + __global__ void testKernel(const SrcTex src) { + const int x = blockIdx.x * blockDim.x + threadIdx.x; + const int y = blockIdx.y * blockDim.y + threadIdx.y; + + src(x, y) = 1;sajfflksajlkfjdsalkfjsadjflkdsaf + } + } +} + +#include "cglg/customthreshold.moc" +*/ diff --git a/openbr/cuda-plugins/cglg/passthrough.cpp b/openbr/cuda-plugins/cglg/passthrough.cpp new file mode 100644 index 0000000..5b1044d --- /dev/null +++ b/openbr/cuda-plugins/cglg/passthrough.cpp @@ -0,0 +1,23 @@ +#include + +#include + +using namespace cv; + +namespace br +{ + class CUDAPassthroughTransform : public UntrainableTransform + { + Q_OBJECT + +private: + void project(const Template &src, Template &dst) const + { + dst = src; + } + }; + + BR_REGISTER(Transform, CUDAPassthroughTransform); +} + +#include "cuda/passthrough.moc" diff --git a/openbr/cuda-plugins/cglg/threshold.cpp b/openbr/cuda-plugins/cglg/threshold.cpp new file mode 100644 index 0000000..219dccf --- /dev/null +++ b/openbr/cuda-plugins/cglg/threshold.cpp @@ -0,0 +1,44 @@ +#include + +#include +#include + +#include + +using namespace std; + +using namespace cv; +using namespace cv::gpu; + +namespace br +{ + class CUDAThreshold : public UntrainableTransform + { + Q_OBJECT + +private: + void project(const Template &src, Template &dst) const + { + // get the mat to send to the GPU + GpuMat gpuMat_src, gpuMat_dst; + + try + { + // copy the contents to the GPU + gpuMat_src.upload(src.m()); + + threshold(gpuMat_src, gpuMat_dst, 128.0, 255.0, CV_THRESH_BINARY); + + gpuMat_dst.download(dst.m()); + } + catch(const cv::Exception& ex) + { + cout << "Error: " << ex.what() << endl; + } + } + }; + + BR_REGISTER(Transform, CUDAThreshold); +} + +#include "cuda/threshold.moc" diff --git a/openbr/cuda-plugins/cuda-plugins.cmake b/openbr/cuda-plugins/cuda-plugins.cmake new file mode 100644 index 0000000..a4bf886 --- /dev/null +++ b/openbr/cuda-plugins/cuda-plugins.cmake @@ -0,0 +1,50 @@ +# Optional Appendable CMake Variables: +# BR_THIRDPARTY_CUDA_PLUGINS - Additional plugins +# BR_THIRDPARTY_CUDA_PLUGINS_DIR - Additional folder(s) of plugins +# BR_EXCLUDED_CUDA_PLUGINS - Plugins that should not be built +# BR_THIRDPARTY_SRC - Additional source code needed by a plugin +# BR_THIRDPARTY_LIBS - Additional libaries needed by a plugin + +# Also look for CMake modules in the thirdparty plugins folder(s) +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${BR_THIRDPARTY_CUDA_PLUGINS_DIR}) + +# Gather all of the plugin subdirectories +file(GLOB SUBFILES cuda-plugins/*) +foreach(FILE ${SUBFILES}) + if(IS_DIRECTORY ${FILE}) + set(BR_CUDA_PLUGINS_DIR ${BR_CUDA_PLUGINS_DIR} ${FILE}) + endif() +endforeach() +set(BR_CUDA_PLUGINS_DIR ${BR_CUDA_PLUGINS_DIR} cuda-plugins/) # Remove this when finished with reorg + +# Exclude pertinent plugins based on .cmake files +mark_as_advanced(BR_EXCLUDED_CUDA_PLUGINS) +foreach(DIR cuda-plugins/cmake ${BR_THIRDPARTY_CUDA_PLUGINS_DIR}) + file(GLOB CMAKE_FILES ${DIR}/*.cmake) + foreach(CMAKE_FILE ${CMAKE_FILES}) + if (NOT ${CMAKE_FILE} MATCHES "Find.*cmake") + include(${CMAKE_FILE}) + endif() + endforeach() +endforeach() + +# Collect all source files except for excluded plugins +foreach(DIR ${BR_CUDA_PLUGINS_DIR} ${BR_THIRDPARTY_CUDA_PLUGINS_DIR}) + get_filename_component(DIR_NAME ${DIR} NAME) + file(GLOB CUDA_PLUGINS ${DIR}/*.cu ${DIR}/*.cpp ${DIR}/*.h) + foreach(CUDA_PLUGIN ${CUDA_PLUGINS}) + get_filename_component(CUDA_PLUGIN_NAME ${CUDA_PLUGIN} NAME) + set(EXCLUDE FALSE) + foreach(EXCLUDED_CUDA_PLUGIN ${BR_EXCLUDED_CUDA_PLUGINS}) + get_filename_component(EXCLUDED_CUDA_PLUGIN_NAME ${EXCLUDED_CUDA_PLUGIN} NAME) + if (${CUDA_PLUGIN_NAME} STREQUAL ${EXCLUDED_CUDA_PLUGIN_NAME}) + set(EXCLUDE TRUE) + endif() + endforeach() + if(NOT ${EXCLUDE}) + set(BR_THIRDPARTY_CUDA_PLUGINS ${BR_THIRDPARTY_CUDA_PLUGINS} ${CUDA_PLUGIN}) + endif() + endforeach() +endforeach() + +set(BR_THIRDPARTY_CUDA_SRC ${BR_THIRDPARTY_CUDA_SRC} ${BR_THIRDPARTY_CUDA_PLUGINS}) diff --git a/openbr/plugins/cuda/copyfrom.cpp b/openbr/plugins/cuda/copyfrom.cpp deleted file mode 100644 index e624aae..0000000 --- a/openbr/plugins/cuda/copyfrom.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include - -#include -#include - -#include - -using namespace std; - -using namespace cv; -using namespace cv::gpu; - - -namespace br -{ - class CUDACopyFrom : public UntrainableTransform - { - Q_OBJECT - -private: - void project(const Template &src, Template &dst) const - { - // reassemble the integer and then build pointer to it - uint64_t gpuMatInt = (((uint64_t)src.m().at(1,0)) << (uint64_t)32) + ((uint64_t)src.m().at(0,0)); - GpuMat* gpuMat = (GpuMat*)gpuMatInt; - - printf("gpuMatInt: %li\n", gpuMatInt); - printf("m.at(0,0): %i\nm.at(1,0): %i\n", src.m().at(0,0), src.m().at(1,0)); - - // download the data back into the destination - Size size = gpuMat->size(); - Mat out = Mat(size.height, size.width, gpuMat->depth()); - - gpuMat->download(out); - - dst = out; - } - }; - - BR_REGISTER(Transform, CUDACopyFrom); -} - -#include "cuda/copyfrom.moc" diff --git a/openbr/plugins/cuda/copyto.cpp b/openbr/plugins/cuda/copyto.cpp deleted file mode 100644 index 36f683f..0000000 --- a/openbr/plugins/cuda/copyto.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include - -#include -#include - -#include - -using namespace std; - -using namespace cv; -using namespace cv::gpu; - -namespace br -{ - class CUDACopyTo : public UntrainableTransform - { - Q_OBJECT - -private: - void project(const Template &src, Template &dst) const - { - // get the mat to send to the GPU - GpuMat* gpuMat = new GpuMat; - - try - { - // copy the contents to the GPU - gpuMat->upload(src.m()); - } - catch(const cv::Exception& ex) - { - cout << "Error: " << ex.what() << endl; - } - - // now create a new Mat that contains the 64-bit pointer - Mat m = Mat(2, 1, CV_32S); - - // pointer magic - uint64_t gpuMatInt = (uint64_t)gpuMat; - m.at(0,0) = (int32_t)(gpuMatInt & 0x00000000FFFFFFFF); - m.at(1,0) = (int32_t)((gpuMatInt & 0xFFFFFFFF00000000) >> (uint64_t)32); - - printf("gpuMatInt: %li\n", gpuMatInt); - printf("m.at(0,0): %i\nm.at(1,0): %i\n", m.at(0,0), m.at(1,0)); - - // save away in the destination mat - dst += m; - } - }; - - BR_REGISTER(Transform, CUDACopyTo); -} - -#include "cuda/copyto.moc" diff --git a/openbr/plugins/cuda/passthrough.cpp b/openbr/plugins/cuda/passthrough.cpp deleted file mode 100644 index 5b1044d..0000000 --- a/openbr/plugins/cuda/passthrough.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include - -#include - -using namespace cv; - -namespace br -{ - class CUDAPassthroughTransform : public UntrainableTransform - { - Q_OBJECT - -private: - void project(const Template &src, Template &dst) const - { - dst = src; - } - }; - - BR_REGISTER(Transform, CUDAPassthroughTransform); -} - -#include "cuda/passthrough.moc" diff --git a/openbr/plugins/cuda/threshold.cpp b/openbr/plugins/cuda/threshold.cpp deleted file mode 100644 index 219dccf..0000000 --- a/openbr/plugins/cuda/threshold.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include - -#include -#include - -#include - -using namespace std; - -using namespace cv; -using namespace cv::gpu; - -namespace br -{ - class CUDAThreshold : public UntrainableTransform - { - Q_OBJECT - -private: - void project(const Template &src, Template &dst) const - { - // get the mat to send to the GPU - GpuMat gpuMat_src, gpuMat_dst; - - try - { - // copy the contents to the GPU - gpuMat_src.upload(src.m()); - - threshold(gpuMat_src, gpuMat_dst, 128.0, 255.0, CV_THRESH_BINARY); - - gpuMat_dst.download(dst.m()); - } - catch(const cv::Exception& ex) - { - cout << "Error: " << ex.what() << endl; - } - } - }; - - BR_REGISTER(Transform, CUDAThreshold); -} - -#include "cuda/threshold.moc" -- libgit2 0.21.4