device/program.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | #pragma once | ||
| 3 | |||
| 4 | #include <chrono> | ||
| 5 | #include <cstdint> | ||
| 6 | #include <map> | ||
| 7 | #include <memory> | ||
| 8 | #include <optional> | ||
| 9 | #include <string> | ||
| 10 | #include <string_view> | ||
| 11 | #include <vector> | ||
| 12 | |||
| 13 | #include "chrono_batch_render/device/adapter.h" | ||
| 14 | #include "chrono_batch_render/device/resource.h" | ||
| 15 | #include "chrono_batch_render/export.h" | ||
| 16 | |||
| 17 | namespace chrono::batch_render { | ||
| 18 | class DiskCache; | ||
| 19 | struct KernelLibrary; | ||
| 20 | struct DeviceGlobals; | ||
| 21 | } | ||
| 22 | |||
| 23 | namespace chrono::batch_render::device { | ||
| 24 | |||
| 25 | struct KernelCode; | ||
| 26 | class Program; | ||
| 27 | |||
| 28 | struct Size3 { | ||
| 29 | uint32_t x = 1; | ||
| 30 | uint32_t y = 1; | ||
| 31 | uint32_t z = 1; | ||
| 32 | |||
| 33 | bool operator==(const Size3&) const = default; | ||
| 34 | }; | ||
| 35 | |||
| 36 | inline constexpr uint32_t kMaxGroupsPerAxis = 65535; | ||
| 37 | |||
| 38 | 171 | constexpr Size3 FoldGroups(uint32_t count) { | |
| 39 | 171 | const uint64_t plane = uint64_t(kMaxGroupsPerAxis) * kMaxGroupsPerAxis; | |
| 40 | 171 | const uint32_t z = uint32_t((count + plane - 1) / plane); | |
| 41 | 171 | const uint64_t perLayer = (uint64_t(count) + z - 1) / z; | |
| 42 | 171 | const uint32_t y = uint32_t((perLayer + kMaxGroupsPerAxis - 1) / kMaxGroupsPerAxis); | |
| 43 | 171 | const uint32_t x = uint32_t((perLayer + y - 1) / y); | |
| 44 | 171 | return {x, y, z}; | |
| 45 | } | ||
| 46 | |||
| 47 | 67378 | constexpr uint32_t GroupCount(uint64_t threadCount, uint32_t threadsPerGroup) { | |
| 48 | 67378 | return uint32_t((threadCount + threadsPerGroup - 1) / threadsPerGroup); | |
| 49 | } | ||
| 50 | |||
| 51 | CHRONO_BATCH_RENDER_API void RegisterFile(std::string path, std::string source); | ||
| 52 | |||
| 53 | struct ProgramDesc { | ||
| 54 | std::vector<std::string> modules; | ||
| 55 | const KernelLibrary* library = nullptr; | ||
| 56 | std::map<std::string, std::string, std::less<>> sources; | ||
| 57 | std::vector<std::string> entries; | ||
| 58 | bool loadEntries = true; | ||
| 59 | std::string label = "program"; | ||
| 60 | Traversal traversal = Traversal::Auto; | ||
| 61 | }; | ||
| 62 | |||
| 63 | ✗ | struct KernelStat { | |
| 64 | std::string name; | ||
| 65 | std::string value; | ||
| 66 | }; | ||
| 67 | |||
| 68 | 1117 | struct KernelStats { | |
| 69 | double compileMs = 0; | ||
| 70 | bool cached = false; | ||
| 71 | std::optional<uint32_t> registers; | ||
| 72 | std::optional<uint32_t> spillBytes; | ||
| 73 | std::optional<uint32_t> sharedBytes; | ||
| 74 | std::vector<KernelStat> driver; | ||
| 75 | }; | ||
| 76 | |||
| 77 | class CHRONO_BATCH_RENDER_API Kernel { | ||
| 78 | public: | ||
| 79 | virtual ~Kernel(); | ||
| 80 | Kernel(const Kernel&) = delete; | ||
| 81 | Kernel& operator=(const Kernel&) = delete; | ||
| 82 | |||
| 83 | 3354 | const std::string& Name() const { | |
| 84 | 4471 | return m_name; | |
| 85 | } | ||
| 86 | |||
| 87 | 233700 | Program& GetProgram() const { | |
| 88 | 233700 | return *m_program; | |
| 89 | } | ||
| 90 | |||
| 91 | bool Ready() const; | ||
| 92 | |||
| 93 | void Wait() const; | ||
| 94 | |||
| 95 | Size3 GroupSize() const; | ||
| 96 | |||
| 97 | uint32_t ParamBytes() const; | ||
| 98 | |||
| 99 | bool Foldable() const; | ||
| 100 | |||
| 101 | const KernelStats& Stats() const; | ||
| 102 | |||
| 103 | protected: | ||
| 104 | Kernel(); | ||
| 105 | |||
| 106 | virtual KernelStats Load(const KernelCode& code, const DiskCache& cache, | ||
| 107 | const DeviceGlobals& globals) = 0; | ||
| 108 | |||
| 109 | private: | ||
| 110 | friend class Program; | ||
| 111 | struct State; | ||
| 112 | |||
| 113 | std::string m_name; | ||
| 114 | Program* m_program = nullptr; | ||
| 115 | std::unique_ptr<State> m_state; | ||
| 116 | }; | ||
| 117 | |||
| 118 | CHRONO_BATCH_RENDER_API void WaitForBuilds(); | ||
| 119 | |||
| 120 | class CHRONO_BATCH_RENDER_API Program : public Resource { | ||
| 121 | public: | ||
| 122 | ~Program() override; | ||
| 123 | |||
| 124 | Kernel& Entry(std::string_view name); | ||
| 125 | |||
| 126 | private: | ||
| 127 | friend class Device; | ||
| 128 | struct State; | ||
| 129 | |||
| 130 | Program(Device& device, const ProgramDesc& desc, | ||
| 131 | std::shared_ptr<const std::map<std::string, std::string, std::less<>>> sources); | ||
| 132 | |||
| 133 | Kernel& Request(std::string_view name, bool wanted); | ||
| 134 | |||
| 135 | void QueueLoad(Kernel& kernel); | ||
| 136 | |||
| 137 | void Load(Kernel& kernel, const KernelCode& code, const DiskCache& cache, | ||
| 138 | std::chrono::steady_clock::time_point start); | ||
| 139 | |||
| 140 | void Finish(Kernel& kernel, const std::string& label); | ||
| 141 | |||
| 142 | std::unique_ptr<State> m_state; | ||
| 143 | }; | ||
| 144 | |||
| 145 | } | ||
| 146 |