Search code examples
c++cudanvcc

Cannot call assert() from inherited class


The below fails to compile with nvcc (v6.0 though 7.5). Scientific Linux; GCC v4.4.7. What is wrong with it? Or, if nothing, is there a workaround?

#include <assert.h>

template <typename T>
struct base {};

template <typename T>
struct A : base<T>
{
    __host__ __device__ void f()
    {
        assert(0);
    }
};

int main()
{
    A<int> a;
    a.f();
    return 0;
}

This is the most minimal I was able to get this example. It appears that both base and A must class templates to hit the error, which is given below.

sam@machine:$ nvcc mwe.cu 
mwe.cu: In member function ‘void A<T>::f()’:
mwe.cu:11:66: error: expected id-expression before ‘__PRETTY_FUNCTION__’
         assert(0);

Edit: After testing on a different system with v8.0 of the CUDA toolkit (and gcc v4.9.2), it appears to compile just fine. But unfortunately upgrading to CUDA 8.0 is out of my hands.


Solution

  • What is wrong with it?

    I'm not sure there is anything wrong with it. As you've already indicated, it compiles fine under CUDA 8, so it's likely an "issue" with CUDA 7.5 etc. Updating to CUDA 8 is probably a good approach to handling it.

    Or, if nothing, is there a workaround?

    Below seems to be a workaround on CUDA 7.5, according to my test. I'm sure there are other possibilities as well.

    One CUDA 7.5 workaround possibility:

    $ cat t952.cu
    #include <assert.h>
    
    __host__ __device__
    void my_assert(bool condition){
    
      assert(condition);
    }
    
    template <typename T>
    struct base {};
    
    template <typename T>
    struct A : base<T>
    {
        __host__ __device__ void f()
        {
            my_assert(0);
        }
    };
    $ nvcc -c t952.cu
    $ nvcc --version
    nvcc: NVIDIA (R) Cuda compiler driver
    Copyright (c) 2005-2015 NVIDIA Corporation
    Built on Tue_Aug_11_14:27:32_CDT_2015
    Cuda compilation tools, release 7.5, V7.5.17
    $