"git@developer.sourcefind.cn:gaoqiong/migraphx.git" did not exist on "ccf51257ce18033563bf40f6e1cc86b0ecd1b2a4"
kernel.cpp 3.4 KB
Newer Older
Paul Fultz II's avatar
Paul Fultz II committed
1
2
3
4
#include <migraphx/gpu/kernel.hpp>
#include <migraphx/manage_ptr.hpp>
#include <migraphx/errors.hpp>
#include <migraphx/gpu/pack_args.hpp>
5
#include <cassert>
Paul Fultz II's avatar
Paul Fultz II committed
6
7
8
9
10
11
12
13
14

// extern declare the function since hip/hip_ext.h header is broken
extern hipError_t hipExtModuleLaunchKernel(hipFunction_t, // NOLINT
                                           uint32_t,
                                           uint32_t,
                                           uint32_t,
                                           uint32_t,
                                           uint32_t,
                                           uint32_t,
Shucai Xiao's avatar
Shucai Xiao committed
15
                                           int,
Paul Fultz II's avatar
Paul Fultz II committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
                                           hipStream_t,
                                           void**,
                                           void**,
                                           hipEvent_t = nullptr,
                                           hipEvent_t = nullptr,
                                           uint32_t   = 0);

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace gpu {

extern std::string hip_error(int error);

using hip_module_ptr = MIGRAPHX_MANAGE_PTR(hipModule_t, hipModuleUnload);

struct kernel_impl
{
    hip_module_ptr module = nullptr;
    hipFunction_t fun     = nullptr;
};

37
hip_module_ptr load_module(const char* image)
Paul Fultz II's avatar
Paul Fultz II committed
38
39
{
    hipModule_t raw_m;
40
    auto status = hipModuleLoadData(&raw_m, image);
Paul Fultz II's avatar
Paul Fultz II committed
41
42
43
44
45
46
    hip_module_ptr m{raw_m};
    if(status != hipSuccess)
        MIGRAPHX_THROW("Failed to load module: " + hip_error(status));
    return m;
}

47
kernel::kernel(const char* image, const std::string& name) : impl(std::make_shared<kernel_impl>())
Paul Fultz II's avatar
Paul Fultz II committed
48
49
50
51
52
53
54
{
    impl->module = load_module(image);
    auto status  = hipModuleGetFunction(&impl->fun, impl->module.get(), name.c_str());
    if(hipSuccess != status)
        MIGRAPHX_THROW("Failed to get function: " + name + ": " + hip_error(status));
}

55
56
void launch_kernel(hipFunction_t fun,
                   hipStream_t stream,
Shucai Xiao's avatar
Shucai Xiao committed
57
58
                   int global,
                   int local,
59
                   void* kernargs,
Shucai Xiao's avatar
Shucai Xiao committed
60
                   int size)
Paul Fultz II's avatar
Paul Fultz II committed
61
62
63
64
{
    void* config[] = {
// HIP_LAUNCH_PARAM_* are macros that do horrible things
#ifdef MIGRAPHX_USE_CLANG_TIDY
65
        nullptr, kernargs, nullptr, &size, nullptr
Paul Fultz II's avatar
Paul Fultz II committed
66
67
#else
        HIP_LAUNCH_PARAM_BUFFER_POINTER,
68
        kernargs,
Paul Fultz II's avatar
Paul Fultz II committed
69
70
71
72
73
74
        HIP_LAUNCH_PARAM_BUFFER_SIZE,
        &size,
        HIP_LAUNCH_PARAM_END
#endif
    };

75
76
    auto status = hipExtModuleLaunchKernel(
        fun, global, 1, 1, local, 1, 1, 0, stream, nullptr, reinterpret_cast<void**>(&config));
Paul Fultz II's avatar
Paul Fultz II committed
77
78
79
80
    if(status != hipSuccess)
        MIGRAPHX_THROW("Failed to launch kernel: " + hip_error(status));
}

81
void kernel::launch(hipStream_t stream,
Shucai Xiao's avatar
Shucai Xiao committed
82
83
                    int global,
                    int local,
84
85
86
87
                    std::vector<void*> args) const
{
    assert(impl != nullptr);
    void* kernargs   = args.data();
Shucai Xiao's avatar
Shucai Xiao committed
88
    int size = args.size() * sizeof(void*);
89
90
91
92
93

    launch_kernel(impl->fun, stream, global, local, kernargs, size);
}

void kernel::launch(hipStream_t stream,
Shucai Xiao's avatar
Shucai Xiao committed
94
95
                    int global,
                    int local,
96
97
98
99
                    const std::vector<kernel_argument>& args) const
{
    assert(impl != nullptr);
    std::vector<char> kernargs = pack_args(args);
Shucai Xiao's avatar
Shucai Xiao committed
100
    int size           = kernargs.size();
101
102
103
104

    launch_kernel(impl->fun, stream, global, local, kernargs.data(), size);
}

Paul Fultz II's avatar
Paul Fultz II committed
105
106
107
} // namespace gpu
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx