hip.cpp 6.43 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/register_op.hpp>
6
#include <migraphx/gpu/context.hpp>
7
#include <migraphx/gpu/device/contiguous.hpp>
Paul's avatar
Paul committed
8
9
10
11
#include <miopen/miopen.h>

#include <vector>

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

16
17
MIGRAPHX_REGISTER_OP(hip_allocate)
MIGRAPHX_REGISTER_OP(hip_sync_device)
18
MIGRAPHX_REGISTER_OP(hip_sync_stream)
19
20
21
22
23
24
MIGRAPHX_REGISTER_OP(hip_copy_to_gpu)
MIGRAPHX_REGISTER_OP(hip_copy_from_gpu)
MIGRAPHX_REGISTER_OP(hip_copy)
MIGRAPHX_REGISTER_OP(hip_allocate_memory)
MIGRAPHX_REGISTER_OP(hip_copy_literal)

25
26
using hip_ptr      = MIGRAPHX_MANAGE_PTR(void, hipFree);
using hip_host_ptr = MIGRAPHX_MANAGE_PTR(void, hipHostUnregister);
Paul's avatar
Paul committed
27

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

30
31
32
33
34
35
36
37
38
bool is_device_ptr(const void* ptr)
{
    hipPointerAttribute_t attr;
    auto status = hipPointerGetAttributes(&attr, ptr);
    if(status != hipSuccess)
        return false;
    return attr.memoryType == hipMemoryTypeDevice;
}

Paul's avatar
Paul committed
39
std::size_t get_available_gpu_memory()
Paul's avatar
Paul committed
40
{
Paul's avatar
Paul committed
41
42
    size_t free;
    size_t total;
Paul's avatar
Paul committed
43
    auto status = hipMemGetInfo(&free, &total);
Paul's avatar
Paul committed
44
    if(status != hipSuccess)
Paul's avatar
Paul committed
45
        MIGRAPHX_THROW("Failed getting available memory: " + hip_error(status));
Paul's avatar
Paul committed
46
47
48
    return free;
}

49
50
51
52
53
54
55
56
57
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
58
hip_ptr allocate_gpu(std::size_t sz, bool host = false)
Paul's avatar
Paul committed
59
60
{
    if(sz > get_available_gpu_memory())
Paul's avatar
Paul committed
61
        MIGRAPHX_THROW("Memory not available to allocate buffer: " + std::to_string(sz));
62
63
    void* result = nullptr;
    auto status  = host ? hipHostMalloc(&result, sz) : hipMalloc(&result, sz);
Paul's avatar
Paul committed
64
65
66
    if(status != hipSuccess)
    {
        if(host)
Paul's avatar
Paul committed
67
            MIGRAPHX_THROW("Gpu allocation failed: " + hip_error(status));
Paul's avatar
Paul committed
68
        else
69
            return allocate_gpu(sz, true);
Paul's avatar
Paul committed
70
    }
71
    assert(result != nullptr);
Paul's avatar
Paul committed
72
73
74
    return hip_ptr{result};
}

75
76
77
78
79
80
81
82
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
83
84
85
template <class T>
std::vector<T> read_from_gpu(const void* x, std::size_t sz)
{
86
    gpu_sync();
Paul's avatar
Paul committed
87
    std::vector<T> result(sz);
88
89
    assert(not is_device_ptr(result.data()));
    assert(is_device_ptr(x));
Paul's avatar
Paul committed
90
91
    auto status = hipMemcpy(result.data(), x, sz * sizeof(T), hipMemcpyDeviceToHost);
    if(status != hipSuccess)
Paul's avatar
Paul committed
92
        MIGRAPHX_THROW("Copy from gpu failed: " + hip_error(status)); // NOLINT
Paul's avatar
Paul committed
93
94
95
    return result;
}

Paul's avatar
Paul committed
96
hip_ptr write_to_gpu(const void* x, std::size_t sz, bool host = false)
Paul's avatar
Paul committed
97
{
98
    gpu_sync();
Paul's avatar
Paul committed
99
    auto result = allocate_gpu(sz, host);
100
101
    assert(is_device_ptr(result.get()));
    assert(not is_device_ptr(x));
Paul's avatar
Paul committed
102
103
    auto status = hipMemcpy(result.get(), x, sz, hipMemcpyHostToDevice);
    if(status != hipSuccess)
Paul's avatar
Paul committed
104
        MIGRAPHX_THROW("Copy to gpu failed: " + hip_error(status));
Paul's avatar
Paul committed
105
106
107
    return result;
}

