hip.cpp 2.09 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
14

hip_ptr allocate_gpu(std::size_t sz)
Paul's avatar
Paul committed
15
16
17
18
{
    void* result;
    // TODO: Check status
    hipMalloc(&result, sz);
mei-ye's avatar
staging  
mei-ye committed
19
20
    if (result == nullptr)
        throw std::runtime_error("can not allocate GPU memory");
mei-ye's avatar
mei-ye committed
21
22
    char * ptr = reinterpret_cast<char*>(result);
    std::cout << "MIGraph allocated mem: [" << result << "," << ptr + sz -1 << "]" << std::endl;
Paul's avatar
Paul committed
23
24
25
    return hip_ptr{result};
}

Paul's avatar
Paul committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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);
    // TODO: Check status
    hipMemcpy(result.data(), x, sz * sizeof(T), hipMemcpyDeviceToHost);
    return result;
}

Paul's avatar
Paul committed
43
44
hip_ptr write_to_gpu(const void* x, std::size_t sz)
{
Paul's avatar
Paul committed
45
    auto result = allocate_gpu(sz);
Paul's avatar
Paul committed
46
47
48
49
50
    // TODO: Check status
    hipMemcpy(result.get(), x, sz, hipMemcpyHostToDevice);
    return result;
}

Paul's avatar
Paul committed
51
migraph::argument allocate_gpu(migraph::shape s)
Paul's avatar
Paul committed
52
53
54
55
56
{
    auto p = share(allocate_gpu(s.bytes()));
    return {s, [p]() mutable { return reinterpret_cast<char*>(p.get()); }};
}

Paul's avatar
Paul committed
57
migraph::argument to_gpu(migraph::argument arg)
Paul's avatar
Paul committed
58
59
60
61
62
{
    auto p = share(write_to_gpu(arg.data(), arg.get_shape().bytes()));
    return {arg.get_shape(), [p]() mutable { return reinterpret_cast<char*>(p.get()); }};
}

Paul's avatar
Paul committed
63
migraph::argument from_gpu(migraph::argument arg)
Paul's avatar
Paul committed
64
{
Paul's avatar
Paul committed
65
    migraph::argument result;
Paul's avatar
Paul committed
66
67
68
69
70
71
72
73
    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;
}

mei-ye's avatar
mei-ye committed
74
75
76
77
78
void copy_to_gpu(char* dst, const char* src, std::size_t size)
{
    hipMemcpy(dst, src, size, hipMemcpyHostToDevice);
}
    
Paul's avatar
Paul committed
79
} // namespace gpu
Paul's avatar
Paul committed
80

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