• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "src/litert/kernel/gpu/opencl/opencl_allocator.h"
18 #include <utility>
19 #include "src/litert/kernel/gpu/opencl/opencl_runtime.h"
20 #include "src/litert/kernel/opencl/utils.h"
21 #include "src/common/log_adapter.h"
22 #include "include/errorcode.h"
23 
24 namespace mindspore::lite::opencl {
OpenCLAllocator(OpenCLRuntime * ocl_runtime)25 OpenCLAllocator::OpenCLAllocator(OpenCLRuntime *ocl_runtime) : ocl_runtime_(ocl_runtime) {}
26 
~OpenCLAllocator()27 OpenCLAllocator::~OpenCLAllocator() { Clear(); }
28 
SetContext(const AllocatorContext & ctx)29 void OpenCLAllocator::SetContext(const AllocatorContext &ctx) {
30   lock_flag_ = ctx.lockFlag;
31   if (ctx.shiftFactor < 0) {
32     MS_LOG(ERROR) << "shiftFactor from AllocatorContext is invalid negative.";
33   }
34   shift_factor_ = ctx.shiftFactor;
35 }
36 
Lock()37 void OpenCLAllocator::Lock() {
38   if (lock_flag_) {
39     lock.lock();
40   }
41 }
42 
UnLock()43 void OpenCLAllocator::UnLock() {
44   if (lock_flag_) {
45     lock.unlock();
46   }
47 }
48 
MinimumFit(MemType mem_type,size_t size,const ImageSize & img_size)49 void *OpenCLAllocator::MinimumFit(MemType mem_type, size_t size, const ImageSize &img_size) {
50   auto iter = free_list_.lower_bound(size);
51   while (iter != free_list_.end() && (iter->second->size_ >= size) && (iter->second->size_ < (size << shift_factor_))) {
52     auto mem_buf = iter->second;
53     bool is_match = mem_buf->mem_type_ == mem_type;
54     if (mem_type == MemType::IMG) {
55       is_match &= mem_buf->device_ptr_ != nullptr;
56       is_match &= mem_buf->img_size_ == img_size;
57     }
58     if (is_match) {
59       free_list_.erase(iter);
60       allocated_list_[mem_buf->host_ptr_] = mem_buf;
61       mem_buf->ref_count_ = 0;
62       MS_LOG(DEBUG) << "Find Mem from free list. size: " << mem_buf->size_ << ", host addr: " << mem_buf->host_ptr_
63                     << ", device addr: " << mem_buf->device_ptr_;
64       return mem_buf->host_ptr_;
65     }
66     ++iter;
67   }
68   return nullptr;
69 }
70 
CreateBuffer(size_t size,void * data,size_t flags,cl::Buffer ** buffer)71 void *OpenCLAllocator::CreateBuffer(size_t size, void *data, size_t flags, cl::Buffer **buffer) {
72   cl_int ret = CL_SUCCESS;
73   MS_ASSERT(buffer);
74   MS_ASSERT(size > 0);
75   *buffer = new (std::nothrow) cl::Buffer(*ocl_runtime_->Context(), static_cast<cl_mem_flags>(flags), size, data, &ret);
76   if (*buffer == nullptr) {
77     MS_LOG(ERROR) << "Create OpenCL buffer failed! (ERROR CODE: " << ret << ")";
78     return nullptr;
79   }
80   void *host_ptr = ocl_runtime_->MapBuffer(**buffer, CL_MAP_READ | CL_MAP_WRITE, size);
81   MS_ASSERT(host_ptr);
82   if (host_ptr == nullptr) {
83     delete *buffer;
84     buffer = nullptr;
85     MS_LOG(ERROR) << "Map buffer failed, can not found buffer.";
86     return nullptr;
87   }
88   cl::Memory *mem = *buffer;
89   MS_ASSERT(mem);
90   ret = ocl_runtime_->UnmapBuffer(*mem, host_ptr);
91   if (ret != RET_OK) {
92     MS_LOG(WARNING) << "UnmapBuffer failed.";
93   }
94   return host_ptr;
95 }
96 
CreateImage2D(size_t size,const ImageSize & img_size,void * data,size_t flags,bool is_map,cl::Buffer ** buffer,cl::Image2D ** image,void ** host_ptr)97 int OpenCLAllocator::CreateImage2D(size_t size, const ImageSize &img_size, void *data, size_t flags, bool is_map,
98                                    cl::Buffer **buffer, cl::Image2D **image, void **host_ptr) {
99   cl_int ret = CL_SUCCESS;
100   MS_ASSERT(buffer);
101   MS_ASSERT(image);
102   if (data == nullptr) {
103     // copy from cl2.hpp
104     cl_image_desc desc = {CL_MEM_OBJECT_IMAGE2D, img_size.width, img_size.height, 0, 0, 0, 0, 0, 0, (**buffer).get()};
105     const cl::Context &context = *ocl_runtime_->Context();
106     cl_image_format image_format{CL_RGBA, static_cast<uint32_t>(img_size.dtype)};
107     *image = new (std::nothrow) cl::Image2D(clCreateImage(context.get(), 0, &image_format, &desc, nullptr, &ret));
108   } else {
109     cl::ImageFormat image_format(CL_RGBA, img_size.dtype);
110     *image = new (std::nothrow) cl::Image2D(*ocl_runtime_->Context(), CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
111                                             image_format, img_size.width, img_size.height, 0, data, &ret);
112   }
113   if (*image == nullptr) {
114     delete *buffer;
115     *buffer = nullptr;
116     MS_LOG(ERROR) << "Create OpenCL Image2D failed! (ERROR CODE: " << mindspore::kernel::CLErrorCode(ret) << ")";
117     return RET_ERROR;
118   }
119   if (ret != CL_SUCCESS) {
120     delete *buffer;
121     delete *image;
122     *buffer = nullptr;
123     *image = nullptr;
124     MS_LOG(ERROR) << "Create OpenCL Image2D  (ERROR CODE: " << mindspore::kernel::CLErrorCode(ret) << ")";
125     return RET_ERROR;
126   }
127   MS_LOG(DEBUG) << "Malloc a new Image2D, width=" << img_size.width << ", height=" << img_size.height;
128 
129   if (is_map) {
130     std::vector<size_t> region{img_size.width, img_size.height, 1};
131     *host_ptr = ocl_runtime_->MapBuffer(**image, true, CL_MAP_READ | CL_MAP_WRITE, region);
132     if (*host_ptr == nullptr) {
133       delete *buffer;
134       delete *image;
135       *buffer = nullptr;
136       *image = nullptr;
137       MS_LOG(ERROR) << "Map image failed, can not found image :" << *image << ", host_ptr=" << *host_ptr;
138       return RET_ERROR;
139     }
140     cl::Memory *mem = *image;
141     ret = ocl_runtime_->UnmapBuffer(*mem, *host_ptr);
142     if (ret != CL_SUCCESS) {
143       MS_LOG(WARNING) << "UnmapBuffer failed.";
144     }
145   }
146   return RET_OK;
147 }
148 
GetImgDtypeSize(const ImageSize & img_size)149 int OpenCLAllocator::GetImgDtypeSize(const ImageSize &img_size) {
150   size_t dtype_size = 0;
151   if (img_size.dtype == CL_FLOAT) {
152     dtype_size = sizeof(cl_float);
153   } else if (img_size.dtype == CL_HALF_FLOAT) {
154     dtype_size = sizeof(cl_half);
155   } else if (img_size.dtype == CL_SIGNED_INT8) {
156     dtype_size = sizeof(cl_uchar);
157   } else if (img_size.dtype == CL_SIGNED_INT32) {
158     dtype_size = sizeof(cl_int);
159   } else {
160     MS_LOG(ERROR) << "Unsupported dtype " << img_size.dtype;
161     return RET_ERROR;
162   }
163   uint32_t image_alignment = ocl_runtime_->GetImagePitchAlignment();
164   size_t size = UP_ROUND(img_size.width, image_alignment) * img_size.height * C4NUM * dtype_size;
165   return size;
166 }
167 
Malloc(size_t weight,size_t height,DataType type)168 void *OpenCLAllocator::Malloc(size_t weight, size_t height, DataType type) {
169   ImageSize img_size = {weight, height};
170   switch (type) {
171     case DataType::kNumberTypeFloat32:
172       img_size.dtype = CL_FLOAT;
173       break;
174     case DataType::kNumberTypeFloat16:
175       img_size.dtype = CL_HALF_FLOAT;
176       break;
177     case DataType::kNumberTypeInt8:
178       img_size.dtype = CL_SIGNED_INT8;
179       break;
180     case DataType::kNumberTypeUInt8:
181       img_size.dtype = CL_UNSIGNED_INT8;
182       break;
183     case DataType::kNumberTypeInt32:
184       img_size.dtype = CL_SIGNED_INT32;
185       break;
186     case DataType::kNumberTypeUInt32:
187       img_size.dtype = CL_UNSIGNED_INT32;
188       break;
189     default:
190       MS_LOG(ERROR) << "Unsupported type " << static_cast<TypeId>(type);
191       return nullptr;
192   }
193   return _Malloc(MemType::IMG, nullptr, 0, img_size);
194 }
195 
_Malloc(MemType mem_type,void * data,size_t size,const ImageSize & img_size)196 void *OpenCLAllocator::_Malloc(MemType mem_type, void *data, size_t size, const ImageSize &img_size) {
197   auto svm_capabilities = ocl_runtime_->GetSVMCapabilities();
198   auto enable_arm_import_memory = ocl_runtime_->isExtensionEnable(EXT_ARM_IMPORT_MEMORY_HOST);
199   if (mem_type == MemType::SHARED && !enable_arm_import_memory) {
200     mem_type = MemType::BUF;
201   }
202   if (mem_type == MemType::IMG) {
203     size = GetImgDtypeSize(img_size);
204   }
205 
206   if (size > ocl_runtime_->GetMaxAllocSize()) {
207     MS_LOG(ERROR) << "MallocData out of max_size, size: " << size;
208     return nullptr;
209   }
210   Lock();
211   void *host_ptr = MinimumFit(mem_type, size, img_size);
212   UNLOCK_AND_RETURN_NULL(host_ptr != nullptr && data == nullptr, host_ptr);
213 
214   total_size_ += size;
215   const uint64_t max_size = ocl_runtime_->GetGlobalMemSize() * 0.8;
216   if (total_size_ >= max_size) {
217     is_oversize_ = true;
218   } else {
219     is_oversize_ = false;
220   }
221 
222   cl::Buffer *buffer = nullptr;
223   cl::Image2D *image = nullptr;
224   cl_mem_flags flags = CL_MEM_READ_WRITE;
225   if (svm_capabilities) {
226     flags |= (svm_capabilities & CL_DEVICE_SVM_FINE_GRAIN_BUFFER) ? CL_MEM_SVM_FINE_GRAIN_BUFFER : 0;
227     flags |= (svm_capabilities & CL_DEVICE_SVM_ATOMICS) ? CL_MEM_SVM_ATOMICS : 0;
228     host_ptr = clSVMAlloc((*ocl_runtime_->Context())(), flags, size, 0);
229   } else {
230     if (mem_type == MemType::SHARED) {
231       size = UP_ROUND(size, ocl_runtime_->GetCacheLineSize());
232       host_ptr = malloc(size);
233       UNLOCK_AND_RETURN_NULL(host_ptr == nullptr, nullptr);
234 
235       buffer = ocl_runtime_->CreateSharedMemoryBuffer(size, host_ptr);
236     } else {
237       flags |= (data == nullptr) ? CL_MEM_ALLOC_HOST_PTR : CL_MEM_COPY_HOST_PTR;
238       if (mem_type == MemType::BUF || data == nullptr) {
239         host_ptr = CreateBuffer(size, data, flags, &buffer);
240         UNLOCK_AND_RETURN_NULL(host_ptr == nullptr, nullptr);
241       }
242       if (mem_type == MemType::IMG) {
243         auto ret = CreateImage2D(size, img_size, data, flags, data != nullptr, &buffer, &image, &host_ptr);
244         UNLOCK_AND_RETURN_NULL(ret != RET_OK, nullptr);
245       }
246     }
247   }
248   MemBuf *mem_buf = new (std::nothrow) MemBuf;
249   if (mem_buf == nullptr) {
250     delete buffer;
251     delete image;
252     if (mem_type == MemType::SHARED) {
253       free(host_ptr);
254     }
255     UnLock();
256     return nullptr;
257   }
258   mem_buf->ref_count_ = 0;
259   mem_buf->size_ = size;
260   mem_buf->device_ptr_ = static_cast<void *>(buffer);
261   mem_buf->host_ptr_ = host_ptr;
262   mem_buf->image_ptr_ = static_cast<void *>(image);
263   mem_buf->mem_type_ = mem_type;
264   mem_buf->img_size_ = img_size;
265   allocated_list_[host_ptr] = mem_buf;
266   UnLock();
267   std::string type_name = (mem_type == MemType::BUF) ? "buffer" : "Image2D";
268   type_name = (mem_type == MemType::SHARED) ? "shared" : type_name;
269 
270   MS_LOG(DEBUG) << "Malloc a new " << type_name << ". size: " << mem_buf->size_ << ", host addr: " << mem_buf->host_ptr_
271                 << ", device addr: " << mem_buf->device_ptr_ << ", image_addr: " << image
272                 << ", total size: " << total_size_;
273   return host_ptr;
274 }
275 
Free(void * buf)276 void OpenCLAllocator::Free(void *buf) {
277   if (buf == nullptr) {
278     return;
279   }
280   Lock();
281   auto iter = allocated_list_.find(buf);
282   if (iter != allocated_list_.end()) {
283     if (iter->second->map_flags_) {
284       int ret = UnmapBuffer(buf);
285       if (ret != RET_OK) {
286         MS_LOG(WARNING) << "UnmapBuffer failed.";
287       }
288       iter->second->map_flags_ = false;
289     }
290     auto mem_buf = iter->second;
291     mem_buf->ref_count_ = 0;
292     allocated_list_.erase(iter);
293     free_list_.insert(std::make_pair(mem_buf->size_, mem_buf));
294     UnLock();
295     MS_LOG(DEBUG) << "Free device buffer. size: " << mem_buf->size_ << ", host addr: " << mem_buf->host_ptr_
296                   << ", device addr: " << mem_buf->device_ptr_ << ", image addr: " << mem_buf->image_ptr_
297                   << ", free list size: " << free_list_.size();
298     return;
299   }
300   UnLock();
301   MS_LOG(WARNING) << "Host ptr has freed";
302 }
RefCount(void * buf)303 int OpenCLAllocator::RefCount(void *buf) {
304   if (buf == nullptr) {
305     return OPENCL_ALLOCATOR_REFCOUNT;
306   }
307   Lock();
308   auto iter = allocated_list_.find(buf);
309   if (iter != allocated_list_.end()) {
310     auto mem_buf = iter->second;
311     int ref_count = std::atomic_load(&mem_buf->ref_count_);
312     UnLock();
313     return ref_count;
314   }
315   UnLock();
316   return -1;
317 }
SetRefCount(void * cl_buf,int ref_count)318 int OpenCLAllocator::SetRefCount(void *cl_buf, int ref_count) {
319   if (cl_buf == nullptr) {
320     return -1;
321   }
322   Lock();
323   auto iter = allocated_list_.find(cl_buf);
324   if (iter != allocated_list_.end()) {
325     auto mem_buf = iter->second;
326     std::atomic_store(&mem_buf->ref_count_, ref_count);
327     UnLock();
328     return ref_count;
329   }
330   UnLock();
331   return -1;
332 }
IncRefCount(void * cl_buf,int ref_count)333 int OpenCLAllocator::IncRefCount(void *cl_buf, int ref_count) {
334   if (cl_buf == nullptr) {
335     return -1;
336   }
337   Lock();
338   auto iter = allocated_list_.find(cl_buf);
339   if (iter != allocated_list_.end()) {
340     auto membuf = iter->second;
341     auto ref = std::atomic_fetch_add(&membuf->ref_count_, ref_count);
342     UnLock();
343     return (ref + ref_count);
344   }
345   UnLock();
346   return -1;
347 }
DecRefCount(void * cl_buf,int ref_count)348 int OpenCLAllocator::DecRefCount(void *cl_buf, int ref_count) {
349   if (cl_buf == nullptr) {
350     return -1;
351   }
352   Lock();
353   auto iter = allocated_list_.find(cl_buf);
354   if (iter != allocated_list_.end()) {
355     auto mem_buf = iter->second;
356     auto ref = std::atomic_fetch_sub(&mem_buf->ref_count_, ref_count);
357     UnLock();
358     return (ref - ref_count);
359   }
360   UnLock();
361   return -1;
362 }
363 
total_size()364 size_t OpenCLAllocator::total_size() {
365   Lock();
366   size_t totalSize = 0;
367 
368   for (auto it = allocated_list_.begin(); it != allocated_list_.end(); it++) {
369     totalSize += it->second->size_;
370   }
371 
372   for (auto it = free_list_.begin(); it != free_list_.end(); it++) {
373     totalSize += it->second->size_;
374   }
375   UnLock();
376   return totalSize;
377 }
378 
GetImage(void * buffer)379 cl::Image2D *OpenCLAllocator::GetImage(void *buffer) {
380   auto it = allocated_list_.find(buffer);
381   if (it != allocated_list_.end()) {
382     if (it->second->mem_type_ != MemType::IMG) {
383       return nullptr;
384     }
385     return reinterpret_cast<cl::Image2D *>(it->second->image_ptr_);
386   }
387   return nullptr;
388 }
389 
GetOpenclMemPtr(void * buffer,MemType * type,bool force_buffer)390 void *OpenCLAllocator::GetOpenclMemPtr(void *buffer, MemType *type, bool force_buffer) {
391   auto it = allocated_list_.find(buffer);
392   if (it != allocated_list_.end()) {
393     if ((it->second->mem_type_ == MemType::IMG) && !force_buffer) {
394       *type = MemType::IMG;
395       return it->second->image_ptr_;
396     }
397     *type = MemType::BUF;
398     return it->second->device_ptr_;
399   }
400   return nullptr;
401 }
402 
403 template <typename T>
ClearMemList(T * list)404 void OpenCLAllocator::ClearMemList(T *list) {
405   auto svm_capabilities = ocl_runtime_->GetSVMCapabilities();
406   for (auto it = list->begin(); it != list->end(); it++) {
407     if (it->second->map_flags_) {
408       int ret = UnmapBuffer(it->second->host_ptr_);
409       if (ret != RET_OK) {
410         MS_LOG(WARNING) << "UnmapBuffer failed.";
411       }
412     }
413     if (svm_capabilities) {
414       clSVMFree((*ocl_runtime_->Context())(), it->second->host_ptr_);
415       MS_LOG(DEBUG) << "OpenCL free svm buffer : " << it->second->host_ptr_;
416     } else {
417       cl::Buffer *buffer = static_cast<cl::Buffer *>(it->second->device_ptr_);
418       MS_LOG(DEBUG) << "OpenCL free device buffer : " << buffer;
419       if (buffer != nullptr) {
420         delete buffer;
421         it->second->device_ptr_ = nullptr;
422       }
423       cl::Image *image = static_cast<cl::Image *>(it->second->image_ptr_);
424       if (image != nullptr) {
425         delete image;
426         it->second->image_ptr_ = nullptr;
427       }
428       if (it->second->mem_type_ == MemType::SHARED) {
429         free(it->second->host_ptr_);
430         it->second->host_ptr_ = nullptr;
431       }
432     }
433     delete it->second;
434   }
435   list->clear();
436 }
437 
Clear()438 void OpenCLAllocator::Clear() {
439   Lock();
440   ClearMemList<std::unordered_map<void *, MemBuf *>>(&allocated_list_);
441   ClearMemList<std::multimap<size_t, MemBuf *>>(&free_list_);
442   UnLock();
443 }
444 
MapBuffer(void * host_ptr,int flags,void * command_queue,bool sync)445 void *OpenCLAllocator::MapBuffer(void *host_ptr, int flags, void *command_queue, bool sync) {
446   auto svm_capabilities = ocl_runtime_->GetSVMCapabilities();
447   if (svm_capabilities) {
448     if (!(svm_capabilities & CL_DEVICE_SVM_FINE_GRAIN_BUFFER)) {
449       auto it = allocated_list_.find(host_ptr);
450       if (it == allocated_list_.end()) {
451         MS_LOG(ERROR) << "Map buffer failed, can not found buffer :" << host_ptr;
452         return nullptr;
453       }
454       int ret = ocl_runtime_->MapBuffer(host_ptr, flags, it->second->size_,
455                                         static_cast<cl::CommandQueue *>(command_queue), sync);
456       if (ret != RET_OK) {
457         MS_LOG(WARNING) << "MapBuffer failed.";
458       }
459     }
460     return host_ptr;
461   }
462   Lock();
463   auto it = allocated_list_.find(host_ptr);
464   if (it == allocated_list_.end()) {
465     UnLock();
466     MS_LOG(ERROR) << "Map buffer failed, can not found buffer :" << host_ptr;
467     return nullptr;
468   }
469 
470   if (it->second->map_flags_) {
471     UnLock();
472     MS_LOG(WARNING) << "Host ptr has mapped";
473     return host_ptr;
474   }
475   MemBuf *mem_buf = it->second;
476   MS_ASSERT(mem_buf);
477   if (mem_buf->mem_type_ == MemType::SHARED) {
478     UnLock();
479     MS_LOG(WARNING) << "Host ptr no need map";
480     return host_ptr;
481   }
482 
483   void *new_host_ptr{nullptr};
484   if (mem_buf->mem_type_ == MemType::BUF) {
485     cl::Buffer *buffer = static_cast<cl::Buffer *>(mem_buf->device_ptr_);
486     MS_ASSERT(buffer);
487     new_host_ptr = ocl_runtime_->MapBuffer(*buffer, flags, mem_buf->size_, nullptr, sync);
488   } else if (mem_buf->mem_type_ == MemType::IMG) {
489     std::vector<size_t> region{mem_buf->img_size_.width, mem_buf->img_size_.height, 1};
490     cl::Image2D *image = static_cast<cl::Image2D *>(mem_buf->image_ptr_);
491     MS_ASSERT(image);
492     new_host_ptr = ocl_runtime_->MapBuffer(*image, sync, CL_MAP_READ | CL_MAP_WRITE, region);
493   }
494   if (new_host_ptr == nullptr) {
495     UnLock();
496     MS_LOG(WARNING) << "Map buffer failed, can not found buffer or already mapped, dev_ptr=" << mem_buf->device_ptr_
497                     << ", host_ptr=" << host_ptr;
498     return nullptr;
499   }
500 
501   mem_buf->map_flags_ = true;
502   mem_buf->host_ptr_ = new_host_ptr;
503   allocated_list_.erase(it);
504   allocated_list_[new_host_ptr] = mem_buf;
505   UnLock();
506   MS_LOG(DEBUG) << "Map buffer form " << host_ptr << " to " << new_host_ptr;
507   return new_host_ptr;
508 }
509 
UnmapBuffer(void * host_ptr,void * command_queue)510 int OpenCLAllocator::UnmapBuffer(void *host_ptr, void *command_queue) {
511   auto svm_capabilities = ocl_runtime_->GetSVMCapabilities();
512   if (svm_capabilities) {
513     if (!(svm_capabilities & CL_DEVICE_SVM_FINE_GRAIN_BUFFER)) {
514       return ocl_runtime_->UnmapBuffer(host_ptr);
515     }
516     return RET_OK;
517   }
518   auto it = allocated_list_.find(host_ptr);
519   if (it == allocated_list_.end()) {
520     MS_LOG(ERROR) << "Map buffer failed, can not found buffer :" << host_ptr;
521     return RET_ERROR;
522   }
523   if (it->second->map_flags_) {
524     it->second->map_flags_ = false;
525     cl::Memory *mem = static_cast<cl::Memory *>(it->second->mem_type_ == MemType::BUF ? it->second->device_ptr_
526                                                                                       : it->second->image_ptr_);
527     return ocl_runtime_->UnmapBuffer(*mem, it->second->host_ptr_, static_cast<cl::CommandQueue *>(command_queue));
528   } else {
529     MS_LOG(WARNING) << "Host ptr do not mapped";
530     return RET_OK;
531   }
532 }
533 
GetMemType(void * host_ptr)534 MemType OpenCLAllocator::GetMemType(void *host_ptr) {
535   MemType mem_type{MemType::BUF};
536   Lock();
537   auto it = allocated_list_.find(host_ptr);
538   if (it == allocated_list_.end()) {
539     UnLock();
540     MS_LOG(ERROR) << "Can not found buffer :" << host_ptr;
541     return mem_type;
542   }
543   MemBuf *mem_buf = it->second;
544   MS_ASSERT(mem_buf);
545   mem_type = mem_buf->mem_type_;
546   UnLock();
547   return mem_type;
548 }
549 
GetImageSize(void * host_ptr,ImageSize * img_size)550 int OpenCLAllocator::GetImageSize(void *host_ptr, ImageSize *img_size) {
551   MS_ASSERT(img_size);
552   Lock();
553   auto it = allocated_list_.find(host_ptr);
554   if (it == allocated_list_.end()) {
555     UnLock();
556     MS_LOG(ERROR) << "Can not found buffer :" << host_ptr;
557     return RET_OK;
558   }
559   MemBuf *mem_buf = it->second;
560   MS_ASSERT(mem_buf);
561   if (mem_buf->mem_type_ == MemType::IMG) {
562     *img_size = mem_buf->img_size_;
563   }
564   UnLock();
565   return RET_OK;
566 }
567 }  // namespace mindspore::lite::opencl
568