|
| 1 | +// Copyright 2024, 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 "correlation_id.h" |
| 28 | + |
| 29 | +namespace triton { namespace backend { namespace python { |
| 30 | + |
| 31 | +CorrelationId::CorrelationId() |
| 32 | + : id_string_(""), id_uint_(0), id_type_(CorrelationIdDataType::UINT64) |
| 33 | +{ |
| 34 | +} |
| 35 | + |
| 36 | +CorrelationId::CorrelationId(const std::string& id_string) |
| 37 | + : id_string_(id_string), id_uint_(0), |
| 38 | + id_type_(CorrelationIdDataType::STRING) |
| 39 | +{ |
| 40 | +} |
| 41 | + |
| 42 | +CorrelationId::CorrelationId(uint64_t id_uint) |
| 43 | + : id_string_(""), id_uint_(id_uint), id_type_(CorrelationIdDataType::UINT64) |
| 44 | +{ |
| 45 | +} |
| 46 | + |
| 47 | +CorrelationId::CorrelationId(const CorrelationId& rhs) |
| 48 | +{ |
| 49 | + id_uint_ = rhs.id_uint_; |
| 50 | + id_type_ = rhs.id_type_; |
| 51 | + id_string_ = rhs.id_string_; |
| 52 | +} |
| 53 | + |
| 54 | +CorrelationId::CorrelationId(std::unique_ptr<CorrelationId>& correlation_id_shm) |
| 55 | +{ |
| 56 | + id_uint_ = correlation_id_shm->id_uint_; |
| 57 | + id_type_ = correlation_id_shm->id_type_; |
| 58 | + id_string_ = correlation_id_shm->id_string_; |
| 59 | +} |
| 60 | + |
| 61 | +CorrelationId& |
| 62 | +CorrelationId::operator=(const CorrelationId& rhs) |
| 63 | +{ |
| 64 | + id_uint_ = rhs.id_uint_; |
| 65 | + id_type_ = rhs.id_type_; |
| 66 | + id_string_ = rhs.id_string_; |
| 67 | + return *this; |
| 68 | +} |
| 69 | + |
| 70 | +void |
| 71 | +CorrelationId::SaveToSharedMemory( |
| 72 | + std::unique_ptr<SharedMemoryManager>& shm_pool) |
| 73 | +{ |
| 74 | + AllocatedSharedMemory<CorrelationIdShm> correlation_id_shm = |
| 75 | + shm_pool->Construct<CorrelationIdShm>(); |
| 76 | + correlation_id_shm_ptr_ = correlation_id_shm.data_.get(); |
| 77 | + |
| 78 | + std::unique_ptr<PbString> id_string_shm = |
| 79 | + PbString::Create(shm_pool, id_string_); |
| 80 | + |
| 81 | + correlation_id_shm_ptr_->id_uint = id_uint_; |
| 82 | + correlation_id_shm_ptr_->id_string_shm_handle = id_string_shm->ShmHandle(); |
| 83 | + correlation_id_shm_ptr_->id_type = id_type_; |
| 84 | + |
| 85 | + // Save the references to shared memory. |
| 86 | + correlation_id_shm_ = std::move(correlation_id_shm); |
| 87 | + id_string_shm_ = std::move(id_string_shm); |
| 88 | + shm_handle_ = correlation_id_shm_.handle_; |
| 89 | +} |
| 90 | + |
| 91 | +std::unique_ptr<CorrelationId> |
| 92 | +CorrelationId::LoadFromSharedMemory( |
| 93 | + std::unique_ptr<SharedMemoryManager>& shm_pool, |
| 94 | + bi::managed_external_buffer::handle_t handle) |
| 95 | +{ |
| 96 | + AllocatedSharedMemory<CorrelationIdShm> correlation_id_shm = |
| 97 | + shm_pool->Load<CorrelationIdShm>(handle); |
| 98 | + CorrelationIdShm* correlation_id_shm_ptr = correlation_id_shm.data_.get(); |
| 99 | + |
| 100 | + std::unique_ptr<PbString> id_string_shm = PbString::LoadFromSharedMemory( |
| 101 | + shm_pool, correlation_id_shm_ptr->id_string_shm_handle); |
| 102 | + |
| 103 | + return std::unique_ptr<CorrelationId>( |
| 104 | + new CorrelationId(correlation_id_shm, id_string_shm)); |
| 105 | +} |
| 106 | + |
| 107 | +CorrelationId::CorrelationId( |
| 108 | + AllocatedSharedMemory<CorrelationIdShm>& correlation_id_shm, |
| 109 | + std::unique_ptr<PbString>& id_string_shm) |
| 110 | + : correlation_id_shm_(std::move(correlation_id_shm)), |
| 111 | + id_string_shm_(std::move(id_string_shm)) |
| 112 | +{ |
| 113 | + correlation_id_shm_ptr_ = correlation_id_shm_.data_.get(); |
| 114 | + shm_handle_ = correlation_id_shm_.handle_; |
| 115 | + id_string_ = id_string_shm_->String(); |
| 116 | + id_uint_ = correlation_id_shm_ptr_->id_uint; |
| 117 | + id_type_ = correlation_id_shm_ptr_->id_type; |
| 118 | +} |
| 119 | + |
| 120 | +}}}; // namespace triton::backend::python |
0 commit comments