Destructors in derived classes

We have C++ code that needs to run in both a CPU-only environment (e.g., GCC 13.x) and in a GPU-enabled one via CUDA (12.x).

What is the proper way to declare the destructors in base and in derived classes ?

Is this base and derived class destructor declaration below supported or will it lead to compilation or run-time errors?

class Base
public:
  ...
    Base() = default;
    __host__ __device__ virtual ~Base() {} 
  ...
};

class Derived : public Base {
public:
  ...
   __host__  __device__ virtual  ~Derived() override { }
  ...
};
  

Can you suggest the proper way to declare derived class destructors in the case where we have to use these classes from either host-only or GPU code?

Thanks!

You cannot use the code you have shown directly with GCC 13.x as the (only) compiler. Keywords like __host__ will trigger syntax errors.

CUDA doesn’t provide a native method to handle this; the solutions I have seen vary. One typical one I have seen is to provide a preamble to your code like:

#ifndef __CUDACC__
#define __host__ 
#define __device__
#endif

I’m not aware of anything that would be needed (differently) specific to host or derived classes, or constructors or destructors, specifically. Perhaps someone else will correct me.

godbolt c++ example
godbolt cuda example

Note that in CUDA, it is illegal to pass class instances containing virtual functions between host and device code. Furthermore, attention would need to be given to proper usage of file name extensions, e.g. .cpp vs. .cu. A typical solution here might be to name everything with .cpp and then pass -x cu as needed when compiling with nvcc.

The above also assumes your function definitions will be “ordinary” C++ code.

If you need to use anything CUDA-specific, you will also need to segregate your handling between CPU-only (cannot use CUDA-specific intrinsics or syntax) and GPU-enabled (can use CUDA-specific intrinsics/syntax). The canonical method is to use another nvcc compiler macro. You can find various examples of such, like here.

__host__ __device__ void foo(){
  #ifdef __CUDA_ARCH__
    // GPU-enabled code variant - GPU device code path here
  #else
    // CPU-only code variant
  #endif
  }

For the more general question of how do I make my code usable in either a GCC environment or a nvcc environment, there may be other issues to address. For example host code that calls a CUDA kernel simply will not work in a GCC-only, CPU-only environment. An alternate path would have to be provided for that sort of situation, as well.

As an aside: Polymorphic classes /objects with virtual members typically store a vtable pointer per object. Accessing it could be an unwanted slow-down in Cuda code. But perhaps your application needs it.

Thanks Robert,
The __host__ __device__ are conditionally inserted when we are compiling with CUDA. For host-only compiles these are invisible.

I read that for non-device code, polymorphism is fully supported. For device code, both base and derived destructors need to be = default but that __device__ virtual ~base {} ... __device__ virtual ~Derived() override {} shouldn’t work (Derived having Base as base class).

I am trying to understand what are the proper ways to declare derived destructors and avoid pitfalls in CUDA.