Skip to content

Commit

Permalink
dnn: make OpenCL DNN code optional
Browse files Browse the repository at this point in the history
  • Loading branch information
alalek committed Mar 1, 2018
1 parent 24bed38 commit 1b83bc4
Show file tree
Hide file tree
Showing 32 changed files with 117 additions and 80 deletions.
8 changes: 6 additions & 2 deletions cmake/OpenCVModule.cmake
Expand Up @@ -747,14 +747,18 @@ endmacro()

# finds and sets headers and sources for the standard OpenCV module
# Usage:
# ocv_glob_module_sources([EXCLUDE_CUDA] <extra sources&headers in the same format as used in ocv_set_module_sources>)
# ocv_glob_module_sources([EXCLUDE_CUDA] [EXCLUDE_OPENCL] <extra sources&headers in the same format as used in ocv_set_module_sources>)
macro(ocv_glob_module_sources)
ocv_debug_message("ocv_glob_module_sources(" ${ARGN} ")")
set(_argn ${ARGN})
list(FIND _argn "EXCLUDE_CUDA" exclude_cuda)
if(NOT exclude_cuda EQUAL -1)
list(REMOVE_AT _argn ${exclude_cuda})
endif()
list(FIND _argn "EXCLUDE_OPENCL" exclude_opencl)
if(NOT exclude_opencl EQUAL -1)
list(REMOVE_AT _argn ${exclude_opencl})
endif()

