未验证 提交 fb94c49d 编写于 作者: Y Yuan Shuai 提交者: GitHub

[LITE][OPENCL] Add FreeImage for Free of OpenCL in memory. test=develop (#3290)

* [LITE][OPENCL] Add ImageFree for Free in memory. test=develop

* add initialize for cl_use_image2d_ when use buffer. test=develop
上级 0161bcb2
...@@ -51,7 +51,7 @@ void* TargetMalloc(TargetType target, size_t size) { ...@@ -51,7 +51,7 @@ void* TargetMalloc(TargetType target, size_t size) {
return data; return data;
} }
void TargetFree(TargetType target, void* data) { void TargetFree(TargetType target, void* data, std::string free_flag) {
switch (target) { switch (target) {
case TargetType::kHost: case TargetType::kHost:
case TargetType::kX86: case TargetType::kX86:
...@@ -66,7 +66,11 @@ void TargetFree(TargetType target, void* data) { ...@@ -66,7 +66,11 @@ void TargetFree(TargetType target, void* data) {
#endif // LITE_WITH_CUDA #endif // LITE_WITH_CUDA
#ifdef LITE_WITH_OPENCL #ifdef LITE_WITH_OPENCL
case TargetType::kOpenCL: case TargetType::kOpenCL:
TargetWrapperCL::Free(data); if (free_flag == "cl_use_image2d_") {
TargetWrapperCL::FreeImage(data);
} else {
TargetWrapperCL::Free(data);
}
break; break;
#endif // LITE_WITH_OPENCL #endif // LITE_WITH_OPENCL
#ifdef LITE_WITH_FPGA #ifdef LITE_WITH_FPGA
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
// limitations under the License. // limitations under the License.
#pragma once #pragma once
#include <string>
#include "lite/api/paddle_place.h" #include "lite/api/paddle_place.h"
#include "lite/core/target_wrapper.h" #include "lite/core/target_wrapper.h"
#include "lite/utils/logging.h" #include "lite/utils/logging.h"
...@@ -39,7 +40,9 @@ LITE_API void* TargetMalloc(TargetType target, size_t size); ...@@ -39,7 +40,9 @@ LITE_API void* TargetMalloc(TargetType target, size_t size);
// Free memory for a specific Target. All the targets should be an element in // Free memory for a specific Target. All the targets should be an element in
// the `switch` here. // the `switch` here.
void LITE_API TargetFree(TargetType target, void* data); void LITE_API TargetFree(TargetType target,
void* data,
std::string free_flag = "");
// Copy a buffer from host to another target. // Copy a buffer from host to another target.
void TargetCopy(TargetType target, void* dst, const void* src, size_t size); void TargetCopy(TargetType target, void* dst, const void* src, size_t size);
...@@ -108,6 +111,9 @@ class Buffer { ...@@ -108,6 +111,9 @@ class Buffer {
data_ = TargetMalloc(target, size); data_ = TargetMalloc(target, size);
target_ = target; target_ = target;
space_ = size; space_ = size;
#ifdef LITE_WITH_OPENCL
cl_use_image2d_ = false;
#endif
} }
} }
...@@ -119,15 +125,15 @@ class Buffer { ...@@ -119,15 +125,15 @@ class Buffer {
const size_t img_w, const size_t img_w,
const size_t img_h, const size_t img_h,
void* host_ptr = nullptr) { void* host_ptr = nullptr) {
size_t size = sizeof(T) * img_w * img_h *
4; // 4 for RGBA, un-used for opencl Image2D
if (target != target_ || cl_image2d_width_ < img_w || if (target != target_ || cl_image2d_width_ < img_w ||
cl_image2d_height_ < img_h) { cl_image2d_height_ < img_h) {
CHECK_EQ(own_data_, true) << "Can not reset unowned buffer."; CHECK_EQ(own_data_, true) << "Can not reset unowned buffer.";
Free(); Free();
data_ = TargetWrapperCL::MallocImage<T>(img_w, img_h, host_ptr); data_ = TargetWrapperCL::MallocImage<T>(img_w, img_h, host_ptr);
target_ = target; target_ = target;
space_ = size; // un-used for opencl Image2D space_ = sizeof(T) * img_w * img_h *
4; // un-used for opencl Image2D, 4 for RGBA,
cl_use_image2d_ = true;
cl_image2d_width_ = img_w; cl_image2d_width_ = img_w;
cl_image2d_height_ = img_h; cl_image2d_height_ = img_h;
} }
...@@ -136,7 +142,11 @@ class Buffer { ...@@ -136,7 +142,11 @@ class Buffer {
void Free() { void Free() {
if (space_ > 0 && own_data_) { if (space_ > 0 && own_data_) {
TargetFree(target_, data_); if (!cl_use_image2d_) {
TargetFree(target_, data_);
} else {
TargetFree(target_, data_, "cl_use_image2d_");
}
} }
data_ = nullptr; data_ = nullptr;
target_ = TargetType::kHost; target_ = TargetType::kHost;
...@@ -155,6 +165,7 @@ class Buffer { ...@@ -155,6 +165,7 @@ class Buffer {
private: private:
// memory it actually malloced. // memory it actually malloced.
size_t space_{0}; size_t space_{0};
bool cl_use_image2d_{false}; // only used for OpenCL Image2D
size_t cl_image2d_width_{0}; // only used for OpenCL Image2D size_t cl_image2d_width_{0}; // only used for OpenCL Image2D
size_t cl_image2d_height_{0}; // only used for OpenCL Image2D size_t cl_image2d_height_{0}; // only used for OpenCL Image2D
void* data_{nullptr}; void* data_{nullptr};
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册