Skip to content

Commit

Permalink
fix nvidia vulkan crash on exit (#5234)
Browse files Browse the repository at this point in the history
  • Loading branch information
nihui authored Dec 20, 2023
1 parent 8c4fc5e commit ded0b78
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 21 deletions.
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 @@ -2039,6 +2040,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

0 comments on commit ded0b78

Please sign in to comment.