file(GLOB_RECURSE lib_srcs
"${CMAKE_CURRENT_LIST_DIR}/src/*.cpp"
Expand Down Expand Up @@ -801,7 +805,7 @@ macro(ocv_glob_module_sources)
file(GLOB cl_kernels
"${CMAKE_CURRENT_LIST_DIR}/src/opencl/*.cl"
)
if(cl_kernels)
if(cl_kernels AND exclude_opencl EQUAL -1)
set(OCL_NAME opencl_kernels_${name})
add_custom_command(
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" # don't add .hpp file here to optimize build process
Expand Down
19 changes: 17 additions & 2 deletions modules/dnn/CMakeLists.txt
Expand Up @@ -11,6 +11,14 @@ set(the_description "Deep neural network module. It allows to load models from d
ocv_add_dispatched_file_force_all("layers/layers_common" AVX AVX2 AVX512_SKX)

ocv_add_module(dnn opencv_core opencv_imgproc WRAP python matlab java js)

ocv_option(OPENCV_DNN_OPENCL "Build with OpenCL support" HAVE_OPENCL)
if(OPENCV_DNN_OPENCL AND HAVE_OPENCL)
add_definitions(-DCV_OCL4DNN=1)
else()
ocv_cmake_hook_append(INIT_MODULE_SOURCES_opencv_dnn "${CMAKE_CURRENT_LIST_DIR}/cmake/hooks/INIT_MODULE_SOURCES_opencv_dnn.cmake")
endif()

ocv_warnings_disable(CMAKE_CXX_FLAGS -Wno-shadow -Wno-parentheses -Wmaybe-uninitialized -Wsign-promo
-Wmissing-declarations -Wmissing-prototypes
)
Expand Down Expand Up @@ -63,8 +71,15 @@ else()
set(fw_inc "${CMAKE_CURRENT_LIST_DIR}/misc/caffe" "${CMAKE_CURRENT_LIST_DIR}/misc/tensorflow")
endif()

ocv_module_include_directories(${fw_inc} ${CMAKE_CURRENT_LIST_DIR}/src/ocl4dnn/include ${OPENCL_INCLUDE_DIRS})
ocv_glob_module_sources(SOURCES ${fw_srcs})
set(include_dirs ${fw_inc})
set(sources_options "")
if(OPENCV_DNN_OPENCL AND HAVE_OPENCL)
list(APPEND include_dirs ${OPENCL_INCLUDE_DIRS})
else()
set(sources_options EXCLUDE_OPENCL)
endif()
ocv_module_include_directories(${include_dirs})
ocv_glob_module_sources(${sources_options} SOURCES ${fw_srcs})
ocv_create_module(libprotobuf ${LAPACK_LIBRARIES})
ocv_add_samples()
ocv_add_accuracy_tests()
Expand Down
3 changes: 3 additions & 0 deletions modules/dnn/cmake/hooks/INIT_MODULE_SOURCES_opencv_dnn.cmake
@@ -0,0 +1,3 @@
message(STATUS "opencv_dnn: filter out ocl4dnn source code")
ocv_list_filterout(OPENCV_MODULE_${the_module}_SOURCES "/ocl4dnn/")
ocv_list_filterout(OPENCV_MODULE_${the_module}_HEADERS "/ocl4dnn/")
3 changes: 3 additions & 0 deletions modules/dnn/src/layers/batch_norm_layer.cpp
Expand Up @@ -13,7 +13,10 @@ Implementation of Batch Normalization layer.
#include "op_halide.hpp"
#include "op_inf_engine.hpp"
#include <opencv2/dnn/shape_utils.hpp>

#ifdef HAVE_OPENCL
#include "opencl_kernels_dnn.hpp"
#endif

namespace cv
{
Expand Down
3 changes: 3 additions & 0 deletions modules/dnn/src/layers/concat_layer.cpp
Expand Up @@ -44,7 +44,10 @@
#include "layers_common.hpp"
#include "op_halide.hpp"
#include "op_inf_engine.hpp"

#ifdef HAVE_OPENCL
#include "opencl_kernels_dnn.hpp"
#endif

namespace cv
{
Expand Down
2 changes: 1 addition & 1 deletion modules/dnn/src/layers/convolution_layer.cpp
Expand Up @@ -47,9 +47,9 @@
#include "opencv2/core/hal/hal.hpp"
#include "opencv2/core/hal/intrin.hpp"
#include <iostream>
#include "opencl_kernels_dnn.hpp"

#ifdef HAVE_OPENCL
#include "opencl_kernels_dnn.hpp"
using namespace cv::dnn::ocl4dnn;
#endif

Expand Down
3 changes: 3 additions & 0 deletions modules/dnn/src/layers/detection_output_layer.cpp
Expand Up @@ -46,7 +46,10 @@
#include <float.h>
#include <string>
#include "../nms.inl.hpp"

#ifdef HAVE_OPENCL
#include "opencl_kernels_dnn.hpp"
#endif

namespace cv
{
Expand Down
5 changes: 4 additions & 1 deletion modules/dnn/src/layers/elementwise_layers.cpp
Expand Up @@ -46,9 +46,12 @@
#include "op_inf_engine.hpp"
#include "opencv2/imgproc.hpp"
#include <opencv2/dnn/shape_utils.hpp>
#include "opencl_kernels_dnn.hpp"
#include <iostream>

#ifdef HAVE_OPENCL
#include "opencl_kernels_dnn.hpp"
#endif

namespace cv
{
namespace dnn
Expand Down
3 changes: 3 additions & 0 deletions modules/dnn/src/layers/eltwise_layer.cpp
Expand Up @@ -44,7 +44,10 @@
#include "layers_common.hpp"
#include "op_halide.hpp"
#include "op_inf_engine.hpp"

#ifdef HAVE_OPENCL
#include "opencl_kernels_dnn.hpp"
#endif

namespace cv
{
Expand Down
2 changes: 1 addition & 1 deletion modules/dnn/src/layers/fully_connected_layer.cpp
Expand Up @@ -44,10 +44,10 @@
#include "layers_common.hpp"
#include "op_halide.hpp"
#include "op_inf_engine.hpp"
#include "opencl_kernels_dnn.hpp"
#include <opencv2/dnn/shape_utils.hpp>

#ifdef HAVE_OPENCL
#include "opencl_kernels_dnn.hpp"
using namespace cv::dnn::ocl4dnn;
#endif

Expand Down
1 change: 1 addition & 0 deletions modules/dnn/src/layers/layers_common.cpp
Expand Up @@ -40,6 +40,7 @@
//
//M*/

#include "../precomp.hpp"
#include "layers_common.hpp"

namespace cv
Expand Down
2 changes: 1 addition & 1 deletion modules/dnn/src/layers/layers_common.hpp
Expand Up @@ -52,7 +52,7 @@
#undef CV_CPU_OPTIMIZATION_DECLARATIONS_ONLY

#ifdef HAVE_OPENCL
#include "ocl4dnn.hpp"
#include "../ocl4dnn/include/ocl4dnn.hpp"
#endif

namespace cv
Expand Down
2 changes: 1 addition & 1 deletion modules/dnn/src/layers/lrn_layer.cpp
Expand Up @@ -47,10 +47,10 @@
#include "opencv2/imgproc.hpp"
#include "opencv2/dnn/shape_utils.hpp"
#include "opencv2/core/hal/hal.hpp"
#include "opencl_kernels_dnn.hpp"
#include <algorithm>

#ifdef HAVE_OPENCL
#include "opencl_kernels_dnn.hpp"
using namespace cv::dnn::ocl4dnn;
#endif

Expand Down
5 changes: 4 additions & 1 deletion modules/dnn/src/layers/mvn_layer.cpp
Expand Up @@ -43,8 +43,11 @@
#include "../precomp.hpp"
#include "layers_common.hpp"
#include <opencv2/dnn/shape_utils.hpp>
#include "math_functions.hpp"

#ifdef HAVE_OPENCL
#include "../ocl4dnn/include/math_functions.hpp"
#include "opencl_kernels_dnn.hpp"
#endif

namespace cv
{
Expand Down
3 changes: 3 additions & 0 deletions modules/dnn/src/layers/permute_layer.cpp
Expand Up @@ -45,7 +45,10 @@
#include "op_inf_engine.hpp"
#include <float.h>
#include <algorithm>

#ifdef HAVE_OPENCL
#include "opencl_kernels_dnn.hpp"
#endif

namespace cv
{
Expand Down
3 changes: 2 additions & 1 deletion modules/dnn/src/layers/pooling_layer.cpp
Expand Up @@ -45,12 +45,13 @@
#include "opencv2/core/hal/intrin.hpp"
#include "op_halide.hpp"
#include "op_inf_engine.hpp"
#include "opencl_kernels_dnn.hpp"
#include <float.h>
#include <algorithm>
using std::max;
using std::min;

#ifdef HAVE_OPENCL
#include "opencl_kernels_dnn.hpp"
using namespace cv::dnn::ocl4dnn;
#endif

Expand Down
3 changes: 3 additions & 0 deletions modules/dnn/src/layers/prior_box_layer.cpp
Expand Up @@ -46,7 +46,10 @@
#include <float.h>
#include <algorithm>
#include <cmath>

#ifdef HAVE_OPENCL
#include "opencl_kernels_dnn.hpp"
#endif

namespace cv
{
Expand Down
3 changes: 3 additions & 0 deletions modules/dnn/src/layers/region_layer.cpp
Expand Up @@ -44,7 +44,10 @@
#include <opencv2/dnn/shape_utils.hpp>
#include <opencv2/dnn/all_layers.hpp>
#include "nms.inl.hpp"

#ifdef HAVE_OPENCL
#include "opencl_kernels_dnn.hpp"
#endif

namespace cv
{
Expand Down
3 changes: 3 additions & 0 deletions modules/dnn/src/layers/reorg_layer.cpp
Expand Up @@ -44,7 +44,10 @@
#include <opencv2/dnn/shape_utils.hpp>
#include <opencv2/dnn/all_layers.hpp>
#include <iostream>

#ifdef HAVE_OPENCL
#include "opencl_kernels_dnn.hpp"
#endif

namespace cv
{
Expand Down
3 changes: 3 additions & 0 deletions modules/dnn/src/layers/slice_layer.cpp
Expand Up @@ -43,7 +43,10 @@
#include "../precomp.hpp"
#include "layers_common.hpp"
#include <opencv2/dnn/shape_utils.hpp>

#ifdef HAVE_OPENCL
#include "opencl_kernels_dnn.hpp"
#endif

namespace cv
{
Expand Down
3 changes: 2 additions & 1 deletion modules/dnn/src/layers/softmax_layer.cpp
Expand Up @@ -44,11 +44,12 @@
#include "layers_common.hpp"
#include "op_halide.hpp"
#include "op_inf_engine.hpp"
#include "opencl_kernels_dnn.hpp"
#include <algorithm>
#include <stdlib.h>
using std::max;

#ifdef HAVE_OPENCL
#include "opencl_kernels_dnn.hpp"
using namespace cv::dnn::ocl4dnn;
#endif

Expand Down
3 changes: 0 additions & 3 deletions modules/dnn/src/ocl4dnn/include/common.hpp
Expand Up @@ -45,8 +45,6 @@
#include "../../caffe/glog_emulator.hpp"
#include <opencv2/core/opencl/runtime/opencl_core.hpp>

#ifdef HAVE_OPENCL

// Macro to select the single (_float) or double (_double) precision kernel
#define CL_KERNEL_SELECT(kernel) kernel "_float"

Expand All @@ -58,5 +56,4 @@

bool clOptionSupport(cv::String option);

#endif // HAVE_OPENCL
#endif
3 changes: 0 additions & 3 deletions modules/dnn/src/ocl4dnn/include/math_functions.hpp
Expand Up @@ -52,7 +52,6 @@ namespace dnn
namespace ocl4dnn
{

#ifdef HAVE_OPENCL
enum CBLAS_TRANSPOSE {CblasNoTrans=111, CblasTrans=112, CblasConjTrans=113};

template<typename Dtype>
Expand Down Expand Up @@ -81,8 +80,6 @@ bool ocl4dnnAXPY(const int32_t N, const Dtype alpha,
const UMat x, const int32_t offx, UMat y,
const int32_t offy);

#endif // HAVE_OPENCL

} // namespace ocl4dnn
} // namespace dnn
} // namespce cv
Expand Down
8 changes: 3 additions & 5 deletions modules/dnn/src/ocl4dnn/include/ocl4dnn.hpp
Expand Up @@ -51,7 +51,6 @@
#include "common.hpp"

namespace cv { namespace dnn { namespace ocl4dnn {
#ifdef HAVE_OPENCL

struct OCL4DNNConvConfig
{
Expand Down Expand Up @@ -507,8 +506,7 @@ class OCL4DNNSoftmax
bool log_softmax_;
UMat scale_data_;
};
#endif // HAVE_OPENCL
} // namespace ocl4dnn
} // namespace dnn
} // namespce cv

}}} // namespace cv::dnn::ocl4dnn

#endif
5 changes: 1 addition & 4 deletions modules/dnn/src/ocl4dnn/src/common.cpp
Expand Up @@ -41,17 +41,14 @@
//M*/

#include "../../precomp.hpp"
#include "common.hpp"
#include "../include/common.hpp"
#include "opencl_kernels_dnn.hpp"

using namespace cv;

#ifdef HAVE_OPENCL
bool clOptionSupport(cv::String option)
{
cv::String errmsg;
ocl::Program program = ocl::Context::getDefault().getProg(ocl::dnn::dummy_oclsrc, option, errmsg);
return program.ptr() ? true : false;
}

#endif // HAVE_OPENCL
18 changes: 4 additions & 14 deletions modules/dnn/src/ocl4dnn/src/math_functions.cpp
Expand Up @@ -41,19 +41,13 @@
//M*/

#include "../../precomp.hpp"
#include "common.hpp"
#include "math_functions.hpp"
#include "../include/common.hpp"
#include "../include/math_functions.hpp"
#include <vector>
#include "opencl_kernels_dnn.hpp"

namespace cv
{
namespace dnn
{
namespace ocl4dnn
{
namespace cv { namespace dnn { namespace ocl4dnn {

#ifdef HAVE_OPENCL
// Create and copy buffer to image for GEMM's matrix A and B.
// Will return image to caller if the input image is NULL. Otherwise,
// will use the image directly. It's caller's responsibility to
Expand Down Expand Up @@ -527,8 +521,4 @@ template bool ocl4dnnAXPY<float>(const int32_t N, const float alpha,
const UMat X, const int32_t offX,
UMat Y, const int32_t offY);

#endif // HAVE_OPENCL

} // namespace ocl4dnn
} // namespace dnn
} // namespce cv
}}} // namespace cv::dnn::ocl4dnn

0 comments on commit 1b83bc4

Please sign in to comment.