GpuRgbMemory.hpp 1.73 KB
#include<string>

#include "cuda_kernels.h"

using namespace std;

static long long get_current_time(){
    chrono::time_point<chrono::system_clock, chrono::milliseconds> tpMicro
        = chrono::time_point_cast<chrono::milliseconds>(chrono::system_clock::now());
    return tpMicro.time_since_epoch().count();
}

class GpuRgbMemory{

public:
     GpuRgbMemory(int _channel, int _width, int _height, string _id, string _gpuid, bool _isused){
        channel = _channel;
        width = _width;
        height = _height;
        size = channel * width * height;
        isused = _isused;
        id = _id;
        gpuid = _gpuid;
        timestamp = get_current_time();

        cudaSetDevice(atoi(gpuid.c_str()));
        cudaMalloc((void **)&pHwRgb, size * sizeof(unsigned char));
    }

    ~GpuRgbMemory(){
        if (pHwRgb) {
            cudaSetDevice(atoi(gpuid.c_str()));
            cudaFree(pHwRgb);
            pHwRgb = nullptr;
        }
    }
    
    int getSize() {
        return size;
    }
    
    bool isIsused() {
        return isused;
    }

    void setIsused(bool _isused) {
        isused = _isused;
        // 更新时间戳
        timestamp = get_current_time();
    }

    string getId() {
        return id;
    }

    string getGpuId() {
        return gpuid;
    }

    unsigned char* getMem(){
        return pHwRgb;
    }

    long long getTimesstamp(){
        return timestamp;
    }

    int getWidth(){
        return width;
    }

    int getHeight(){
        return height;
    }

    int getChannel(){
        return channel;
    }

private:
    int size;
    bool isused;
    string id;
    string gpuid;
    unsigned char * pHwRgb{nullptr};
    long long timestamp;
    int width{0};
    int height{0};
    int channel{3};
};