@Robert_Crovella I have this problem too, My main application dlopens a library which is linked with a compiled .cu file. I can not have any cuda code linked to my main application. I am using cagra from cuvs. I make several API calls that first create dev_resources and an index. Then several more API calls come in that batch load (150 chunks) data into the input for the cagra build.
The CU file has this:
struct cagra_context_s {
raft::device_resources dev_resources;
cuvs::neighbors::cagra::index_params index_params;
cuvs::neighbors::cagra::search_params search_params;
std::optional<raft::device_matrix<float, int64_t>> dataset_d;
std::optional<cuvs::neighbors::cagra::index<float, uint32_t>> index;
void allocate_dataset(int64_t num_vectors, int64_t vec_size) {
if (!dataset_d) {
dataset_d.emplace(raft::make_device_matrix<float, int64_t>(dev_resources, num_vectors, vec_size));
}
}
void set_index(cuvs::neighbors::cagra::index<float, uint32_t>&& idx) {
index.emplace(std::move(idx)); // moves into the optional
}
};
// the key is the mem_id from CUVS.cpp
std::unordered_map<unsigned long long, cagra_context_s> cagra_context;
If I manually eliminate this context before application exits the application exit is fine. But If I forget to tear down the context and just exit I get the cudaErrorCudartUnloading when atexit runs the destructor for the map.
I tried calling std:atexit in both the main and the library and also tried:
#if 0
// Doesn’t actually work for a halt
// The cudart is getting unloaded before the cagra destructors so they fail
void gpu_cleanup() noexcept {
// stop work, sync, free device buffers, clear containers, etc.
// e.g., for your map of contexts:
for (auto& kv : cagra_context) {
auto key = kv.first;
isccu_free_gpu_mem(key);
}
}
attribute((destructor)) static void lib_fini() { gpu_cleanup(); }
But not matter what I get during exit:
CUDA Error detected. cudaErrorCudartUnloading driver shutting down
app: /home/pmilosla/projects/cuda/cuvs_install/include/rmm/mr/device/cuda_memory_resource.hpp:80: virtual void rmm::mr::cuda_memory_resource::do_deallocate(void*, std::size_t, rmm::cuda_stream_view): Assertion `status__ == cudaSuccess’ failed.
Aborted (core dumped)
What’s a good approach to this? I cant put anything in main, i Can only have some library context which I’ve chose to have as a variable at the top of the file so its available to multiple library API calls
I am guessing what is happening is main app calls dlclose before it runs atexit. I am unclear what is triggering the cudart unload. I havent been able to find it with gdb breakpoints.