From 2425f9a16b86914040ef1ea887da9fa1cb2c8252 Mon Sep 17 00:00:00 2001 From: zhaoyang-star Date: Thu, 24 Sep 2020 09:36:34 +0800 Subject: [PATCH] [Cherry-pick][Bugfix][OpenCL][Core] fix opencl multi-run result error (#4413) * [Bugfix][OpenCL][Core] fix opencl multi-run result error when using memory_optimize_pass (#4410) * [Bugfix][OpenCL][Core] fix opencl multi-run result error when using memory_optimize_pass. test=develop * test=develop Co-authored-by: ysh329 --- lite/core/memory.h | 18 ++++++++++-------- lite/core/memory_test.cc | 6 ++++++ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/lite/core/memory.h b/lite/core/memory.h index a1013910019..380222c7643 100644 --- a/lite/core/memory.h +++ b/lite/core/memory.h @@ -13,6 +13,7 @@ // limitations under the License. #pragma once +#include #include #include "lite/api/paddle_place.h" #include "lite/core/target_wrapper.h" @@ -135,20 +136,21 @@ class Buffer { #ifdef LITE_WITH_OPENCL template void ResetLazyImage2D(TargetType target, - const size_t img_w, - const size_t img_h, + const size_t img_w_req, + const size_t img_h_req, void* host_ptr = nullptr) { - if (target != target_ || cl_image2d_width_ < img_w || - cl_image2d_height_ < img_h || host_ptr != nullptr) { + if (target != target_ || cl_image2d_width_ < img_w_req || + cl_image2d_height_ < img_h_req || host_ptr != nullptr) { CHECK_EQ(own_data_, true) << "Can not reset unowned buffer."; + cl_image2d_width_ = std::max(cl_image2d_width_, img_w_req); + cl_image2d_height_ = std::max(cl_image2d_height_, img_h_req); Free(); - data_ = TargetWrapperCL::MallocImage(img_w, img_h, host_ptr); + data_ = TargetWrapperCL::MallocImage( + cl_image2d_width_, cl_image2d_height_, host_ptr); target_ = target; - space_ = sizeof(T) * img_w * img_h * + space_ = sizeof(T) * cl_image2d_width_ * cl_image2d_height_ * 4; // un-used for opencl Image2D, 4 for RGBA, cl_use_image2d_ = true; - cl_image2d_width_ = img_w; - cl_image2d_height_ = img_h; } } #endif diff --git a/lite/core/memory_test.cc b/lite/core/memory_test.cc index cd9062afca7..6343854db2b 100644 --- a/lite/core/memory_test.cc +++ b/lite/core/memory_test.cc @@ -28,6 +28,12 @@ TEST(memory, test) { ASSERT_TRUE(buf_cuda); TargetFree(TARGET(kCUDA), buf_cuda); #endif + +#ifdef LITE_WITH_OPENCL + auto* buf_cl = TargetMalloc(TARGET(kOpenCL), 10); + ASSERT_TRUE(buf_cl); + TargetFree(TARGET(kOpenCL), buf_cl); +#endif } } // namespace lite