hip.cpp 7.59 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
Paul's avatar
Paul committed
24

Paul's avatar
Paul committed
25
#include <migraphx/gpu/hip.hpp>
Paul's avatar
Paul committed
26

Paul's avatar
Paul committed
27
#include <migraphx/manage_ptr.hpp>
28
#include <migraphx/register_op.hpp>
29
#include <migraphx/gpu/context.hpp>
30
#include <migraphx/gpu/device/contiguous.hpp>
Paul's avatar
Paul committed
31
32
33
34
#include <miopen/miopen.h>

#include <vector>

Paul's avatar
Paul committed
35
namespace migraphx {
Paul's avatar
Paul committed
36
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
37
namespace gpu {
Paul's avatar
Paul committed
38

39
40
MIGRAPHX_REGISTER_OP(hip_allocate)
MIGRAPHX_REGISTER_OP(hip_sync_device)
41
MIGRAPHX_REGISTER_OP(hip_sync_stream)
42
43
44
45
46
47
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)

48
49
using hip_ptr      = MIGRAPHX_MANAGE_PTR(void, hipFree);
using hip_host_ptr = MIGRAPHX_MANAGE_PTR(void, hipHostUnregister);
Paul's avatar
Paul committed
50

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

53
54
55
56
57
58
59
60
61
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
62
std::size_t get_available_gpu_memory()
Paul's avatar
Paul committed
63
{
Paul's avatar
Paul committed
64
65
    size_t free;
    size_t total;
Paul's avatar
Paul committed
66
    auto status = hipMemGetInfo(&free, &total);
Paul's avatar
Paul committed
67
    if(status != hipSuccess)
Paul's avatar
Paul committed
68
        MIGRAPHX_THROW("Failed getting available memory: " + hip_error(status));
Paul's avatar
Paul committed
69
70
71
    return free;
}

72
73
74
75
76
77
78
79
80
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
81
hip_ptr allocate_gpu(std::size_t sz, bool host = false)
Paul's avatar
Paul committed
82
83
{
    if(sz > get_available_gpu_memory())
Paul's avatar
Paul committed
84
        MIGRAPHX_THROW("Memory not available to allocate buffer: " + std::to_string(sz));
85
86
    void* result = nullptr;
    auto status  = host ? hipHostMalloc(&result, sz) : hipMalloc(&result, sz);
Paul's avatar
Paul committed
87
88
89
    if(status != hipSuccess)
    {
        if(host)
Paul's avatar
Paul committed
90
            MIGRAPHX_THROW("Gpu allocation failed: " + hip_error(status));
Paul's avatar
Paul committed
91
        else
92
            return allocate_gpu(sz, true);
Paul's avatar
Paul committed
93
    }
94
    assert(result != nullptr);
Paul's avatar
Paul committed
95
96
97
    return hip_ptr{result};
}

98
99
100
101
102
103
104
105
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
106
107
108
template <class T>
std::vector<T> read_from_gpu(const void* x, std::size_t sz)
{
109
    gpu_sync();
Paul's avatar
Paul committed
110
    std::vector<T> result(sz);
111
112
    assert(not is_device_ptr(result.data()));
    assert(is_device_ptr(x));
Paul's avatar
Paul committed
113
114
    auto status = hipMemcpy(result.data(), x, sz * sizeof(T), hipMemcpyDeviceToHost);
    if(status != hipSuccess)
Paul's avatar
Paul committed
115
        MIGRAPHX_THROW("Copy from gpu failed: " + hip_error(status)); // NOLINT
Paul's avatar
Paul committed
116
117
118
    return result;
}

Paul's avatar
Paul committed
119
hip_ptr write_to_gpu(const void* x, std::size_t sz, bool host = false)
Paul's avatar
Paul committed
120
{
121
    gpu_sync();
Paul's avatar
Paul committed
122
    auto result = allocate_gpu(sz, host);
123
124
    assert(is_device_ptr(result.get()));
    assert(not is_device_ptr(x));
Paul's avatar
Paul committed
125
126
    auto status = hipMemcpy(result.get(), x, sz, hipMemcpyHostToDevice);
    if(status != hipSuccess)
Paul's avatar
Paul committed
127
        MIGRAPHX_THROW("Copy to gpu failed: " + hip_error(status));
Paul's avatar
Paul committed
128
129
130
    return result;
}

Paul's avatar
Paul committed
131
132
133
134
135
136
137
138
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
139
argument allocate_gpu(const shape& s, bool host)
Paul's avatar
Paul committed
140
{
Paul's avatar
Paul committed
141
    auto p = share(allocate_gpu(s.bytes() + 1, host));
Paul's avatar
Paul committed
142
143
144
    return {s, [p]() mutable { return reinterpret_cast<char*>(p.get()); }};
}

145
146
147
148
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
149
150
151
    return {arg_shared.get_shape(), [p, a = std::move(arg_shared)]() mutable {
                return get_device_ptr(p.get());
            }}; // namespace gpu
152
153
} // namespace MIGRAPHX_INLINE_NS

