hip.cpp 7.56 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
MIGRAPHX_REGISTER_OP(hip_allocate)
40
MIGRAPHX_REGISTER_OP(hip_sync_stream)
41
42
43
44
45
46
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)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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