Use mutex instead of atomics for vk_instance counters

This commit is contained in:
0cc4m
2024-12-30 05:14:58 +00:00
parent d9b0958f59
commit aa014d7e89

View File

@ -23,7 +23,6 @@
#include <mutex> #include <mutex>
#include <future> #include <future>
#include <thread> #include <thread>
#include <atomic>
#include "ggml-impl.h" #include "ggml-impl.h"
#include "ggml-backend-impl.h" #include "ggml-backend-impl.h"
@ -61,8 +60,9 @@
#define VK_LOG_DEBUG(msg) ((void) 0) #define VK_LOG_DEBUG(msg) ((void) 0)
#endif // GGML_VULKAN_DEBUG #endif // GGML_VULKAN_DEBUG
static std::atomic_uint64_t vk_instance_contexts = 0; static std::mutex vk_instance_counter_mutex;
static std::atomic_uint64_t vk_instance_buffers = 0; static uint64_t vk_instance_contexts = 0;
static uint64_t vk_instance_buffers = 0;
struct ggml_backend_vk_context; struct ggml_backend_vk_context;
@ -7358,6 +7358,7 @@ static void ggml_backend_vk_buffer_free_buffer(ggml_backend_buffer_t buffer) {
ggml_vk_destroy_buffer(ctx->dev_buffer); ggml_vk_destroy_buffer(ctx->dev_buffer);
delete ctx; delete ctx;
std::lock_guard guard(vk_instance_counter_mutex);
vk_instance_buffers--; vk_instance_buffers--;
if (vk_instance_contexts == 0 && vk_instance_buffers == 0) { if (vk_instance_contexts == 0 && vk_instance_buffers == 0) {
@ -7450,7 +7451,10 @@ static ggml_backend_buffer_t ggml_backend_vk_buffer_type_alloc_buffer(ggml_backe
return nullptr; return nullptr;
} }
vk_instance_buffers++; {
std::lock_guard guard(vk_instance_counter_mutex);
vk_instance_buffers++;
}
ggml_backend_vk_buffer_context * bufctx = new ggml_backend_vk_buffer_context(device, std::move(dev_buffer), ctx->name); ggml_backend_vk_buffer_context * bufctx = new ggml_backend_vk_buffer_context(device, std::move(dev_buffer), ctx->name);
@ -7572,6 +7576,7 @@ static void ggml_backend_vk_free(ggml_backend_t backend) {
delete ctx; delete ctx;
delete backend; delete backend;
std::lock_guard guard(vk_instance_counter_mutex);
vk_instance_contexts--; vk_instance_contexts--;
if (vk_instance_contexts == 0 && vk_instance_buffers == 0) { if (vk_instance_contexts == 0 && vk_instance_buffers == 0) {
@ -7804,6 +7809,7 @@ ggml_backend_t ggml_backend_vk_init(size_t dev_num) {
/* .context = */ ctx, /* .context = */ ctx,
}; };
std::lock_guard guard(vk_instance_counter_mutex);
vk_instance_contexts++; vk_instance_contexts++;
return vk_backend; return vk_backend;