hip.cpp 5.16 KB
Newer Older
Paul's avatar
Paul committed
1

Paul's avatar
Paul committed
2
#include <migraphx/gpu/hip.hpp>
Paul's avatar
Paul committed
3

Paul's avatar
Paul committed
4
#include <migraphx/manage_ptr.hpp>
5
#include <migraphx/gpu/context.hpp>
6
#include <migraphx/gpu/device/contiguous.hpp>
Paul's avatar
Paul committed
7
8
9
10
#include <miopen/miopen.h>

#include <vector>

Paul's avatar
Paul committed
11
namespace migraphx {
Paul's avatar
Paul committed
12
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
13
namespace gpu {
Paul's avatar
Paul committed
14

15
16
using hip_ptr      = MIGRAPHX_MANAGE_PTR(void, hipFree);
using hip_host_ptr = MIGRAPHX_MANAGE_PTR(void, hipHostUnregister);
Paul's avatar
Paul committed
17

Paul's avatar
Paul committed
18
std::string hip_error(int error) { return hipGetErrorString(static_cast<hipError_t>(error)); }
Paul's avatar
Paul committed
19

Paul's avatar
Paul committed
20
std::size_t get_available_gpu_memory()
Paul's avatar
Paul committed
21
{
Paul's avatar
Paul committed
22
23
    size_t free;
    size_t total;
Paul's avatar
Paul committed
24
    auto status = hipMemGetInfo(&free, &total);
Paul's avatar
Paul committed
25
    if(status != hipSuccess)
Paul's avatar
Paul committed
26
        MIGRAPHX_THROW("Failed getting available memory: " + hip_error(status));
Paul's avatar
Paul committed
27
28
29
    return free;
}

30
31
32
33
34
35
36
37
38
void* get_device_ptr(void* hptr)
{
    void* result = nullptr;
    auto status  = hipHostGetDevicePointer(&result, hptr, 0);
    if(status != hipSuccess)
        MIGRAPHX_THROW("Failed getting device pointer: " + hip_error(status));
    return result;
}

Paul's avatar
Paul committed
39
hip_ptr allocate_gpu(std::size_t sz, bool host = false)
Paul's avatar
Paul committed
40
41
{
    if(sz > get_available_gpu_memory())
Paul's avatar
Paul committed
42
        MIGRAPHX_THROW("Memory not available to allocate buffer: " + std::to_string(sz));
Paul's avatar
Paul committed
43
44
    void* result;
    auto status = host ? hipHostMalloc(&result, sz) : hipMalloc(&result, sz);
Paul's avatar
Paul committed
45
46
47
    if(status != hipSuccess)
    {
        if(host)
Paul's avatar
Paul committed
48
            MIGRAPHX_THROW("Gpu allocation failed: " + hip_error(status));
Paul's avatar
Paul committed
49
        else
50
            return allocate_gpu(sz, true);
Paul's avatar
Paul committed
51
    }
Paul's avatar
Paul committed
52
53
54
    return hip_ptr{result};
}

55
56
57
58
59
60
61
62
hip_host_ptr register_on_gpu(void* ptr, std::size_t sz)
{
    auto status = hipHostRegister(ptr, sz, hipHostRegisterMapped);
    if(status != hipSuccess)
        MIGRAPHX_THROW("Gpu register failed: " + hip_error(status));
    return hip_host_ptr{ptr};
}

Paul's avatar
Paul committed
63
64
65
template <class T>
std::vector<T> read_from_gpu(const void* x, std::size_t sz)
{
66
    gpu_sync();
Paul's avatar
Paul committed
67
    std::vector<T> result(sz);
Paul's avatar
Paul committed
68
69
    auto status = hipMemcpy(result.data(), x, sz * sizeof(T), hipMemcpyDeviceToHost);
    if(status != hipSuccess)
Paul's avatar
Paul committed
70
        MIGRAPHX_THROW("Copy from gpu failed: " + hip_error(status)); // NOLINT
Paul's avatar
Paul committed
71
72
73
    return result;
}

Paul's avatar
Paul committed
74
hip_ptr write_to_gpu(const void* x, std::size_t sz, bool host = false)
Paul's avatar
Paul committed
75
{
76
    gpu_sync();
Paul's avatar
Paul committed
77
    auto result = allocate_gpu(sz, host);
Paul's avatar
Paul committed
78
79
    auto status = hipMemcpy(result.get(), x, sz, hipMemcpyHostToDevice);
    if(status != hipSuccess)
Paul's avatar
Paul committed
80
        MIGRAPHX_THROW("Copy to gpu failed: " + hip_error(status));
Paul's avatar
Paul committed
81
82
83
    return result;
}

Paul's avatar
Paul committed
84
85
86
87
88
89
90
91
template <class T>
hip_ptr write_to_gpu(const T& x)
{
    using type = typename T::value_type;
    auto size  = x.size() * sizeof(type);
    return write_to_gpu(x.data(), size);
}

Paul's avatar
Paul committed
92
argument allocate_gpu(const shape& s, bool host)
Paul's avatar
Paul committed
93
{
Paul's avatar
Paul committed
94
    auto p = share(allocate_gpu(s.bytes() + 1, host));
Paul's avatar
Paul committed
95
96
97
    return {s, [p]() mutable { return reinterpret_cast<char*>(p.get()); }};
}

98
99
100
101
102
103
104
105
106
107
argument register_on_gpu(const argument& arg)
{
    auto arg_shared = arg.share();
    auto p          = share(register_on_gpu(arg_shared.data(), arg_shared.get_shape().bytes()));
    return {arg_shared.get_shape(),
            [ p, a = std::move(arg_shared) ]() mutable {return get_device_ptr(p.get());
}
}; // namespace gpu
} // namespace MIGRAPHX_INLINE_NS

Paul's avatar
Paul committed
108
argument to_gpu(const argument& arg, bool host)
Paul's avatar
Paul committed
109
{
Paul's avatar
Paul committed
110
    auto p = share(write_to_gpu(arg.data(), arg.get_shape().bytes(), host));
111
    return {arg.get_shape(), p};
Paul's avatar
Paul committed
112
113
}

Paul's avatar
Paul committed
114
argument from_gpu(const argument& arg)
Paul's avatar
Paul committed
115
{
Paul's avatar
Paul committed
116
    argument result;
Paul's avatar
Paul committed
117
118
119
    arg.visit([&](auto x) {
        using type = typename decltype(x)::value_type;
        auto v     = read_from_gpu<type>(arg.data(), x.get_shape().bytes() / sizeof(type));
120
121
        // cppcheck-suppress returnDanglingLifetime
        result = {x.get_shape(), [v]() mutable { return v.data(); }};
Paul's avatar
Paul committed
122
123
124
125
    });
    return result;
}

Paul's avatar
Paul committed
126
127
128
129
void set_device(std::size_t id)
{
    auto status = hipSetDevice(id);
    if(status != hipSuccess)
Paul's avatar
Paul committed
130
        MIGRAPHX_THROW("Error setting device");
Paul's avatar
Paul committed
131
132
}

Paul's avatar
Paul committed
133
134
void gpu_sync() { hipDeviceSynchronize(); }

135
void hip_async_copy(context& ctx, const argument& src, const argument& dst, hipMemcpyKind kind)
mei-ye's avatar
mei-ye committed
136
{
137
138
139
    std::size_t src_size = src.get_shape().bytes();
    std::size_t dst_size = dst.get_shape().bytes();
    if(src_size > dst_size)
Paul's avatar
Paul committed
140
        MIGRAPHX_THROW("Not enough memory available in destination to do copy");
141
    auto status = hipMemcpyAsync(dst.data(), src.data(), src_size, kind, ctx.get_stream().get());
142
    if(status != hipSuccess)
143
        MIGRAPHX_THROW("Gpu copy failed: " + hip_error(status));
mei-ye's avatar
mei-ye committed
144
}
145

146
147
void gpu_copy(context& ctx, const argument& src, const argument& dst)
{
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
    // Workaround: Use contiguous as hip's memcpy is broken
    device::contiguous(ctx.get_stream().get(), dst, src);
    // hip_async_copy(ctx, src, dst, hipMemcpyDeviceToDevice);
}

void copy_to_gpu(context& ctx, const argument& src, const argument& dst)
{
    gpu_copy(ctx, register_on_gpu(src), dst);
}

void copy_from_gpu(context& ctx, const argument& src, const argument& dst)
{
    gpu_copy(ctx, src, register_on_gpu(dst));
}

argument get_preallocation(context& ctx, const std::string& id)
{
    return ctx.get_current_device().preallocations.at(id);
166
167
}

168
169
170
171
172
void store_preallocated_param(context& ctx, const std::string& id, const argument& a)
{
    ctx.get_current_device().preallocations[id] = a;
}

173
// clang-format off
Paul's avatar
Paul committed
174
} // namespace gpu
Paul's avatar
Paul committed
175
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
176
} // namespace migraphx