Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix nvidia vulkan crash on exit #5234

Merged
merged 4 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion benchmark/benchncnn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,6 @@ int main(int argc, char** argv)
#if NCNN_VULKAN
delete g_blob_vkallocator;
delete g_staging_vkallocator;
ncnn::destroy_gpu_instance();
#endif // NCNN_VULKAN

return 0;
Expand Down
12 changes: 12 additions & 0 deletions src/gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#if NCNN_VULKAN

#include <stdlib.h>
#include <string.h>

#include "glslang/SPIRV/GlslangToSpv.h"
Expand Down Expand Up @@ -2025,6 +2026,17 @@ int create_gpu_instance(const char* driver_path)

glslang::InitializeProcess();

// the global __ncnn_vulkan_instance_holder destructor will call destroy_gpu_instance() on exit
// but it seems to be too late for nvidia driver :(
// driver's internal data structure has been destroyed when called, causing segfault
// atexit() seems to be helpful for calling it earlier --- nihui
static int destroy_gpu_instance_atexit_registered = 0;
if (!destroy_gpu_instance_atexit_registered)
{
atexit(destroy_gpu_instance);
destroy_gpu_instance_atexit_registered = 1;
}

return 0;
}

Expand Down
21 changes: 1 addition & 20 deletions tests/testutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,9 @@
#endif // NCNN_VULKAN

static struct prng_rand_t g_prng_rand_state;
#if NCNN_VULKAN
class GlobalGpuInstance
{
public:
GlobalGpuInstance()
{
ncnn::create_gpu_instance();
}
~GlobalGpuInstance()
{
ncnn::destroy_gpu_instance();
}
};
// HACK workaround nvidia driver crash on exit
#define SRAND(seed) \
GlobalGpuInstance __ncnn_gpu_instance_guard; \
prng_srand(seed, &g_prng_rand_state)
#define RAND() prng_rand(&g_prng_rand_state)
#else // NCNN_VULKAN

#define SRAND(seed) prng_srand(seed, &g_prng_rand_state)
#define RAND() prng_rand(&g_prng_rand_state)
#endif // NCNN_VULKAN

#define TEST_LAYER_DISABLE_AUTO_INPUT_PACKING (1 << 0)
#define TEST_LAYER_DISABLE_AUTO_INPUT_CASTING (1 << 1)
Expand Down
Loading