Program Listing for File buffer.hpp
↰ Return to documentation for file (include/holoscan/utils/cuda/buffer.hpp)
/*
* SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed 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.
*/
#ifndef HOLOSCAN_UTILS_CUDA_BUFFER_HPP
#define HOLOSCAN_UTILS_CUDA_BUFFER_HPP
#include <cuda_runtime.h>
#include <memory>
#include <stdexcept>
namespace holoscan {
namespace utils {
namespace cuda {
enum class BufferDataType {
Float32 = 0,
Int8 = 1,
Int32 = 2,
Int64 = 3,
UInt8 = 4,
Float16 = 5,
Unsupported = 6
};
inline uint32_t get_element_size(BufferDataType data_type) noexcept {
switch (data_type) {
case BufferDataType::Float32:
case BufferDataType::Int32:
return 4;
case BufferDataType::Int64:
return 8;
case BufferDataType::Int8:
case BufferDataType::UInt8:
return 1;
case BufferDataType::Float16:
return 2;
case BufferDataType::Unsupported:
return 0;
}
return 0;
}
class CudaAllocator {
public:
bool operator()(void** ptr, size_t size) const { return cudaMalloc(ptr, size) == cudaSuccess; }
};
class CudaFree {
public:
void operator()(void* ptr) const {
if (ptr) {
cudaFree(ptr);
}
}
};
class CudaHostMappedAllocator {
public:
bool operator()(void** ptr, size_t size) const {
return cudaHostAlloc(ptr, size, cudaHostAllocMapped) == cudaSuccess;
}
};
class CudaHostFree {
public:
void operator()(void* ptr) const {
if (ptr) {
cudaFreeHost(ptr);
}
}
};
class Buffer {
public:
explicit Buffer(BufferDataType data_type = BufferDataType::Float32, int device_id = 0)
: data_type_(data_type), device_id_(device_id) {}
virtual ~Buffer() = default;
virtual void* data() = 0;
virtual size_t size() const = 0;
virtual size_t get_bytes() const = 0;
virtual void resize(size_t number_of_elements) = 0;
BufferDataType get_datatype() const { return data_type_; }
int get_device() const { return device_id_; }
protected:
BufferDataType data_type_;
int device_id_;
};
class DeviceBuffer : public Buffer {
public:
explicit DeviceBuffer(size_t size, int device_id = 0);
~DeviceBuffer();
// Delete copy operations to prevent double-free errors
DeviceBuffer(const DeviceBuffer&) = delete;
DeviceBuffer& operator=(const DeviceBuffer&) = delete;
// Delete move operations
DeviceBuffer(DeviceBuffer&&) = delete;
DeviceBuffer& operator=(DeviceBuffer&&) = delete;
void* data() override;
size_t size() const override;
size_t get_bytes() const override;
void resize(size_t number_of_elements) override;
private:
size_t size_{0}, capacity_{0};
void* buffer_ = nullptr;
CudaAllocator allocator_;
CudaFree free_;
};
class CudaHostMappedBuffer : public Buffer {
public:
explicit CudaHostMappedBuffer(size_t size, int device_id = 0);
~CudaHostMappedBuffer();
void* data() override;
size_t size() const override;
size_t get_bytes() const override;
void resize(size_t number_of_elements) override;
void* device_data() const { return device_buffer_; }
private:
size_t size_{0}, capacity_{0};
void *buffer_ = nullptr, *device_buffer_ = nullptr;
CudaHostMappedAllocator allocator_;
CudaHostFree free_;
};
} // namespace cuda
} // namespace utils
} // namespace holoscan
#endif// HOLOSCAN_UTILS_CUDA_BUFFER_HPP