c++cudathrust

error: "__forceinline__" redefined in simple program


Compiling the 3-line program test-cuda.cpp

#include <thrust/execution_policy.h>

int main() { return 0; }

results in a compiler warning/error:

$ g++ -std=c++17 test-cuda.cpp -I/opt/cuda/targets/x86_64-linux/include -Werror
In file included from /opt/cuda/targets/x86_64-linux/include/cuda_runtime_api.h:147,
                 from /opt/cuda/targets/x86_64-linux/include/cub/detail/detect_cuda_runtime.cuh:38,
                 from /opt/cuda/targets/x86_64-linux/include/cub/util_arch.cuh:41,
                 from /opt/cuda/targets/x86_64-linux/include/cub/util_debug.cuh:40,
                 from /opt/cuda/targets/x86_64-linux/include/thrust/system/cuda/config.h:43,
                 from /opt/cuda/targets/x86_64-linux/include/thrust/system/cuda/detail/execution_policy.h:35,
                 from /opt/cuda/targets/x86_64-linux/include/thrust/iterator/detail/device_system_tag.h:23,
                 from /opt/cuda/targets/x86_64-linux/include/thrust/iterator/iterator_traits.h:62,
                 from /opt/cuda/targets/x86_64-linux/include/thrust/detail/type_traits/pointer_traits.h:23,
                 from /opt/cuda/targets/x86_64-linux/include/thrust/detail/raw_pointer_cast.h:20,
                 from /opt/cuda/targets/x86_64-linux/include/thrust/detail/execute_with_allocator.h:23,
                 from /opt/cuda/targets/x86_64-linux/include/thrust/execution_policy.h:25,
                 from test-cuda.cpp:1:
/opt/cuda/targets/x86_64-linux/include/crt/host_defines.h:86: error: "__forceinline__" redefined [-Werror]
   86 | #define __forceinline__ \
      | 
In file included from /opt/cuda/targets/x86_64-linux/include/cuda/std/version:13,
                 from /opt/cuda/targets/x86_64-linux/include/cuda/std/cstddef:20,
                 from /opt/cuda/targets/x86_64-linux/include/cuda/std/type_traits:17,
                 from /opt/cuda/targets/x86_64-linux/include/thrust/detail/type_traits.h:27,
                 from /opt/cuda/targets/x86_64-linux/include/thrust/detail/execute_with_allocator_fwd.h:21,
                 from /opt/cuda/targets/x86_64-linux/include/thrust/detail/execute_with_allocator.h:21:
/opt/cuda/targets/x86_64-linux/include/cuda/std/detail/__config:38: note: this is the location of the previous definition
   38 |         #define __forceinline__
      | 
cc1plus: all warnings being treated as errors

This is a stripped down version of an issue we're facing in a larger project in which we compile w/ -Werror.

Is there something we're missing, or a workaround without having to change the CUDA/thrust source code?

Similar warning occurs compiling w/ nvcc -std=c++17 test-cuda.cpp.

OS: arch linux

Cuda: 12.1.0

GCC: 12.2.1


Solution

  • Edit: please prefer Jake's solution below. Thanks!

    Add #include <cuda_runtime.h> before including thrust headers in order to address your issue. Example:

    $ cat test.cc
    #include <cuda_runtime.h>
    #include <thrust/execution_policy.h>
    
    int main() { return 0; }
    $ g++ -std=c++17 test.cc -I/usr/local/cuda/include -Werror
    $ echo $?
    0