Paul's avatar
Paul committed
154
argument to_gpu(const argument& arg, bool host)
Paul's avatar
Paul committed
155
{
Paul's avatar
Paul committed
156
    auto p = share(write_to_gpu(arg.data(), arg.get_shape().bytes(), host));
157
    return {arg.get_shape(), p};
Paul's avatar
Paul committed
158
159
}

Paul's avatar
Paul committed
160
argument from_gpu(const argument& arg)
Paul's avatar
Paul committed
161
{
Paul's avatar
Paul committed
162
    argument result;
Paul's avatar
Paul committed
163
164
165
    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));
166
167
        // cppcheck-suppress returnDanglingLifetime
        result = {x.get_shape(), [v]() mutable { return v.data(); }};
Paul's avatar
Paul committed
168
169
170
171
    });
    return result;
}

Paul's avatar
Paul committed
172
173
174
175
void set_device(std::size_t id)
{
    auto status = hipSetDevice(id);
    if(status != hipSuccess)
Paul's avatar
Paul committed
176
        MIGRAPHX_THROW("Error setting device");
Paul's avatar
Paul committed
177
178
}

179
180
181
182
183
184
void gpu_sync()
{
    auto status = hipDeviceSynchronize();
    if(status != hipSuccess)
        MIGRAPHX_THROW("hip device synchronization failed: " + hip_error(status));
}
Paul's avatar
Paul committed
185

186
187
void gpu_sync(const context& ctx) { ctx.finish(); }

188
void hip_async_copy(context& ctx, const argument& src, const argument& dst, hipMemcpyKind kind)
mei-ye's avatar
mei-ye committed
189
{
190
191
192
    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
193
        MIGRAPHX_THROW("Not enough memory available in destination to do copy");
194
    auto status = hipMemcpyAsync(dst.data(), src.data(), src_size, kind, ctx.get_stream().get());
195
    if(status != hipSuccess)
196
        MIGRAPHX_THROW("Gpu copy failed: " + hip_error(status));
mei-ye's avatar
mei-ye committed
197
}
198

199
200
void gpu_copy(context& ctx, const argument& src, const argument& dst)
{
201
202
203
204
205
206
207
    // 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
208
209
210
211
212
213
214
215
    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);
    }
216
217
218
219
}

void copy_from_gpu(context& ctx, const argument& src, const argument& dst)
{
Shucai Xiao's avatar
Shucai Xiao committed
220
221
222
223
224
225
226
227
    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));
    }
228
229
230
231
232
}

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

235
236
237
238
239
void store_preallocated_param(context& ctx, const std::string& id, const argument& a)
{
    ctx.get_current_device().preallocations[id] = a;
}

240
// clang-format off
Paul's avatar
Paul committed
241
} // namespace gpu
Paul's avatar
Paul committed
242
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
243
} // namespace migraphx