device.cpp 3.63 KB
Newer Older
1
#include <chrono>
Chao Liu's avatar
Chao Liu committed
2
3
4
5
6
7
8
9
10
#include "device.hpp"

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

void* DeviceMem::GetDeviceBuffer() { return mpDeviceBuf; }

Chao Liu's avatar
Chao Liu committed
11
12
std::size_t DeviceMem::GetBufferSize() { return mMemSize; }

Chao Liu's avatar
Chao Liu committed
13
14
15
16
17
18
19
20
21
22
23
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));
}

Chao Liu's avatar
Chao Liu committed
24
25
void DeviceMem::SetZero() { hipGetErrorString(hipMemset(mpDeviceBuf, 0, mMemSize)); }

26
DeviceMem::~DeviceMem() { hipGetErrorString(hipFree(mpDeviceBuf)); }
Chao Liu's avatar
Chao Liu committed
27

28
29
30
DeviceAlignedMemCPU::DeviceAlignedMemCPU(std::size_t mem_size, std::size_t alignment)
    : mMemSize(mem_size), mAlignment(alignment)
{
31
32
33
34
35
36
37
    if(mem_size == 0)
    {
        mpDeviceBuf = nullptr;
    }
    else
    {
        assert(!(alignment == 0 || (alignment & (alignment - 1)))); // check pow of 2
38

39
40
41
42
43
        void* p1;
        void** p2;
        int offset = alignment - 1 + sizeof(void*);
        p1         = malloc(mem_size + offset);
        assert(p1 != nullptr);
44

45
46
47
48
        p2 = reinterpret_cast<void**>((reinterpret_cast<size_t>(p1) + offset) & ~(alignment - 1));
        p2[-1]      = p1;
        mpDeviceBuf = reinterpret_cast<void*>(p2);
    }
49
50
51
52
53
54
}

void* DeviceAlignedMemCPU::GetDeviceBuffer() { return mpDeviceBuf; }

std::size_t DeviceAlignedMemCPU::GetBufferSize() { return mMemSize; }

55
56
57
58
void DeviceAlignedMemCPU::ToDevice(const void* p) { memcpy(mpDeviceBuf, p, mMemSize); }

void DeviceAlignedMemCPU::FromDevice(void* p) { memcpy(p, mpDeviceBuf, mMemSize); }

59
60
void DeviceAlignedMemCPU::SetZero() { memset(mpDeviceBuf, 0, mMemSize); }

61
62
63
64
65
DeviceAlignedMemCPU::~DeviceAlignedMemCPU()
{
    if(mpDeviceBuf != nullptr)
        free((reinterpret_cast<void**>(mpDeviceBuf))[-1]);
}
66

Chao Liu's avatar
Chao Liu committed
67
68
69
70
struct KernelTimerImpl
{
    KernelTimerImpl()
    {
Chao Liu's avatar
Chao Liu committed
71
72
        hipGetErrorString(hipEventCreate(&mStart));
        hipGetErrorString(hipEventCreate(&mEnd));
Chao Liu's avatar
Chao Liu committed
73
74
75
76
    }

    ~KernelTimerImpl()
    {
Chao Liu's avatar
Chao Liu committed
77
78
        hipGetErrorString(hipEventDestroy(mStart));
        hipGetErrorString(hipEventDestroy(mEnd));
Chao Liu's avatar
Chao Liu committed
79
80
81
82
    }

    void Start()
    {
Chao Liu's avatar
Chao Liu committed
83
84
        hipGetErrorString(hipDeviceSynchronize());
        hipGetErrorString(hipEventRecord(mStart, nullptr));
Chao Liu's avatar
Chao Liu committed
85
86
87
88
    }

    void End()
    {
Chao Liu's avatar
Chao Liu committed
89
90
        hipGetErrorString(hipEventRecord(mEnd, nullptr));
        hipGetErrorString(hipEventSynchronize(mEnd));
Chao Liu's avatar
Chao Liu committed
91
92
93
94
95
    }

    float GetElapsedTime() const
    {
        float time;
Chao Liu's avatar
Chao Liu committed
96
        hipGetErrorString(hipEventElapsedTime(&time, mStart, mEnd));
Chao Liu's avatar
Chao Liu committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
        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(); }
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

struct WallTimerImpl
{
    void Start() { mStart = std::chrono::high_resolution_clock::now(); }

    void End() { mStop = std::chrono::high_resolution_clock::now(); }

    float GetElapsedTime() const
    {
        return static_cast<float>(
                   std::chrono::duration_cast<std::chrono::microseconds>(mStop - mStart).count()) *
               1e-3;
    }

    std::chrono::time_point<std::chrono::high_resolution_clock> mStart;
    std::chrono::time_point<std::chrono::high_resolution_clock> mStop;
};

WallTimer::WallTimer() : impl(new WallTimerImpl()) {}

WallTimer::~WallTimer() {}

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

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

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