|
| 1 | +// Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// |
| 3 | +// Redistribution and use in source and binary forms, with or without |
| 4 | +// modification, are permitted provided that the following conditions |
| 5 | +// are met: |
| 6 | +// * Redistributions of source code must retain the above copyright |
| 7 | +// notice, this list of conditions and the following disclaimer. |
| 8 | +// * Redistributions in binary form must reproduce the above copyright |
| 9 | +// notice, this list of conditions and the following disclaimer in the |
| 10 | +// documentation and/or other materials provided with the distribution. |
| 11 | +// * Neither the name of NVIDIA CORPORATION nor the names of its |
| 12 | +// contributors may be used to endorse or promote products derived |
| 13 | +// from this software without specific prior written permission. |
| 14 | +// |
| 15 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY |
| 16 | +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 18 | +// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 19 | +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 | +// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 22 | +// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 23 | +// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | + |
| 27 | +#include "memory_manager.h" |
| 28 | + |
| 29 | + |
| 30 | +namespace triton { namespace backend { namespace python { |
| 31 | + |
| 32 | + |
| 33 | +#ifdef TRITON_ENABLE_GPU |
| 34 | +GPUMemoryRecord::GPUMemoryRecord(void* ptr) |
| 35 | +{ |
| 36 | + ptr_ = ptr; |
| 37 | + release_callback_ = [](void* ptr) { |
| 38 | + cudaError_t err = cudaFree(ptr); |
| 39 | + if (err != cudaSuccess) { |
| 40 | + LOG_MESSAGE( |
| 41 | + TRITONSERVER_LOG_ERROR, |
| 42 | + (std::string("Failed to free the allocated cuda memory. error: ") + |
| 43 | + cudaGetErrorString(err)) |
| 44 | + .c_str()); |
| 45 | + } |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | +void* |
| 50 | +GPUMemoryRecord::MemoryId() |
| 51 | +{ |
| 52 | + return ptr_; |
| 53 | +} |
| 54 | + |
| 55 | +const std::function<void(void*)>& |
| 56 | +GPUMemoryRecord::ReleaseCallback() |
| 57 | +{ |
| 58 | + return release_callback_; |
| 59 | +} |
| 60 | +#endif |
| 61 | + |
| 62 | +MemoryManager::MemoryManager( |
| 63 | + std::unique_ptr<MessageQueue<uint64_t>>&& memory_message_queue) |
| 64 | +{ |
| 65 | + message_queue_ = std::move(memory_message_queue); |
| 66 | + record_count_ = 0; |
| 67 | + thread_ = std::thread(&MemoryManager::QueueMonitorThread, this); |
| 68 | +} |
| 69 | + |
| 70 | +uint64_t |
| 71 | +MemoryManager::AddRecord(std::unique_ptr<MemoryRecord>&& memory_record) |
| 72 | +{ |
| 73 | + std::lock_guard<std::mutex> lock{mu_}; |
| 74 | + |
| 75 | + record_count_++; |
| 76 | + records_.emplace(record_count_, std::move(memory_record)); |
| 77 | + |
| 78 | + return record_count_; |
| 79 | +} |
| 80 | + |
| 81 | +void |
| 82 | +MemoryManager::QueueMonitorThread() |
| 83 | +{ |
| 84 | + while (true) { |
| 85 | + uint64_t memory = message_queue_->Pop(); |
| 86 | + if (memory == 0) { |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + { |
| 91 | + std::lock_guard<std::mutex> lock{mu_}; |
| 92 | + auto it = records_.find(memory); |
| 93 | + if (it == records_.end()) { |
| 94 | + LOG_MESSAGE( |
| 95 | + TRITONSERVER_LOG_ERROR, |
| 96 | + "Unexpected memory index received for deallocation."); |
| 97 | + continue; |
| 98 | + } |
| 99 | + |
| 100 | + // Call the release callback. |
| 101 | + it->second->ReleaseCallback()(it->second->MemoryId()); |
| 102 | + records_.erase(it); |
| 103 | + |
| 104 | + // Reset the record_count_ when the number of records is zero. |
| 105 | + if (records_.size() == 0) |
| 106 | + record_count_ = 0; |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +MemoryManager::~MemoryManager() |
| 112 | +{ |
| 113 | + // Push a dummy 0 message that will trigger the destruction of the background |
| 114 | + // thread. |
| 115 | + message_queue_->Push(0); |
| 116 | + thread_.join(); |
| 117 | +} |
| 118 | + |
| 119 | +}}}; // namespace triton::backend::python |
0 commit comments