File size: 3,810 Bytes
4a28d4d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
#include <cuda_runtime.h>
#include <cuda_runtime_api.h>
#include "src/turbomind/core/allocator.h"
#include "src/turbomind/core/check.h"
namespace turbomind::core {
AllocatorImpl::~AllocatorImpl() = default;
Stream AllocatorImpl::stream() const noexcept
{
return Stream{};
}
class CudaMemPoolAllocator: public AllocatorImpl {
public:
CudaMemPoolAllocator(Stream stream, bool use_default_pool):
pool_{}, stream_{stream}, device_{kDEVICE}, use_default_pool_{use_default_pool}
{
check_cuda_error(cudaGetDevice(&device_.id));
if (use_default_pool_) {
check_cuda_error(cudaDeviceGetDefaultMemPool(&pool_, device_.id));
}
else {
cudaMemPoolProps props{};
props.allocType = cudaMemAllocationTypePinned;
props.handleTypes = cudaMemHandleTypeNone;
props.location.type = cudaMemLocationTypeDevice;
props.location.id = device_.id;
check_cuda_error(cudaMemPoolCreate(&pool_, &props));
cuuint64_t thres = (cuuint64_t)-1;
check_cuda_error(cudaMemPoolSetAttribute(pool_, cudaMemPoolAttrReleaseThreshold, &thres));
}
}
~CudaMemPoolAllocator() override
{
if (!use_default_pool_) {
check_cuda_error(cudaMemPoolDestroy(pool_));
}
pool_ = {};
}
void* allocate(ssize_t size) override
{
void* ptr{};
check_cuda_error(cudaMallocFromPoolAsync(&ptr, size, pool_, stream_.handle()));
return ptr;
}
void deallocate(void* p, ssize_t) override
{
check_cuda_error(cudaFreeAsync(p, stream_.handle()));
}
Device device() const noexcept override
{
return device_;
}
Stream stream() const noexcept override
{
return stream_;
}
void trim(size_t bytes_to_keep)
{
check_cuda_error(cudaMemPoolTrimTo(pool_, bytes_to_keep));
}
private:
cudaMemPool_t pool_;
Stream stream_;
Device device_;
bool use_default_pool_;
};
class CudaAllocator: public AllocatorImpl {
public:
void* allocate(ssize_t size) override
{
void* ptr{};
check_cuda_error(cudaMalloc(&ptr, size));
return ptr;
}
void deallocate(void* p, ssize_t) override
{
check_cuda_error(cudaFree(p));
}
Device device() const noexcept override
{
return kDEVICE;
}
};
class CudaHostAllocator: public AllocatorImpl {
public:
void* allocate(ssize_t size) override
{
void* ptr{};
check_cuda_error(cudaHostAlloc(&ptr, size, cudaHostAllocDefault));
return ptr;
}
void deallocate(void* p, ssize_t) override
{
check_cuda_error(cudaFreeHost(p));
}
Device device() const noexcept override
{
return kCPUpinned;
}
};
class HostAllocator: public AllocatorImpl {
public:
void* allocate(ssize_t size) override
{
return ::operator new(size);
}
void deallocate(void* p, ssize_t) override
{
::operator delete(p);
}
Device device() const noexcept override
{
return kCPU;
}
};
Allocator::Allocator(DeviceType type)
{
impl_ = [&]() -> shared_ptr<AllocatorImpl> {
switch (type) {
case kCPU:
return std::make_shared<HostAllocator>();
case kDEVICE:
return std::make_shared<CudaAllocator>();
case kCPUpinned:
return std::make_shared<CudaHostAllocator>();
}
return {};
}();
TM_CHECK_NOTNULL(impl_);
}
Allocator::Allocator(Stream stream, bool use_default_pool)
{
impl_ = std::make_shared<CudaMemPoolAllocator>(std::move(stream), use_default_pool);
}
} // namespace turbomind::core
|