vulkan: reduce host memory lock contention (#23376)

* vulkan: reduces lock contention

* replace unique_lock with lock_guard
This commit is contained in:
Winston Ma
2026-06-01 14:03:32 +02:00
committed by GitHub
parent 5aba5364d9
commit bef69f1306
+5 -3
View File
@@ -62,6 +62,7 @@ typedef struct VkPhysicalDeviceCooperativeMatrixDecodeVectorFeaturesNV {
#include <map> #include <map>
#include <set> #include <set>
#include <unordered_map> #include <unordered_map>
#include <shared_mutex>
#include <mutex> #include <mutex>
#include <future> #include <future>
#include <thread> #include <thread>
@@ -618,6 +619,7 @@ static constexpr std::initializer_list<std::array<int, 3>> rms_norm_mul_rope_vie
struct vk_device_struct { struct vk_device_struct {
std::recursive_mutex mutex; std::recursive_mutex mutex;
mutable std::shared_mutex pinned_memory_mutex;
vk::PhysicalDevice physical_device; vk::PhysicalDevice physical_device;
vk::PhysicalDeviceProperties properties; vk::PhysicalDeviceProperties properties;
@@ -7010,7 +7012,7 @@ static void * ggml_vk_host_malloc(vk_device& device, size_t size) {
return nullptr; return nullptr;
} }
std::lock_guard<std::recursive_mutex> guard(device->mutex); std::lock_guard<std::shared_mutex> guard(device->pinned_memory_mutex);
device->pinned_memory.push_back(std::make_tuple(buf->ptr, size, buf)); device->pinned_memory.push_back(std::make_tuple(buf->ptr, size, buf));
return buf->ptr; return buf->ptr;
@@ -7021,7 +7023,7 @@ static void ggml_vk_host_free(vk_device& device, void* ptr) {
return; return;
} }
VK_LOG_MEMORY("ggml_vk_host_free(" << ptr << ")"); VK_LOG_MEMORY("ggml_vk_host_free(" << ptr << ")");
std::lock_guard<std::recursive_mutex> guard(device->mutex); std::lock_guard<std::shared_mutex> guard(device->pinned_memory_mutex);
vk_buffer buf; vk_buffer buf;
size_t index; size_t index;
@@ -7045,7 +7047,7 @@ static void ggml_vk_host_free(vk_device& device, void* ptr) {
} }
static void ggml_vk_host_get(const vk_device& device, const void * ptr, vk_buffer& buf, size_t& buf_offset) { static void ggml_vk_host_get(const vk_device& device, const void * ptr, vk_buffer& buf, size_t& buf_offset) {
std::lock_guard<std::recursive_mutex> guard(device->mutex); std::shared_lock<std::shared_mutex> guard(device->pinned_memory_mutex);
buf = nullptr; buf = nullptr;
buf_offset = 0; buf_offset = 0;
for (size_t i = 0; i < device->pinned_memory.size(); i++) { for (size_t i = 0; i < device->pinned_memory.size(); i++) {