device_tensor.cuh 1.11 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#pragma once
#include "helper_cuda.h"
#include "tensor.hpp"

struct DeviceTensorDescriptor
{
    DeviceTensorDescriptor() = delete;

    __host__ DeviceTensorDescriptor(const TensorDescriptor& host_desc)
        : mDataType(host_desc.GetDataType()), mDim(host_desc.GetDimension())
    {
        std::size_t data_sz = host_desc.GetDataType() == DataType_t::Float ? 4 : 2;

        checkCudaErrors(cudaMalloc(&mpLengths, data_sz * mDim));
        checkCudaErrors(cudaMalloc(&mpStrides, data_sz * mDim));

Chao Liu's avatar
Chao Liu committed
17
18
19
20
        checkCudaErrors(cudaMemcpy(
            mpLengths, host_desc.GetLengths().data(), data_sz * mDim, cudaMemcpyHostToDevice));
        checkCudaErrors(cudaMemcpy(
            mpStrides, host_desc.GetStrides().data(), data_sz * mDim, cudaMemcpyHostToDevice));
Chao Liu's avatar
Chao Liu committed
21
22
23
24
    }

    __host__ ~DeviceTensorDescriptor()
    {
Chao Liu's avatar
Chao Liu committed
25
26
27
28
29
30
#if 0
        if(mpLengths != nullptr)
            checkCudaErrors(cudaFree(mpLengths));
        if(mpStrides != nullptr)
            checkCudaErrors(cudaFree(mpStrides));
#endif
Chao Liu's avatar
Chao Liu committed
31
32
33
34
    }

    DataType_t mDataType;
    unsigned long mDim;
Chao Liu's avatar
Chao Liu committed
35
36
    unsigned long* mpLengths = nullptr;
    unsigned long* mpStrides = nullptr;
Chao Liu's avatar
Chao Liu committed
37
};