diff options
| author | FJBP-code <franciscojavier.s629475@cesurformacion.com> | 2026-11-22 02:27:59 +0100 |
|---|---|---|
| committer | FJBP-code <franciscojavier.s629475@cesurformacion.com> | 2026-11-22 02:27:59 +0100 |
| commit | b90a2d0914719cbccb417c3f21feebd7e7a0fdb6 (patch) | |
| tree | abe1ae2524d79e7338467bea38c8b45e4db0fd07 /runtime | |
| parent | ee97b843bedba7b1e3d61e2273ec5c92a5b3e7b5 (diff) | |
D3D-56: add GPU descriptor hash-table micro-benchmark (vk_descriptor_bench)
Diffstat (limited to 'runtime')
| -rw-r--r-- | runtime/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | runtime/test/vk_descriptor_bench.cpp | 2122 |
2 files changed, 2124 insertions, 1 deletions
diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index e8ff18960..508badc47 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt | |||
| @@ -397,7 +397,7 @@ endif() | |||
| 397 | find_package(Vulkan QUIET) | 397 | find_package(Vulkan QUIET) |
| 398 | if(Vulkan_FOUND) | 398 | if(Vulkan_FOUND) |
| 399 | foreach(tool vk_harness vk_texture_test vk_compute_test vk_store_test vk_pipeline_check vk_command_replay_test | 399 | foreach(tool vk_harness vk_texture_test vk_compute_test vk_store_test vk_pipeline_check vk_command_replay_test |
| 400 | vk_bindless_test) | 400 | vk_bindless_test vk_descriptor_bench) |
| 401 | add_executable(${tool} test/${tool}.cpp) | 401 | add_executable(${tool} test/${tool}.cpp) |
| 402 | target_link_libraries(${tool} PRIVATE Vulkan::Headers ${CMAKE_DL_LIBS}) | 402 | target_link_libraries(${tool} PRIVATE Vulkan::Headers ${CMAKE_DL_LIBS}) |
| 403 | if(WIN32) | 403 | if(WIN32) |
| @@ -505,6 +505,7 @@ if(Vulkan_FOUND) | |||
| 505 | add_executable(gfx10_descriptors_test test/gfx10_descriptors_test.cpp) | 505 | add_executable(gfx10_descriptors_test test/gfx10_descriptors_test.cpp) |
| 506 | target_link_libraries(gfx10_descriptors_test PRIVATE Vulkan::Headers) | 506 | target_link_libraries(gfx10_descriptors_test PRIVATE Vulkan::Headers) |
| 507 | add_test(NAME gfx10_descriptors COMMAND gfx10_descriptors_test) | 507 | add_test(NAME gfx10_descriptors COMMAND gfx10_descriptors_test) |
| 508 | add_test(NAME vk_descriptor_bench_selftest COMMAND vk_descriptor_bench --self-test) | ||
| 508 | endif() | 509 | endif() |
| 509 | else() | 510 | else() |
| 510 | message(STATUS "Vulkan SDK not found: GPU test tools, vk_pipeline_check, d3d12_state_map_test and " | 511 | message(STATUS "Vulkan SDK not found: GPU test tools, vk_pipeline_check, d3d12_state_map_test and " |
diff --git a/runtime/test/vk_descriptor_bench.cpp b/runtime/test/vk_descriptor_bench.cpp new file mode 100644 index 000000000..89a749f6d --- /dev/null +++ b/runtime/test/vk_descriptor_bench.cpp | |||
| @@ -0,0 +1,2122 @@ | |||
| 1 | // GPU micro-benchmark for the descriptor hash table (set 0, binding 2 contract, D3D-56). | ||
| 2 | // Measures T# and S# hash-table lookups at 1k, 10k, and 100k entries across wave-uniform and lane-divergent | ||
| 3 | // access patterns, hits and misses, and buffer memory placements (DEVICE_LOCAL vs HOST_VISIBLE vs ReBAR). | ||
| 4 | // | ||
| 5 | // The compute SPIR-V modules are built in memory using the exact FNV-1a + 8-slot linear probe loop emitted by | ||
| 6 | // tools/rdna2spirv.py (desc_probe), so this tool has no glslangValidator build dependency and checks every | ||
| 7 | // dispatch against CPU DescriptorTable::lookup() before timing. | ||
| 8 | // | ||
| 9 | // Usage: | ||
| 10 | // vk_descriptor_bench [--sizes 1000,10000,100000] [--groups 4096] [--iters 64] [--reps 25] [--warmup 5] | ||
| 11 | // [--memory all|device|host|rebar] [--device N] [--list-devices] [--quick] [--csv] | ||
| 12 | // [--dump-spv PREFIX] [--self-test] | ||
| 13 | // LEONIDA_VK_VALIDATION=1 enables VK_LAYER_KHRONOS_validation; any validation error fails the run. | ||
| 14 | #define VK_NO_PROTOTYPES | ||
| 15 | #include "vulkan_loader.h" | ||
| 16 | #include "../src/descriptor_table.h" | ||
| 17 | |||
| 18 | #include <algorithm> | ||
| 19 | #include <array> | ||
| 20 | #include <chrono> | ||
| 21 | #include <cmath> | ||
| 22 | #include <cstdint> | ||
| 23 | #include <cstdio> | ||
| 24 | #include <cstdlib> | ||
| 25 | #include <cstring> | ||
| 26 | #include <fstream> | ||
| 27 | #include <limits> | ||
| 28 | #include <map> | ||
| 29 | #include <numeric> | ||
| 30 | #include <string> | ||
| 31 | #include <string_view> | ||
| 32 | #include <utility> | ||
| 33 | #include <vector> | ||
| 34 | |||
| 35 | #define CHECK_VK(expr) \ | ||
| 36 | do { \ | ||
| 37 | const VkResult vk_res_ = (expr); \ | ||
| 38 | if (vk_res_ != VK_SUCCESS) { \ | ||
| 39 | std::fprintf(stderr, "FAILED %s -> %d (line %d)\n", #expr, int(vk_res_), __LINE__); \ | ||
| 40 | return 1; \ | ||
| 41 | } \ | ||
| 42 | } while (0) | ||
| 43 | |||
| 44 | namespace { | ||
| 45 | |||
| 46 | using namespace leonida; | ||
| 47 | |||
| 48 | constexpr int kUnsupportedExit = 77; | ||
| 49 | constexpr std::uint32_t kWorkgroupSize = 64; | ||
| 50 | unsigned g_validation_errors = 0; | ||
| 51 | |||
| 52 | VKAPI_ATTR VkBool32 VKAPI_CALL on_debug_message(VkDebugUtilsMessageSeverityFlagBitsEXT severity, | ||
| 53 | VkDebugUtilsMessageTypeFlagsEXT types, | ||
| 54 | const VkDebugUtilsMessengerCallbackDataEXT* data, void*) { | ||
| 55 | if ((severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) && | ||
| 56 | (types & VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT)) { | ||
| 57 | ++g_validation_errors; | ||
| 58 | } | ||
| 59 | std::fprintf(stderr, "validation: %s\n", data && data->pMessage ? data->pMessage : "(no message)"); | ||
| 60 | return VK_FALSE; | ||
| 61 | } | ||
| 62 | |||
| 63 | // ---- SPIR-V 1.3 binary builder matching tools/rdna2spirv.py desc_probe ---- | ||
| 64 | |||
| 65 | namespace spv_op { | ||
| 66 | constexpr std::uint32_t kMagic = 0x07230203u; | ||
| 67 | constexpr std::uint32_t kVersion13 = 0x00010300u; | ||
| 68 | |||
| 69 | constexpr std::uint32_t kCapShader = 1u; | ||
| 70 | constexpr std::uint32_t kAddrLogical = 0u; | ||
| 71 | constexpr std::uint32_t kMemGlsl450 = 1u; | ||
| 72 | constexpr std::uint32_t kExecGLCompute = 5u; | ||
| 73 | constexpr std::uint32_t kExecModeLocalSize = 17u; | ||
| 74 | |||
| 75 | constexpr std::uint32_t kScInput = 1u; | ||
| 76 | constexpr std::uint32_t kScFunction = 7u; | ||
| 77 | constexpr std::uint32_t kScPushConstant = 9u; | ||
| 78 | constexpr std::uint32_t kScStorageBuffer = 12u; | ||
| 79 | |||
| 80 | constexpr std::uint32_t kDecBlock = 2u; | ||
| 81 | constexpr std::uint32_t kDecArrayStride = 6u; | ||
| 82 | constexpr std::uint32_t kDecBuiltIn = 11u; | ||
| 83 | constexpr std::uint32_t kDecNonWritable = 24u; | ||
| 84 | constexpr std::uint32_t kDecNonReadable = 25u; | ||
| 85 | constexpr std::uint32_t kDecBinding = 33u; | ||
| 86 | constexpr std::uint32_t kDecDescriptorSet = 34u; | ||
| 87 | constexpr std::uint32_t kDecOffset = 35u; | ||
| 88 | constexpr std::uint32_t kBuiltInGlobalInvocationId = 28u; | ||
| 89 | |||
| 90 | constexpr std::uint32_t kOpMemoryModel = 14u; | ||
| 91 | constexpr std::uint32_t kOpEntryPoint = 15u; | ||
| 92 | constexpr std::uint32_t kOpExecutionMode = 16u; | ||
| 93 | constexpr std::uint32_t kOpCapability = 17u; | ||
| 94 | constexpr std::uint32_t kOpTypeVoid = 19u; | ||
| 95 | constexpr std::uint32_t kOpTypeBool = 20u; | ||
| 96 | constexpr std::uint32_t kOpTypeInt = 21u; | ||
| 97 | constexpr std::uint32_t kOpTypeVector = 23u; | ||
| 98 | constexpr std::uint32_t kOpTypeRuntimeArray = 29u; | ||
| 99 | constexpr std::uint32_t kOpTypeStruct = 30u; | ||
| 100 | constexpr std::uint32_t kOpTypePointer = 32u; | ||
| 101 | constexpr std::uint32_t kOpTypeFunction = 33u; | ||
| 102 | constexpr std::uint32_t kOpConstantTrue = 41u; | ||
| 103 | constexpr std::uint32_t kOpConstant = 43u; | ||
| 104 | constexpr std::uint32_t kOpFunction = 54u; | ||
| 105 | constexpr std::uint32_t kOpFunctionParameter = 55u; | ||
| 106 | constexpr std::uint32_t kOpFunctionEnd = 56u; | ||
| 107 | constexpr std::uint32_t kOpFunctionCall = 57u; | ||
| 108 | constexpr std::uint32_t kOpVariable = 59u; | ||
| 109 | constexpr std::uint32_t kOpLoad = 61u; | ||
| 110 | constexpr std::uint32_t kOpStore = 62u; | ||
| 111 | constexpr std::uint32_t kOpAccessChain = 65u; | ||
| 112 | constexpr std::uint32_t kOpDecorate = 71u; | ||
| 113 | constexpr std::uint32_t kOpMemberDecorate = 72u; | ||
| 114 | constexpr std::uint32_t kOpIAdd = 128u; | ||
| 115 | constexpr std::uint32_t kOpIMul = 132u; | ||
| 116 | constexpr std::uint32_t kOpLogicalAnd = 167u; | ||
| 117 | constexpr std::uint32_t kOpIEqual = 170u; | ||
| 118 | constexpr std::uint32_t kOpULessThan = 176u; | ||
| 119 | constexpr std::uint32_t kOpShiftRightLogical = 194u; | ||
| 120 | constexpr std::uint32_t kOpShiftLeftLogical = 196u; | ||
| 121 | constexpr std::uint32_t kOpBitwiseXor = 198u; | ||
| 122 | constexpr std::uint32_t kOpBitwiseAnd = 199u; | ||
| 123 | constexpr std::uint32_t kOpLoopMerge = 246u; | ||
| 124 | constexpr std::uint32_t kOpSelectionMerge = 247u; | ||
| 125 | constexpr std::uint32_t kOpLabel = 248u; | ||
| 126 | constexpr std::uint32_t kOpBranch = 249u; | ||
| 127 | constexpr std::uint32_t kOpBranchConditional = 250u; | ||
| 128 | constexpr std::uint32_t kOpReturn = 253u; | ||
| 129 | constexpr std::uint32_t kOpReturnValue = 254u; | ||
| 130 | } // namespace spv_op | ||
| 131 | |||
| 132 | enum class ShaderKind { | ||
| 133 | Baseline, | ||
| 134 | SingleLookup, | ||
| 135 | PairTSLookup, | ||
| 136 | }; | ||
| 137 | |||
| 138 | struct alignas(4) PushConstants { | ||
| 139 | std::uint32_t key_mask; // pc[0]: mask for T#/Miss ring (power of two minus 1) | ||
| 140 | std::uint32_t iters; // pc[1]: loop iterations per invocation | ||
| 141 | std::uint32_t group_stride; // pc[2]: start index delta per 64-invocation group | ||
| 142 | std::uint32_t lane_stride; // pc[3]: start index delta per lane (0 => wave-uniform, 1 => divergent) | ||
| 143 | std::uint32_t iter_stride; // pc[4]: index delta per loop iteration | ||
| 144 | std::uint32_t base_index; // pc[5]: starting key offset within bank | ||
| 145 | std::uint32_t dep_mask; // pc[6]: data-dependency mask from previous result into next key index | ||
| 146 | std::uint32_t t_base; // pc[7]: base key slot inside keys_buf for primary key | ||
| 147 | std::uint32_t s_base; // pc[8]: base key slot inside keys_buf for S# key (PairTSLookup) | ||
| 148 | std::uint32_t s_mask; // pc[9]: mask for S# ring (PairTSLookup) | ||
| 149 | }; | ||
| 150 | static_assert(sizeof(PushConstants) == 40); | ||
| 151 | |||
| 152 | class SpirvBuilder { | ||
| 153 | public: | ||
| 154 | std::vector<std::uint32_t> build(ShaderKind kind) { | ||
| 155 | const bool has_desc_map = kind != ShaderKind::Baseline; | ||
| 156 | |||
| 157 | const std::uint32_t ty_void = id(); | ||
| 158 | const std::uint32_t ty_bool = id(); | ||
| 159 | const std::uint32_t ty_uint = id(); | ||
| 160 | const std::uint32_t ty_v3u = id(); | ||
| 161 | const std::uint32_t ty_rt_u = id(); | ||
| 162 | const std::uint32_t ty_buf = id(); | ||
| 163 | const std::uint32_t ty_pc = id(); | ||
| 164 | const std::uint32_t ptr_in_v3u = id(); | ||
| 165 | const std::uint32_t ptr_in_u = id(); | ||
| 166 | const std::uint32_t ptr_sb_buf = id(); | ||
| 167 | const std::uint32_t ptr_sb_u = id(); | ||
| 168 | const std::uint32_t ptr_pc = id(); | ||
| 169 | const std::uint32_t ptr_pc_u = id(); | ||
| 170 | const std::uint32_t ptr_fn_u = id(); | ||
| 171 | const std::uint32_t ty_fn_main = id(); | ||
| 172 | const std::uint32_t ty_fn_lookup = has_desc_map ? id() : 0u; | ||
| 173 | const std::uint32_t true_id = has_desc_map ? id() : 0u; | ||
| 174 | |||
| 175 | const std::uint32_t gid_var = id(); | ||
| 176 | const std::uint32_t keys_var = id(); | ||
| 177 | const std::uint32_t map_var = has_desc_map ? id() : 0u; | ||
| 178 | const std::uint32_t out_var = id(); | ||
| 179 | const std::uint32_t pc_var = id(); | ||
| 180 | const std::uint32_t fn_lookup = has_desc_map ? id() : 0u; | ||
| 181 | const std::uint32_t fn_main = id(); | ||
| 182 | |||
| 183 | // Prologue: Capability, MemoryModel, EntryPoint, ExecutionMode | ||
| 184 | emit(preamble_, spv_op::kOpCapability, {spv_op::kCapShader}); | ||
| 185 | emit(preamble_, spv_op::kOpMemoryModel, {spv_op::kAddrLogical, spv_op::kMemGlsl450}); | ||
| 186 | emit(preamble_, spv_op::kOpEntryPoint, | ||
| 187 | {spv_op::kExecGLCompute, fn_main, 0x6E69616Du, 0x00000000u, gid_var}); | ||
| 188 | emit(preamble_, spv_op::kOpExecutionMode, | ||
| 189 | {fn_main, spv_op::kExecModeLocalSize, kWorkgroupSize, 1u, 1u}); | ||
| 190 | |||
| 191 | // Annotations | ||
| 192 | emit(decs_, spv_op::kOpDecorate, {gid_var, spv_op::kDecBuiltIn, spv_op::kBuiltInGlobalInvocationId}); | ||
| 193 | emit(decs_, spv_op::kOpDecorate, {ty_rt_u, spv_op::kDecArrayStride, 4u}); | ||
| 194 | emit(decs_, spv_op::kOpMemberDecorate, {ty_buf, 0u, spv_op::kDecOffset, 0u}); | ||
| 195 | emit(decs_, spv_op::kOpDecorate, {ty_buf, spv_op::kDecBlock}); | ||
| 196 | |||
| 197 | emit(decs_, spv_op::kOpDecorate, {keys_var, spv_op::kDecDescriptorSet, 0u}); | ||
| 198 | emit(decs_, spv_op::kOpDecorate, {keys_var, spv_op::kDecBinding, 0u}); | ||
| 199 | emit(decs_, spv_op::kOpDecorate, {keys_var, spv_op::kDecNonWritable}); | ||
| 200 | |||
| 201 | if (has_desc_map) { | ||
| 202 | emit(decs_, spv_op::kOpDecorate, {map_var, spv_op::kDecDescriptorSet, 0u}); | ||
| 203 | emit(decs_, spv_op::kOpDecorate, {map_var, spv_op::kDecBinding, 2u}); | ||
| 204 | emit(decs_, spv_op::kOpDecorate, {map_var, spv_op::kDecNonWritable}); | ||
| 205 | } | ||
| 206 | |||
| 207 | emit(decs_, spv_op::kOpDecorate, {out_var, spv_op::kDecDescriptorSet, 0u}); | ||
| 208 | emit(decs_, spv_op::kOpDecorate, {out_var, spv_op::kDecBinding, 3u}); | ||
| 209 | emit(decs_, spv_op::kOpDecorate, {out_var, spv_op::kDecNonReadable}); | ||
| 210 | |||
| 211 | for (std::uint32_t m = 0; m < 10u; ++m) { | ||
| 212 | emit(decs_, spv_op::kOpMemberDecorate, {ty_pc, m, spv_op::kDecOffset, m * 4u}); | ||
| 213 | } | ||
| 214 | emit(decs_, spv_op::kOpDecorate, {ty_pc, spv_op::kDecBlock}); | ||
| 215 | |||
| 216 | // Types | ||
| 217 | emit(types_, spv_op::kOpTypeVoid, {ty_void}); | ||
| 218 | emit(types_, spv_op::kOpTypeBool, {ty_bool}); | ||
| 219 | emit(types_, spv_op::kOpTypeInt, {ty_uint, 32u, 0u}); | ||
| 220 | emit(types_, spv_op::kOpTypeVector, {ty_v3u, ty_uint, 3u}); | ||
| 221 | emit(types_, spv_op::kOpTypeRuntimeArray, {ty_rt_u, ty_uint}); | ||
| 222 | emit(types_, spv_op::kOpTypeStruct, {ty_buf, ty_rt_u}); | ||
| 223 | emit(types_, spv_op::kOpTypeStruct, | ||
| 224 | {ty_pc, ty_uint, ty_uint, ty_uint, ty_uint, ty_uint, ty_uint, ty_uint, ty_uint, ty_uint, ty_uint}); | ||
| 225 | emit(types_, spv_op::kOpTypePointer, {ptr_in_v3u, spv_op::kScInput, ty_v3u}); | ||
| 226 | emit(types_, spv_op::kOpTypePointer, {ptr_in_u, spv_op::kScInput, ty_uint}); | ||
| 227 | emit(types_, spv_op::kOpTypePointer, {ptr_sb_buf, spv_op::kScStorageBuffer, ty_buf}); | ||
| 228 | emit(types_, spv_op::kOpTypePointer, {ptr_sb_u, spv_op::kScStorageBuffer, ty_uint}); | ||
| 229 | emit(types_, spv_op::kOpTypePointer, {ptr_pc, spv_op::kScPushConstant, ty_pc}); | ||
| 230 | emit(types_, spv_op::kOpTypePointer, {ptr_pc_u, spv_op::kScPushConstant, ty_uint}); | ||
| 231 | emit(types_, spv_op::kOpTypePointer, {ptr_fn_u, spv_op::kScFunction, ty_uint}); | ||
| 232 | emit(types_, spv_op::kOpTypeFunction, {ty_fn_main, ty_void}); | ||
| 233 | if (has_desc_map) { | ||
| 234 | emit(types_, spv_op::kOpTypeFunction, | ||
| 235 | {ty_fn_lookup, ty_uint, ty_uint, ty_uint, ty_uint, ty_uint, ty_uint, ty_uint, ty_uint, ty_uint}); | ||
| 236 | emit(consts_, spv_op::kOpConstantTrue, {ty_bool, true_id}); | ||
| 237 | } | ||
| 238 | |||
| 239 | // Global variables (placed after constants & pointer types) | ||
| 240 | emit(gvars_, spv_op::kOpVariable, {ptr_in_v3u, gid_var, spv_op::kScInput}); | ||
| 241 | emit(gvars_, spv_op::kOpVariable, {ptr_sb_buf, keys_var, spv_op::kScStorageBuffer}); | ||
| 242 | if (has_desc_map) { | ||
| 243 | emit(gvars_, spv_op::kOpVariable, {ptr_sb_buf, map_var, spv_op::kScStorageBuffer}); | ||
| 244 | } | ||
| 245 | emit(gvars_, spv_op::kOpVariable, {ptr_sb_buf, out_var, spv_op::kScStorageBuffer}); | ||
| 246 | emit(gvars_, spv_op::kOpVariable, {ptr_pc, pc_var, spv_op::kScPushConstant}); | ||
| 247 | |||
| 248 | ty_bool_ = ty_bool; | ||
| 249 | ty_uint_ = ty_uint; | ||
| 250 | ptr_sb_u_ = ptr_sb_u; | ||
| 251 | ptr_fn_u_ = ptr_fn_u; | ||
| 252 | |||
| 253 | // %desc_lookup_fn(w0..w7) -> uint: identical probe loop to tools/rdna2spirv.py desc_probe() | ||
| 254 | if (has_desc_map) { | ||
| 255 | emit_desc_lookup_fn(fn_lookup, ty_fn_lookup, map_var, true_id); | ||
| 256 | } | ||
| 257 | |||
| 258 | // %main() | ||
| 259 | emit_main_fn(kind, fn_main, ty_void, ty_fn_main, ptr_in_u, ptr_pc_u, gid_var, keys_var, out_var, pc_var, | ||
| 260 | fn_lookup); | ||
| 261 | |||
| 262 | std::vector<std::uint32_t> spv; | ||
| 263 | spv.reserve(5 + preamble_.size() + decs_.size() + types_.size() + consts_.size() + gvars_.size() + funcs_.size()); | ||
| 264 | spv.push_back(spv_op::kMagic); | ||
| 265 | spv.push_back(spv_op::kVersion13); | ||
| 266 | spv.push_back(0u); | ||
| 267 | spv.push_back(next_id_); | ||
| 268 | spv.push_back(0u); | ||
| 269 | append(spv, preamble_); | ||
| 270 | append(spv, decs_); | ||
| 271 | append(spv, types_); | ||
| 272 | append(spv, consts_); | ||
| 273 | append(spv, gvars_); | ||
| 274 | append(spv, funcs_); | ||
| 275 | return spv; | ||
| 276 | } | ||
| 277 | |||
| 278 | private: | ||
| 279 | std::uint32_t id() { | ||
| 280 | return next_id_++; | ||
| 281 | } | ||
| 282 | |||
| 283 | std::uint32_t uconst(std::uint32_t value) { | ||
| 284 | const auto [it, inserted] = uconsts_.emplace(value, 0u); | ||
| 285 | if (inserted) { | ||
| 286 | it->second = id(); | ||
| 287 | emit(consts_, spv_op::kOpConstant, {ty_uint_, it->second, value}); | ||
| 288 | } | ||
| 289 | return it->second; | ||
| 290 | } | ||
| 291 | |||
| 292 | static void emit(std::vector<std::uint32_t>& dst, std::uint32_t opcode, std::initializer_list<std::uint32_t> ops) { | ||
| 293 | const std::uint32_t words = static_cast<std::uint32_t>(1 + ops.size()); | ||
| 294 | dst.push_back((words << 16) | opcode); | ||
| 295 | for (const std::uint32_t w : ops) dst.push_back(w); | ||
| 296 | } | ||
| 297 | |||
| 298 | static void emit_vec(std::vector<std::uint32_t>& dst, std::uint32_t opcode, const std::vector<std::uint32_t>& ops) { | ||
| 299 | const std::uint32_t words = static_cast<std::uint32_t>(1 + ops.size()); | ||
| 300 | dst.push_back((words << 16) | opcode); | ||
| 301 | for (const std::uint32_t w : ops) dst.push_back(w); | ||
| 302 | } | ||
| 303 | |||
| 304 | static void append(std::vector<std::uint32_t>& dst, const std::vector<std::uint32_t>& src) { | ||
| 305 | dst.insert(dst.end(), src.begin(), src.end()); | ||
| 306 | } | ||
| 307 | |||
| 308 | std::uint32_t binop(std::uint32_t opcode, std::uint32_t res_type, std::uint32_t a, std::uint32_t b) { | ||
| 309 | const std::uint32_t r = id(); | ||
| 310 | emit(funcs_, opcode, {res_type, r, a, b}); | ||
| 311 | return r; | ||
| 312 | } | ||
| 313 | |||
| 314 | std::uint32_t load(std::uint32_t res_type, std::uint32_t ptr) { | ||
| 315 | const std::uint32_t r = id(); | ||
| 316 | emit(funcs_, spv_op::kOpLoad, {res_type, r, ptr}); | ||
| 317 | return r; | ||
| 318 | } | ||
| 319 | |||
| 320 | std::uint32_t access2(std::uint32_t ptr_type, std::uint32_t base, std::uint32_t idx0, std::uint32_t idx1) { | ||
| 321 | const std::uint32_t r = id(); | ||
| 322 | emit(funcs_, spv_op::kOpAccessChain, {ptr_type, r, base, idx0, idx1}); | ||
| 323 | return r; | ||
| 324 | } | ||
| 325 | |||
| 326 | void emit_desc_lookup_fn(std::uint32_t fn_id, std::uint32_t fn_type, std::uint32_t map_var, | ||
| 327 | std::uint32_t true_id) { | ||
| 328 | const std::uint32_t c0 = uconst(0u); | ||
| 329 | const std::uint32_t c1 = uconst(1u); | ||
| 330 | const std::uint32_t c8 = uconst(kDescriptorProbes); | ||
| 331 | const std::uint32_t c9 = uconst(9u); | ||
| 332 | const std::uint32_t c10 = uconst(10u); | ||
| 333 | const std::uint32_t c_offset = uconst(kFnvOffsetBasis); | ||
| 334 | const std::uint32_t c_prime = uconst(kFnvPrime); | ||
| 335 | |||
| 336 | emit(funcs_, spv_op::kOpFunction, {ty_uint_, fn_id, 0u, fn_type}); | ||
| 337 | std::array<std::uint32_t, 8> params{}; | ||
| 338 | for (int i = 0; i < 8; ++i) { | ||
| 339 | params[i] = id(); | ||
| 340 | emit(funcs_, spv_op::kOpFunctionParameter, {ty_uint_, params[i]}); | ||
| 341 | } | ||
| 342 | |||
| 343 | const std::uint32_t lbl_entry = id(); | ||
| 344 | const std::uint32_t lbl_head = id(); | ||
| 345 | const std::uint32_t lbl_body = id(); | ||
| 346 | const std::uint32_t lbl_hit = id(); | ||
| 347 | const std::uint32_t lbl_next = id(); | ||
| 348 | const std::uint32_t lbl_cont = id(); | ||
| 349 | const std::uint32_t lbl_end = id(); | ||
| 350 | |||
| 351 | emit(funcs_, spv_op::kOpLabel, {lbl_entry}); | ||
| 352 | const std::uint32_t kv = id(); | ||
| 353 | const std::uint32_t iv = id(); | ||
| 354 | emit(funcs_, spv_op::kOpVariable, {ptr_fn_u_, kv, spv_op::kScFunction, c0}); | ||
| 355 | emit(funcs_, spv_op::kOpVariable, {ptr_fn_u_, iv, spv_op::kScFunction, c0}); | ||
| 356 | |||
| 357 | std::uint32_t h = c_offset; | ||
| 358 | for (int i = 0; i < 8; ++i) { | ||
| 359 | const std::uint32_t x = binop(spv_op::kOpBitwiseXor, ty_uint_, h, params[i]); | ||
| 360 | h = binop(spv_op::kOpIMul, ty_uint_, x, c_prime); | ||
| 361 | } | ||
| 362 | const std::uint32_t mask_ptr = access2(ptr_sb_u_, map_var, c0, c0); | ||
| 363 | const std::uint32_t mask = load(ty_uint_, mask_ptr); | ||
| 364 | emit(funcs_, spv_op::kOpBranch, {lbl_head}); | ||
| 365 | |||
| 366 | emit(funcs_, spv_op::kOpLabel, {lbl_head}); | ||
| 367 | emit(funcs_, spv_op::kOpLoopMerge, {lbl_end, lbl_cont, 0u}); | ||
| 368 | emit(funcs_, spv_op::kOpBranch, {lbl_body}); | ||
| 369 | |||
| 370 | emit(funcs_, spv_op::kOpLabel, {lbl_body}); | ||
| 371 | const std::uint32_t k = load(ty_uint_, kv); | ||
| 372 | const std::uint32_t s = binop(spv_op::kOpIAdd, ty_uint_, h, k); | ||
| 373 | const std::uint32_t slot = binop(spv_op::kOpBitwiseAnd, ty_uint_, s, mask); | ||
| 374 | const std::uint32_t base = binop(spv_op::kOpIMul, ty_uint_, slot, c10); | ||
| 375 | std::uint32_t match = true_id; | ||
| 376 | for (std::uint32_t j = 0; j < 8u; ++j) { | ||
| 377 | const std::uint32_t off = binop(spv_op::kOpIAdd, ty_uint_, base, uconst(1u + j)); | ||
| 378 | const std::uint32_t p = access2(ptr_sb_u_, map_var, c0, off); | ||
| 379 | const std::uint32_t v = load(ty_uint_, p); | ||
| 380 | const std::uint32_t eq = binop(spv_op::kOpIEqual, ty_bool_, v, params[j]); | ||
| 381 | match = binop(spv_op::kOpLogicalAnd, ty_bool_, match, eq); | ||
| 382 | } | ||
| 383 | const std::uint32_t idx_off = binop(spv_op::kOpIAdd, ty_uint_, base, c9); | ||
| 384 | const std::uint32_t idx_ptr = access2(ptr_sb_u_, map_var, c0, idx_off); | ||
| 385 | const std::uint32_t idx_val = load(ty_uint_, idx_ptr); | ||
| 386 | emit(funcs_, spv_op::kOpSelectionMerge, {lbl_next, 0u}); | ||
| 387 | emit(funcs_, spv_op::kOpBranchConditional, {match, lbl_hit, lbl_next}); | ||
| 388 | |||
| 389 | emit(funcs_, spv_op::kOpLabel, {lbl_hit}); | ||
| 390 | emit(funcs_, spv_op::kOpStore, {iv, idx_val}); | ||
| 391 | emit(funcs_, spv_op::kOpStore, {kv, c8}); | ||
| 392 | emit(funcs_, spv_op::kOpBranch, {lbl_next}); | ||
| 393 | |||
| 394 | emit(funcs_, spv_op::kOpLabel, {lbl_next}); | ||
| 395 | emit(funcs_, spv_op::kOpBranch, {lbl_cont}); | ||
| 396 | |||
| 397 | emit(funcs_, spv_op::kOpLabel, {lbl_cont}); | ||
| 398 | const std::uint32_t k2 = load(ty_uint_, kv); | ||
| 399 | const std::uint32_t k3 = binop(spv_op::kOpIAdd, ty_uint_, k2, c1); | ||
| 400 | emit(funcs_, spv_op::kOpStore, {kv, k3}); | ||
| 401 | const std::uint32_t more = binop(spv_op::kOpULessThan, ty_bool_, k3, c8); | ||
| 402 | emit(funcs_, spv_op::kOpBranchConditional, {more, lbl_head, lbl_end}); | ||
| 403 | |||
| 404 | emit(funcs_, spv_op::kOpLabel, {lbl_end}); | ||
| 405 | const std::uint32_t ret = load(ty_uint_, iv); | ||
| 406 | emit(funcs_, spv_op::kOpReturnValue, {ret}); | ||
| 407 | emit(funcs_, spv_op::kOpFunctionEnd, {}); | ||
| 408 | } | ||
| 409 | |||
| 410 | void emit_main_fn(ShaderKind kind, std::uint32_t fn_id, std::uint32_t ty_void, std::uint32_t ty_fn_main, | ||
| 411 | std::uint32_t ptr_in_u, std::uint32_t ptr_pc_u, std::uint32_t gid_var, std::uint32_t keys_var, | ||
| 412 | std::uint32_t out_var, std::uint32_t pc_var, std::uint32_t fn_lookup) { | ||
| 413 | const std::uint32_t c0 = uconst(0u); | ||
| 414 | const std::uint32_t c1 = uconst(1u); | ||
| 415 | const std::uint32_t c3 = uconst(3u); | ||
| 416 | const std::uint32_t c6 = uconst(6u); | ||
| 417 | const std::uint32_t c63 = uconst(63u); | ||
| 418 | const std::uint32_t c_offset = uconst(kFnvOffsetBasis); | ||
| 419 | const std::uint32_t c_prime = uconst(kFnvPrime); | ||
| 420 | const std::uint32_t c_ff = uconst(0xFFFFFFFFu); | ||
| 421 | |||
| 422 | emit(funcs_, spv_op::kOpFunction, {ty_void, fn_id, 0u, ty_fn_main}); | ||
| 423 | const std::uint32_t lbl_entry = id(); | ||
| 424 | const std::uint32_t lbl_head = id(); | ||
| 425 | const std::uint32_t lbl_body = id(); | ||
| 426 | const std::uint32_t lbl_cont = id(); | ||
| 427 | const std::uint32_t lbl_end = id(); | ||
| 428 | |||
| 429 | emit(funcs_, spv_op::kOpLabel, {lbl_entry}); | ||
| 430 | const std::uint32_t i_var = id(); | ||
| 431 | const std::uint32_t acc_var = id(); | ||
| 432 | const std::uint32_t prev_var = id(); | ||
| 433 | emit(funcs_, spv_op::kOpVariable, {ptr_fn_u_, i_var, spv_op::kScFunction, c0}); | ||
| 434 | emit(funcs_, spv_op::kOpVariable, {ptr_fn_u_, acc_var, spv_op::kScFunction, c_offset}); | ||
| 435 | emit(funcs_, spv_op::kOpVariable, {ptr_fn_u_, prev_var, spv_op::kScFunction, c0}); | ||
| 436 | |||
| 437 | const std::uint32_t gid_ptr = id(); | ||
| 438 | emit(funcs_, spv_op::kOpAccessChain, {ptr_in_u, gid_ptr, gid_var, c0}); | ||
| 439 | const std::uint32_t gid_x = load(ty_uint_, gid_ptr); | ||
| 440 | |||
| 441 | std::array<std::uint32_t, 10> pc{}; | ||
| 442 | for (std::uint32_t m = 0; m < 10u; ++m) { | ||
| 443 | const std::uint32_t p = id(); | ||
| 444 | emit(funcs_, spv_op::kOpAccessChain, {ptr_pc_u, p, pc_var, uconst(m)}); | ||
| 445 | pc[m] = load(ty_uint_, p); | ||
| 446 | } | ||
| 447 | |||
| 448 | const std::uint32_t wg = binop(spv_op::kOpShiftRightLogical, ty_uint_, gid_x, c6); | ||
| 449 | const std::uint32_t lid = binop(spv_op::kOpBitwiseAnd, ty_uint_, gid_x, c63); | ||
| 450 | const std::uint32_t wg_term = binop(spv_op::kOpIMul, ty_uint_, wg, pc[2]); | ||
| 451 | const std::uint32_t lid_term = binop(spv_op::kOpIMul, ty_uint_, lid, pc[3]); | ||
| 452 | const std::uint32_t s0 = binop(spv_op::kOpIAdd, ty_uint_, pc[5], wg_term); | ||
| 453 | const std::uint32_t start = binop(spv_op::kOpIAdd, ty_uint_, s0, lid_term); | ||
| 454 | emit(funcs_, spv_op::kOpBranch, {lbl_head}); | ||
| 455 | |||
| 456 | emit(funcs_, spv_op::kOpLabel, {lbl_head}); | ||
| 457 | const std::uint32_t iv = load(ty_uint_, i_var); | ||
| 458 | const std::uint32_t cond = binop(spv_op::kOpULessThan, ty_bool_, iv, pc[1]); | ||
| 459 | emit(funcs_, spv_op::kOpLoopMerge, {lbl_end, lbl_cont, 0u}); | ||
| 460 | emit(funcs_, spv_op::kOpBranchConditional, {cond, lbl_body, lbl_end}); | ||
| 461 | |||
| 462 | emit(funcs_, spv_op::kOpLabel, {lbl_body}); | ||
| 463 | const std::uint32_t prev = load(ty_uint_, prev_var); | ||
| 464 | const std::uint32_t dep = binop(spv_op::kOpBitwiseAnd, ty_uint_, prev, pc[6]); | ||
| 465 | const std::uint32_t iter_delta = binop(spv_op::kOpIMul, ty_uint_, iv, pc[4]); | ||
| 466 | const std::uint32_t idx_base = binop(spv_op::kOpIAdd, ty_uint_, start, iter_delta); | ||
| 467 | const std::uint32_t idx1 = binop(spv_op::kOpIAdd, ty_uint_, idx_base, dep); | ||
| 468 | |||
| 469 | const std::uint32_t masked_t = binop(spv_op::kOpBitwiseAnd, ty_uint_, idx1, pc[0]); | ||
| 470 | const std::uint32_t kidx = binop(spv_op::kOpIAdd, ty_uint_, pc[7], masked_t); | ||
| 471 | const std::uint32_t koff = binop(spv_op::kOpShiftLeftLogical, ty_uint_, kidx, c3); | ||
| 472 | |||
| 473 | std::array<std::uint32_t, 8> w{}; | ||
| 474 | for (std::uint32_t j = 0; j < 8u; ++j) { | ||
| 475 | const std::uint32_t ko = binop(spv_op::kOpIAdd, ty_uint_, koff, uconst(j)); | ||
| 476 | const std::uint32_t kp = access2(ptr_sb_u_, keys_var, c0, ko); | ||
| 477 | w[j] = load(ty_uint_, kp); | ||
| 478 | } | ||
| 479 | |||
| 480 | if (kind == ShaderKind::Baseline) { | ||
| 481 | std::uint32_t mix = w[0]; | ||
| 482 | for (int j = 1; j < 8; ++j) { | ||
| 483 | mix = binop(spv_op::kOpBitwiseXor, ty_uint_, mix, w[j]); | ||
| 484 | } | ||
| 485 | const std::uint32_t acc0 = load(ty_uint_, acc_var); | ||
| 486 | const std::uint32_t acc1 = binop(spv_op::kOpIMul, ty_uint_, acc0, c_prime); | ||
| 487 | const std::uint32_t acc2 = binop(spv_op::kOpBitwiseXor, ty_uint_, acc1, mix); | ||
| 488 | emit(funcs_, spv_op::kOpStore, {acc_var, acc2}); | ||
| 489 | emit(funcs_, spv_op::kOpStore, {prev_var, mix}); | ||
| 490 | } else if (kind == ShaderKind::SingleLookup) { | ||
| 491 | const std::uint32_t res = id(); | ||
| 492 | emit_vec(funcs_, spv_op::kOpFunctionCall, | ||
| 493 | {ty_uint_, res, fn_lookup, w[0], w[1], w[2], w[3], w[4], w[5], w[6], w[7]}); | ||
| 494 | const std::uint32_t acc0 = load(ty_uint_, acc_var); | ||
| 495 | const std::uint32_t acc1 = binop(spv_op::kOpIMul, ty_uint_, acc0, c_prime); | ||
| 496 | const std::uint32_t acc2 = binop(spv_op::kOpBitwiseXor, ty_uint_, acc1, res); | ||
| 497 | emit(funcs_, spv_op::kOpStore, {acc_var, acc2}); | ||
| 498 | emit(funcs_, spv_op::kOpStore, {prev_var, res}); | ||
| 499 | } else { | ||
| 500 | const std::uint32_t res_t = id(); | ||
| 501 | emit_vec(funcs_, spv_op::kOpFunctionCall, | ||
| 502 | {ty_uint_, res_t, fn_lookup, w[0], w[1], w[2], w[3], w[4], w[5], w[6], w[7]}); | ||
| 503 | const std::uint32_t masked_s = binop(spv_op::kOpBitwiseAnd, ty_uint_, idx1, pc[9]); | ||
| 504 | const std::uint32_t skidx = binop(spv_op::kOpIAdd, ty_uint_, pc[8], masked_s); | ||
| 505 | const std::uint32_t soff = binop(spv_op::kOpShiftLeftLogical, ty_uint_, skidx, c3); | ||
| 506 | std::array<std::uint32_t, 4> s{}; | ||
| 507 | for (std::uint32_t j = 0; j < 4u; ++j) { | ||
| 508 | const std::uint32_t so = binop(spv_op::kOpIAdd, ty_uint_, soff, uconst(j)); | ||
| 509 | const std::uint32_t sp = access2(ptr_sb_u_, keys_var, c0, so); | ||
| 510 | s[j] = load(ty_uint_, sp); | ||
| 511 | } | ||
| 512 | const std::uint32_t res_s = id(); | ||
| 513 | emit_vec(funcs_, spv_op::kOpFunctionCall, | ||
| 514 | {ty_uint_, res_s, fn_lookup, s[0], s[1], s[2], s[3], c_ff, c_ff, c_ff, c_ff}); | ||
| 515 | const std::uint32_t acc0 = load(ty_uint_, acc_var); | ||
| 516 | const std::uint32_t acc1 = binop(spv_op::kOpIMul, ty_uint_, acc0, c_prime); | ||
| 517 | const std::uint32_t acc2 = binop(spv_op::kOpBitwiseXor, ty_uint_, acc1, res_t); | ||
| 518 | const std::uint32_t acc3 = binop(spv_op::kOpIMul, ty_uint_, acc2, c_prime); | ||
| 519 | const std::uint32_t acc4 = binop(spv_op::kOpBitwiseXor, ty_uint_, acc3, res_s); | ||
| 520 | emit(funcs_, spv_op::kOpStore, {acc_var, acc4}); | ||
| 521 | emit(funcs_, spv_op::kOpStore, {prev_var, res_t}); | ||
| 522 | } | ||
| 523 | emit(funcs_, spv_op::kOpBranch, {lbl_cont}); | ||
| 524 | |||
| 525 | emit(funcs_, spv_op::kOpLabel, {lbl_cont}); | ||
| 526 | const std::uint32_t i_cur = load(ty_uint_, i_var); | ||
| 527 | const std::uint32_t i_nxt = binop(spv_op::kOpIAdd, ty_uint_, i_cur, c1); | ||
| 528 | emit(funcs_, spv_op::kOpStore, {i_var, i_nxt}); | ||
| 529 | emit(funcs_, spv_op::kOpBranch, {lbl_head}); | ||
| 530 | |||
| 531 | emit(funcs_, spv_op::kOpLabel, {lbl_end}); | ||
| 532 | const std::uint32_t final_acc = load(ty_uint_, acc_var); | ||
| 533 | const std::uint32_t out_ptr = access2(ptr_sb_u_, out_var, c0, gid_x); | ||
| 534 | emit(funcs_, spv_op::kOpStore, {out_ptr, final_acc}); | ||
| 535 | emit(funcs_, spv_op::kOpReturn, {}); | ||
| 536 | emit(funcs_, spv_op::kOpFunctionEnd, {}); | ||
| 537 | } | ||
| 538 | |||
| 539 | std::uint32_t next_id_ = 1u; | ||
| 540 | std::uint32_t ty_bool_ = 0u; | ||
| 541 | std::uint32_t ty_uint_ = 0u; | ||
| 542 | std::uint32_t ptr_sb_u_ = 0u; | ||
| 543 | std::uint32_t ptr_fn_u_ = 0u; | ||
| 544 | std::map<std::uint32_t, std::uint32_t> uconsts_; | ||
| 545 | std::vector<std::uint32_t> preamble_; | ||
| 546 | std::vector<std::uint32_t> decs_; | ||
| 547 | std::vector<std::uint32_t> types_; | ||
| 548 | std::vector<std::uint32_t> consts_; | ||
| 549 | std::vector<std::uint32_t> gvars_; | ||
| 550 | std::vector<std::uint32_t> funcs_; | ||
| 551 | }; | ||
| 552 | |||
| 553 | std::vector<std::uint32_t> build_bench_spv(ShaderKind kind) { | ||
| 554 | SpirvBuilder b; | ||
| 555 | return b.build(kind); | ||
| 556 | } | ||
| 557 | |||
| 558 | bool check_spv_structure(const std::vector<std::uint32_t>& words, const char* label) { | ||
| 559 | if (words.size() < 5u || words[0] != spv_op::kMagic || words[1] != spv_op::kVersion13 || words[3] < 2u) { | ||
| 560 | std::fprintf(stderr, "self-test: invalid SPIR-V header for %s\n", label); | ||
| 561 | return false; | ||
| 562 | } | ||
| 563 | const std::uint32_t bound = words[3]; | ||
| 564 | std::size_t idx = 5u; | ||
| 565 | int open_fns = 0; | ||
| 566 | bool seen_entry = false; | ||
| 567 | while (idx < words.size()) { | ||
| 568 | const std::uint32_t head = words[idx]; | ||
| 569 | const std::uint32_t wc = head >> 16; | ||
| 570 | const std::uint32_t op = head & 0xFFFFu; | ||
| 571 | if (wc == 0u || idx + wc > words.size()) { | ||
| 572 | std::fprintf(stderr, "self-test: truncated instruction in %s at word %zu\n", label, idx); | ||
| 573 | return false; | ||
| 574 | } | ||
| 575 | if (op == spv_op::kOpEntryPoint) seen_entry = true; | ||
| 576 | if (op == spv_op::kOpFunction) { | ||
| 577 | if (wc < 3u || words[idx + 2u] >= bound) return false; | ||
| 578 | ++open_fns; | ||
| 579 | } else if (op == spv_op::kOpFunctionEnd) { | ||
| 580 | if (open_fns <= 0) return false; | ||
| 581 | --open_fns; | ||
| 582 | } | ||
| 583 | idx += wc; | ||
| 584 | } | ||
| 585 | if (!seen_entry || open_fns != 0) { | ||
| 586 | std::fprintf(stderr, "self-test: malformed function or entry point in %s\n", label); | ||
| 587 | return false; | ||
| 588 | } | ||
| 589 | return true; | ||
| 590 | } | ||
| 591 | |||
| 592 | bool write_spv_file(const std::string& path, const std::vector<std::uint32_t>& words) { | ||
| 593 | std::ofstream f(path, std::ios::binary); | ||
| 594 | if (!f) { | ||
| 595 | std::fprintf(stderr, "could not open %s for writing\n", path.c_str()); | ||
| 596 | return false; | ||
| 597 | } | ||
| 598 | f.write(reinterpret_cast<const char*>(words.data()), static_cast<std::streamsize>(words.size() * sizeof(std::uint32_t))); | ||
| 599 | if (f.fail()) { | ||
| 600 | std::fprintf(stderr, "failed writing %s\n", path.c_str()); | ||
| 601 | return false; | ||
| 602 | } | ||
| 603 | return true; | ||
| 604 | } | ||
| 605 | |||
| 606 | // ---- Workload generation & exact CPU oracle ---- | ||
| 607 | |||
| 608 | std::uint64_t splitmix64(std::uint64_t& state) { | ||
| 609 | state += 0x9E3779B97F4A7C15ULL; | ||
| 610 | std::uint64_t z = state; | ||
| 611 | z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ULL; | ||
| 612 | z = (z ^ (z >> 27)) * 0x94D049BB133111EBULL; | ||
| 613 | return z ^ (z >> 31); | ||
| 614 | } | ||
| 615 | |||
| 616 | std::uint32_t next_pow2_u32(std::uint32_t n) { | ||
| 617 | std::uint32_t p = 1u; | ||
| 618 | while (p < n) p <<= 1; | ||
| 619 | return p; | ||
| 620 | } | ||
| 621 | |||
| 622 | DescriptorKey make_bench_tsharp(std::uint32_t id) { | ||
| 623 | const std::uint64_t va = 0x0000010000000000ULL + static_cast<std::uint64_t>(id) * 256ULL; | ||
| 624 | const std::uint32_t fmt = 10u + (id % 24u); | ||
| 625 | const std::uint32_t w = 63u + ((id * 37u) % 2048u); | ||
| 626 | const std::uint32_t h = 63u + ((id * 53u) % 2048u); | ||
| 627 | return { | ||
| 628 | static_cast<std::uint32_t>(va >> 8), | ||
| 629 | (static_cast<std::uint32_t>(va >> 40) & 0xFFu) | 0x2000u | (fmt << 20), | ||
| 630 | (w & 0x3FFFu) | ((h & 0x3FFFu) << 14), | ||
| 631 | 0x000AA688u ^ (id & 0xFFu), | ||
| 632 | id ^ 0x9E3779B9u, | ||
| 633 | (id * 0x85EBCA6Bu) & 0x00FFFFFFu, | ||
| 634 | 0x00000010u | ((id % 7u) << 12), | ||
| 635 | 0x80000000u ^ (id << 16), | ||
| 636 | }; | ||
| 637 | } | ||
| 638 | |||
| 639 | DescriptorKey make_bench_ssharp(std::uint32_t id) { | ||
| 640 | const std::uint32_t raw[4] = { | ||
| 641 | 0x00000092u | ((id & 0x7u) << 20), | ||
| 642 | 0x00C00000u ^ (id << 8), | ||
| 643 | 0x14500000u | (id & 0xFFFu), | ||
| 644 | id ^ 0xA5A50001u, | ||
| 645 | }; | ||
| 646 | return make_ssharp_key(raw); | ||
| 647 | } | ||
| 648 | |||
| 649 | DescriptorKey make_bench_miss(std::uint32_t id) { | ||
| 650 | return { | ||
| 651 | 0xDEAD0000u ^ (id * 0x9E3779B9u), | ||
| 652 | 0xBEEF0000u ^ id, | ||
| 653 | 0xCAFE0001u + id, | ||
| 654 | 0xBAAD0002u ^ (id << 3), | ||
| 655 | 0x13572468u, | ||
| 656 | 0x24681357u, | ||
| 657 | 0xFACEB00Cu, | ||
| 658 | 0x40000000u | ((id & 0xFFFFFFu) << 1), | ||
| 659 | }; | ||
| 660 | } | ||
| 661 | |||
| 662 | // Mirrors the shader's exact probe loop against the exported storage buffer, returning {index, 1-based probe count}. | ||
| 663 | std::pair<std::uint32_t, std::uint32_t> probe_gpu_table(const std::vector<std::uint32_t>& table_buf, | ||
| 664 | const DescriptorKey& key) { | ||
| 665 | const std::uint32_t h = hash_descriptor_key(key); | ||
| 666 | const std::uint32_t mask = table_buf[0]; | ||
| 667 | for (std::uint32_t k = 0; k < kDescriptorProbes; ++k) { | ||
| 668 | const std::uint32_t base = ((h + k) & mask) * 10u; | ||
| 669 | bool match = true; | ||
| 670 | for (std::uint32_t j = 0; j < 8u; ++j) { | ||
| 671 | match = match && (table_buf[1u + base + j] == key[j]); | ||
| 672 | } | ||
| 673 | if (match) return {table_buf[base + 9u], k + 1u}; | ||
| 674 | } | ||
| 675 | return {0u, kDescriptorProbes}; | ||
| 676 | } | ||
| 677 | |||
| 678 | struct TableDataset { | ||
| 679 | std::uint32_t entry_count = 0; | ||
| 680 | std::uint32_t t_count = 0; | ||
| 681 | std::uint32_t s_count = 0; | ||
| 682 | std::uint32_t capacity = 0; | ||
| 683 | double load_pct = 0.0; | ||
| 684 | double mean_probes = 0.0; | ||
| 685 | double probe1_pct = 0.0; | ||
| 686 | std::uint32_t max_probes = 0; | ||
| 687 | std::uint32_t t_pow2 = 0; | ||
| 688 | std::uint32_t s_pow2 = 0; | ||
| 689 | std::uint32_t t_base = 0; | ||
| 690 | std::uint32_t s_base = 0; | ||
| 691 | std::uint32_t miss_base = 0; | ||
| 692 | std::vector<std::uint32_t> gpu_table; | ||
| 693 | std::vector<std::uint32_t> keys_buf; // [T# (t_pow2) | S# (s_pow2) | Miss (t_pow2)] x 8 u32 | ||
| 694 | std::vector<std::uint32_t> expected_baseline; // per key slot: XOR of 8 key words | ||
| 695 | std::vector<std::uint32_t> expected_lookup; // per key slot: expected bindless index | ||
| 696 | }; | ||
| 697 | |||
| 698 | bool build_dataset(std::uint32_t entry_count, TableDataset& out) { | ||
| 699 | out = TableDataset{}; | ||
| 700 | out.entry_count = entry_count; | ||
| 701 | out.s_count = std::min(entry_count / 2u, std::max<std::uint32_t>(16u, entry_count / 10u)); | ||
| 702 | out.t_count = entry_count - out.s_count; | ||
| 703 | |||
| 704 | DescriptorTable table(std::max<std::uint32_t>(32u, next_pow2_u32(entry_count * 2u))); | ||
| 705 | std::vector<DescriptorKey> t_keys(out.t_count); | ||
| 706 | std::vector<DescriptorKey> s_keys(out.s_count); | ||
| 707 | |||
| 708 | for (std::uint32_t i = 0; i < out.t_count; ++i) { | ||
| 709 | t_keys[i] = make_bench_tsharp(i + 1u); | ||
| 710 | table.insert(t_keys[i], 1u + i); | ||
| 711 | } | ||
| 712 | for (std::uint32_t i = 0; i < out.s_count; ++i) { | ||
| 713 | s_keys[i] = make_bench_ssharp(i + 1u); | ||
| 714 | table.insert(s_keys[i], 1u + out.t_count + i); | ||
| 715 | } | ||
| 716 | |||
| 717 | out.capacity = static_cast<std::uint32_t>(table.capacity()); | ||
| 718 | out.load_pct = 100.0 * static_cast<double>(entry_count) / static_cast<double>(out.capacity); | ||
| 719 | out.gpu_table = table.build_gpu_buffer(); | ||
| 720 | |||
| 721 | std::uint64_t probe_sum = 0; | ||
| 722 | std::uint32_t probe1 = 0; | ||
| 723 | std::uint32_t max_p = 0; | ||
| 724 | auto check_live = [&](const DescriptorKey& k, std::uint32_t expected_idx) -> bool { | ||
| 725 | const auto [got, probes] = probe_gpu_table(out.gpu_table, k); | ||
| 726 | if (got != expected_idx || table.lookup(k) != expected_idx) { | ||
| 727 | std::fprintf(stderr, "table oracle mismatch: got=%u cpu=%u expected=%u\n", got, table.lookup(k), | ||
| 728 | expected_idx); | ||
| 729 | return false; | ||
| 730 | } | ||
| 731 | probe_sum += probes; | ||
| 732 | if (probes == 1u) ++probe1; | ||
| 733 | if (probes > max_p) max_p = probes; | ||
| 734 | return true; | ||
| 735 | }; | ||
| 736 | for (std::uint32_t i = 0; i < out.t_count; ++i) { | ||
| 737 | if (!check_live(t_keys[i], 1u + i)) return false; | ||
| 738 | } | ||
| 739 | for (std::uint32_t i = 0; i < out.s_count; ++i) { | ||
| 740 | if (!check_live(s_keys[i], 1u + out.t_count + i)) return false; | ||
| 741 | } | ||
| 742 | out.mean_probes = static_cast<double>(probe_sum) / static_cast<double>(entry_count); | ||
| 743 | out.probe1_pct = 100.0 * static_cast<double>(probe1) / static_cast<double>(entry_count); | ||
| 744 | out.max_probes = max_p; | ||
| 745 | |||
| 746 | // Shuffle key rings deterministically so unit-stride walks visit scattered table slots across L2/DRAM. | ||
| 747 | std::uint64_t rng = 0x4C454F4E49444156ULL ^ static_cast<std::uint64_t>(entry_count); | ||
| 748 | std::vector<std::uint32_t> t_perm(out.t_count); | ||
| 749 | std::iota(t_perm.begin(), t_perm.end(), 0u); | ||
| 750 | for (std::uint32_t i = out.t_count; i > 1u; --i) { | ||
| 751 | const std::uint32_t j = static_cast<std::uint32_t>(splitmix64(rng) % i); | ||
| 752 | std::swap(t_perm[i - 1u], t_perm[j]); | ||
| 753 | } | ||
| 754 | // Pin t_perm[0] to a depth-1 entry so uniform_hot_tsharp measures identical probe depth across sizes. | ||
| 755 | for (std::uint32_t k = 0; k < out.t_count; ++k) { | ||
| 756 | if (probe_gpu_table(out.gpu_table, t_keys[t_perm[k]]).second == 1u) { | ||
| 757 | std::swap(t_perm[0], t_perm[k]); | ||
| 758 | break; | ||
| 759 | } | ||
| 760 | } | ||
| 761 | std::vector<std::uint32_t> s_perm(out.s_count); | ||
| 762 | std::iota(s_perm.begin(), s_perm.end(), 0u); | ||
| 763 | for (std::uint32_t i = out.s_count; i > 1u; --i) { | ||
| 764 | const std::uint32_t j = static_cast<std::uint32_t>(splitmix64(rng) % i); | ||
| 765 | std::swap(s_perm[i - 1u], s_perm[j]); | ||
| 766 | } | ||
| 767 | |||
| 768 | out.t_pow2 = next_pow2_u32(out.t_count); | ||
| 769 | out.s_pow2 = next_pow2_u32(out.s_count); | ||
| 770 | out.t_base = 0u; | ||
| 771 | out.s_base = out.t_pow2; | ||
| 772 | out.miss_base = out.t_pow2 + out.s_pow2; | ||
| 773 | |||
| 774 | const std::uint32_t total_slots = out.t_pow2 + out.s_pow2 + out.t_pow2; | ||
| 775 | out.keys_buf.resize(static_cast<std::size_t>(total_slots) * 8u); | ||
| 776 | out.expected_baseline.resize(total_slots); | ||
| 777 | out.expected_lookup.resize(total_slots); | ||
| 778 | |||
| 779 | const auto store_slot = [&](std::uint32_t slot, const DescriptorKey& key, std::uint32_t expected_idx) { | ||
| 780 | std::uint32_t xor_mix = 0u; | ||
| 781 | for (std::uint32_t j = 0; j < 8u; ++j) { | ||
| 782 | out.keys_buf[static_cast<std::size_t>(slot) * 8u + j] = key[j]; | ||
| 783 | xor_mix ^= key[j]; | ||
| 784 | } | ||
| 785 | out.expected_baseline[slot] = xor_mix; | ||
| 786 | out.expected_lookup[slot] = expected_idx; | ||
| 787 | }; | ||
| 788 | |||
| 789 | for (std::uint32_t i = 0; i < out.t_pow2; ++i) { | ||
| 790 | const std::uint32_t idx = t_perm[i % out.t_count]; | ||
| 791 | store_slot(out.t_base + i, t_keys[idx], 1u + idx); | ||
| 792 | } | ||
| 793 | for (std::uint32_t i = 0; i < out.s_pow2; ++i) { | ||
| 794 | const std::uint32_t idx = s_perm[i % out.s_count]; | ||
| 795 | store_slot(out.s_base + i, s_keys[idx], 1u + out.t_count + idx); | ||
| 796 | } | ||
| 797 | std::uint32_t miss_seq = 0u; | ||
| 798 | for (std::uint32_t i = 0; i < out.t_pow2; ++i) { | ||
| 799 | DescriptorKey mk{}; | ||
| 800 | for (;;) { | ||
| 801 | mk = make_bench_miss(miss_seq++); | ||
| 802 | if (!table.contains(mk) && probe_gpu_table(out.gpu_table, mk).first == 0u) break; | ||
| 803 | } | ||
| 804 | store_slot(out.miss_base + i, mk, 0u); | ||
| 805 | } | ||
| 806 | return true; | ||
| 807 | } | ||
| 808 | |||
| 809 | struct ScenarioSpec { | ||
| 810 | const char* name; | ||
| 811 | ShaderKind shader; | ||
| 812 | bool divergent; | ||
| 813 | std::uint32_t group_stride; | ||
| 814 | std::uint32_t lane_stride; | ||
| 815 | std::uint32_t iter_stride; | ||
| 816 | bool dep_chain; | ||
| 817 | enum class Bank { TSharp, SSharp, Miss } bank; | ||
| 818 | double lookups_per_iter; | ||
| 819 | }; | ||
| 820 | |||
| 821 | PushConstants make_push_constants(const ScenarioSpec& sc, const TableDataset& ds, std::uint32_t iters) { | ||
| 822 | PushConstants pc{}; | ||
| 823 | pc.iters = iters; | ||
| 824 | pc.group_stride = sc.group_stride; | ||
| 825 | pc.lane_stride = sc.lane_stride; | ||
| 826 | pc.iter_stride = sc.iter_stride; | ||
| 827 | pc.base_index = 0u; | ||
| 828 | pc.s_base = ds.s_base; | ||
| 829 | pc.s_mask = ds.s_pow2 - 1u; | ||
| 830 | if (sc.bank == ScenarioSpec::Bank::TSharp) { | ||
| 831 | pc.t_base = ds.t_base; | ||
| 832 | pc.key_mask = ds.t_pow2 - 1u; | ||
| 833 | } else if (sc.bank == ScenarioSpec::Bank::SSharp) { | ||
| 834 | pc.t_base = ds.s_base; | ||
| 835 | pc.key_mask = ds.s_pow2 - 1u; | ||
| 836 | } else { | ||
| 837 | pc.t_base = ds.miss_base; | ||
| 838 | pc.key_mask = ds.t_pow2 - 1u; | ||
| 839 | } | ||
| 840 | pc.dep_mask = sc.dep_chain ? pc.key_mask : 0u; | ||
| 841 | return pc; | ||
| 842 | } | ||
| 843 | |||
| 844 | std::uint32_t eval_invocation_oracle(const ScenarioSpec& sc, const TableDataset& ds, const PushConstants& pc, | ||
| 845 | std::uint32_t tid) { | ||
| 846 | const std::uint32_t wg = tid >> 6; | ||
| 847 | const std::uint32_t lid = tid & 63u; | ||
| 848 | const std::uint32_t start = pc.base_index + wg * pc.group_stride + lid * pc.lane_stride; | ||
| 849 | std::uint32_t acc = kFnvOffsetBasis; | ||
| 850 | std::uint32_t prev = 0u; | ||
| 851 | |||
| 852 | for (std::uint32_t i = 0; i < pc.iters; ++i) { | ||
| 853 | const std::uint32_t dep = prev & pc.dep_mask; | ||
| 854 | const std::uint32_t idx1 = start + i * pc.iter_stride + dep; | ||
| 855 | const std::uint32_t kidx = pc.t_base + (idx1 & pc.key_mask); | ||
| 856 | if (sc.shader == ShaderKind::Baseline) { | ||
| 857 | const std::uint32_t mix = ds.expected_baseline[kidx]; | ||
| 858 | acc = (acc * kFnvPrime) ^ mix; | ||
| 859 | prev = mix; | ||
| 860 | } else if (sc.shader == ShaderKind::SingleLookup) { | ||
| 861 | const std::uint32_t res = ds.expected_lookup[kidx]; | ||
| 862 | acc = (acc * kFnvPrime) ^ res; | ||
| 863 | prev = res; | ||
| 864 | } else { | ||
| 865 | const std::uint32_t res_t = ds.expected_lookup[kidx]; | ||
| 866 | const std::uint32_t skidx = pc.s_base + (idx1 & pc.s_mask); | ||
| 867 | const std::uint32_t res_s = ds.expected_lookup[skidx]; | ||
| 868 | acc = (acc * kFnvPrime) ^ res_t; | ||
| 869 | acc = (acc * kFnvPrime) ^ res_s; | ||
| 870 | prev = res_t; | ||
| 871 | } | ||
| 872 | } | ||
| 873 | return acc; | ||
| 874 | } | ||
| 875 | |||
| 876 | bool verify_gpu_output(const ScenarioSpec& sc, const TableDataset& ds, const PushConstants& pc, | ||
| 877 | const std::uint32_t* gpu_out, std::uint32_t total_invocations) { | ||
| 878 | const std::uint64_t total_work = static_cast<std::uint64_t>(total_invocations) * pc.iters; | ||
| 879 | std::uint32_t mismatches = 0u; | ||
| 880 | auto check_tid = [&](std::uint32_t tid) { | ||
| 881 | const std::uint32_t want = eval_invocation_oracle(sc, ds, pc, tid); | ||
| 882 | const std::uint32_t got = gpu_out[tid]; | ||
| 883 | if (got != want) { | ||
| 884 | if (mismatches < 5u) { | ||
| 885 | std::fprintf(stderr, "mismatch %s tid=%u: got=0x%08x want=0x%08x\n", sc.name, tid, got, want); | ||
| 886 | } | ||
| 887 | ++mismatches; | ||
| 888 | } | ||
| 889 | }; | ||
| 890 | |||
| 891 | if (total_work <= 4000000ULL) { | ||
| 892 | for (std::uint32_t tid = 0; tid < total_invocations; ++tid) check_tid(tid); | ||
| 893 | } else { | ||
| 894 | const std::uint32_t edge = std::min<std::uint32_t>(512u, total_invocations); | ||
| 895 | for (std::uint32_t tid = 0; tid < edge; ++tid) check_tid(tid); | ||
| 896 | const std::uint32_t tail = total_invocations - edge; | ||
| 897 | for (std::uint32_t tid = std::max(edge, tail); tid < total_invocations; ++tid) check_tid(tid); | ||
| 898 | const std::uint32_t step = std::max<std::uint32_t>(65u, total_invocations / 4096u); | ||
| 899 | for (std::uint32_t tid = edge; tid < tail; tid += step) check_tid(tid); | ||
| 900 | } | ||
| 901 | return mismatches == 0u; | ||
| 902 | } | ||
| 903 | |||
| 904 | // ---- Vulkan helpers & CLI options ---- | ||
| 905 | |||
| 906 | enum class MemoryFilter { All, Device, Host, Rebar }; | ||
| 907 | |||
| 908 | struct Options { | ||
| 909 | std::vector<std::uint32_t> sizes = {1000u, 10000u, 100000u}; | ||
| 910 | std::uint32_t groups = 4096u; | ||
| 911 | std::uint32_t iters = 64u; | ||
| 912 | std::uint32_t reps = 25u; | ||
| 913 | std::uint32_t warmup = 5u; | ||
| 914 | double max_dispatch_ms = 120.0; | ||
| 915 | int device_index = -1; | ||
| 916 | MemoryFilter mem_filter = MemoryFilter::All; | ||
| 917 | bool list_devices = false; | ||
| 918 | bool csv = false; | ||
| 919 | bool self_test = false; | ||
| 920 | std::string dump_spv_prefix; | ||
| 921 | }; | ||
| 922 | |||
| 923 | void print_usage() { | ||
| 924 | std::printf( | ||
| 925 | "usage: vk_descriptor_bench [options]\n" | ||
| 926 | " --sizes LIST comma-separated table entry counts (default: 1000,10000,100000)\n" | ||
| 927 | " --groups N workgroups of 64 invocations per dispatch (default: 4096)\n" | ||
| 928 | " --iters N lookups per invocation per dispatch (default: 64)\n" | ||
| 929 | " --reps N timed repetitions per scenario (default: 25)\n" | ||
| 930 | " --warmup N warmup dispatches before timing (default: 5)\n" | ||
| 931 | " --max-dispatch-ms F auto-scale slow dispatches to avoid OS TDR, 0=off (default: 120)\n" | ||
| 932 | " --memory MODE all|device|host|rebar (default: all)\n" | ||
| 933 | " --device N select physical device index from --list-devices\n" | ||
| 934 | " --list-devices list Vulkan physical devices and exit\n" | ||
| 935 | " --quick fast check profile (sizes 1000,10000; groups 16; iters 4; reps 3)\n" | ||
| 936 | " --csv emit machine-readable CSV lines\n" | ||
| 937 | " --dump-spv PREFIX write the 3 SPIR-V compute modules to PREFIX_*.spv and exit\n" | ||
| 938 | " --self-test CPU-only check of SPIR-V encoding and DescriptorTable oracle\n" | ||
| 939 | " --help print this message\n"); | ||
| 940 | } | ||
| 941 | |||
| 942 | bool parse_u32(const char* s, std::uint32_t& out) { | ||
| 943 | if (!s || *s < '0' || *s > '9') return false; | ||
| 944 | char* end = nullptr; | ||
| 945 | const unsigned long long v = std::strtoull(s, &end, 10); | ||
| 946 | if (*end != '\0' || v > std::numeric_limits<std::uint32_t>::max()) return false; | ||
| 947 | out = static_cast<std::uint32_t>(v); | ||
| 948 | return true; | ||
| 949 | } | ||
| 950 | |||
| 951 | bool parse_args(int argc, char** argv, Options& opt, bool& show_help) { | ||
| 952 | show_help = false; | ||
| 953 | for (int i = 1; i < argc; ++i) { | ||
| 954 | const std::string_view arg(argv[i]); | ||
| 955 | auto need_val = [&](const char*& val) -> bool { | ||
| 956 | if (i + 1 >= argc) { | ||
| 957 | std::fprintf(stderr, "missing argument for %.*s\n", int(arg.size()), arg.data()); | ||
| 958 | return false; | ||
| 959 | } | ||
| 960 | val = argv[++i]; | ||
| 961 | return true; | ||
| 962 | }; | ||
| 963 | if (arg == "--help" || arg == "-h") { | ||
| 964 | show_help = true; | ||
| 965 | return true; | ||
| 966 | } else if (arg == "--self-test") { | ||
| 967 | opt.self_test = true; | ||
| 968 | } else if (arg == "--list-devices") { | ||
| 969 | opt.list_devices = true; | ||
| 970 | } else if (arg == "--csv") { | ||
| 971 | opt.csv = true; | ||
| 972 | } else if (arg == "--quick") { | ||
| 973 | opt.sizes = {1000u, 10000u}; | ||
| 974 | opt.groups = 16u; | ||
| 975 | opt.iters = 4u; | ||
| 976 | opt.warmup = 1u; | ||
| 977 | opt.reps = 3u; | ||
| 978 | } else if (arg == "--groups") { | ||
| 979 | const char* v = nullptr; | ||
| 980 | if (!need_val(v) || !parse_u32(v, opt.groups) || opt.groups == 0u) return false; | ||
| 981 | } else if (arg == "--iters") { | ||
| 982 | const char* v = nullptr; | ||
| 983 | if (!need_val(v) || !parse_u32(v, opt.iters) || opt.iters == 0u) return false; | ||
| 984 | } else if (arg == "--reps") { | ||
| 985 | const char* v = nullptr; | ||
| 986 | if (!need_val(v) || !parse_u32(v, opt.reps) || opt.reps == 0u) return false; | ||
| 987 | } else if (arg == "--warmup") { | ||
| 988 | const char* v = nullptr; | ||
| 989 | if (!need_val(v) || !parse_u32(v, opt.warmup)) return false; | ||
| 990 | } else if (arg == "--max-dispatch-ms") { | ||
| 991 | const char* v = nullptr; | ||
| 992 | if (!need_val(v)) return false; | ||
| 993 | char* end = nullptr; | ||
| 994 | const double val = std::strtod(v, &end); | ||
| 995 | if (*v == '\0' || *end != '\0' || !std::isfinite(val) || val < 0.0) return false; | ||
| 996 | opt.max_dispatch_ms = val; | ||
| 997 | } else if (arg == "--device") { | ||
| 998 | const char* v = nullptr; | ||
| 999 | std::uint32_t idx = 0u; | ||
| 1000 | if (!need_val(v) || !parse_u32(v, idx)) return false; | ||
| 1001 | opt.device_index = static_cast<int>(idx); | ||
| 1002 | } else if (arg == "--dump-spv") { | ||
| 1003 | const char* v = nullptr; | ||
| 1004 | if (!need_val(v)) return false; | ||
| 1005 | opt.dump_spv_prefix = v; | ||
| 1006 | } else if (arg == "--memory") { | ||
| 1007 | const char* v = nullptr; | ||
| 1008 | if (!need_val(v)) return false; | ||
| 1009 | const std::string_view mv(v); | ||
| 1010 | if (mv == "all") opt.mem_filter = MemoryFilter::All; | ||
| 1011 | else if (mv == "device") opt.mem_filter = MemoryFilter::Device; | ||
| 1012 | else if (mv == "host") opt.mem_filter = MemoryFilter::Host; | ||
| 1013 | else if (mv == "rebar") opt.mem_filter = MemoryFilter::Rebar; | ||
| 1014 | else { | ||
| 1015 | std::fprintf(stderr, "invalid --memory value: %s\n", v); | ||
| 1016 | return false; | ||
| 1017 | } | ||
| 1018 | } else if (arg == "--sizes") { | ||
| 1019 | const char* v = nullptr; | ||
| 1020 | if (!need_val(v)) return false; | ||
| 1021 | opt.sizes.clear(); | ||
| 1022 | std::string_view sv(v); | ||
| 1023 | while (!sv.empty()) { | ||
| 1024 | const std::size_t comma = sv.find(','); | ||
| 1025 | const std::string part(sv.substr(0, comma)); | ||
| 1026 | std::uint32_t sz = 0u; | ||
| 1027 | if (!parse_u32(part.c_str(), sz) || sz < 16u) { | ||
| 1028 | std::fprintf(stderr, "invalid size in --sizes (must be >= 16): %s\n", part.c_str()); | ||
| 1029 | return false; | ||
| 1030 | } | ||
| 1031 | opt.sizes.push_back(sz); | ||
| 1032 | if (comma == std::string_view::npos) break; | ||
| 1033 | sv.remove_prefix(comma + 1u); | ||
| 1034 | } | ||
| 1035 | if (opt.sizes.empty()) return false; | ||
| 1036 | } else { | ||
| 1037 | std::fprintf(stderr, "unknown option: %.*s\n", int(arg.size()), arg.data()); | ||
| 1038 | return false; | ||
| 1039 | } | ||
| 1040 | } | ||
| 1041 | return true; | ||
| 1042 | } | ||
| 1043 | |||
| 1044 | const char* device_type_name(VkPhysicalDeviceType t) { | ||
| 1045 | switch (t) { | ||
| 1046 | case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU: return "discrete"; | ||
| 1047 | case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU: return "integrated"; | ||
| 1048 | case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU: return "virtual"; | ||
| 1049 | case VK_PHYSICAL_DEVICE_TYPE_CPU: return "cpu"; | ||
| 1050 | default: return "other"; | ||
| 1051 | } | ||
| 1052 | } | ||
| 1053 | |||
| 1054 | std::string format_mem_flags(VkMemoryPropertyFlags flags) { | ||
| 1055 | std::string s; | ||
| 1056 | auto add = [&](VkMemoryPropertyFlags bit, const char* name) { | ||
| 1057 | if ((flags & bit) == bit) { | ||
| 1058 | if (!s.empty()) s += '|'; | ||
| 1059 | s += name; | ||
| 1060 | } | ||
| 1061 | }; | ||
| 1062 | add(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, "DEVICE_LOCAL"); | ||
| 1063 | add(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, "HOST_VISIBLE"); | ||
| 1064 | add(VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, "HOST_COHERENT"); | ||
| 1065 | add(VK_MEMORY_PROPERTY_HOST_CACHED_BIT, "HOST_CACHED"); | ||
| 1066 | return s.empty() ? "NONE" : s; | ||
| 1067 | } | ||
| 1068 | |||
| 1069 | std::uint32_t pick_memory_type_exact_or_fallback(const VkPhysicalDeviceMemoryProperties& mp, std::uint32_t type_bits, | ||
| 1070 | VkMemoryPropertyFlags req, VkMemoryPropertyFlags avoid) { | ||
| 1071 | for (std::uint32_t i = 0; i < mp.memoryTypeCount; ++i) { | ||
| 1072 | if (!(type_bits & (1u << i))) continue; | ||
| 1073 | const VkMemoryPropertyFlags f = mp.memoryTypes[i].propertyFlags; | ||
| 1074 | if ((f & req) == req && (f & avoid) == 0u) return i; | ||
| 1075 | } | ||
| 1076 | for (std::uint32_t i = 0; i < mp.memoryTypeCount; ++i) { | ||
| 1077 | if (!(type_bits & (1u << i))) continue; | ||
| 1078 | if ((mp.memoryTypes[i].propertyFlags & req) == req) return i; | ||
| 1079 | } | ||
| 1080 | return 0xFFFFFFFFu; | ||
| 1081 | } | ||
| 1082 | |||
| 1083 | struct GpuBuffer { | ||
| 1084 | VkBuffer buffer = VK_NULL_HANDLE; | ||
| 1085 | VkDeviceMemory memory = VK_NULL_HANDLE; | ||
| 1086 | VkDeviceSize size = 0; | ||
| 1087 | std::uint32_t memory_type_index = 0xFFFFFFFFu; | ||
| 1088 | void* mapped = nullptr; | ||
| 1089 | }; | ||
| 1090 | |||
| 1091 | struct MemoryPlacement { | ||
| 1092 | const char* label; | ||
| 1093 | std::uint32_t memory_type_index; | ||
| 1094 | std::uint32_t heap_index; | ||
| 1095 | VkDeviceSize heap_size; | ||
| 1096 | VkMemoryPropertyFlags flags; | ||
| 1097 | bool use_staging; | ||
| 1098 | }; | ||
| 1099 | |||
| 1100 | int run_self_test() { | ||
| 1101 | const struct { | ||
| 1102 | const char* name; | ||
| 1103 | ShaderKind kind; | ||
| 1104 | } modules[3] = { | ||
| 1105 | {"baseline", ShaderKind::Baseline}, | ||
| 1106 | {"single", ShaderKind::SingleLookup}, | ||
| 1107 | {"pair_ts", ShaderKind::PairTSLookup}, | ||
| 1108 | }; | ||
| 1109 | for (const auto& m : modules) { | ||
| 1110 | const auto spv = build_bench_spv(m.kind); | ||
| 1111 | if (!check_spv_structure(spv, m.name)) return 1; | ||
| 1112 | } | ||
| 1113 | |||
| 1114 | TableDataset ds; | ||
| 1115 | if (!build_dataset(1000u, ds)) return 1; | ||
| 1116 | if (ds.entry_count != 1000u || ds.capacity < 2048u || ds.max_probes > kDescriptorProbes) { | ||
| 1117 | std::fprintf(stderr, "self-test: unexpected 1k table metrics\n"); | ||
| 1118 | return 1; | ||
| 1119 | } | ||
| 1120 | const ScenarioSpec single_sc{"self_test", ShaderKind::SingleLookup, false, 1u, 0u, 1u, false, | ||
| 1121 | ScenarioSpec::Bank::TSharp, 1.0}; | ||
| 1122 | const PushConstants pc = make_push_constants(single_sc, ds, 8u); | ||
| 1123 | if (eval_invocation_oracle(single_sc, ds, pc, 0u) == kFnvOffsetBasis) { | ||
| 1124 | std::fprintf(stderr, "self-test: degenerate checksum on lane 0\n"); | ||
| 1125 | return 1; | ||
| 1126 | } | ||
| 1127 | std::printf("vk_descriptor_bench self-test passed (1k table cap=%u, load=%.1f%%, mean_probes=%.3f, max=%u)\n", | ||
| 1128 | ds.capacity, ds.load_pct, ds.mean_probes, ds.max_probes); | ||
| 1129 | return 0; | ||
| 1130 | } | ||
| 1131 | |||
| 1132 | int run_dump_spv(const std::string& prefix) { | ||
| 1133 | const struct { | ||
| 1134 | const char* suffix; | ||
| 1135 | ShaderKind kind; | ||
| 1136 | } items[3] = { | ||
| 1137 | {"_baseline.spv", ShaderKind::Baseline}, | ||
| 1138 | {"_single.spv", ShaderKind::SingleLookup}, | ||
| 1139 | {"_pair_ts.spv", ShaderKind::PairTSLookup}, | ||
| 1140 | }; | ||
| 1141 | for (const auto& item : items) { | ||
| 1142 | const std::string path = prefix + item.suffix; | ||
| 1143 | const auto spv = build_bench_spv(item.kind); | ||
| 1144 | if (!check_spv_structure(spv, path.c_str()) || !write_spv_file(path, spv)) return 1; | ||
| 1145 | std::printf("wrote %s (%zu bytes)\n", path.c_str(), spv.size() * sizeof(std::uint32_t)); | ||
| 1146 | } | ||
| 1147 | return 0; | ||
| 1148 | } | ||
| 1149 | |||
| 1150 | } // namespace | ||
| 1151 | |||
| 1152 | int main(int argc, char** argv) { | ||
| 1153 | Options opt; | ||
| 1154 | bool show_help = false; | ||
| 1155 | if (!parse_args(argc, argv, opt, show_help)) { | ||
| 1156 | print_usage(); | ||
| 1157 | return 2; | ||
| 1158 | } | ||
| 1159 | if (show_help) { | ||
| 1160 | print_usage(); | ||
| 1161 | return 0; | ||
| 1162 | } | ||
| 1163 | if (opt.self_test) return run_self_test(); | ||
| 1164 | if (!opt.dump_spv_prefix.empty()) return run_dump_spv(opt.dump_spv_prefix); | ||
| 1165 | |||
| 1166 | const PFN_vkGetInstanceProcAddr gipa = load_vulkan(); | ||
| 1167 | if (!gipa) return 1; | ||
| 1168 | |||
| 1169 | #define LI(n) const auto n = reinterpret_cast<PFN_##n>(gipa(inst, #n)) | ||
| 1170 | VkInstance inst = VK_NULL_HANDLE; | ||
| 1171 | LI(vkCreateInstance); | ||
| 1172 | LI(vkEnumerateInstanceLayerProperties); | ||
| 1173 | if (!vkCreateInstance) return 1; | ||
| 1174 | |||
| 1175 | const char* val_env = std::getenv("LEONIDA_VK_VALIDATION"); | ||
| 1176 | const bool validation = val_env && std::strcmp(val_env, "0") != 0 && *val_env != '\0'; | ||
| 1177 | const char* layer = "VK_LAYER_KHRONOS_validation"; | ||
| 1178 | const char* debug_utils = VK_EXT_DEBUG_UTILS_EXTENSION_NAME; | ||
| 1179 | |||
| 1180 | VkDebugUtilsMessengerCreateInfoEXT messenger_info{VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT}; | ||
| 1181 | messenger_info.messageSeverity = | ||
| 1182 | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT; | ||
| 1183 | messenger_info.messageType = | ||
| 1184 | VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT; | ||
| 1185 | messenger_info.pfnUserCallback = on_debug_message; | ||
| 1186 | |||
| 1187 | VkApplicationInfo app{VK_STRUCTURE_TYPE_APPLICATION_INFO}; | ||
| 1188 | app.pApplicationName = "vk_descriptor_bench"; | ||
| 1189 | app.apiVersion = VK_API_VERSION_1_2; | ||
| 1190 | |||
| 1191 | VkInstanceCreateInfo ici{VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO}; | ||
| 1192 | ici.pApplicationInfo = &app; | ||
| 1193 | if (validation) { | ||
| 1194 | std::uint32_t layer_count = 0; | ||
| 1195 | vkEnumerateInstanceLayerProperties(&layer_count, nullptr); | ||
| 1196 | std::vector<VkLayerProperties> layers(layer_count); | ||
| 1197 | vkEnumerateInstanceLayerProperties(&layer_count, layers.data()); | ||
| 1198 | bool found = false; | ||
| 1199 | for (const auto& lp : layers) { | ||
| 1200 | if (std::strcmp(lp.layerName, layer) == 0) found = true; | ||
| 1201 | } | ||
| 1202 | if (!found) { | ||
| 1203 | std::fprintf(stderr, "LEONIDA_VK_VALIDATION is set but %s is not installed\n", layer); | ||
| 1204 | return 1; | ||
| 1205 | } | ||
| 1206 | ici.pNext = &messenger_info; | ||
| 1207 | ici.enabledLayerCount = 1; | ||
| 1208 | ici.ppEnabledLayerNames = &layer; | ||
| 1209 | ici.enabledExtensionCount = 1; | ||
| 1210 | ici.ppEnabledExtensionNames = &debug_utils; | ||
| 1211 | } | ||
| 1212 | CHECK_VK(vkCreateInstance(&ici, nullptr, &inst)); | ||
| 1213 | |||
| 1214 | LI(vkDestroyInstance); | ||
| 1215 | LI(vkEnumeratePhysicalDevices); | ||
| 1216 | LI(vkGetPhysicalDeviceProperties); | ||
| 1217 | LI(vkGetPhysicalDeviceProperties2); | ||
| 1218 | LI(vkGetPhysicalDeviceQueueFamilyProperties); | ||
| 1219 | LI(vkGetPhysicalDeviceMemoryProperties); | ||
| 1220 | LI(vkCreateDevice); | ||
| 1221 | LI(vkGetDeviceProcAddr); | ||
| 1222 | |||
| 1223 | VkDebugUtilsMessengerEXT messenger = VK_NULL_HANDLE; | ||
| 1224 | PFN_vkDestroyDebugUtilsMessengerEXT vkDestroyDebugUtilsMessengerEXT = nullptr; | ||
| 1225 | if (validation) { | ||
| 1226 | LI(vkCreateDebugUtilsMessengerEXT); | ||
| 1227 | vkDestroyDebugUtilsMessengerEXT = | ||
| 1228 | reinterpret_cast<PFN_vkDestroyDebugUtilsMessengerEXT>(gipa(inst, "vkDestroyDebugUtilsMessengerEXT")); | ||
| 1229 | CHECK_VK(vkCreateDebugUtilsMessengerEXT(inst, &messenger_info, nullptr, &messenger)); | ||
| 1230 | } | ||
| 1231 | |||
| 1232 | auto destroy_instance = [&]() { | ||
| 1233 | if (validation && messenger && vkDestroyDebugUtilsMessengerEXT) { | ||
| 1234 | vkDestroyDebugUtilsMessengerEXT(inst, messenger, nullptr); | ||
| 1235 | } | ||
| 1236 | if (inst && vkDestroyInstance) vkDestroyInstance(inst, nullptr); | ||
| 1237 | }; | ||
| 1238 | |||
| 1239 | std::uint32_t pd_count = 0; | ||
| 1240 | vkEnumeratePhysicalDevices(inst, &pd_count, nullptr); | ||
| 1241 | std::vector<VkPhysicalDevice> pds(pd_count); | ||
| 1242 | if (pd_count > 0) vkEnumeratePhysicalDevices(inst, &pd_count, pds.data()); | ||
| 1243 | |||
| 1244 | if (opt.list_devices) { | ||
| 1245 | for (std::uint32_t i = 0; i < pd_count; ++i) { | ||
| 1246 | VkPhysicalDeviceProperties p{}; | ||
| 1247 | vkGetPhysicalDeviceProperties(pds[i], &p); | ||
| 1248 | std::printf("[%u] %s (%s, vendor=0x%04x, device=0x%04x, api=%u.%u.%u)\n", i, p.deviceName, | ||
| 1249 | device_type_name(p.deviceType), p.vendorID, p.deviceID, VK_API_VERSION_MAJOR(p.apiVersion), | ||
| 1250 | VK_API_VERSION_MINOR(p.apiVersion), VK_API_VERSION_PATCH(p.apiVersion)); | ||
| 1251 | } | ||
| 1252 | destroy_instance(); | ||
| 1253 | return 0; | ||
| 1254 | } | ||
| 1255 | |||
| 1256 | auto find_compute_queue = [&](VkPhysicalDevice cand, std::uint32_t& out_qi, | ||
| 1257 | std::uint32_t& out_ts_bits) -> bool { | ||
| 1258 | std::uint32_t qn = 0; | ||
| 1259 | vkGetPhysicalDeviceQueueFamilyProperties(cand, &qn, nullptr); | ||
| 1260 | std::vector<VkQueueFamilyProperties> qf(qn); | ||
| 1261 | vkGetPhysicalDeviceQueueFamilyProperties(cand, &qn, qf.data()); | ||
| 1262 | for (std::uint32_t i = 0; i < qn; ++i) { | ||
| 1263 | if (qf[i].queueFlags & VK_QUEUE_COMPUTE_BIT) { | ||
| 1264 | out_qi = i; | ||
| 1265 | out_ts_bits = qf[i].timestampValidBits; | ||
| 1266 | return true; | ||
| 1267 | } | ||
| 1268 | } | ||
| 1269 | return false; | ||
| 1270 | }; | ||
| 1271 | |||
| 1272 | VkPhysicalDevice pd = VK_NULL_HANDLE; | ||
| 1273 | VkPhysicalDeviceProperties pd_props{}; | ||
| 1274 | std::uint32_t qi = 0; | ||
| 1275 | std::uint32_t ts_valid_bits = 0; | ||
| 1276 | if (opt.device_index >= 0) { | ||
| 1277 | if (static_cast<std::uint32_t>(opt.device_index) >= pd_count) { | ||
| 1278 | std::fprintf(stderr, "invalid --device index %d (found %u devices)\n", opt.device_index, pd_count); | ||
| 1279 | destroy_instance(); | ||
| 1280 | return 2; | ||
| 1281 | } | ||
| 1282 | VkPhysicalDevice cand = pds[static_cast<std::size_t>(opt.device_index)]; | ||
| 1283 | VkPhysicalDeviceProperties p{}; | ||
| 1284 | vkGetPhysicalDeviceProperties(cand, &p); | ||
| 1285 | if (p.apiVersion < VK_API_VERSION_1_1 || !find_compute_queue(cand, qi, ts_valid_bits)) { | ||
| 1286 | std::printf("unsupported: selected device lacks Vulkan 1.1 or compute queue\n"); | ||
| 1287 | destroy_instance(); | ||
| 1288 | return kUnsupportedExit; | ||
| 1289 | } | ||
| 1290 | pd = cand; | ||
| 1291 | pd_props = p; | ||
| 1292 | } else { | ||
| 1293 | auto score = [](VkPhysicalDeviceType t) -> int { | ||
| 1294 | if (t == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU) return 4; | ||
| 1295 | if (t == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU) return 3; | ||
| 1296 | if (t == VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU) return 2; | ||
| 1297 | if (t == VK_PHYSICAL_DEVICE_TYPE_CPU) return 1; | ||
| 1298 | return 0; | ||
| 1299 | }; | ||
| 1300 | int best_score = -1; | ||
| 1301 | for (VkPhysicalDevice cand : pds) { | ||
| 1302 | VkPhysicalDeviceProperties p{}; | ||
| 1303 | vkGetPhysicalDeviceProperties(cand, &p); | ||
| 1304 | std::uint32_t c_qi = 0, c_ts = 0; | ||
| 1305 | if (p.apiVersion < VK_API_VERSION_1_1 || !find_compute_queue(cand, c_qi, c_ts)) continue; | ||
| 1306 | const int s = score(p.deviceType); | ||
| 1307 | if (s > best_score) { | ||
| 1308 | best_score = s; | ||
| 1309 | pd = cand; | ||
| 1310 | pd_props = p; | ||
| 1311 | qi = c_qi; | ||
| 1312 | ts_valid_bits = c_ts; | ||
| 1313 | } | ||
| 1314 | } | ||
| 1315 | } | ||
| 1316 | |||
| 1317 | if (!pd) { | ||
| 1318 | std::printf("unsupported: no Vulkan 1.1+ compute physical device\n"); | ||
| 1319 | destroy_instance(); | ||
| 1320 | return kUnsupportedExit; | ||
| 1321 | } | ||
| 1322 | |||
| 1323 | if (opt.groups > pd_props.limits.maxComputeWorkGroupCount[0]) { | ||
| 1324 | std::fprintf(stderr, "--groups %u exceeds maxComputeWorkGroupCount[0] (%u)\n", opt.groups, | ||
| 1325 | pd_props.limits.maxComputeWorkGroupCount[0]); | ||
| 1326 | destroy_instance(); | ||
| 1327 | return 2; | ||
| 1328 | } | ||
| 1329 | |||
| 1330 | std::uint32_t subgroup_size = 0; | ||
| 1331 | std::string driver_desc = "unknown"; | ||
| 1332 | if (vkGetPhysicalDeviceProperties2) { | ||
| 1333 | VkPhysicalDeviceSubgroupProperties sg_props{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES}; | ||
| 1334 | VkPhysicalDeviceVulkan12Properties vk12_props{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_PROPERTIES}; | ||
| 1335 | VkPhysicalDeviceProperties2 props2{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2}; | ||
| 1336 | props2.pNext = &sg_props; | ||
| 1337 | if (pd_props.apiVersion >= VK_API_VERSION_1_2) { | ||
| 1338 | sg_props.pNext = &vk12_props; | ||
| 1339 | } | ||
| 1340 | vkGetPhysicalDeviceProperties2(pd, &props2); | ||
| 1341 | subgroup_size = sg_props.subgroupSize; | ||
| 1342 | if (pd_props.apiVersion >= VK_API_VERSION_1_2 && vk12_props.driverName[0] != '\0') { | ||
| 1343 | driver_desc = vk12_props.driverName; | ||
| 1344 | if (vk12_props.driverInfo[0] != '\0') { | ||
| 1345 | driver_desc += " ("; | ||
| 1346 | driver_desc += vk12_props.driverInfo; | ||
| 1347 | driver_desc += ')'; | ||
| 1348 | } | ||
| 1349 | } | ||
| 1350 | } | ||
| 1351 | |||
| 1352 | const float q_prio = 1.0f; | ||
| 1353 | VkDeviceQueueCreateInfo qci{VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO}; | ||
| 1354 | qci.queueFamilyIndex = qi; | ||
| 1355 | qci.queueCount = 1; | ||
| 1356 | qci.pQueuePriorities = &q_prio; | ||
| 1357 | VkDeviceCreateInfo dci{VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO}; | ||
| 1358 | dci.queueCreateInfoCount = 1; | ||
| 1359 | dci.pQueueCreateInfos = &qci; | ||
| 1360 | |||
| 1361 | VkDevice dev = VK_NULL_HANDLE; | ||
| 1362 | CHECK_VK(vkCreateDevice(pd, &dci, nullptr, &dev)); | ||
| 1363 | |||
| 1364 | #define LD(n) const auto n = reinterpret_cast<PFN_##n>(vkGetDeviceProcAddr(dev, #n)) | ||
| 1365 | LD(vkGetDeviceQueue); | ||
| 1366 | LD(vkDeviceWaitIdle); | ||
| 1367 | LD(vkDestroyDevice); | ||
| 1368 | LD(vkCreateBuffer); | ||
| 1369 | LD(vkDestroyBuffer); | ||
| 1370 | LD(vkGetBufferMemoryRequirements); | ||
| 1371 | LD(vkAllocateMemory); | ||
| 1372 | LD(vkFreeMemory); | ||
| 1373 | LD(vkBindBufferMemory); | ||
| 1374 | LD(vkMapMemory); | ||
| 1375 | LD(vkCreateShaderModule); | ||
| 1376 | LD(vkDestroyShaderModule); | ||
| 1377 | LD(vkCreateDescriptorSetLayout); | ||
| 1378 | LD(vkDestroyDescriptorSetLayout); | ||
| 1379 | LD(vkCreatePipelineLayout); | ||
| 1380 | LD(vkDestroyPipelineLayout); | ||
| 1381 | LD(vkCreateComputePipelines); | ||
| 1382 | LD(vkDestroyPipeline); | ||
| 1383 | LD(vkCreateDescriptorPool); | ||
| 1384 | LD(vkDestroyDescriptorPool); | ||
| 1385 | LD(vkAllocateDescriptorSets); | ||
| 1386 | LD(vkUpdateDescriptorSets); | ||
| 1387 | LD(vkCreateCommandPool); | ||
| 1388 | LD(vkDestroyCommandPool); | ||
| 1389 | LD(vkAllocateCommandBuffers); | ||
| 1390 | LD(vkBeginCommandBuffer); | ||
| 1391 | LD(vkEndCommandBuffer); | ||
| 1392 | LD(vkResetCommandBuffer); | ||
| 1393 | LD(vkCmdBindPipeline); | ||
| 1394 | LD(vkCmdBindDescriptorSets); | ||
| 1395 | LD(vkCmdPushConstants); | ||
| 1396 | LD(vkCmdDispatch); | ||
| 1397 | LD(vkCmdCopyBuffer); | ||
| 1398 | LD(vkCmdPipelineBarrier); | ||
| 1399 | LD(vkCreateFence); | ||
| 1400 | LD(vkDestroyFence); | ||
| 1401 | LD(vkWaitForFences); | ||
| 1402 | LD(vkResetFences); | ||
| 1403 | LD(vkQueueSubmit); | ||
| 1404 | LD(vkCreateQueryPool); | ||
| 1405 | LD(vkDestroyQueryPool); | ||
| 1406 | LD(vkGetQueryPoolResults); | ||
| 1407 | LD(vkCmdResetQueryPool); | ||
| 1408 | LD(vkCmdWriteTimestamp); | ||
| 1409 | #undef LD | ||
| 1410 | |||
| 1411 | VkQueue queue = VK_NULL_HANDLE; | ||
| 1412 | vkGetDeviceQueue(dev, qi, 0, &queue); | ||
| 1413 | VkPhysicalDeviceMemoryProperties mp{}; | ||
| 1414 | vkGetPhysicalDeviceMemoryProperties(pd, &mp); | ||
| 1415 | |||
| 1416 | const bool use_gpu_timestamps = (ts_valid_bits > 0u) && (pd_props.limits.timestampPeriod > 0.0f) && | ||
| 1417 | vkCreateQueryPool && vkCmdResetQueryPool && vkCmdWriteTimestamp && | ||
| 1418 | vkGetQueryPoolResults; | ||
| 1419 | |||
| 1420 | VkDescriptorSetLayout dsl = VK_NULL_HANDLE; | ||
| 1421 | VkPipelineLayout pl = VK_NULL_HANDLE; | ||
| 1422 | VkDescriptorPool dpool = VK_NULL_HANDLE; | ||
| 1423 | VkDescriptorSet dset = VK_NULL_HANDLE; | ||
| 1424 | VkCommandPool cpool = VK_NULL_HANDLE; | ||
| 1425 | VkCommandBuffer util_cmd = VK_NULL_HANDLE; | ||
| 1426 | VkCommandBuffer bench_cmd = VK_NULL_HANDLE; | ||
| 1427 | VkFence fence = VK_NULL_HANDLE; | ||
| 1428 | VkQueryPool qpool = VK_NULL_HANDLE; | ||
| 1429 | std::array<VkShaderModule, 3> sms{}; | ||
| 1430 | std::array<VkPipeline, 3> pipes{}; | ||
| 1431 | |||
| 1432 | auto destroy_buf = [&](GpuBuffer& b) { | ||
| 1433 | if (b.buffer) vkDestroyBuffer(dev, b.buffer, nullptr); | ||
| 1434 | if (b.memory) vkFreeMemory(dev, b.memory, nullptr); | ||
| 1435 | b = GpuBuffer{}; | ||
| 1436 | }; | ||
| 1437 | |||
| 1438 | auto cleanup_device = [&]() { | ||
| 1439 | if (dev && vkDeviceWaitIdle) vkDeviceWaitIdle(dev); | ||
| 1440 | for (VkPipeline p : pipes) { | ||
| 1441 | if (p) vkDestroyPipeline(dev, p, nullptr); | ||
| 1442 | } | ||
| 1443 | for (VkShaderModule m : sms) { | ||
| 1444 | if (m) vkDestroyShaderModule(dev, m, nullptr); | ||
| 1445 | } | ||
| 1446 | if (qpool) vkDestroyQueryPool(dev, qpool, nullptr); | ||
| 1447 | if (fence) vkDestroyFence(dev, fence, nullptr); | ||
| 1448 | if (cpool) vkDestroyCommandPool(dev, cpool, nullptr); | ||
| 1449 | if (dpool) vkDestroyDescriptorPool(dev, dpool, nullptr); | ||
| 1450 | if (pl) vkDestroyPipelineLayout(dev, pl, nullptr); | ||
| 1451 | if (dsl) vkDestroyDescriptorSetLayout(dev, dsl, nullptr); | ||
| 1452 | if (dev) vkDestroyDevice(dev, nullptr); | ||
| 1453 | destroy_instance(); | ||
| 1454 | }; | ||
| 1455 | |||
| 1456 | // Set 0 bindings: 0 = keys_buf, 2 = desc_map, 3 = out_buf | ||
| 1457 | const std::array<VkDescriptorSetLayoutBinding, 3> dsl_bindings{{ | ||
| 1458 | {0u, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1u, VK_SHADER_STAGE_COMPUTE_BIT, nullptr}, | ||
| 1459 | {2u, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1u, VK_SHADER_STAGE_COMPUTE_BIT, nullptr}, | ||
| 1460 | {3u, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1u, VK_SHADER_STAGE_COMPUTE_BIT, nullptr}, | ||
| 1461 | }}; | ||
| 1462 | VkDescriptorSetLayoutCreateInfo dsli{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO}; | ||
| 1463 | dsli.bindingCount = static_cast<std::uint32_t>(dsl_bindings.size()); | ||
| 1464 | dsli.pBindings = dsl_bindings.data(); | ||
| 1465 | if (vkCreateDescriptorSetLayout(dev, &dsli, nullptr, &dsl) != VK_SUCCESS) { | ||
| 1466 | cleanup_device(); | ||
| 1467 | return 1; | ||
| 1468 | } | ||
| 1469 | |||
| 1470 | const VkPushConstantRange pcr{VK_SHADER_STAGE_COMPUTE_BIT, 0u, static_cast<std::uint32_t>(sizeof(PushConstants))}; | ||
| 1471 | VkPipelineLayoutCreateInfo pli{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO}; | ||
| 1472 | pli.setLayoutCount = 1u; | ||
| 1473 | pli.pSetLayouts = &dsl; | ||
| 1474 | pli.pushConstantRangeCount = 1u; | ||
| 1475 | pli.pPushConstantRanges = &pcr; | ||
| 1476 | if (vkCreatePipelineLayout(dev, &pli, nullptr, &pl) != VK_SUCCESS) { | ||
| 1477 | cleanup_device(); | ||
| 1478 | return 1; | ||
| 1479 | } | ||
| 1480 | |||
| 1481 | const std::array<ShaderKind, 3> kinds{ShaderKind::Baseline, ShaderKind::SingleLookup, ShaderKind::PairTSLookup}; | ||
| 1482 | for (std::size_t i = 0; i < kinds.size(); ++i) { | ||
| 1483 | const auto spv = build_bench_spv(kinds[i]); | ||
| 1484 | VkShaderModuleCreateInfo smi{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO}; | ||
| 1485 | smi.codeSize = spv.size() * sizeof(std::uint32_t); | ||
| 1486 | smi.pCode = spv.data(); | ||
| 1487 | if (vkCreateShaderModule(dev, &smi, nullptr, &sms[i]) != VK_SUCCESS) { | ||
| 1488 | cleanup_device(); | ||
| 1489 | return 1; | ||
| 1490 | } | ||
| 1491 | VkComputePipelineCreateInfo cpci{VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO}; | ||
| 1492 | cpci.stage.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; | ||
| 1493 | cpci.stage.stage = VK_SHADER_STAGE_COMPUTE_BIT; | ||
| 1494 | cpci.stage.module = sms[i]; | ||
| 1495 | cpci.stage.pName = "main"; | ||
| 1496 | cpci.layout = pl; | ||
| 1497 | if (vkCreateComputePipelines(dev, VK_NULL_HANDLE, 1u, &cpci, nullptr, &pipes[i]) != VK_SUCCESS) { | ||
| 1498 | cleanup_device(); | ||
| 1499 | return 1; | ||
| 1500 | } | ||
| 1501 | } | ||
| 1502 | |||
| 1503 | const VkDescriptorPoolSize pool_size{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 3u}; | ||
| 1504 | VkDescriptorPoolCreateInfo dpi{VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO}; | ||
| 1505 | dpi.maxSets = 1u; | ||
| 1506 | dpi.poolSizeCount = 1u; | ||
| 1507 | dpi.pPoolSizes = &pool_size; | ||
| 1508 | if (vkCreateDescriptorPool(dev, &dpi, nullptr, &dpool) != VK_SUCCESS) { | ||
| 1509 | cleanup_device(); | ||
| 1510 | return 1; | ||
| 1511 | } | ||
| 1512 | VkDescriptorSetAllocateInfo dsai{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO}; | ||
| 1513 | dsai.descriptorPool = dpool; | ||
| 1514 | dsai.descriptorSetCount = 1u; | ||
| 1515 | dsai.pSetLayouts = &dsl; | ||
| 1516 | if (vkAllocateDescriptorSets(dev, &dsai, &dset) != VK_SUCCESS) { | ||
| 1517 | cleanup_device(); | ||
| 1518 | return 1; | ||
| 1519 | } | ||
| 1520 | |||
| 1521 | VkCommandPoolCreateInfo cpci{VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO}; | ||
| 1522 | cpci.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; | ||
| 1523 | cpci.queueFamilyIndex = qi; | ||
| 1524 | if (vkCreateCommandPool(dev, &cpci, nullptr, &cpool) != VK_SUCCESS) { | ||
| 1525 | cleanup_device(); | ||
| 1526 | return 1; | ||
| 1527 | } | ||
| 1528 | VkCommandBuffer cmds[2] = {}; | ||
| 1529 | VkCommandBufferAllocateInfo cbai{VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO}; | ||
| 1530 | cbai.commandPool = cpool; | ||
| 1531 | cbai.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; | ||
| 1532 | cbai.commandBufferCount = 2u; | ||
| 1533 | if (vkAllocateCommandBuffers(dev, &cbai, cmds) != VK_SUCCESS) { | ||
| 1534 | cleanup_device(); | ||
| 1535 | return 1; | ||
| 1536 | } | ||
| 1537 | util_cmd = cmds[0]; | ||
| 1538 | bench_cmd = cmds[1]; | ||
| 1539 | |||
| 1540 | const VkFenceCreateInfo fci{VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, nullptr, 0u}; | ||
| 1541 | if (vkCreateFence(dev, &fci, nullptr, &fence) != VK_SUCCESS) { | ||
| 1542 | cleanup_device(); | ||
| 1543 | return 1; | ||
| 1544 | } | ||
| 1545 | if (use_gpu_timestamps) { | ||
| 1546 | VkQueryPoolCreateInfo qpi{VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO}; | ||
| 1547 | qpi.queryType = VK_QUERY_TYPE_TIMESTAMP; | ||
| 1548 | qpi.queryCount = 2u; | ||
| 1549 | if (vkCreateQueryPool(dev, &qpi, nullptr, &qpool) != VK_SUCCESS) { | ||
| 1550 | cleanup_device(); | ||
| 1551 | return 1; | ||
| 1552 | } | ||
| 1553 | } | ||
| 1554 | |||
| 1555 | auto alloc_buffer_mt = [&](VkDeviceSize bytes, VkBufferUsageFlags usage, std::uint32_t mt_idx, bool map_mem, | ||
| 1556 | GpuBuffer& out_buf) -> bool { | ||
| 1557 | out_buf = GpuBuffer{}; | ||
| 1558 | out_buf.size = bytes; | ||
| 1559 | VkBufferCreateInfo bci{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; | ||
| 1560 | bci.size = bytes; | ||
| 1561 | bci.usage = usage; | ||
| 1562 | if (vkCreateBuffer(dev, &bci, nullptr, &out_buf.buffer) != VK_SUCCESS) return false; | ||
| 1563 | VkMemoryRequirements mr{}; | ||
| 1564 | vkGetBufferMemoryRequirements(dev, out_buf.buffer, &mr); | ||
| 1565 | if (mt_idx >= mp.memoryTypeCount || !(mr.memoryTypeBits & (1u << mt_idx))) { | ||
| 1566 | destroy_buf(out_buf); | ||
| 1567 | return false; | ||
| 1568 | } | ||
| 1569 | out_buf.memory_type_index = mt_idx; | ||
| 1570 | VkMemoryAllocateInfo mai{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO}; | ||
| 1571 | mai.allocationSize = mr.size; | ||
| 1572 | mai.memoryTypeIndex = mt_idx; | ||
| 1573 | if (vkAllocateMemory(dev, &mai, nullptr, &out_buf.memory) != VK_SUCCESS || | ||
| 1574 | vkBindBufferMemory(dev, out_buf.buffer, out_buf.memory, 0u) != VK_SUCCESS) { | ||
| 1575 | destroy_buf(out_buf); | ||
| 1576 | return false; | ||
| 1577 | } | ||
| 1578 | if (map_mem && vkMapMemory(dev, out_buf.memory, 0u, bytes, 0u, &out_buf.mapped) != VK_SUCCESS) { | ||
| 1579 | destroy_buf(out_buf); | ||
| 1580 | return false; | ||
| 1581 | } | ||
| 1582 | return true; | ||
| 1583 | }; | ||
| 1584 | |||
| 1585 | auto alloc_typed_buffer = [&](VkDeviceSize bytes, VkBufferUsageFlags usage, VkMemoryPropertyFlags req, | ||
| 1586 | VkMemoryPropertyFlags avoid, bool map_mem, GpuBuffer& out_buf) -> bool { | ||
| 1587 | VkBuffer probe = VK_NULL_HANDLE; | ||
| 1588 | VkBufferCreateInfo bci{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; | ||
| 1589 | bci.size = bytes; | ||
| 1590 | bci.usage = usage; | ||
| 1591 | if (vkCreateBuffer(dev, &bci, nullptr, &probe) != VK_SUCCESS) return false; | ||
| 1592 | VkMemoryRequirements mr{}; | ||
| 1593 | vkGetBufferMemoryRequirements(dev, probe, &mr); | ||
| 1594 | vkDestroyBuffer(dev, probe, nullptr); | ||
| 1595 | const std::uint32_t mt_idx = pick_memory_type_exact_or_fallback(mp, mr.memoryTypeBits, req, avoid); | ||
| 1596 | return alloc_buffer_mt(bytes, usage, mt_idx, map_mem, out_buf); | ||
| 1597 | }; | ||
| 1598 | |||
| 1599 | auto submit_and_wait = [&](VkCommandBuffer cmd) -> bool { | ||
| 1600 | VkResult r = vkResetFences(dev, 1u, &fence); | ||
| 1601 | if (r != VK_SUCCESS) { | ||
| 1602 | std::fprintf(stderr, "FAILED vkResetFences -> %d\n", int(r)); | ||
| 1603 | return false; | ||
| 1604 | } | ||
| 1605 | VkSubmitInfo si{VK_STRUCTURE_TYPE_SUBMIT_INFO}; | ||
| 1606 | si.commandBufferCount = 1u; | ||
| 1607 | si.pCommandBuffers = &cmd; | ||
| 1608 | r = vkQueueSubmit(queue, 1u, &si, fence); | ||
| 1609 | if (r != VK_SUCCESS) { | ||
| 1610 | std::fprintf(stderr, "FAILED vkQueueSubmit -> %d\n", int(r)); | ||
| 1611 | return false; | ||
| 1612 | } | ||
| 1613 | r = vkWaitForFences(dev, 1u, &fence, VK_TRUE, std::numeric_limits<std::uint64_t>::max()); | ||
| 1614 | if (r != VK_SUCCESS) { | ||
| 1615 | std::fprintf(stderr, "FAILED vkWaitForFences -> %d\n", int(r)); | ||
| 1616 | return false; | ||
| 1617 | } | ||
| 1618 | return true; | ||
| 1619 | }; | ||
| 1620 | |||
| 1621 | auto upload_via_staging = [&](const void* src_data, VkDeviceSize bytes, GpuBuffer& dst_buf) -> bool { | ||
| 1622 | GpuBuffer stage{}; | ||
| 1623 | if (!alloc_typed_buffer(bytes, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, | ||
| 1624 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 0u, true, | ||
| 1625 | stage)) { | ||
| 1626 | return false; | ||
| 1627 | } | ||
| 1628 | std::memcpy(stage.mapped, src_data, static_cast<std::size_t>(bytes)); | ||
| 1629 | |||
| 1630 | if (vkResetCommandBuffer(util_cmd, 0u) != VK_SUCCESS) { | ||
| 1631 | destroy_buf(stage); | ||
| 1632 | return false; | ||
| 1633 | } | ||
| 1634 | VkCommandBufferBeginInfo bi{VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO}; | ||
| 1635 | bi.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; | ||
| 1636 | if (vkBeginCommandBuffer(util_cmd, &bi) != VK_SUCCESS) { | ||
| 1637 | destroy_buf(stage); | ||
| 1638 | return false; | ||
| 1639 | } | ||
| 1640 | const VkBufferCopy copy{0u, 0u, bytes}; | ||
| 1641 | vkCmdCopyBuffer(util_cmd, stage.buffer, dst_buf.buffer, 1u, ©); | ||
| 1642 | VkMemoryBarrier mb{VK_STRUCTURE_TYPE_MEMORY_BARRIER}; | ||
| 1643 | mb.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; | ||
| 1644 | mb.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; | ||
| 1645 | vkCmdPipelineBarrier(util_cmd, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0u, 1u, &mb, | ||
| 1646 | 0u, nullptr, 0u, nullptr); | ||
| 1647 | if (vkEndCommandBuffer(util_cmd) != VK_SUCCESS || !submit_and_wait(util_cmd)) { | ||
| 1648 | destroy_buf(stage); | ||
| 1649 | return false; | ||
| 1650 | } | ||
| 1651 | destroy_buf(stage); | ||
| 1652 | return true; | ||
| 1653 | }; | ||
| 1654 | |||
| 1655 | const std::uint32_t total_invocations = opt.groups * kWorkgroupSize; | ||
| 1656 | const VkDeviceSize out_bytes = static_cast<VkDeviceSize>(total_invocations) * sizeof(std::uint32_t); | ||
| 1657 | |||
| 1658 | // Output buffer (device local when available) + host readback staging buffer | ||
| 1659 | GpuBuffer out_buf{}; | ||
| 1660 | GpuBuffer readback_buf{}; | ||
| 1661 | if (!alloc_typed_buffer(out_bytes, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT, | ||
| 1662 | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, false, | ||
| 1663 | out_buf) || | ||
| 1664 | !alloc_typed_buffer(out_bytes, VK_BUFFER_USAGE_TRANSFER_DST_BIT, | ||
| 1665 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 0u, true, | ||
| 1666 | readback_buf)) { | ||
| 1667 | destroy_buf(out_buf); | ||
| 1668 | destroy_buf(readback_buf); | ||
| 1669 | cleanup_device(); | ||
| 1670 | return 1; | ||
| 1671 | } | ||
| 1672 | |||
| 1673 | auto cleanup_all = [&](GpuBuffer& a, GpuBuffer& b) { | ||
| 1674 | destroy_buf(a); | ||
| 1675 | destroy_buf(b); | ||
| 1676 | destroy_buf(out_buf); | ||
| 1677 | destroy_buf(readback_buf); | ||
| 1678 | cleanup_device(); | ||
| 1679 | }; | ||
| 1680 | |||
| 1681 | const double total_single_lookups = static_cast<double>(total_invocations) * static_cast<double>(opt.iters); | ||
| 1682 | const std::uint64_t ts_mask = | ||
| 1683 | (ts_valid_bits == 64u) ? ~0ULL : (ts_valid_bits == 0u ? 0ULL : ((1ULL << ts_valid_bits) - 1ULL)); | ||
| 1684 | |||
| 1685 | if (!opt.csv) { | ||
| 1686 | std::printf("device: %s (%s, vendor=0x%04x, device=0x%04x, subgroup=%u, driver=%s)\n", pd_props.deviceName, | ||
| 1687 | device_type_name(pd_props.deviceType), pd_props.vendorID, pd_props.deviceID, subgroup_size, | ||
| 1688 | driver_desc.c_str()); | ||
| 1689 | if (use_gpu_timestamps) { | ||
| 1690 | std::printf("timing: Vulkan GPU timestamps (valid_bits=%u, period=%.3f ns); validation=%s\n", ts_valid_bits, | ||
| 1691 | pd_props.limits.timestampPeriod, validation ? "on" : "off"); | ||
| 1692 | } else { | ||
| 1693 | std::printf("timing: host wall-clock fallback; validation=%s\n", validation ? "on" : "off"); | ||
| 1694 | } | ||
| 1695 | std::printf("dispatch: %u workgroups x %u threads = %u invocations x %u iters = %.2fM single / %.2fM pair lookups " | ||
| 1696 | "(warmup=%u, reps=%u)\n", | ||
| 1697 | opt.groups, kWorkgroupSize, total_invocations, opt.iters, total_single_lookups / 1e6, | ||
| 1698 | (total_single_lookups * 2.0) / 1e6, opt.warmup, opt.reps); | ||
| 1699 | } else { | ||
| 1700 | std::printf("# device,%s,%s\n", pd_props.deviceName, driver_desc.c_str()); | ||
| 1701 | std::printf("entries,capacity,load_pct,size_bytes,mean_probes,max_probes,placement,mem_type,flags,scenario," | ||
| 1702 | "groups,iters,reps,med_ms,min_ms,max_ms,us_per_1m_lk,net_us_per_1m_lk,glk_per_s\n"); | ||
| 1703 | } | ||
| 1704 | |||
| 1705 | const std::array<ScenarioSpec, 11> scenarios{{ | ||
| 1706 | {"baseline_uniform", ShaderKind::Baseline, false, 1u, 0u, 1u, false, ScenarioSpec::Bank::TSharp, 0.0}, | ||
| 1707 | {"baseline_divergent", ShaderKind::Baseline, true, 64u, 1u, 1u, false, ScenarioSpec::Bank::TSharp, 0.0}, | ||
| 1708 | {"uniform_hot_tsharp", ShaderKind::SingleLookup, false, 0u, 0u, 0u, false, ScenarioSpec::Bank::TSharp, 1.0}, | ||
| 1709 | {"uniform_tsharp_hit", ShaderKind::SingleLookup, false, 1u, 0u, 1u, false, ScenarioSpec::Bank::TSharp, 1.0}, | ||
| 1710 | {"uniform_ssharp_hit", ShaderKind::SingleLookup, false, 1u, 0u, 1u, false, ScenarioSpec::Bank::SSharp, 1.0}, | ||
| 1711 | {"uniform_pair_ts", ShaderKind::PairTSLookup, false, 1u, 0u, 1u, false, ScenarioSpec::Bank::TSharp, 2.0}, | ||
| 1712 | {"uniform_miss", ShaderKind::SingleLookup, false, 1u, 0u, 1u, false, ScenarioSpec::Bank::Miss, 1.0}, | ||
| 1713 | {"divergent_tsharp_hit", ShaderKind::SingleLookup, true, 64u, 1u, 1u, false, ScenarioSpec::Bank::TSharp, 1.0}, | ||
| 1714 | {"divergent_pair_ts", ShaderKind::PairTSLookup, true, 64u, 1u, 1u, false, ScenarioSpec::Bank::TSharp, 2.0}, | ||
| 1715 | {"divergent_miss", ShaderKind::SingleLookup, true, 64u, 1u, 1u, false, ScenarioSpec::Bank::Miss, 1.0}, | ||
| 1716 | {"latency_divergent", ShaderKind::SingleLookup, true, 64u, 1u, 1u, true, ScenarioSpec::Bank::TSharp, 1.0}, | ||
| 1717 | }}; | ||
| 1718 | |||
| 1719 | bool seen_rebar = false; | ||
| 1720 | for (const std::uint32_t entry_count : opt.sizes) { | ||
| 1721 | TableDataset ds; | ||
| 1722 | if (!build_dataset(entry_count, ds)) { | ||
| 1723 | GpuBuffer unused_a{}, unused_b{}; | ||
| 1724 | cleanup_all(unused_a, unused_b); | ||
| 1725 | return 1; | ||
| 1726 | } | ||
| 1727 | const VkDeviceSize keys_bytes = static_cast<VkDeviceSize>(ds.keys_buf.size()) * sizeof(std::uint32_t); | ||
| 1728 | const VkDeviceSize map_bytes = static_cast<VkDeviceSize>(ds.gpu_table.size()) * sizeof(std::uint32_t); | ||
| 1729 | |||
| 1730 | if (map_bytes > pd_props.limits.maxStorageBufferRange || keys_bytes > pd_props.limits.maxStorageBufferRange) { | ||
| 1731 | std::printf("unsupported: table (%llu bytes) exceeds maxStorageBufferRange (%u)\n", | ||
| 1732 | static_cast<unsigned long long>(map_bytes), pd_props.limits.maxStorageBufferRange); | ||
| 1733 | GpuBuffer unused_a{}, unused_b{}; | ||
| 1734 | cleanup_all(unused_a, unused_b); | ||
| 1735 | return kUnsupportedExit; | ||
| 1736 | } | ||
| 1737 | |||
| 1738 | // Upload keys_buf once per size into DEVICE_LOCAL (fallback HOST_VISIBLE). | ||
| 1739 | GpuBuffer keys_buf{}; | ||
| 1740 | GpuBuffer map_buf{}; | ||
| 1741 | if (!alloc_typed_buffer(keys_bytes, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, | ||
| 1742 | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, false, | ||
| 1743 | keys_buf) || | ||
| 1744 | !upload_via_staging(ds.keys_buf.data(), keys_bytes, keys_buf)) { | ||
| 1745 | cleanup_all(keys_buf, map_buf); | ||
| 1746 | return 1; | ||
| 1747 | } | ||
| 1748 | |||
| 1749 | // Query desc_map memoryRequirements to discover valid placements. | ||
| 1750 | std::uint32_t map_type_bits = 0; | ||
| 1751 | { | ||
| 1752 | VkBuffer probe = VK_NULL_HANDLE; | ||
| 1753 | VkBufferCreateInfo bci{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; | ||
| 1754 | bci.size = map_bytes; | ||
| 1755 | bci.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; | ||
| 1756 | if (vkCreateBuffer(dev, &bci, nullptr, &probe) != VK_SUCCESS) { | ||
| 1757 | cleanup_all(keys_buf, map_buf); | ||
| 1758 | return 1; | ||
| 1759 | } | ||
| 1760 | VkMemoryRequirements mr{}; | ||
| 1761 | vkGetBufferMemoryRequirements(dev, probe, &mr); | ||
| 1762 | vkDestroyBuffer(dev, probe, nullptr); | ||
| 1763 | map_type_bits = mr.memoryTypeBits; | ||
| 1764 | } | ||
| 1765 | |||
| 1766 | std::vector<MemoryPlacement> placements; | ||
| 1767 | auto add_placement = [&](const char* label, std::uint32_t mt_idx, bool use_staging) { | ||
| 1768 | if (mt_idx == 0xFFFFFFFFu) return; | ||
| 1769 | for (const auto& existing : placements) { | ||
| 1770 | if (existing.memory_type_index == mt_idx) { | ||
| 1771 | if (!opt.csv) { | ||
| 1772 | std::printf(" note: skipping placement %s (same memoryTypeIndex %u as %s)\n", label, mt_idx, | ||
| 1773 | existing.label); | ||
| 1774 | } | ||
| 1775 | return; | ||
| 1776 | } | ||
| 1777 | } | ||
| 1778 | const auto& mt = mp.memoryTypes[mt_idx]; | ||
| 1779 | placements.push_back( | ||
| 1780 | {label, mt_idx, mt.heapIndex, mp.memoryHeaps[mt.heapIndex].size, mt.propertyFlags, use_staging}); | ||
| 1781 | }; | ||
| 1782 | |||
| 1783 | if (opt.mem_filter == MemoryFilter::All || opt.mem_filter == MemoryFilter::Device) { | ||
| 1784 | const std::uint32_t dev_mt = pick_memory_type_exact_or_fallback( | ||
| 1785 | mp, map_type_bits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT); | ||
| 1786 | add_placement("device_local", dev_mt, true); | ||
| 1787 | } | ||
| 1788 | if (opt.mem_filter == MemoryFilter::All || opt.mem_filter == MemoryFilter::Host) { | ||
| 1789 | // Exact BindlessSet::create() search order: first HOST_VISIBLE | HOST_COHERENT memory type. | ||
| 1790 | const std::uint32_t bindless_mt = pick_memory_type_exact_or_fallback( | ||
| 1791 | mp, map_type_bits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 0u); | ||
| 1792 | add_placement("host_visible", bindless_mt, false); | ||
| 1793 | } | ||
| 1794 | if (opt.mem_filter == MemoryFilter::All || opt.mem_filter == MemoryFilter::Rebar) { | ||
| 1795 | std::uint32_t rebar_mt = 0xFFFFFFFFu; | ||
| 1796 | const VkMemoryPropertyFlags want = | ||
| 1797 | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | | ||
| 1798 | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; | ||
| 1799 | for (std::uint32_t i = 0; i < mp.memoryTypeCount; ++i) { | ||
| 1800 | if (!(map_type_bits & (1u << i))) continue; | ||
| 1801 | if ((mp.memoryTypes[i].propertyFlags & want) != want) continue; | ||
| 1802 | if (mp.memoryHeaps[mp.memoryTypes[i].heapIndex].size >= map_bytes * 2u) { | ||
| 1803 | rebar_mt = i; | ||
| 1804 | break; | ||
| 1805 | } | ||
| 1806 | } | ||
| 1807 | if (rebar_mt != 0xFFFFFFFFu) { | ||
| 1808 | seen_rebar = true; | ||
| 1809 | add_placement("host_device_local", rebar_mt, false); | ||
| 1810 | } | ||
| 1811 | } | ||
| 1812 | |||
| 1813 | if (!opt.csv) { | ||
| 1814 | std::printf( | ||
| 1815 | "\n=== entries=%u (T#=%u, S#=%u) | cap=%u | load=%.1f%% | size=%.2f MiB (%.1f KiB) | " | ||
| 1816 | "probes mean=%.3f max=%u p1=%.1f%% ===\n", | ||
| 1817 | ds.entry_count, ds.t_count, ds.s_count, ds.capacity, ds.load_pct, | ||
| 1818 | static_cast<double>(map_bytes) / (1024.0 * 1024.0), static_cast<double>(map_bytes) / 1024.0, | ||
| 1819 | ds.mean_probes, ds.max_probes, ds.probe1_pct); | ||
| 1820 | } | ||
| 1821 | |||
| 1822 | for (const auto& pl_cfg : placements) { | ||
| 1823 | destroy_buf(map_buf); | ||
| 1824 | const VkBufferUsageFlags map_usage = | ||
| 1825 | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | (pl_cfg.use_staging ? VK_BUFFER_USAGE_TRANSFER_DST_BIT : 0u); | ||
| 1826 | if (!alloc_buffer_mt(map_bytes, map_usage, pl_cfg.memory_type_index, !pl_cfg.use_staging, map_buf)) { | ||
| 1827 | cleanup_all(keys_buf, map_buf); | ||
| 1828 | return 1; | ||
| 1829 | } | ||
| 1830 | if (pl_cfg.use_staging) { | ||
| 1831 | if (!upload_via_staging(ds.gpu_table.data(), map_bytes, map_buf)) { | ||
| 1832 | cleanup_all(keys_buf, map_buf); | ||
| 1833 | return 1; | ||
| 1834 | } | ||
| 1835 | } else { | ||
| 1836 | std::memcpy(map_buf.mapped, ds.gpu_table.data(), static_cast<std::size_t>(map_bytes)); | ||
| 1837 | } | ||
| 1838 | |||
| 1839 | const std::array<VkDescriptorBufferInfo, 3> buf_infos{{ | ||
| 1840 | {keys_buf.buffer, 0u, keys_bytes}, | ||
| 1841 | {map_buf.buffer, 0u, map_bytes}, | ||
| 1842 | {out_buf.buffer, 0u, out_bytes}, | ||
| 1843 | }}; | ||
| 1844 | const std::array<VkWriteDescriptorSet, 3> writes{{ | ||
| 1845 | {VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, nullptr, dset, 0u, 0u, 1u, | ||
| 1846 | VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, nullptr, &buf_infos[0], nullptr}, | ||
| 1847 | {VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, nullptr, dset, 2u, 0u, 1u, | ||
| 1848 | VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, nullptr, &buf_infos[1], nullptr}, | ||
| 1849 | {VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, nullptr, dset, 3u, 0u, 1u, | ||
| 1850 | VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, nullptr, &buf_infos[2], nullptr}, | ||
| 1851 | }}; | ||
| 1852 | vkUpdateDescriptorSets(dev, static_cast<std::uint32_t>(writes.size()), writes.data(), 0u, nullptr); | ||
| 1853 | |||
| 1854 | if (!opt.csv) { | ||
| 1855 | std::printf("-- placement: %s (type=%u, heap=%u %.0f MiB, flags=%s) --\n", pl_cfg.label, | ||
| 1856 | pl_cfg.memory_type_index, pl_cfg.heap_index, | ||
| 1857 | static_cast<double>(pl_cfg.heap_size) / (1024.0 * 1024.0), | ||
| 1858 | format_mem_flags(pl_cfg.flags).c_str()); | ||
| 1859 | std::printf(" %-22s %9s %9s %9s %11s %12s %9s\n", "scenario", "med_ms", "min_ms", "max_ms", | ||
| 1860 | "us/1M_lk", "net_us/1M", "Glk/s"); | ||
| 1861 | } | ||
| 1862 | |||
| 1863 | double base_uniform_us_per_1m = 0.0; | ||
| 1864 | double base_divergent_us_per_1m = 0.0; | ||
| 1865 | |||
| 1866 | for (const auto& sc : scenarios) { | ||
| 1867 | const VkPipeline pipe = pipes[static_cast<std::size_t>(sc.shader)]; | ||
| 1868 | |||
| 1869 | std::uint32_t eff_groups = opt.groups; | ||
| 1870 | std::uint32_t eff_iters = opt.iters; | ||
| 1871 | std::uint32_t eff_reps = opt.reps; | ||
| 1872 | std::uint32_t eff_warmup = opt.warmup; | ||
| 1873 | |||
| 1874 | // Probe slow scenarios on a small grid so heavy PCIe/sysram lookups stay well below Windows TDR. | ||
| 1875 | if (opt.max_dispatch_ms > 0.0 && opt.groups > 64u) { | ||
| 1876 | const std::uint32_t probe_groups = 64u; | ||
| 1877 | const std::uint32_t probe_iters = std::min(opt.iters, 4u); | ||
| 1878 | const PushConstants probe_pc = make_push_constants(sc, ds, probe_iters); | ||
| 1879 | if (vkResetCommandBuffer(util_cmd, 0u) != VK_SUCCESS) { | ||
| 1880 | cleanup_all(keys_buf, map_buf); | ||
| 1881 | return 1; | ||
| 1882 | } | ||
| 1883 | VkCommandBufferBeginInfo bi{VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO}; | ||
| 1884 | bi.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; | ||
| 1885 | if (vkBeginCommandBuffer(util_cmd, &bi) != VK_SUCCESS) { | ||
| 1886 | cleanup_all(keys_buf, map_buf); | ||
| 1887 | return 1; | ||
| 1888 | } | ||
| 1889 | if (use_gpu_timestamps) vkCmdResetQueryPool(util_cmd, qpool, 0u, 2u); | ||
| 1890 | vkCmdBindPipeline(util_cmd, VK_PIPELINE_BIND_POINT_COMPUTE, pipe); | ||
| 1891 | vkCmdBindDescriptorSets(util_cmd, VK_PIPELINE_BIND_POINT_COMPUTE, pl, 0u, 1u, &dset, 0u, nullptr); | ||
| 1892 | vkCmdPushConstants(util_cmd, pl, VK_SHADER_STAGE_COMPUTE_BIT, 0u, | ||
| 1893 | static_cast<std::uint32_t>(sizeof(probe_pc)), &probe_pc); | ||
| 1894 | if (use_gpu_timestamps) vkCmdWriteTimestamp(util_cmd, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, qpool, 0u); | ||
| 1895 | vkCmdDispatch(util_cmd, probe_groups, 1u, 1u); | ||
| 1896 | if (use_gpu_timestamps) { | ||
| 1897 | vkCmdWriteTimestamp(util_cmd, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, qpool, 1u); | ||
| 1898 | } | ||
| 1899 | if (vkEndCommandBuffer(util_cmd) != VK_SUCCESS) { | ||
| 1900 | cleanup_all(keys_buf, map_buf); | ||
| 1901 | return 1; | ||
| 1902 | } | ||
| 1903 | const auto pt0 = std::chrono::steady_clock::now(); | ||
| 1904 | if (!submit_and_wait(util_cmd)) { | ||
| 1905 | cleanup_all(keys_buf, map_buf); | ||
| 1906 | return 1; | ||
| 1907 | } | ||
| 1908 | const auto pt1 = std::chrono::steady_clock::now(); | ||
| 1909 | double probe_ms = 0.0; | ||
| 1910 | if (use_gpu_timestamps) { | ||
| 1911 | std::uint64_t ts[2] = {}; | ||
| 1912 | if (vkGetQueryPoolResults(dev, qpool, 0u, 2u, sizeof(ts), ts, sizeof(std::uint64_t), | ||
| 1913 | VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT) != VK_SUCCESS) { | ||
| 1914 | cleanup_all(keys_buf, map_buf); | ||
| 1915 | return 1; | ||
| 1916 | } | ||
| 1917 | const std::uint64_t ticks = (ts[1] - ts[0]) & ts_mask; | ||
| 1918 | probe_ms = | ||
| 1919 | static_cast<double>(ticks) * static_cast<double>(pd_props.limits.timestampPeriod) * 1e-6; | ||
| 1920 | } else { | ||
| 1921 | probe_ms = std::chrono::duration<double, std::milli>(pt1 - pt0).count(); | ||
| 1922 | } | ||
| 1923 | |||
| 1924 | const double est_full_ms = | ||
| 1925 | std::max(0.0, probe_ms - 0.005) * | ||
| 1926 | (static_cast<double>(opt.groups) / static_cast<double>(probe_groups)) * | ||
| 1927 | (static_cast<double>(opt.iters) / static_cast<double>(probe_iters)); | ||
| 1928 | if (est_full_ms > opt.max_dispatch_ms) { | ||
| 1929 | const double target_ms = opt.max_dispatch_ms * 0.5; | ||
| 1930 | const double work_scale = target_ms / est_full_ms; | ||
| 1931 | const std::uint32_t min_iters = std::min(opt.iters, 4u); | ||
| 1932 | const double max_iter_reduction = | ||
| 1933 | static_cast<double>(opt.iters) / static_cast<double>(min_iters); | ||
| 1934 | if (work_scale * max_iter_reduction >= 1.0) { | ||
| 1935 | eff_iters = std::max( | ||
| 1936 | min_iters, static_cast<std::uint32_t>(std::floor(static_cast<double>(opt.iters) * work_scale))); | ||
| 1937 | } else { | ||
| 1938 | eff_iters = min_iters; | ||
| 1939 | const double rem_scale = work_scale * max_iter_reduction; | ||
| 1940 | eff_groups = std::max<std::uint32_t>( | ||
| 1941 | 64u, static_cast<std::uint32_t>(std::floor(static_cast<double>(opt.groups) * rem_scale))); | ||
| 1942 | } | ||
| 1943 | eff_reps = std::min(opt.reps, 9u); | ||
| 1944 | eff_warmup = std::min(opt.warmup, 2u); | ||
| 1945 | } | ||
| 1946 | } | ||
| 1947 | |||
| 1948 | const PushConstants pc = make_push_constants(sc, ds, eff_iters); | ||
| 1949 | const std::uint32_t eff_invocations = eff_groups * kWorkgroupSize; | ||
| 1950 | const VkDeviceSize eff_out_bytes = static_cast<VkDeviceSize>(eff_invocations) * sizeof(std::uint32_t); | ||
| 1951 | const double eff_single_lookups = static_cast<double>(eff_invocations) * static_cast<double>(eff_iters); | ||
| 1952 | const bool was_scaled = (eff_groups != opt.groups || eff_iters != opt.iters); | ||
| 1953 | |||
| 1954 | // Warmup + correctness verification with readback | ||
| 1955 | for (std::uint32_t w = 0; w <= eff_warmup; ++w) { | ||
| 1956 | const bool readback = (w == 0u); | ||
| 1957 | if (vkResetCommandBuffer(util_cmd, 0u) != VK_SUCCESS) { | ||
| 1958 | cleanup_all(keys_buf, map_buf); | ||
| 1959 | return 1; | ||
| 1960 | } | ||
| 1961 | VkCommandBufferBeginInfo bi{VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO}; | ||
| 1962 | bi.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; | ||
| 1963 | if (vkBeginCommandBuffer(util_cmd, &bi) != VK_SUCCESS) { | ||
| 1964 | cleanup_all(keys_buf, map_buf); | ||
| 1965 | return 1; | ||
| 1966 | } | ||
| 1967 | vkCmdBindPipeline(util_cmd, VK_PIPELINE_BIND_POINT_COMPUTE, pipe); | ||
| 1968 | vkCmdBindDescriptorSets(util_cmd, VK_PIPELINE_BIND_POINT_COMPUTE, pl, 0u, 1u, &dset, 0u, nullptr); | ||
| 1969 | vkCmdPushConstants(util_cmd, pl, VK_SHADER_STAGE_COMPUTE_BIT, 0u, | ||
| 1970 | static_cast<std::uint32_t>(sizeof(pc)), &pc); | ||
| 1971 | vkCmdDispatch(util_cmd, eff_groups, 1u, 1u); | ||
| 1972 | if (readback) { | ||
| 1973 | VkMemoryBarrier b1{VK_STRUCTURE_TYPE_MEMORY_BARRIER}; | ||
| 1974 | b1.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT; | ||
| 1975 | b1.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; | ||
| 1976 | vkCmdPipelineBarrier(util_cmd, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, | ||
| 1977 | VK_PIPELINE_STAGE_TRANSFER_BIT, 0u, 1u, &b1, 0u, nullptr, 0u, nullptr); | ||
| 1978 | const VkBufferCopy copy{0u, 0u, eff_out_bytes}; | ||
| 1979 | vkCmdCopyBuffer(util_cmd, out_buf.buffer, readback_buf.buffer, 1u, ©); | ||
| 1980 | VkMemoryBarrier b2{VK_STRUCTURE_TYPE_MEMORY_BARRIER}; | ||
| 1981 | b2.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; | ||
| 1982 | b2.dstAccessMask = VK_ACCESS_HOST_READ_BIT; | ||
| 1983 | vkCmdPipelineBarrier(util_cmd, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0u, | ||
| 1984 | 1u, &b2, 0u, nullptr, 0u, nullptr); | ||
| 1985 | } | ||
| 1986 | if (vkEndCommandBuffer(util_cmd) != VK_SUCCESS || !submit_and_wait(util_cmd)) { | ||
| 1987 | cleanup_all(keys_buf, map_buf); | ||
| 1988 | return 1; | ||
| 1989 | } | ||
| 1990 | if (readback) { | ||
| 1991 | const auto* gpu_out = static_cast<const std::uint32_t*>(readback_buf.mapped); | ||
| 1992 | if (!verify_gpu_output(sc, ds, pc, gpu_out, eff_invocations)) { | ||
| 1993 | cleanup_all(keys_buf, map_buf); | ||
| 1994 | return 1; | ||
| 1995 | } | ||
| 1996 | } | ||
| 1997 | } | ||
| 1998 | |||
| 1999 | // Record reusable timed command buffer (dispatch + timestamps only) | ||
| 2000 | if (vkResetCommandBuffer(bench_cmd, 0u) != VK_SUCCESS) { | ||
| 2001 | cleanup_all(keys_buf, map_buf); | ||
| 2002 | return 1; | ||
| 2003 | } | ||
| 2004 | { | ||
| 2005 | const VkCommandBufferBeginInfo bi{VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, nullptr, 0u, nullptr}; | ||
| 2006 | if (vkBeginCommandBuffer(bench_cmd, &bi) != VK_SUCCESS) { | ||
| 2007 | cleanup_all(keys_buf, map_buf); | ||
| 2008 | return 1; | ||
| 2009 | } | ||
| 2010 | if (use_gpu_timestamps) vkCmdResetQueryPool(bench_cmd, qpool, 0u, 2u); | ||
| 2011 | vkCmdBindPipeline(bench_cmd, VK_PIPELINE_BIND_POINT_COMPUTE, pipe); | ||
| 2012 | vkCmdBindDescriptorSets(bench_cmd, VK_PIPELINE_BIND_POINT_COMPUTE, pl, 0u, 1u, &dset, 0u, nullptr); | ||
| 2013 | vkCmdPushConstants(bench_cmd, pl, VK_SHADER_STAGE_COMPUTE_BIT, 0u, | ||
| 2014 | static_cast<std::uint32_t>(sizeof(pc)), &pc); | ||
| 2015 | if (use_gpu_timestamps) vkCmdWriteTimestamp(bench_cmd, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, qpool, 0u); | ||
| 2016 | vkCmdDispatch(bench_cmd, eff_groups, 1u, 1u); | ||
| 2017 | if (use_gpu_timestamps) { | ||
| 2018 | vkCmdWriteTimestamp(bench_cmd, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, qpool, 1u); | ||
| 2019 | } | ||
| 2020 | if (vkEndCommandBuffer(bench_cmd) != VK_SUCCESS) { | ||
| 2021 | cleanup_all(keys_buf, map_buf); | ||
| 2022 | return 1; | ||
| 2023 | } | ||
| 2024 | } | ||
| 2025 | |||
| 2026 | std::vector<double> samples_ms; | ||
| 2027 | samples_ms.reserve(eff_reps); | ||
| 2028 | for (std::uint32_t r = 0; r < eff_reps; ++r) { | ||
| 2029 | const auto t0 = std::chrono::steady_clock::now(); | ||
| 2030 | if (!submit_and_wait(bench_cmd)) { | ||
| 2031 | cleanup_all(keys_buf, map_buf); | ||
| 2032 | return 1; | ||
| 2033 | } | ||
| 2034 | const auto t1 = std::chrono::steady_clock::now(); | ||
| 2035 | if (use_gpu_timestamps) { | ||
| 2036 | std::uint64_t ts[2] = {}; | ||
| 2037 | if (vkGetQueryPoolResults(dev, qpool, 0u, 2u, sizeof(ts), ts, sizeof(std::uint64_t), | ||
| 2038 | VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT) != VK_SUCCESS) { | ||
| 2039 | cleanup_all(keys_buf, map_buf); | ||
| 2040 | return 1; | ||
| 2041 | } | ||
| 2042 | const std::uint64_t ticks = (ts[1] - ts[0]) & ts_mask; | ||
| 2043 | const double ms = | ||
| 2044 | static_cast<double>(ticks) * static_cast<double>(pd_props.limits.timestampPeriod) * 1e-6; | ||
| 2045 | samples_ms.push_back(ms); | ||
| 2046 | } else { | ||
| 2047 | samples_ms.push_back(std::chrono::duration<double, std::milli>(t1 - t0).count()); | ||
| 2048 | } | ||
| 2049 | } | ||
| 2050 | |||
| 2051 | std::sort(samples_ms.begin(), samples_ms.end()); | ||
| 2052 | const double med_ms = samples_ms[samples_ms.size() / 2u]; | ||
| 2053 | const double min_ms = samples_ms.front(); | ||
| 2054 | const double max_ms = samples_ms.back(); | ||
| 2055 | |||
| 2056 | if (std::strcmp(sc.name, "baseline_uniform") == 0) { | ||
| 2057 | base_uniform_us_per_1m = (med_ms > 0.0) ? (med_ms * 1e9 / eff_single_lookups) : 0.0; | ||
| 2058 | } | ||
| 2059 | if (std::strcmp(sc.name, "baseline_divergent") == 0) { | ||
| 2060 | base_divergent_us_per_1m = (med_ms > 0.0) ? (med_ms * 1e9 / eff_single_lookups) : 0.0; | ||
| 2061 | } | ||
| 2062 | |||
| 2063 | if (sc.lookups_per_iter <= 0.0) { | ||
| 2064 | if (!opt.csv) { | ||
| 2065 | std::printf(" %-22s %9.4f %9.4f %9.4f %11s %12s %9s", sc.name, med_ms, min_ms, max_ms, "-", | ||
| 2066 | "-", "-"); | ||
| 2067 | if (was_scaled) std::printf(" [scaled %ux%u, n=%u]", eff_groups, eff_iters, eff_reps); | ||
| 2068 | std::printf("\n"); | ||
| 2069 | } else { | ||
| 2070 | std::printf("%u,%u,%.2f,%llu,%.4f,%u,%s,%u,\"%s\",%s,%u,%u,%u,%.6f,%.6f,%.6f,,,\n", | ||
| 2071 | ds.entry_count, ds.capacity, ds.load_pct, static_cast<unsigned long long>(map_bytes), | ||
| 2072 | ds.mean_probes, ds.max_probes, pl_cfg.label, pl_cfg.memory_type_index, | ||
| 2073 | format_mem_flags(pl_cfg.flags).c_str(), sc.name, eff_groups, eff_iters, eff_reps, | ||
| 2074 | med_ms, min_ms, max_ms); | ||
| 2075 | } | ||
| 2076 | } else { | ||
| 2077 | const double total_lookups = eff_single_lookups * sc.lookups_per_iter; | ||
| 2078 | const double us_per_1m = (med_ms > 0.0) ? (med_ms * 1e9 / total_lookups) : 0.0; | ||
| 2079 | const double base_us = sc.divergent ? base_divergent_us_per_1m : base_uniform_us_per_1m; | ||
| 2080 | const double net_us_per_1m = std::max(0.0, us_per_1m - (base_us / sc.lookups_per_iter)); | ||
| 2081 | const double glk_s = (med_ms > 1e-9) ? (total_lookups / (med_ms * 1e6)) : 0.0; | ||
| 2082 | if (!opt.csv) { | ||
| 2083 | std::printf(" %-22s %9.4f %9.4f %9.4f %11.3f %12.3f %9.2f", sc.name, med_ms, min_ms, max_ms, | ||
| 2084 | us_per_1m, net_us_per_1m, glk_s); | ||
| 2085 | if (was_scaled) std::printf(" [scaled %ux%u, n=%u]", eff_groups, eff_iters, eff_reps); | ||
| 2086 | std::printf("\n"); | ||
| 2087 | } else { | ||
| 2088 | std::printf("%u,%u,%.2f,%llu,%.4f,%u,%s,%u,\"%s\",%s,%u,%u,%u,%.6f,%.6f,%.6f,%.4f,%.4f,%.4f\n", | ||
| 2089 | ds.entry_count, ds.capacity, ds.load_pct, | ||
| 2090 | static_cast<unsigned long long>(map_bytes), ds.mean_probes, ds.max_probes, | ||
| 2091 | pl_cfg.label, pl_cfg.memory_type_index, format_mem_flags(pl_cfg.flags).c_str(), | ||
| 2092 | sc.name, eff_groups, eff_iters, eff_reps, med_ms, min_ms, max_ms, us_per_1m, | ||
| 2093 | net_us_per_1m, glk_s); | ||
| 2094 | } | ||
| 2095 | } | ||
| 2096 | std::fflush(stdout); | ||
| 2097 | } | ||
| 2098 | } | ||
| 2099 | destroy_buf(keys_buf); | ||
| 2100 | destroy_buf(map_buf); | ||
| 2101 | } | ||
| 2102 | |||
| 2103 | if (opt.mem_filter == MemoryFilter::Rebar && !seen_rebar) { | ||
| 2104 | if (!opt.csv) { | ||
| 2105 | std::printf("unsupported: no HOST_VISIBLE | DEVICE_LOCAL (ReBAR) memory type large enough for table\n"); | ||
| 2106 | } | ||
| 2107 | destroy_buf(out_buf); | ||
| 2108 | destroy_buf(readback_buf); | ||
| 2109 | cleanup_device(); | ||
| 2110 | return kUnsupportedExit; | ||
| 2111 | } | ||
| 2112 | |||
| 2113 | destroy_buf(out_buf); | ||
| 2114 | destroy_buf(readback_buf); | ||
| 2115 | cleanup_device(); | ||
| 2116 | |||
| 2117 | if (validation && g_validation_errors > 0u) { | ||
| 2118 | std::fprintf(stderr, "%u Vulkan validation errors\n", g_validation_errors); | ||
| 2119 | return 1; | ||
| 2120 | } | ||
| 2121 | return 0; | ||
| 2122 | } | ||