From 4e28c00e7bb5f2e7c49f48ddf3cdb897828ea9a7 Mon Sep 17 00:00:00 2001 From: Dmitry Kurtaev Date: Thu, 17 Aug 2017 12:48:02 +0300 Subject: [PATCH] Fix Halide buffer behavior in case of OpenCL device memory allocation --- modules/dnn/src/op_halide.cpp | 12 ++++++++++++ modules/dnn/src/op_halide.hpp | 5 +++++ 2 files changed, 17 insertions(+) diff --git a/modules/dnn/src/op_halide.cpp b/modules/dnn/src/op_halide.cpp index dc46ace..51c7a57 100644 --- a/modules/dnn/src/op_halide.cpp +++ b/modules/dnn/src/op_halide.cpp @@ -76,6 +76,7 @@ HalideBackendNode::HalideBackendNode(const Ptr& base, HalideBackendWrapper::HalideBackendWrapper(int targetId, const cv::Mat& m) : BackendWrapper(DNN_BACKEND_HALIDE, targetId) { + managesDevMemory = true; buffer = wrapToHalideBuffer(m); if (targetId == DNN_TARGET_CPU) { @@ -95,6 +96,7 @@ HalideBackendWrapper::HalideBackendWrapper(const Ptr& base, const MatShape& shape) : BackendWrapper(DNN_BACKEND_HALIDE, base->targetId) { + managesDevMemory = false; int w, h, c, n; getCanonicalSize(shape, &w, &h, &c, &n); Halide::Buffer baseBuffer = halideBuffer(base); @@ -113,6 +115,16 @@ HalideBackendWrapper::HalideBackendWrapper(const Ptr& base, } } +HalideBackendWrapper::~HalideBackendWrapper() +{ + if (buffer.has_device_allocation() && !managesDevMemory) + { + buffer.raw_buffer()->device = 0; + buffer.raw_buffer()->device_interface = 0; + buffer.set_device_dirty(false); + } +} + void HalideBackendWrapper::copyToHost() { CV_Assert(targetId == DNN_TARGET_CPU || buffer.device_dirty()); diff --git a/modules/dnn/src/op_halide.hpp b/modules/dnn/src/op_halide.hpp index f91e3fb..1e0358e 100644 --- a/modules/dnn/src/op_halide.hpp +++ b/modules/dnn/src/op_halide.hpp @@ -57,9 +57,14 @@ namespace dnn HalideBackendWrapper(const Ptr& base, const MatShape& shape); + ~HalideBackendWrapper(); + virtual void copyToHost(); Halide::Buffer buffer; + + private: + bool managesDevMemory; }; #endif // HAVE_HALIDE -- 2.7.4