device.hpp 2.96 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
2
#ifndef DEVICE_HPP
#define DEVICE_HPP
3

Chao Liu's avatar
Chao Liu committed
4
#include <memory>
5
#include <functional>
6
7
#include "hip/hip_runtime.h"
#include "hip/hip_fp16.h"
8

Chao Liu's avatar
Chao Liu committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
struct DeviceMem
{
    DeviceMem() = delete;
    DeviceMem(std::size_t mem_size);
    void* GetDeviceBuffer();
    void ToDevice(const void* p);
    void FromDevice(void* p);
    ~DeviceMem();

    void* mpDeviceBuf;
    std::size_t mMemSize;
};

struct KernelTimerImpl;

struct KernelTimer
{
    KernelTimer();
    ~KernelTimer();
    void Start();
    void End();
    float GetElapsedTime() const;

    std::unique_ptr<KernelTimerImpl> impl;
};

35
36
using device_stream_t = hipStream_t;

Chao Liu's avatar
Chao Liu committed
37
template <typename... Args, typename F>
Chao Liu's avatar
tidy  
Chao Liu committed
38
void launch_kernel(F kernel, dim3 grid_dim, dim3 block_dim, std::size_t lds_byte, Args... args)
39
{
Chao Liu's avatar
tidy  
Chao Liu committed
40
41
    hipStream_t stream_id = nullptr;

42
43
44
45
    hipLaunchKernelGGL(kernel, grid_dim, block_dim, lds_byte, stream_id, args...);
}

template <typename... Args, typename F>
Chao Liu's avatar
tidy  
Chao Liu committed
46
47
float launch_and_time_kernel(
    F kernel, int nrepeat, dim3 grid_dim, dim3 block_dim, std::size_t lds_byte, Args... args)
Chao Liu's avatar
Chao Liu committed
48
49
50
{
    KernelTimer timer;

Chao Liu's avatar
Chao Liu committed
51
    printf("%s: grid_dim {%d, %d, %d}, block_dim {%d, %d, %d} \n",
Chao Liu's avatar
Chao Liu committed
52
53
54
55
56
57
58
59
60
           __func__,
           grid_dim.x,
           grid_dim.y,
           grid_dim.z,
           block_dim.x,
           block_dim.y,
           block_dim.z);

    printf("Warm up\n");
Chao Liu's avatar
Chao Liu committed
61

Chao Liu's avatar
tidy  
Chao Liu committed
62
63
    hipStream_t stream_id = nullptr;

Chao Liu's avatar
Chao Liu committed
64
    // warm up
65
    hipLaunchKernelGGL(kernel, grid_dim, block_dim, lds_byte, stream_id, args...);
Chao Liu's avatar
Chao Liu committed
66

Chao Liu's avatar
Chao Liu committed
67
    printf("Start running %d times...\n", nrepeat);
Chao Liu's avatar
Chao Liu committed
68

Chao Liu's avatar
Chao Liu committed
69
70
71
72
73
74
75
76
    timer.Start();

    for(int i = 0; i < nrepeat; ++i)
    {
        hipLaunchKernelGGL(kernel, grid_dim, block_dim, lds_byte, stream_id, args...);
    }

    timer.End();
77

Chao Liu's avatar
Chao Liu committed
78
    return timer.GetElapsedTime() / nrepeat;
79
80
}

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
template <typename... Args, typename F>
float launch_time_and_out_call_kernel(F kernel,
                                      int nrepeat,
                                      dim3 grid_dim,
                                      dim3 block_dim,
                                      std::size_t lds_byte,
                                      const std::function<void()>* func,
                                      Args... args)
{
    KernelTimer timer;

    printf("%s: grid_dim {%d, %d, %d}, block_dim {%d, %d, %d} \n",
           __func__,
           grid_dim.x,
           grid_dim.y,
           grid_dim.z,
           block_dim.x,
           block_dim.y,
           block_dim.z);

    printf("Warm up\n");

    hipStream_t stream_id = nullptr;

    // warm up
    hipLaunchKernelGGL(kernel, grid_dim, block_dim, lds_byte, stream_id, args...);

    printf("Start running %d times...\n", nrepeat);

    timer.Start();

    for(int i = 0; i < nrepeat; ++i)
    {
        hipLaunchKernelGGL(kernel, grid_dim, block_dim, lds_byte, stream_id, args...);
    }

    timer.End();
    // call out function
    if(func)
    {
        (*func)();
        hipLaunchKernelGGL(kernel, grid_dim, block_dim, lds_byte, stream_id, args...);
    }

    return timer.GetElapsedTime() / nrepeat;
}
127
#endif