device.cpp 1.84 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
2
3
4
5
6
7
#include "device.hpp"

DeviceMem::DeviceMem(std::size_t mem_size) : mMemSize(mem_size)
{
    hipGetErrorString(hipMalloc(static_cast<void**>(&mpDeviceBuf), mMemSize));
}

Jing Zhang's avatar
Jing Zhang committed
8
9
10
11
12
13
DeviceMem::DeviceMem(const DeviceMem& p) : mpDeviceBuf(p.mpDeviceBuf), mMemSize(p.mMemSize)
{
    // hipGetErrorString(hipMalloc(static_cast<void**>(&mpDeviceBuf), mMemSize));
    // hipGetErrorString(hipMemcpy(mpDeviceBuf, p.mpDeviceBuf, mMemSize, hipMemcpyDeviceToDevice));
}

Chao Liu's avatar
Chao Liu committed
14
15
16
17
18
19
20
21
22
23
24
25
26
void* DeviceMem::GetDeviceBuffer() { return mpDeviceBuf; }

void DeviceMem::ToDevice(const void* p)
{
    hipGetErrorString(
        hipMemcpy(mpDeviceBuf, const_cast<void*>(p), mMemSize, hipMemcpyHostToDevice));
}

void DeviceMem::FromDevice(void* p)
{
    hipGetErrorString(hipMemcpy(p, mpDeviceBuf, mMemSize, hipMemcpyDeviceToHost));
}

27
DeviceMem::~DeviceMem() { hipGetErrorString(hipFree(mpDeviceBuf)); }
Chao Liu's avatar
Chao Liu committed
28
29
30
31
32

struct KernelTimerImpl
{
    KernelTimerImpl()
    {
Chao Liu's avatar
Chao Liu committed
33
34
        hipGetErrorString(hipEventCreate(&mStart));
        hipGetErrorString(hipEventCreate(&mEnd));
Chao Liu's avatar
Chao Liu committed
35
36
37
38
    }

    ~KernelTimerImpl()
    {
Chao Liu's avatar
Chao Liu committed
39
40
        hipGetErrorString(hipEventDestroy(mStart));
        hipGetErrorString(hipEventDestroy(mEnd));
Chao Liu's avatar
Chao Liu committed
41
42
43
44
    }

    void Start()
    {
Chao Liu's avatar
Chao Liu committed
45
46
        hipGetErrorString(hipDeviceSynchronize());
        hipGetErrorString(hipEventRecord(mStart, nullptr));
Chao Liu's avatar
Chao Liu committed
47
48
49
50
    }

    void End()
    {
Chao Liu's avatar
Chao Liu committed
51
52
        hipGetErrorString(hipEventRecord(mEnd, nullptr));
        hipGetErrorString(hipEventSynchronize(mEnd));
Chao Liu's avatar
Chao Liu committed
53
54
55
56
57
    }

    float GetElapsedTime() const
    {
        float time;
Chao Liu's avatar
Chao Liu committed
58
        hipGetErrorString(hipEventElapsedTime(&time, mStart, mEnd));
Chao Liu's avatar
Chao Liu committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
        return time;
    }

    hipEvent_t mStart, mEnd;
};

KernelTimer::KernelTimer() : impl(new KernelTimerImpl()) {}

KernelTimer::~KernelTimer() {}

void KernelTimer::Start() { impl->Start(); }

void KernelTimer::End() { impl->End(); }

float KernelTimer::GetElapsedTime() const { return impl->GetElapsedTime(); }