Skip to content

Commit

Permalink
destroy_gpu_instance() will internally ensure that all vulkan devices…
Browse files Browse the repository at this point in the history
… are idle before proceeding with destruction.
  • Loading branch information
whyb committed Apr 7, 2024
1 parent cf2f520 commit 70f1169
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
2 changes: 1 addition & 1 deletion python/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,7 @@ PYBIND11_MODULE(ncnn, m)

#if NCNN_VULKAN
m.def("create_gpu_instance", &create_gpu_instance, py::arg("driver_path") = ((const char*)0));
m.def("destroy_gpu_instance", &destroy_gpu_instance, py::arg("need_wait") = 0);
m.def("destroy_gpu_instance", &destroy_gpu_instance);
m.def("get_gpu_count", &get_gpu_count);
m.def("get_default_gpu_index", &get_default_gpu_index);
m.def("get_gpu_info", &get_gpu_info, py::arg("device_index") = 0, py::return_value_policy::reference);
Expand Down
17 changes: 7 additions & 10 deletions src/gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2084,25 +2084,22 @@ VkInstance get_gpu_instance()
return (VkInstance)g_instance;
}

void destroy_gpu_instance(int need_wait)
void destroy_gpu_instance()
{
MutexLockGuard lock(g_instance_lock);

if (g_instance.created == 0)
return;

if (need_wait)
for (int i = 0; i < NCNN_MAX_GPU_COUNT; i++)
{
for (int i = 0; i < NCNN_MAX_GPU_COUNT; i++)
VulkanDevice* vulkan_device = g_default_vkdev[i];
if (vulkan_device)
{
VulkanDevice* vulkan_device = g_default_vkdev[i];
if (vulkan_device)
VkDevice vkdev = g_default_vkdev[i]->vkdevice();
if (vkdev)
{
VkDevice vkdev = g_default_vkdev[i]->vkdevice();
if (vkdev)
{
vkDeviceWaitIdle(vkdev);
}
vkDeviceWaitIdle(vkdev);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ NCNN_EXPORT VkInstance get_gpu_instance();

// Destroy VkInstance object and free the memory of the associated object
// Usually called in the destructor of the main program exit
// If need_wait != 0, it will wait for all devices to be idle before destroy
NCNN_EXPORT void destroy_gpu_instance(int need_wait = 0);
// The function will internally ensure that all vulkan devices are idle before proceeding with destruction.
NCNN_EXPORT void destroy_gpu_instance();

// vulkan core
extern PFN_vkAllocateCommandBuffers vkAllocateCommandBuffers;
Expand Down

0 comments on commit 70f1169

Please sign in to comment.