Paul's avatar
Paul committed
108
109
110
111
112
113
114
115
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
116
argument allocate_gpu(const shape& s, bool host)
Paul's avatar
Paul committed
117
{
Paul's avatar
Paul committed
118
    auto p = share(allocate_gpu(s.bytes() + 1, host));
Paul's avatar
Paul committed
119
120
121
    return {s, [p]() mutable { return reinterpret_cast<char*>(p.get()); }};
}

122
123
124
125
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()));
bpickrel's avatar
bpickrel committed
126
127
128
    return {arg_shared.get_shape(), [p, a = std::move(arg_shared)]() mutable {
                return get_device_ptr(p.get());
            }}; // namespace gpu
129
130
} // namespace MIGRAPHX_INLINE_NS

Paul's avatar
Paul committed
131
argument to_gpu(const argument& arg, bool host)
Paul's avatar
Paul committed
132
{
Paul's avatar
Paul committed
133
    auto p = share(write_to_gpu(arg.data(), arg.get_shape().bytes(), host));
134
    return {arg.get_shape(), p};
Paul's avatar
Paul committed
135
136
}

Paul's avatar
Paul committed
137
argument from_gpu(const argument& arg)
Paul's avatar
Paul committed
138
{
Paul's avatar
Paul committed
139
    argument result;
Paul's avatar
Paul committed
140
141
142
    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));
143
144
        // cppcheck-suppress returnDanglingLifetime
        result = {x.get_shape(), [v]() mutable { return v.data(); }};
Paul's avatar
Paul committed
145
146
147
148
    });
    return result;
}

Paul's avatar
Paul committed
149
150
151
152
void set_device(std::size_t id)
{
    auto status = hipSetDevice(id);
    if(status != hipSuccess)
Paul's avatar
Paul committed
153
        MIGRAPHX_THROW("Error setting device");
Paul's avatar
Paul committed
154
155
}

156
157
158
159
160
161
void gpu_sync()
{
    auto status = hipDeviceSynchronize();
    if(status != hipSuccess)
        MIGRAPHX_THROW("hip device synchronization failed: " + hip_error(status));
}
Paul's avatar
Paul committed
162

163
164
void gpu_sync(const context& ctx) { ctx.finish(); }

165
void hip_async_copy(context& ctx, const argument& src, const argument& dst, hipMemcpyKind kind)
mei-ye's avatar
mei-ye committed
166
{
167
168
169
    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
170
        MIGRAPHX_THROW("Not enough memory available in destination to do copy");
171
    auto status = hipMemcpyAsync(dst.data(), src.data(), src_size, kind, ctx.get_stream().get());
172
    if(status != hipSuccess)
173
        MIGRAPHX_THROW("Gpu copy failed: " + hip_error(status));
mei-ye's avatar
mei-ye committed
174
}
175

176
177
void gpu_copy(context& ctx, const argument& src, const argument& dst)
{
178
179
180
181
182
183
184
    // 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)
{
Shucai Xiao's avatar
Shucai Xiao committed
185
186
187
188
189
190
191
192
    if(src.get_shape() == dst.get_shape() and dst.get_shape().packed())
    {
        hip_async_copy(ctx, src, dst, hipMemcpyHostToDevice);
    }
    else
    {
        gpu_copy(ctx, register_on_gpu(src), dst);
    }
193
194
195
196
}

void copy_from_gpu(context& ctx, const argument& src, const argument& dst)
{
Shucai Xiao's avatar
Shucai Xiao committed
197
198
199
200
201
202
203
204
    if(src.get_shape() == dst.get_shape() and dst.get_shape().packed())
    {
        hip_async_copy(ctx, src, dst, hipMemcpyDeviceToHost);
    }
    else
    {
        gpu_copy(ctx, src, register_on_gpu(dst));
    }
205
206
207
208
209
}

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

212
213
214
215
216
void store_preallocated_param(context& ctx, const std::string& id, const argument& a)
{
    ctx.get_current_device().preallocations[id] = a;
}

217
// clang-format off
Paul's avatar
Paul committed
218
} // namespace gpu
Paul's avatar
Paul committed
219
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
220
} // namespace migraphx