kernel.cpp 5.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
Paul Fultz II's avatar
Paul Fultz II committed
24
25
26
27
#include <migraphx/gpu/kernel.hpp>
#include <migraphx/manage_ptr.hpp>
#include <migraphx/errors.hpp>
#include <migraphx/gpu/pack_args.hpp>
28
#include <cassert>
Paul Fultz II's avatar
Paul Fultz II committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

// 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,
                                           size_t,
                                           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;
};

60
hip_module_ptr load_module(const char* image)
Paul Fultz II's avatar
Paul Fultz II committed
61
62
{
    hipModule_t raw_m;
63
    auto status = hipModuleLoadData(&raw_m, image);
Paul Fultz II's avatar
Paul Fultz II committed
64
65
66
67
68
69
    hip_module_ptr m{raw_m};
    if(status != hipSuccess)
        MIGRAPHX_THROW("Failed to load module: " + hip_error(status));
    return m;
}

70
kernel::kernel(const char* image, const std::string& name) : impl(std::make_shared<kernel_impl>())
Paul Fultz II's avatar
Paul Fultz II committed
71
72
73
74
75
76
77
{
    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));
}

78
79
80
81
82
void launch_kernel(hipFunction_t fun,
                   hipStream_t stream,
                   std::size_t global,
                   std::size_t local,
                   void* kernargs,
Paul's avatar
Format  
Paul committed
83
84
85
                   std::size_t size,
                   hipEvent_t start,
                   hipEvent_t stop)
Paul Fultz II's avatar
Paul Fultz II committed
86
{
Paul Fultz II's avatar
Paul Fultz II committed
87
88
    assert(global > 0);
    assert(local > 0);
Paul Fultz II's avatar
Paul Fultz II committed
89
90
91
    void* config[] = {
// HIP_LAUNCH_PARAM_* are macros that do horrible things
#ifdef MIGRAPHX_USE_CLANG_TIDY
92
        nullptr, kernargs, nullptr, &size, nullptr
Paul Fultz II's avatar
Paul Fultz II committed
93
94
#else
        HIP_LAUNCH_PARAM_BUFFER_POINTER,
95
        kernargs,
Paul Fultz II's avatar
Paul Fultz II committed
96
97
98
99
100
101
        HIP_LAUNCH_PARAM_BUFFER_SIZE,
        &size,
        HIP_LAUNCH_PARAM_END
#endif
    };

Paul's avatar
Format  
Paul committed
102
103
104
105
106
107
108
109
110
111
112
113
114
    auto status = hipExtModuleLaunchKernel(fun,
                                           global,
                                           1,
                                           1,
                                           local,
                                           1,
                                           1,
                                           0,
                                           stream,
                                           nullptr,
                                           reinterpret_cast<void**>(&config),
                                           start,
                                           stop);
Paul Fultz II's avatar
Paul Fultz II committed
115
116
    if(status != hipSuccess)
        MIGRAPHX_THROW("Failed to launch kernel: " + hip_error(status));
Paul's avatar
Format  
Paul committed
117
    if(stop)
Paul's avatar
Paul committed
118
119
120
121
122
    {
        status = hipEventSynchronize(stop);
        if(status != hipSuccess)
            MIGRAPHX_THROW("Failed to sync event: " + hip_error(status));
    }
Paul Fultz II's avatar
Paul Fultz II committed
123
124
}

125
126
127
void kernel::launch(hipStream_t stream,
                    std::size_t global,
                    std::size_t local,
Paul's avatar
Format  
Paul committed
128
129
130
                    std::vector<void*> args,
                    hipEvent_t start,
                    hipEvent_t stop) const
131
132
133
134
135
{
    assert(impl != nullptr);
    void* kernargs   = args.data();
    std::size_t size = args.size() * sizeof(void*);

Paul's avatar
Paul committed
136
    launch_kernel(impl->fun, stream, global, local, kernargs, size, start, stop);
137
138
139
140
141
}

void kernel::launch(hipStream_t stream,
                    std::size_t global,
                    std::size_t local,
Paul's avatar
Format  
Paul committed
142
143
144
                    const std::vector<kernel_argument>& args,
                    hipEvent_t start,
                    hipEvent_t stop) const
145
146
147
148
149
{
    assert(impl != nullptr);
    std::vector<char> kernargs = pack_args(args);
    std::size_t size           = kernargs.size();

Paul's avatar
Paul committed
150
    launch_kernel(impl->fun, stream, global, local, kernargs.data(), size, start, stop);
151
152
}

Paul Fultz II's avatar
Paul Fultz II committed
153
154
155
} // namespace gpu
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx