Skip to content

Refactor CUDA backend: replace CudaInternal singleton with HostSharedPtr default_instance - #28

Draft
crtrott with Copilot wants to merge 2 commits into
developfrom
copilot/refactor-cuda-internal-class
Draft

Refactor CUDA backend: replace CudaInternal singleton with HostSharedPtr default_instance#28
crtrott with Copilot wants to merge 2 commits into
developfrom
copilot/refactor-cuda-internal-class

Conversation

Copilot AI commented Feb 10, 2026

Copy link
Copy Markdown

Refactors CudaInternal to match the HIP backend pattern established in kokkos#7646. Replaces the static singleton with HostSharedPtr-managed lifetime, moves init/finalize logic into constructor/destructor.

Changes

  • CudaInternal class (Kokkos_Cuda_Instance.hpp):

    • static CudaInternal& singleton()static HostSharedPtr<CudaInternal> default_instance
    • Default constructor → CudaInternal(cudaStream_t stream) (initialization in ctor)
    • finalize() cleanup logic → ~CudaInternal() (with fence before destruction)
    • Removed initialize(), finalize(), is_initialized(), was_initialized, was_finalized
    • Copy/assign marked = delete
  • Cuda execution space (Kokkos_Cuda_Instance.cpp):

    • impl_initialize(): creates default_instance via HostSharedPtr(new CudaInternal(stream), deleter)
    • impl_finalize(): default_instance = nullptr
    • Cuda(): copies default_instance (was: non-owning ptr to singleton)
    • Cuda(stream, manage_stream): new CudaInternal(stream) with conditional stream-destroying deleter
  • Kokkos_CudaSpace.cpp: CudaInternal::singleton().CudaInternal::default_instance->

Pattern (mirrors HIP)

// init
Impl::CudaInternal::default_instance = Impl::HostSharedPtr(
    new Impl::CudaInternal(stream), [](Impl::CudaInternal *ptr) {
      cudaStream_t s = ptr->m_stream;
      delete ptr;
      KOKKOS_IMPL_CUDA_SAFE_CALL(cudaStreamDestroy(s));
    });

// finalize
Impl::CudaInternal::default_instance = nullptr;

// default Cuda instance copies the shared ptr
Cuda::Cuda() : m_space_instance(Impl::CudaInternal::default_instance) {}
Original prompt

I want the CUDA backend refactored so that the CudaInternal class doesn't have the singleton member, and that the initialization and finalization happens in constructor and destructor respectively. Instead of the singleton it should use a static HostSharedPtr member in the CudaInternal class to track the default instance. This refactor should follow the refactor done for the HIP backend in the PR 7646 (kokkos#7646).


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…nstance

Move initialization from CudaInternal::initialize() to constructor,
and finalization from CudaInternal::finalize() to destructor.
Replace CudaInternal::singleton() with static HostSharedPtr<CudaInternal>
default_instance, following the HIP backend pattern from PR 7646.

Co-authored-by: crtrott <9490481+crtrott@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor CudaInternal class to remove singleton pattern Refactor CUDA backend: replace CudaInternal singleton with HostSharedPtr default_instance Feb 10, 2026
Copilot AI requested a review from crtrott February 10, 2026 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants