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

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

Paul's avatar
Paul committed
4
#include <migraph/manage_ptr.hpp>
Paul's avatar
Paul committed
5
6
7
8
#include <miopen/miopen.h>

#include <vector>

Paul's avatar
Paul committed
9
namespace migraph {
Paul's avatar
Paul committed
10
namespace gpu {
Paul's avatar
Paul committed
11

Paul's avatar
Paul committed
12
using hip_ptr = MIGRAPH_MANAGE_PTR(void, hipFree);
Paul's avatar
Paul committed
13

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

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

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

Paul's avatar
Paul committed
41
42
43
44
45
46
47
48
49
50
51
52
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);
}

template <class T>
std::vector<T> read_from_gpu(const void* x, std::size_t sz)
{
    std::vector<T> result(sz);
Paul's avatar
Paul committed
53
54
    auto status = hipMemcpy(result.data(), x, sz * sizeof(T), hipMemcpyDeviceToHost);
    if(status != hipSuccess)
Paul's avatar
Paul committed
55
        MIGRAPH_THROW("Copy from gpu failed: " + hip_error(status)); // NOLINT
Paul's avatar
Paul committed
56
57
58
    return result;
}

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

Paul's avatar
Paul committed
68
argument allocate_gpu(const shape& s, bool host)
Paul's avatar
Paul committed
69
{
Paul's avatar
Paul committed
70
    auto p = share(allocate_gpu(s.bytes() + 1, host));
Paul's avatar
Paul committed
71
72
73
    return {s, [p]() mutable { return reinterpret_cast<char*>(p.get()); }};
}

Paul's avatar
Paul committed
74
argument to_gpu(argument arg, bool host)
Paul's avatar
Paul committed
75
{
Paul's avatar
Paul committed
76
    auto p = share(write_to_gpu(arg.data(), arg.get_shape().bytes(), host));
Paul's avatar
Paul committed
77
78
79
    return {arg.get_shape(), [p]() mutable { return reinterpret_cast<char*>(p.get()); }};
}

Paul's avatar
Paul committed
80
argument from_gpu(argument arg)
Paul's avatar
Paul committed
81
{
Paul's avatar
Paul committed
82
    argument result;
Paul's avatar
Paul committed
83
84
85
86
87
88
89
90
    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
91
void gpu_sync() { hipDeviceSynchronize(); }
Paul's avatar
Paul committed
92

mei-ye's avatar
mei-ye committed
93
94
95
96
97
void copy_to_gpu(char* dst, const char* src, std::size_t size)
{
    hipMemcpy(dst, src, size, hipMemcpyHostToDevice);
}    

Paul's avatar
Paul committed
98
} // namespace gpu
Paul Fultz II's avatar
Paul Fultz II committed
99

Paul's avatar
Paul committed
100
} // namespace migraph