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/runtime/gpu/opencl/opencl_allocator.h"
18 #include <utility>
19 #include "src/runtime/gpu/opencl/opencl_runtime.h"
20 #include "src/runtime/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 UNLOCK_AND_RETURN_NULL(total_size_ >= max_size, nullptr);
217
218 cl::Buffer *buffer = nullptr;
219 cl::Image2D *image = nullptr;
220 cl_mem_flags flags = CL_MEM_READ_WRITE;
221 if (svm_capabilities) {
222 flags |= (svm_capabilities & CL_DEVICE_SVM_FINE_GRAIN_BUFFER) ? CL_MEM_SVM_FINE_GRAIN_BUFFER : 0;
223 flags |= (svm_capabilities & CL_DEVICE_SVM_ATOMICS) ? CL_MEM_SVM_ATOMICS : 0;
224 host_ptr = clSVMAlloc((*ocl_runtime_->Context())(), flags, size, 0);
225 } else {
226 if (mem_type == MemType::SHARED) {
227 size = UP_ROUND(size, ocl_runtime_->GetCacheLineSize());
228 host_ptr = malloc(size);
229 UNLOCK_AND_RETURN_NULL(host_ptr == nullptr, nullptr);
230
231 buffer = ocl_runtime_->CreateSharedMemoryBuffer(size, host_ptr);
232 } else {
233 flags |= (data == nullptr) ? CL_MEM_ALLOC_HOST_PTR : CL_MEM_COPY_HOST_PTR;
234 if (mem_type == MemType::BUF || data == nullptr) {
235 host_ptr = CreateBuffer(size, data, flags, &buffer);
236 UNLOCK_AND_RETURN_NULL(host_ptr == nullptr, nullptr);
237 }
238 if (mem_type == MemType::IMG) {
239 auto ret = CreateImage2D(size, img_size, data, flags, data != nullptr, &buffer, &image, &host_ptr);
240 UNLOCK_AND_RETURN_NULL(ret != RET_OK, nullptr);
241 }
242 }
243 }
244 MemBuf *mem_buf = new (std::nothrow) MemBuf;
245 if (mem_buf == nullptr) {
246 delete buffer;
247 delete image;
248 if (mem_type == MemType::SHARED) {
249 free(host_ptr);
250 }
251 UnLock();
252 return nullptr;
253 }
254 mem_buf->ref_count_ = 0;
255 mem_buf->size_ = size;
256 mem_buf->device_ptr_ = static_cast<void *>(buffer);
257 mem_buf->host_ptr_ = host_ptr;
258 mem_buf->image_ptr_ = static_cast<void *>(image);
259 mem_buf->mem_type_ = mem_type;
260 mem_buf->img_size_ = img_size;
261 allocated_list_[host_ptr] = mem_buf;
262 UnLock();
263 std::string type_name = (mem_type == MemType::BUF) ? "buffer" : "Image2D";
264 type_name = (mem_type == MemType::SHARED) ? "shared" : type_name;
265
266 MS_LOG(DEBUG) << "Malloc a new " << type_name << ". size: " << mem_buf->size_ << ", host addr: " << mem_buf->host_ptr_
267 << ", device addr: " << mem_buf->device_ptr_ << ", image_addr: " << image
268 << ", total size: " << total_size_;
269 return host_ptr;
270 }
271
Free(void * buf)272 void OpenCLAllocator::Free(void *buf) {
273 if (buf == nullptr) {
274 return;
275 }
276 Lock();
277 auto iter = allocated_list_.find(buf);
278 if (iter != allocated_list_.end()) {
279 if (iter->second->map_flags_) {
280 int ret = UnmapBuffer(buf);
281 if (ret != RET_OK) {
282 MS_LOG(WARNING) << "UnmapBuffer failed.";
283 }
284 iter->second->map_flags_ = false;
285 }
286 auto mem_buf = iter->second;
287 mem_buf->ref_count_ = 0;
288 allocated_list_.erase(iter);
289 free_list_.insert(std::make_pair(mem_buf->size_, mem_buf));
290 UnLock();
291 MS_LOG(DEBUG) << "Free device buffer. size: " << mem_buf->size_ << ", host addr: " << mem_buf->host_ptr_
292 << ", device addr: " << mem_buf->device_ptr_ << ", image addr: " << mem_buf->image_ptr_
293 << ", free list size: " << free_list_.size();
294 return;
295 }
296 UnLock();
297 MS_LOG(WARNING) << "Host ptr has freed";
298 }
RefCount(void * buf)299 int OpenCLAllocator::RefCount(void *buf) {
300 if (buf == nullptr) {
301 return -1;
302 }
303 Lock();
304 auto iter = allocated_list_.find(buf);
305 if (iter != allocated_list_.end()) {
306 auto mem_buf = iter->second;
307 int ref_count = std::atomic_load(&mem_buf->ref_count_);
308 UnLock();
309 return ref_count;
310 }
311 UnLock();
312 return -1;
313 }
SetRefCount(void * buf,int ref_count)314 int OpenCLAllocator::SetRefCount(void *buf, int ref_count) {
315 if (buf == nullptr) {
316 return -1;
317 }
318 Lock();
319 auto iter = allocated_list_.find(buf);
320 if (iter != allocated_list_.end()) {
321 auto mem_buf = iter->second;
322 std::atomic_store(&mem_buf->ref_count_, ref_count);
323 UnLock();
324 return ref_count;
325 }
326 UnLock();
327 return -1;
328 }
IncRefCount(void * buf,int ref_count)329 int OpenCLAllocator::IncRefCount(void *buf, int ref_count) {
330 if (buf == nullptr) {
331 return -1;
332 }
333 Lock();
334 auto iter = allocated_list_.find(buf);
335 if (iter != allocated_list_.end()) {
336 auto membuf = iter->second;
337 auto ref = std::atomic_fetch_add(&membuf->ref_count_, ref_count);
338 UnLock();
339 return (ref - ref_count);
340 }
341 UnLock();
342 return -1;
343 }
DecRefCount(void * buf,int ref_count)344 int OpenCLAllocator::DecRefCount(void *buf, int ref_count) {
345 if (buf == nullptr) {
346 return -1;
347 }
348 Lock();
349 auto iter = allocated_list_.find(buf);
350 if (iter != allocated_list_.end()) {
351 auto mem_buf = iter->second;
352 auto ref = std::atomic_fetch_sub(&mem_buf->ref_count_, ref_count);
353 UnLock();
354 return (ref - ref_count);
355 }
356 UnLock();
357 return -1;
358 }
359
total_size()360 size_t OpenCLAllocator::total_size() {
361 Lock();
362 size_t totalSize = 0;
363
364 for (auto it = allocated_list_.begin(); it != allocated_list_.end(); it++) {
365 totalSize += it->second->size_;
366 }
367
368 for (auto it = free_list_.begin(); it != free_list_.end(); it++) {
369 totalSize += it->second->size_;
370 }
371 UnLock();
372 return totalSize;
373 }
374
GetImage(void * buffer)375 cl::Image2D *OpenCLAllocator::GetImage(void *buffer) {
376 auto it = allocated_list_.find(buffer);
377 if (it != allocated_list_.end()) {
378 if (it->second->mem_type_ != MemType::IMG) {
379 return nullptr;
380 }
381 return reinterpret_cast<cl::Image2D *>(it->second->image_ptr_);
382 }
383 return nullptr;
384 }
385
GetOpenclMemPtr(void * buffer,MemType * type,bool force_buffer)386 void *OpenCLAllocator::GetOpenclMemPtr(void *buffer, MemType *type, bool force_buffer) {
387 auto it = allocated_list_.find(buffer);
388 if (it != allocated_list_.end()) {
389 if ((it->second->mem_type_ == MemType::IMG) && !force_buffer) {
390 *type = MemType::IMG;
391 return it->second->image_ptr_;
392 }
393 *type = MemType::BUF;
394 return it->second->device_ptr_;
395 }
396 return nullptr;
397 }
398
399 template <typename T>
ClearMemList(T * list)400 void OpenCLAllocator::ClearMemList(T *list) {
401 auto svm_capabilities = ocl_runtime_->GetSVMCapabilities();
402 for (auto it = list->begin(); it != list->end(); it++) {
403 if (it->second->map_flags_) {
404 int ret = UnmapBuffer(it->second->host_ptr_);
405 if (ret != RET_OK) {
406 MS_LOG(WARNING) << "UnmapBuffer failed.";
407 }
408 }
409 if (svm_capabilities) {
410 clSVMFree((*ocl_runtime_->Context())(), it->second->host_ptr_);
411 MS_LOG(DEBUG) << "OpenCL free svm buffer : " << it->second->host_ptr_;
412 } else {
413 cl::Buffer *buffer = static_cast<cl::Buffer *>(it->second->device_ptr_);
414 MS_LOG(DEBUG) << "OpenCL free device buffer : " << buffer;
415 if (buffer != nullptr) {
416 delete buffer;
417 it->second->device_ptr_ = nullptr;
418 }
419 cl::Image *image = static_cast<cl::Image *>(it->second->image_ptr_);
420 if (image != nullptr) {
421 delete image;
422 it->second->image_ptr_ = nullptr;
423 }
424 if (it->second->mem_type_ == MemType::SHARED) {
425 free(it->second->host_ptr_);
426 it->second->host_ptr_ = nullptr;
427 }
428 }
429 delete it->second;
430 }
431 list->clear();
432 }
433
Clear()434 void OpenCLAllocator::Clear() {
435 Lock();
436 ClearMemList<std::unordered_map<void *, MemBuf *>>(&allocated_list_);
437 ClearMemList<std::multimap<size_t, MemBuf *>>(&free_list_);
438 UnLock();
439 }
440
MapBuffer(void * host_ptr,int flags,void * command_queue,bool sync)441 void *OpenCLAllocator::MapBuffer(void *host_ptr, int flags, void *command_queue, bool sync) {
442 auto svm_capabilities = ocl_runtime_->GetSVMCapabilities();
443 if (svm_capabilities) {
444 if (!(svm_capabilities & CL_DEVICE_SVM_FINE_GRAIN_BUFFER)) {
445 auto it = allocated_list_.find(host_ptr);
446 if (it == allocated_list_.end()) {
447 MS_LOG(ERROR) << "Map buffer failed, can not found buffer :" << host_ptr;
448 return nullptr;
449 }
450 int ret = ocl_runtime_->MapBuffer(host_ptr, flags, it->second->size_,
451 static_cast<cl::CommandQueue *>(command_queue), sync);
452 if (ret != RET_OK) {
453 MS_LOG(WARNING) << "MapBuffer failed.";
454 }
455 }
456 return host_ptr;
457 }
458 Lock();
459 auto it = allocated_list_.find(host_ptr);
460 if (it == allocated_list_.end()) {
461 UnLock();
462 MS_LOG(ERROR) << "Map buffer failed, can not found buffer :" << host_ptr;
463 return nullptr;
464 }
465
466 if (it->second->map_flags_) {
467 UnLock();
468 MS_LOG(WARNING) << "Host ptr has mapped";
469 return host_ptr;
470 }
471 MemBuf *mem_buf = it->second;
472 MS_ASSERT(mem_buf);
473 if (mem_buf->mem_type_ == MemType::SHARED) {
474 UnLock();
475 MS_LOG(WARNING) << "Host ptr no need map";
476 return host_ptr;
477 }
478
479 void *new_host_ptr{nullptr};
480 if (mem_buf->mem_type_ == MemType::BUF) {
481 cl::Buffer *buffer = static_cast<cl::Buffer *>(mem_buf->device_ptr_);
482 MS_ASSERT(buffer);
483 new_host_ptr = ocl_runtime_->MapBuffer(*buffer, flags, mem_buf->size_, nullptr, sync);
484 } else if (mem_buf->mem_type_ == MemType::IMG) {
485 std::vector<size_t> region{mem_buf->img_size_.width, mem_buf->img_size_.height, 1};
486 cl::Image2D *image = static_cast<cl::Image2D *>(mem_buf->image_ptr_);
487 MS_ASSERT(image);
488 new_host_ptr = ocl_runtime_->MapBuffer(*image, sync, CL_MAP_READ | CL_MAP_WRITE, region);
489 }
490 if (new_host_ptr == nullptr) {
491 UnLock();
492 MS_LOG(WARNING) << "Map buffer failed, can not found buffer or already mapped, dev_ptr=" << mem_buf->device_ptr_
493 << ", host_ptr=" << host_ptr;
494 return nullptr;
495 }
496
497 mem_buf->map_flags_ = true;
498 mem_buf->host_ptr_ = new_host_ptr;
499 allocated_list_.erase(it);
500 allocated_list_[new_host_ptr] = mem_buf;
501 UnLock();
502 MS_LOG(DEBUG) << "Map buffer form " << host_ptr << " to " << new_host_ptr;
503 return new_host_ptr;
504 }
505
UnmapBuffer(void * host_ptr,void * command_queue)506 int OpenCLAllocator::UnmapBuffer(void *host_ptr, void *command_queue) {
507 auto svm_capabilities = ocl_runtime_->GetSVMCapabilities();
508 if (svm_capabilities) {
509 if (!(svm_capabilities & CL_DEVICE_SVM_FINE_GRAIN_BUFFER)) {
510 return ocl_runtime_->UnmapBuffer(host_ptr);
511 }
512 return RET_OK;
513 }
514 auto it = allocated_list_.find(host_ptr);
515 if (it == allocated_list_.end()) {
516 MS_LOG(ERROR) << "Map buffer failed, can not found buffer :" << host_ptr;
517 return RET_ERROR;
518 }
519 if (it->second->map_flags_) {
520 it->second->map_flags_ = false;
521 cl::Memory *mem = static_cast<cl::Memory *>(it->second->mem_type_ == MemType::BUF ? it->second->device_ptr_
522 : it->second->image_ptr_);
523 return ocl_runtime_->UnmapBuffer(*mem, it->second->host_ptr_, static_cast<cl::CommandQueue *>(command_queue));
524 } else {
525 MS_LOG(WARNING) << "Host ptr do not mapped";
526 return RET_OK;
527 }
528 }
529
GetMemType(void * host_ptr)530 MemType OpenCLAllocator::GetMemType(void *host_ptr) {
531 MemType mem_type{MemType::BUF};
532 Lock();
533 auto it = allocated_list_.find(host_ptr);
534 if (it == allocated_list_.end()) {
535 UnLock();
536 MS_LOG(ERROR) << "Can not found buffer :" << host_ptr;
537 return mem_type;
538 }
539 MemBuf *mem_buf = it->second;
540 MS_ASSERT(mem_buf);
541 mem_type = mem_buf->mem_type_;
542 UnLock();
543 return mem_type;
544 }
545
GetImageSize(void * host_ptr,ImageSize * img_size)546 int OpenCLAllocator::GetImageSize(void *host_ptr, ImageSize *img_size) {
547 MS_ASSERT(img_size);
548 Lock();
549 auto it = allocated_list_.find(host_ptr);
550 if (it == allocated_list_.end()) {
551 UnLock();
552 MS_LOG(ERROR) << "Can not found buffer :" << host_ptr;
553 return RET_OK;
554 }
555 MemBuf *mem_buf = it->second;
556 MS_ASSERT(mem_buf);
557 if (mem_buf->mem_type_ == MemType::IMG) {
558 *img_size = mem_buf->img_size_;
559 }
560 UnLock();
561 return RET_OK;
562 }
563 } // namespace mindspore::lite::opencl
564