srkreddy1238 commented on code in PR #15058: URL: https://github.com/apache/tvm/pull/15058#discussion_r1229030103
########## src/runtime/opencl/memory_pool.cc: ########## @@ -0,0 +1,186 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/*! + * \file memory_pool.h + * \brief OpenCL Workspace pool utility. + */ +#include "memory_pool.h" + +#include <memory> + +#include "opencl_common.h" + +#define ALIGN_UP(num, align) (((num) + ((align)-1)) & ~((align)-1)) + +namespace tvm { +namespace runtime { +namespace cl { + +void* Pool::Alloc(Device dev, DeviceAPI* device, size_t nbytes) { + Entry e; + DLDataType type; + type.code = kDLUInt; + type.bits = 8; + type.lanes = 1; + if (free_list_.size() == 2) { + e = free_list_.back(); + free_list_.pop_back(); + if (e.size < nbytes) { + // resize the page + OpenCLWorkspace::Global()->FreeCLBuffer(dev, e.data); + e.data = OpenCLWorkspace::Global()->AllocCLBuffer(dev, nbytes, kTempAllocaAlignment, type); + e.size = nbytes; + } + } else if (free_list_.size() == 1) { + e.data = OpenCLWorkspace::Global()->AllocCLBuffer(dev, nbytes, kTempAllocaAlignment, type); + e.size = nbytes; + } else { + if (free_list_.back().size >= nbytes) { + // find smallest fit + auto it = free_list_.end() - 2; + for (; it->size >= nbytes; --it) { + } + e = *(it + 1); + free_list_.erase(it + 1); + } else { + // resize the page + e = free_list_.back(); + free_list_.pop_back(); + OpenCLWorkspace::Global()->FreeCLBuffer(dev, e.data); + e.data = OpenCLWorkspace::Global()->AllocCLBuffer(dev, nbytes, kTempAllocaAlignment, type); + e.size = nbytes; + } + } + e.dev = dev; + allocated_.push_back(e); + return e.data; +} + +void Pool::Free(void* data) { + Entry e; + if (allocated_.back().data == data) { + // quick path, last allocated. + e = allocated_.back(); + allocated_.pop_back(); + } else { + int index = static_cast<int>(allocated_.size()) - 2; + for (; index > 0 && allocated_[index].data != data; --index) { + } + ICHECK_GT(index, 0) << "trying to free things that has not been allocated"; + e = allocated_[index]; + allocated_.erase(allocated_.begin() + index); + } + if (free_list_.back().size < e.size) { + free_list_.push_back(e); + } else if (free_list_.size() == 2) { + free_list_.push_back(free_list_.back()); + free_list_[1] = e; + } else { + size_t i = free_list_.size() - 1; + free_list_.resize(free_list_.size() + 1); + for (; e.size < free_list_[i].size; --i) { + free_list_[i + 1] = free_list_[i]; + } + free_list_[i + 1] = e; + } +} + +void Pool::Release(Device dev, DeviceAPI* device) { + for (size_t i = 1; i < free_list_.size(); ++i) { + OpenCLWorkspace::Global()->FreeCLBuffer(dev, free_list_[i].data); + } + free_list_.clear(); +} + +MemoryPool::MemoryPool(DLDeviceType device_type, DeviceAPI* device) + : device_type_(device_type), device_(device) {} + +MemoryPool::~MemoryPool() { + for (size_t i = 0; i < array_.size(); ++i) { + if (array_[i] != nullptr) { + Device dev; + dev.device_type = device_type_; + dev.device_id = static_cast<int>(i); + array_[i]->Release(dev, device_); + delete array_[i]; + } + } +} + +void MemoryPool::InitDevicePool(int device_id) { + if (static_cast<size_t>(device_id) >= array_.size()) { + array_.resize(device_id + 1, nullptr); + } + if (array_[device_id] == nullptr) { + array_[device_id] = new Pool(); + } +} + +void* MemoryPool::AllocMemory(Device dev, size_t size, DLDataType type_hint) { + InitDevicePool(dev.device_id); + return array_[dev.device_id]->Alloc(dev, device_, size); +} + +size_t GetRowPitch(Device dev, size_t width, DLDataType type_hint) { + cl_uint row_align = OpenCLWorkspace::Global()->GetImageAlignment(dev.device_id); + size_t pixel_size = (type_hint.bits * type_hint.lanes + 7) / 8; + return ALIGN_UP(width * pixel_size * 4, row_align); // CL_RGBA = 4 +} + +void* MemoryPool::AllocMemory(Device dev, size_t width, size_t height, DLDataType type_hint, + Optional<String> mem_scope) { + InitDevicePool(dev.device_id); + + // Texture allocation given width and height + size_t row_pitch = GetRowPitch(dev, width, type_hint); + size_t mem_size = row_pitch * height; + + // Alloc back buffer from pool + void* back_buffer = nullptr; + if (OpenCLWorkspace::Global()->IsBufferToImageSupported(dev.device_id)) { + back_buffer = array_[dev.device_id]->Alloc(dev, device_, mem_size); + } + + if (!mem_scope.defined()) { + mem_scope = Optional<String>("global.texture"); + } + return OpenCLWorkspace::Global()->AllocCLImage(dev, back_buffer, width, height, row_pitch, Review Comment: No problem. In fall back path (IsBufferToImageSupported is not supported) where we allocate Image2D with out back buffer. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
