From 6674a024fc54fbee1e6cde06a16ef59da8827387 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Sun, 7 Jan 2018 18:38:14 +0000 Subject: [PATCH] dnn: add OPENCV_DNN_DISABLE_MEMORY_OPTIMIZATIONS runtime option replaces REUSE_DNN_MEMORY compile-time option --- modules/dnn/CMakeLists.txt | 5 -- modules/dnn/src/dnn.cpp | 121 ++++++++++++++++++++----------------- 2 files changed, 65 insertions(+), 61 deletions(-) diff --git a/modules/dnn/CMakeLists.txt b/modules/dnn/CMakeLists.txt index abe07cf9f7..f3ebb36dac 100644 --- a/modules/dnn/CMakeLists.txt +++ b/modules/dnn/CMakeLists.txt @@ -97,8 +97,3 @@ if(BUILD_PERF_TESTS) endif() endif() endif() - -ocv_option(${the_module}_REUSE_MEMORY "Enable reusing strategy of memory management" ON) -if (${the_module}_REUSE_MEMORY) - add_definitions(-DREUSE_DNN_MEMORY=1) -endif() diff --git a/modules/dnn/src/dnn.cpp b/modules/dnn/src/dnn.cpp index 8889d6020b..834a9ebe84 100644 --- a/modules/dnn/src/dnn.cpp +++ b/modules/dnn/src/dnn.cpp @@ -51,10 +51,15 @@ #include #include +#include + namespace cv { namespace dnn { CV__DNN_EXPERIMENTAL_NS_BEGIN +// this option is usefull to run valgrind memory errors detection +static bool DNN_DISABLE_MEMORY_OPTIMIZATIONS = utils::getConfigurationParameterBool("OPENCV_DNN_DISABLE_MEMORY_OPTIMIZATIONS", false); + using std::vector; using std::map; using std::make_pair; @@ -369,40 +374,42 @@ public: void reuseOrCreate(const MatShape& shape, const LayerPin& lp, Mat& dst) { -#ifdef REUSE_DNN_MEMORY - Mat bestBlob; - LayerPin bestBlobPin; - - std::map::iterator hostIt; - std::map::iterator refIt; - - const int targetTotal = total(shape); - int bestBlobTotal = INT_MAX; - - for (hostIt = memHosts.begin(); hostIt != memHosts.end(); ++hostIt) + if (!DNN_DISABLE_MEMORY_OPTIMIZATIONS) { - refIt = refCounter.find(hostIt->first); - // Use only blobs that had references before because if not, - // it might be used as output. - if (refIt != refCounter.end() && refIt->second == 0) + Mat bestBlob; + LayerPin bestBlobPin; + + std::map::iterator hostIt; + std::map::iterator refIt; + + const int targetTotal = total(shape); + int bestBlobTotal = INT_MAX; + + for (hostIt = memHosts.begin(); hostIt != memHosts.end(); ++hostIt) { - Mat& unusedBlob = hostIt->second; - if (unusedBlob.total() >= targetTotal && - unusedBlob.total() < bestBlobTotal) + refIt = refCounter.find(hostIt->first); + // Use only blobs that had references before because if not, + // it might be used as output. + if (refIt != refCounter.end() && refIt->second == 0) { - bestBlobPin = hostIt->first; - bestBlob = unusedBlob; - bestBlobTotal = unusedBlob.total(); + Mat& unusedBlob = hostIt->second; + if (unusedBlob.total() >= targetTotal && + unusedBlob.total() < bestBlobTotal) + { + bestBlobPin = hostIt->first; + bestBlob = unusedBlob; + bestBlobTotal = unusedBlob.total(); + } } } + if (!bestBlob.empty()) + { + reuse(bestBlobPin, lp); + dst = bestBlob.reshape(1, 1).colRange(0, targetTotal).reshape(1, shape); + return; + } } - if (!bestBlob.empty()) - { - reuse(bestBlobPin, lp); - dst = bestBlob.reshape(1, 1).colRange(0, targetTotal).reshape(1, shape); - } - else -#endif // REUSE_DNN_MEMORY + { // if dst already has been allocated with total(shape) elements, // it won't be recrreated and pointer of dst.data remains the same. @@ -413,40 +420,42 @@ public: void reuseOrCreate(const MatShape& shape, const LayerPin& lp, UMat &umat_dst) { -#ifdef REUSE_DNN_MEMORY - UMat bestBlob; - LayerPin bestBlobPin; - - std::map::iterator hostIt; - std::map::iterator refIt; - - const int targetTotal = total(shape); - int bestBlobTotal = INT_MAX; - - for (hostIt = umat_memHosts.begin(); hostIt != umat_memHosts.end(); ++hostIt) + if (!DNN_DISABLE_MEMORY_OPTIMIZATIONS) { - refIt = refCounter.find(hostIt->first); - // Use only blobs that had references before because if not, - // it might be used as output. - if (refIt != refCounter.end() && refIt->second == 0) + UMat bestBlob; + LayerPin bestBlobPin; + + std::map::iterator hostIt; + std::map::iterator refIt; + + const int targetTotal = total(shape); + int bestBlobTotal = INT_MAX; + + for (hostIt = umat_memHosts.begin(); hostIt != umat_memHosts.end(); ++hostIt) { - UMat& unusedBlob = hostIt->second; - if (unusedBlob.total() >= targetTotal && - unusedBlob.total() < bestBlobTotal) + refIt = refCounter.find(hostIt->first); + // Use only blobs that had references before because if not, + // it might be used as output. + if (refIt != refCounter.end() && refIt->second == 0) { - bestBlobPin = hostIt->first; - bestBlob = unusedBlob; - bestBlobTotal = unusedBlob.total(); + UMat& unusedBlob = hostIt->second; + if (unusedBlob.total() >= targetTotal && + unusedBlob.total() < bestBlobTotal) + { + bestBlobPin = hostIt->first; + bestBlob = unusedBlob; + bestBlobTotal = unusedBlob.total(); + } } } + if (!bestBlob.empty()) + { + reuse(bestBlobPin, lp); + umat_dst.create(shape, CV_32F); + return; + } } - if (!bestBlob.empty()) - { - reuse(bestBlobPin, lp); - umat_dst.create(shape, CV_32F); - } - else -#endif // REUSE_DNN_MEMORY + { // if dst already has been allocated with total(shape) elements, // it won't be recrreated and pointer of dst.data remains the same.