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

Paul's avatar
Paul committed
2
#include <migraph/miopen/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
11
namespace miopen {

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

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
template <int NDIM> 
struct HIPTensorDescriptor 
{ 
    size_t lens[NDIM];
    size_t strides[NDIM]; 
};

template <typename T, int NDIM>
__global__
void contiguous_gpu(const T* A,
                    HIPTensorDescriptor<NDIM> td_a,
                    T* At,
                    HIPTensorDescriptor<NDIM> td_at,
                    size_t nelements) {
  for (size_t i = blockIdx.x * blockDim.x + threadIdx.x;
       i < nelements; i += blockDim.x * gridDim.x) {
    size_t s[NDIM];
    multiindex<NDIM>(td_at.strides, i, s);
    size_t lidx = 0;
    for (size_t j = 0; j < NDIM; j++) lidx += s[j] * td_a.strides[j];
    At[i] = A[lidx];
  }
}

Paul's avatar
Paul committed
38
hip_ptr allocate_gpu(std::size_t sz)
Paul's avatar
Paul committed
39
40
41
42
43
44
45
{
    void* result;
    // TODO: Check status
    hipMalloc(&result, sz);
    return hip_ptr{result};
}

Paul's avatar
Paul committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
63
64
hip_ptr write_to_gpu(const void* x, std::size_t sz)
{
Paul's avatar
Paul committed
65
    auto result = allocate_gpu(sz);
Paul's avatar
Paul committed
66
67
68
69
70
    // TODO: Check status
    hipMemcpy(result.get(), x, sz, hipMemcpyHostToDevice);
    return result;
}

Paul's avatar
Paul committed
71
migraph::argument allocate_gpu(migraph::shape s)
Paul's avatar
Paul committed
72
73
74
75
76
{
    auto p = share(allocate_gpu(s.bytes()));
    return {s, [p]() mutable { return reinterpret_cast<char*>(p.get()); }};
}

Paul's avatar
Paul committed
77
migraph::argument to_gpu(migraph::argument arg)
Paul's avatar
Paul committed
78
79
80
81
82
{
    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
83
migraph::argument from_gpu(migraph::argument arg)
Paul's avatar
Paul committed
84
{
Paul's avatar
Paul committed
85
    migraph::argument result;
Paul's avatar
Paul committed
86
87
88
89
90
91
92
93
    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;
}

94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
migraph::argument hip_contiguous(migraph::argument arg, migraph::shape output_shape) 
{
    migraph::argument result{output_shape};
    visit_all(result, arg)([&](auto output, auto input) {
        HIPTensorDescriptor td_a, td_at;
        auto s = arg.get_shape();
        for (int i = 0; i < output_shape.lens().size(); i++) {
          td_a.strides[i] = s.strides().at(i);
          td_at.strides[i] = output_shape.strides().at(i);
        }
        dim3 nblocks(512);
        dim3 nthreads(512);
        hipLaunchKernelGGL((contiguous_gpu<int, 4>), nblocks, nthreads, 0, 0, 
                     input.data(),
                     td_a,
                     output.data(),
                     td_at,
                     s.elements());        
    });
    return result;
}

Paul's avatar
Paul committed
116
117
} // namespace miopen

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