cuda memory封裝

來源

手寫AI

說明

這段代碼定義了一個(gè) BaseMemory 類,主要用于管理 CPU 和 GPU 內(nèi)存的分配和釋放。以下是其主要功能和操作:

  • GPU 內(nèi)存重新分配:gpu_realloc 方法用于重新分配 GPU 內(nèi)存,如果現(xiàn)有 GPU 內(nèi)存容量小于請求的大小。
  • CPU 內(nèi)存重新分配:cpu_realloc 方法用于重新分配 CPU 內(nèi)存,如果現(xiàn)有 CPU 內(nèi)存容量小于請求的大小。
  • 釋放 CPU 內(nèi)存:release_cpu 方法用于釋放 CPU 內(nèi)存,如果 cpu_ 不為空且當(dāng)前對象是該內(nèi)存的所有者。
  • 釋放 GPU 內(nèi)存:release_gpu 方法用于釋放 GPU 內(nèi)存,如果 gpu_ 不為空且當(dāng)前對象是該內(nèi)存的所有者。
  • 釋放所有內(nèi)存:release 方法用于同時(shí)釋放 CPU 和 GPU 內(nèi)存。
  • 內(nèi)存對齊函數(shù):upbound 函數(shù)用于將給定大小 n 向上對齊到 align 的最小倍數(shù)。
    內(nèi)存對齊是我修改后加到里面的,后續(xù)分配內(nèi)存不用手動(dòng)考慮內(nèi)存對齊

頭文件

memory.hpp

#ifndef __MEMORY_HPP__
#define __MEMORY_HPP__

#include <initializer_list>
#include <memory>
#include <string>
#include <vector>

namespace tensor
{

class BaseMemory
{
  public:
    BaseMemory() = default;
    BaseMemory(void *cpu, size_t cpu_bytes, void *gpu, size_t gpu_bytes);
    virtual ~BaseMemory();
    virtual void *gpu_realloc(size_t bytes);
    virtual void *cpu_realloc(size_t bytes);
    void release_gpu();
    void release_cpu();
    void release();
    inline bool owner_gpu() const { return owner_gpu_; }
    inline bool owner_cpu() const { return owner_cpu_; }
    inline size_t cpu_bytes() const { return cpu_bytes_; }
    inline size_t gpu_bytes() const { return gpu_bytes_; }
    virtual inline void *get_gpu() const { return gpu_; }
    virtual inline void *get_cpu() const { return cpu_; }
    void reference(void *cpu, size_t cpu_bytes, void *gpu, size_t gpu_bytes);

  protected:
    void *cpu_           = nullptr;
    size_t cpu_bytes_    = 0;
    size_t cpu_capacity_ = 0;
    bool owner_cpu_      = true;

    void *gpu_           = nullptr;
    size_t gpu_bytes_    = 0;
    size_t gpu_capacity_ = 0;
    bool owner_gpu_      = true;
};

template <typename _DT> class Memory : public BaseMemory
{
  public:
    Memory()                               = default;
    Memory(const Memory &other)            = delete;
    Memory &operator=(const Memory &other) = delete;
    virtual _DT *gpu(size_t size) { return (_DT *)BaseMemory::gpu_realloc(size * sizeof(_DT)); }
    virtual _DT *cpu(size_t size) { return (_DT *)BaseMemory::cpu_realloc(size * sizeof(_DT)); }

    inline size_t cpu_size() const { return cpu_bytes_ / sizeof(_DT); }
    inline size_t gpu_size() const { return gpu_bytes_ / sizeof(_DT); }

    virtual inline _DT *gpu() const { return (_DT *)gpu_; }
    virtual inline _DT *cpu() const { return (_DT *)cpu_; }
};

} // namespace tensor

實(shí)現(xiàn)代碼

memory.cu

#include "common/check.hpp"
#include "common/memory.hpp"
#include <cuda_runtime.h>

namespace tensor
{

using namespace std;

static size_t upbound(size_t n, size_t align) { return (n + align - 1) / align * align; }

BaseMemory::BaseMemory(void *cpu, size_t cpu_bytes, void *gpu, size_t gpu_bytes)
{
    reference(cpu, cpu_bytes, gpu, gpu_bytes);
}

void BaseMemory::reference(void *cpu, size_t cpu_bytes, void *gpu, size_t gpu_bytes)
{
    release();

    if (cpu == nullptr || cpu_bytes == 0)
    {
        cpu       = nullptr;
        cpu_bytes = 0;
    }

    if (gpu == nullptr || gpu_bytes == 0)
    {
        gpu       = nullptr;
        gpu_bytes = 0;
    }

    this->cpu_          = cpu;
    this->cpu_capacity_ = cpu_bytes;
    this->cpu_bytes_    = cpu_bytes;
    this->gpu_          = gpu;
    this->gpu_capacity_ = gpu_bytes;
    this->gpu_bytes_    = gpu_bytes;

    this->owner_cpu_ = !(cpu && cpu_bytes > 0);
    this->owner_gpu_ = !(gpu && gpu_bytes > 0);
}

BaseMemory::~BaseMemory() { release(); }

void *BaseMemory::gpu_realloc(size_t bytes)
{
    // 內(nèi)存對齊
    size_t size = upbound(bytes, 32);
    if (gpu_capacity_ < size)
    {
        release_gpu();

        gpu_capacity_ = size;
        checkRuntime(cudaMalloc(&gpu_, size));
        // checkRuntime(cudaMemset(gpu_, 0, size));
    }
    gpu_bytes_ = bytes;
    return gpu_;
}

void *BaseMemory::cpu_realloc(size_t bytes)
{
    size_t size = upbound(bytes, 32);
    if (cpu_capacity_ < size)
    {
        release_cpu();

        cpu_capacity_ = size;
        checkRuntime(cudaMallocHost(&cpu_, size));
        Assert(cpu_ != nullptr);
        // memset(cpu_, 0, size);
    }
    cpu_bytes_ = bytes;
    return cpu_;
}

void BaseMemory::release_cpu()
{
    if (cpu_)
    {
        if (owner_cpu_)
        {
            checkRuntime(cudaFreeHost(cpu_));
        }
        cpu_ = nullptr;
    }
    cpu_capacity_ = 0;
    cpu_bytes_    = 0;
}

void BaseMemory::release_gpu()
{
    if (gpu_)
    {
        if (owner_gpu_)
        {
            checkRuntime(cudaFree(gpu_));
        }
        gpu_ = nullptr;
    }
    gpu_capacity_ = 0;
    gpu_bytes_    = 0;
}

void BaseMemory::release()
{
    release_cpu();
    release_gpu();
}

} // namespace tensor

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容