| 1 | /******************************************************************************* |
| 2 | * |
| 3 | * MIT License |
| 4 | * |
| 5 | * Copyright (C) 2022 Advanced Micro Devices, Inc. All rights reserved. |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | * of this software and associated documentation files (the "Software"), to deal |
| 9 | * in the Software without restriction, including without limitation the rights |
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | * copies of the Software, and to permit persons to whom the Software is |
| 12 | * furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included in |
| 15 | * all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | * SOFTWARE. |
| 24 | * |
| 25 | *******************************************************************************/ |
| 26 | |
| 27 | #pragma once |
| 28 | |
| 29 | #include <Tensile/AMDGPU.hpp> |
| 30 | #include <Tensile/Tensile.hpp> |
| 31 | #include <hip/hip_runtime.h> |
| 32 | #include <unordered_set> |
| 33 | |
| 34 | #include <mutex> |
| 35 | |
| 36 | namespace TensileLite |
| 37 | { |
| 38 | namespace hip |
| 39 | { |
| 40 | class SolutionAdapter : public TensileLite::SolutionAdapter |
| 41 | { |
| 42 | public: |
| 43 | SolutionAdapter(); |
| 44 | SolutionAdapter(bool debug); |
| 45 | SolutionAdapter(bool debug, std::string const& name); |
| 46 | ~SolutionAdapter(); |
| 47 | |
| 48 | virtual std::string name() const |
| 49 | { |
| 50 | return m_name; |
| 51 | } |
| 52 | |
| 53 | void codeObjectDir(std::string codeObjectDir); |
| 54 | |
| 55 | hipError_t loadCodeObjectFile(std::string const& path); |
| 56 | |
| 57 | hipError_t initializeLazyLoading(std::string architecture, std::string codeObjectDir); |
| 58 | |
| 59 | hipError_t loadCodeObject(const void* image); |
| 60 | |
| 61 | hipError_t loadCodeObjectBytes(std::vector<uint8_t> const& bytes); |
| 62 | |
| 63 | void loadEmbeddedCodeObjects(); |
| 64 | void loadEmbeddedCodeObjects(std::string const& key); |
| 65 | |
| 66 | hipError_t launchKernel(KernelInvocation const& kernel); |
| 67 | hipError_t launchKernel(KernelInvocation const& kernel, |
| 68 | hipStream_t stream, |
| 69 | hipEvent_t startEvent, |
| 70 | hipEvent_t stopEvent, |
| 71 | bool isKernelLoaded = false); |
| 72 | |
| 73 | hipError_t launchKernels(std::vector<KernelInvocation> const& kernels); |
| 74 | |
| 75 | hipError_t launchKernels(std::vector<KernelInvocation> const& kernels, |
| 76 | hipStream_t stream, |
| 77 | hipEvent_t startEvent, |
| 78 | hipEvent_t stopEvent, |
| 79 | bool isKernelLoaded = false); |
| 80 | |
| 81 | hipError_t launchKernels(std::vector<KernelInvocation> const& kernels, |
| 82 | hipStream_t stream, |
| 83 | std::vector<hipEvent_t> const& startEvents, |
| 84 | std::vector<hipEvent_t> const& stopEvents); |
| 85 | |
| 86 | bool FindCodeObject(std::string const& codeObjectFile); |
| 87 | |
| 88 | hipError_t initKernel(std::string const& name); |
| 89 | |
| 90 | hipError_t initKernels(std::vector<std::string> const& kernelNames); |
| 91 | |
| 92 | private: |
| 93 | hipError_t getKernel(hipFunction_t& rv, std::string const& name); |
| 94 | |
| 95 | std::mutex m_access; |
| 96 | |
| 97 | std::vector<hipModule_t> m_modules; |
| 98 | std::unordered_map<std::string, hipFunction_t> m_kernels; |
| 99 | bool m_debug = false; |
| 100 | bool m_debugSkipLaunch = false; |
| 101 | std::string m_name = "HipSolutionAdapter"; |
| 102 | std::string m_codeObjectDirectory; |
| 103 | |
| 104 | std::vector<std::string> m_loadedModuleNames; |
| 105 | std::unordered_set<std::string> m_loadedCOFiles; |
| 106 | |
| 107 | friend std::ostream& operator<<(std::ostream& stream, SolutionAdapter const& adapter); |
| 108 | }; |
| 109 | |
| 110 | std::ostream& operator<<(std::ostream& stream, SolutionAdapter const& adapter); |
| 111 | std::ostream& operator<<(std::ostream& stream, std::shared_ptr<SolutionAdapter> const& ptr); |
| 112 | } // namespace hip |
| 113 | } // namespace TensileLite |