1

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

5
  • According to lines 38-40 in this link, that should not be happening. Commented Apr 6, 2023 at 22:14
  • 3
    I don't have any trouble with nvcc when compiling it as a .cu file. That is my usual suggestion with thrust. Commented Apr 6, 2023 at 23:05
  • Don’t use g++ to compile thrust code. Use nvcc Commented Apr 7, 2023 at 2:26
  • @PaulSanders That's where it is first defined. The problem is that host_defines.h unconditionally #defines __forceinline__ which conflicts with the prior #define in your link. Thank you Robert that does work. Seems we will need to find a way to name our files in such a way that builds on both gcc and nvcc. Commented Apr 7, 2023 at 12:48
  • @Matt You can use the -x option with most compilers to explicitly set the language (e.g. nvcc -x cu test.cpp or gcc -x cpp test.cu). I believe this can also be controlled using target / source-file properties in CMake if you're using that. Commented Jun 7, 2023 at 15:46

2 Answers 2

4

Anis' answer is a good option for a short-term workaround.

The other option would be to do #undef __forceinline__ between the includes:

    #include <thrust/execution_policy.h>
    #undef __forceinline__
    #include <cuda_runtime.h>
    
    int main() { return 0; }

Also note that the root cause of this issue has already been address in libcudacxx here: https://github.com/NVIDIA/libcudacxx/pull/476

This will ship with the next 2.2 release of Thrust/libcudacxx.

Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't seem to help w/ Cuda 12.1. The problem is that thrust/execution_policy.h alone includes conflicting #define __forceinline__ lines as shown in the question.
3

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

1 Comment

Sorry to disagree but yours is still the better answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.