hip.cpp 3.89 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>
Paul's avatar
Paul committed
6
7
8
9
#include <miopen/miopen.h>

#include <vector>

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

Paul's avatar
Paul committed
14
using hip_ptr = MIGRAPHX_MANAGE_PTR(void, hipFree);
Paul's avatar
Paul committed
15

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

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

Paul's avatar
Paul committed
28
hip_ptr allocate_gpu(std::size_t sz, bool host = false)
Paul's avatar
Paul committed
29
30
{
    if(sz > get_available_gpu_memory())
Paul's avatar
Paul committed
31
        MIGRAPHX_THROW("Memory not available to allocate buffer: " + std::to_string(sz));
Paul's avatar
Paul committed
32
33
    void* result;
    auto status = host ? hipHostMalloc(&result, sz) : hipMalloc(&result, sz);
Paul's avatar
Paul committed
34
35
36
    if(status != hipSuccess)
    {
        if(host)
Paul's avatar
Paul committed
37
            MIGRAPHX_THROW("Gpu allocation failed: " + hip_error(status));
Paul's avatar
Paul committed
38
39
40
        else
            allocate_gpu(sz, true);
    }
Paul's avatar
Paul committed
41
42
43
    return hip_ptr{result};
}

Paul's avatar
Paul committed
44
45
46
template <class T>
std::vector<T> read_from_gpu(const void* x, std::size_t sz)
{
47
    gpu_sync();
Paul's avatar
Paul committed
48
    std::vector<T> result(sz);
Paul's avatar
Paul committed
49
50
    auto status = hipMemcpy(result.data(), x, sz * sizeof(T), hipMemcpyDeviceToHost);
    if(status != hipSuccess)
Paul's avatar
Paul committed
51
        MIGRAPHX_THROW("Copy from gpu failed: " + hip_error(status)); // NOLINT
Paul's avatar
Paul committed
52
53
54
    return result;
}

Paul's avatar
Paul committed
55
hip_ptr write_to_gpu(const void* x, std::size_t sz, bool host = false)
Paul's avatar
Paul committed
56
{
57
    gpu_sync();
Paul's avatar
Paul committed
58
    auto result = allocate_gpu(sz, host);
Paul's avatar
Paul committed
59
60
    auto status = hipMemcpy(result.get(), x, sz, hipMemcpyHostToDevice);
    if(status != hipSuccess)
Paul's avatar
Paul committed
61
        MIGRAPHX_THROW("Copy to gpu failed: " + hip_error(status));
Paul's avatar
Paul committed
62
63
64
    return result;
}

Paul's avatar
Paul committed
65
66
67
68
69
70
71
72
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
73
argument allocate_gpu(const shape& s, bool host)
Paul's avatar
Paul committed
74
{
Paul's avatar
Paul committed
75
    auto p = share(allocate_gpu(s.bytes() + 1, host));
Paul's avatar
Paul committed
76
77
78
    return {s, [p]() mutable { return reinterpret_cast<char*>(p.get()); }};
}

Paul's avatar
Paul committed
79
argument to_gpu(const argument& arg, bool host)
Paul's avatar
Paul committed
80
{
Paul's avatar
Paul committed
81
    auto p = share(write_to_gpu(arg.data(), arg.get_shape().bytes(), host));
Paul's avatar
Paul committed
82
83
84
    return {arg.get_shape(), [p]() mutable { return reinterpret_cast<char*>(p.get()); }};
}

Paul's avatar
Paul committed
85
argument from_gpu(const argument& arg)
Paul's avatar
Paul committed
86
{
Paul's avatar
Paul committed
87
    argument result;
Paul's avatar
Paul committed
88
89
90
91
92
93
94
95
    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));
        result     = {x.get_shape(), [v]() mutable { return reinterpret_cast<char*>(v.data()); }};
    });
    return result;
}

Paul's avatar
Paul committed
96
97
98
99
void set_device(std::size_t id)
{
    auto status = hipSetDevice(id);
    if(status != hipSuccess)
Paul's avatar
Paul committed
100
        MIGRAPHX_THROW("Error setting device");
Paul's avatar
Paul committed
101
102
}

Paul's avatar
Paul committed
103
104
void gpu_sync() { hipDeviceSynchronize(); }

Paul's avatar
Paul committed
105
void copy_to_gpu(const argument& src, const argument& dst)
mei-ye's avatar
mei-ye committed
106
{
107
108
109
    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
110
        MIGRAPHX_THROW("Not enough memory available in destination to do copy");
111
112
    auto status = hipMemcpy(dst.data(), src.data(), src_size, hipMemcpyHostToDevice);
    if(status != hipSuccess)
Paul's avatar
Paul committed
113
        MIGRAPHX_THROW("Copy to gpu failed: " + hip_error(status));
mei-ye's avatar
mei-ye committed
114
}
115

116
117
118
119
120
121
122
123
124
125
126
127
void gpu_copy(context& ctx, const argument& src, const argument& dst)
{
    std::size_t src_size = src.get_shape().bytes();
    std::size_t dst_size = dst.get_shape().bytes();
    if(src_size > dst_size)
        MIGRAPHX_THROW("Not enough memory available in destination to do copy");
    auto status = hipMemcpyAsync(
        dst.data(), src.data(), src_size, hipMemcpyDeviceToDevice, ctx.get_stream().get());
    if(status != hipSuccess)
        MIGRAPHX_THROW("Gpu copy failed: " + hip_error(status));
}

Paul's avatar
Paul committed
128
} // namespace gpu
Paul's avatar
Paul committed
129
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
130
} // namespace migraphx