device/hip/device.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | #pragma once | ||
| 3 | |||
| 4 | #include <memory> | ||
| 5 | #include <string> | ||
| 6 | #include <string_view> | ||
| 7 | #include <utility> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "chrono_batch_render/device/device.h" | ||
| 11 | #include "chrono_batch_render/device/hip/api.h" | ||
| 12 | #include "chrono_batch_render/device/slang/compiler.h" | ||
| 13 | |||
| 14 | namespace chrono::batch_render::device::hip { | ||
| 15 | |||
| 16 | std::vector<AdapterInfo> ListAdapters(); | ||
| 17 | |||
| 18 | class DeviceScope { | ||
| 19 | public: | ||
| 20 | DeviceScope(const Api& hip, hipDevice_t device); | ||
| 21 | ~DeviceScope(); | ||
| 22 | DeviceScope(const DeviceScope&) = delete; | ||
| 23 | DeviceScope& operator=(const DeviceScope&) = delete; | ||
| 24 | |||
| 25 | private: | ||
| 26 | const Api& m_hip; | ||
| 27 | hipDevice_t m_previous = 0; | ||
| 28 | }; | ||
| 29 | |||
| 30 | class HipDevice : public Device { | ||
| 31 | public: | ||
| 32 | HipDevice(const AdapterInfo& adapter, const DeviceDesc& desc, Traversal traversal); | ||
| 33 | |||
| 34 | ✗ | const Api& Functions() const { | |
| 35 | ✗ | return m_hip; | |
| 36 | } | ||
| 37 | |||
| 38 | ✗ | hipDevice_t Ordinal() const { | |
| 39 | ✗ | return m_device; | |
| 40 | } | ||
| 41 | |||
| 42 | ✗ | compute::ModuleCache& Modules() { | |
| 43 | ✗ | return m_modules; | |
| 44 | } | ||
| 45 | |||
| 46 | protected: | ||
| 47 | Ref<Buffer> NewBuffer(const BufferDesc& desc) override; | ||
| 48 | Ref<Texture> NewTexture(const TextureDesc& desc) override; | ||
| 49 | void ExternalCopy(BufferRange dst, uint64_t source, Stream producer) override; | ||
| 50 | std::unique_ptr<Kernel> NewKernel() override; | ||
| 51 | std::vector<MemoryHeap> Heaps() const override; | ||
| 52 | |||
| 53 | private: | ||
| 54 | const Api& m_hip; | ||
| 55 | hipDevice_t m_device; | ||
| 56 | compute::ModuleCache m_modules; | ||
| 57 | }; | ||
| 58 | |||
| 59 | struct Traits { | ||
| 60 | using Device = HipDevice; | ||
| 61 | using Api = hip::Api; | ||
| 62 | using Stream = hipStream_t; | ||
| 63 | using Event = hipEvent_t; | ||
| 64 | using Graph = hipGraphExec_t; | ||
| 65 | using Pool = hipMemPool_t; | ||
| 66 | using Module = hipModule_t; | ||
| 67 | using Function = hipFunction_t; | ||
| 68 | using ArrayDesc = HIP_ARRAY3D_DESCRIPTOR; | ||
| 69 | using ResourceDesc = HIP_RESOURCE_DESC; | ||
| 70 | using SamplerDesc = HIP_TEXTURE_DESC; | ||
| 71 | |||
| 72 | static DeviceScope Enter(const HipDevice& device); | ||
| 73 | |||
| 74 | static uint64_t Allocate(const Api& hip, uint64_t bytes); | ||
| 75 | static void Free(const Api& hip, uint64_t address); | ||
| 76 | static hipMemPool_t CreatePool(const HipDevice& device, uint64_t keptBytes); | ||
| 77 | static void DestroyPool(const Api& hip, hipMemPool_t pool); | ||
| 78 | static uint64_t AllocateFrom(const Api& hip, hipMemPool_t pool, uint64_t bytes, | ||
| 79 | hipStream_t stream); | ||
| 80 | static void FreeTo(const Api& hip, uint64_t address, hipStream_t stream); | ||
| 81 | static void SynchronizeStream(const Api& hip, hipStream_t stream); | ||
| 82 | static void* AllocateHost(const Api& hip, uint64_t bytes, bool writeCombined); | ||
| 83 | static uint64_t DeviceAddress(const Api& hip, void* host); | ||
| 84 | static void FreeHost(const Api& hip, void* host); | ||
| 85 | |||
| 86 | static hipStream_t CreateStream(const Api& hip); | ||
| 87 | static void DestroyStream(const Api& hip, hipStream_t stream); | ||
| 88 | static hipEvent_t CreateEvent(const Api& hip, bool timed); | ||
| 89 | static void DestroyEvent(const Api& hip, hipEvent_t event); | ||
| 90 | static void Record(const Api& hip, hipEvent_t event, hipStream_t stream, bool node); | ||
| 91 | static void WaitEvent(const Api& hip, hipStream_t stream, hipEvent_t event); | ||
| 92 | static hipStream_t ApplicationStream(void* handle); | ||
| 93 | static uint64_t ElapsedNs(const Api& hip, hipEvent_t from, hipEvent_t to); | ||
| 94 | static bool Query(const Api& hip, hipEvent_t event); | ||
| 95 | static void Synchronize(const Api& hip, hipEvent_t event); | ||
| 96 | static void Copy(const Api& hip, uint64_t dst, uint64_t src, uint64_t bytes, | ||
| 97 | hipStream_t stream); | ||
| 98 | static void Fill(const Api& hip, uint64_t dst, uint32_t value, uint64_t bytes, | ||
| 99 | hipStream_t stream); | ||
| 100 | static void Upload(const Api& hip, uint64_t dst, const void* host, uint64_t bytes, | ||
| 101 | hipStream_t stream); | ||
| 102 | static void* CreateTextureArray(const Api& hip, const ArrayDesc& desc, uint32_t levels); | ||
| 103 | static void DestroyTextureArray(const Api& hip, void* array); | ||
| 104 | static void* TextureLevel(const Api& hip, void* array, uint32_t level); | ||
| 105 | static uint64_t CreateTextureObject(const Api& hip, const ResourceDesc& resource, | ||
| 106 | const SamplerDesc& sampler); | ||
| 107 | static void DestroyTextureObject(const Api& hip, uint64_t object); | ||
| 108 | static void CopyToArray(const Api& hip, void* level, uint64_t source, uint64_t rowBytes, | ||
| 109 | uint32_t rows, hipStream_t stream); | ||
| 110 | static void BeginCapture(const Api& hip, hipStream_t stream); | ||
| 111 | static hipGraphExec_t EndCapture(const Api& hip, hipStream_t stream); | ||
| 112 | static void LaunchGraph(const Api& hip, hipGraphExec_t graph, hipStream_t stream); | ||
| 113 | static void DestroyGraph(const Api& hip, hipGraphExec_t graph); | ||
| 114 | |||
| 115 | static const compute::RuntimeCompiler& RuntimeCompiler(); | ||
| 116 | static std::string_view Prelude(); | ||
| 117 | static std::vector<std::string> CompileOptions(const std::string& architecture, Size3 groupSize, | ||
| 118 | uint32_t maxRegisters, bool lineInfo); | ||
| 119 | static hipModule_t LoadModule(const Api& hip, const std::string& binary); | ||
| 120 | static void UnloadModule(const Api& hip, hipModule_t module); | ||
| 121 | static std::pair<uint64_t, size_t> Symbol(const Api& hip, hipModule_t module, const char* name); | ||
| 122 | static void CopyToDevice(const Api& hip, uint64_t dst, const void* host, uint64_t bytes); | ||
| 123 | static hipFunction_t GetFunction(const Api& hip, hipModule_t module, const char* name); | ||
| 124 | static KernelStats Stats(const Api& hip, hipFunction_t function); | ||
| 125 | static void Launch(const Api& hip, hipFunction_t function, Size3 groups, Size3 threads, | ||
| 126 | void** extra, hipStream_t stream); | ||
| 127 | static constexpr uintptr_t kParamBuffer = HIP_LAUNCH_PARAM_BUFFER_POINTER; | ||
| 128 | static constexpr uintptr_t kParamBufferSize = HIP_LAUNCH_PARAM_BUFFER_SIZE; | ||
| 129 | static constexpr uintptr_t kParamEnd = HIP_LAUNCH_PARAM_END; | ||
| 130 | }; | ||
| 131 | |||
| 132 | } | ||
| 133 |