From be279261ffbf91d945d194a9f3cd1cd07e137254 Mon Sep 17 00:00:00 2001 From: luncliff Date: Thu, 29 Aug 2019 00:24:16 +0900 Subject: [PATCH] test cuda: add more comment / macro check * checks both `__NVCC__` and `__CUDACC__` More comments for CMake and CUDA source file. --- test/cuda-test/CMakeLists.txt | 40 +++++++++++++++++++++-------------- test/cuda-test/cpp14.cc | 12 ++++++++--- test/cuda-test/cuda-cpp14.cu | 33 +++++++++++++++++++++-------- 3 files changed, 57 insertions(+), 28 deletions(-) diff --git a/test/cuda-test/CMakeLists.txt b/test/cuda-test/CMakeLists.txt index df6e9046..9873f409 100644 --- a/test/cuda-test/CMakeLists.txt +++ b/test/cuda-test/CMakeLists.txt @@ -10,20 +10,26 @@ # And we can't sure most of the CUDA projects are using those latest # because the latest C++ standard for NVCC is C++ 14 at this moment. # -# In conclusion, this test will use older version of CMake, -# and rely on 'find_package(CUDA)'. For its version, do follow the Root CMakeLists.txt. +# In conclusion, +# this test should follow the version of the Root CMakeLists.txt +# and rely on 'find_package(CUDA)'. # +# +# This part is for future update # # cmake_minimum_required(VERSION 3.10) # project(fmt-cuda-test LANGUAGES CXX CUDA) # see 'enable_language(CUDA)' -cmake_minimum_required(VERSION 3.1) # for C++ 14 +# + +# Follow the Root CMakeLists.txt +cmake_minimum_required(VERSION 3.1) project(fmt-cuda-test LANGUAGES CXX) -# find from ${PROJECT_BINARY_DIR} +# See 'test/CMakeLists.txt'. It's using ${PROJECT_BINARY_DIR} find_package(FMT REQUIRED) -# The environment variables(CUDA_BIN_PATH & CUDA_PATH) must be specified at this point +# The environment variables (CUDA_BIN_PATH & CUDA_PATH) must be specified find_package(CUDA REQUIRED) # @@ -44,12 +50,12 @@ endif() # # In this test, we will assume that -# the user is going to pre-compile CUDA source codes -# with some libraries (including this 'fmt'). +# the user is going to compile CUDA source codes with some libraries. +# Of course, it's 'fmt' in this case. # # In addition to that, -# this test will invoke both C++ Host compiler and NVCC -# by providing another (non-CUDA) C++ source code +# this test will invoke both C++ Host compiler and NVCC by providing +# another (non-CUDA) C++ source code # cuda_add_executable(fmt-in-cuda-test cuda-cpp14.cu @@ -62,12 +68,11 @@ cuda_add_executable(fmt-in-cuda-test set_target_properties(fmt-in-cuda-test PROPERTIES CXX_STANDARD 14 # Notice this is for C++ code - POSITION_INDEPENDENT_CODE ON ) -# target_compile_features(fmt-in-cuda-test -# PRIVATE -# cxx_std_14 # just make sure of the property is available -# ) +target_compile_features(fmt-in-cuda-test +PRIVATE + cxx_std_14 # just make sure of the property +) get_target_property(cuda_standard fmt-in-cuda-test CUDA_STANDARD @@ -91,10 +96,13 @@ target_link_libraries(fmt-in-cuda-test if(MSVC) # - # MSVC places incorrect '__cplusplus' macro. Follow the guideline. + # This part is for (non-CUDA) C++ code. + # MSVC can define incorrect '__cplusplus' macro. + # Fix for the issue is to use additional compiler flag. # - # https://github.com/Microsoft/vscode-cpptools/issues/2595 + # See Also: # https://devblogs.microsoft.com/cppblog/msvc-now-correctly-reports-__cplusplus/ + # https://github.com/Microsoft/vscode-cpptools/issues/2595 # target_compile_options(fmt-in-cuda-test PRIVATE diff --git a/test/cuda-test/cpp14.cc b/test/cuda-test/cpp14.cc index 3c292e21..629a0e64 100644 --- a/test/cuda-test/cpp14.cc +++ b/test/cuda-test/cpp14.cc @@ -1,8 +1,14 @@ #include -static_assert(__cplusplus >= 201402L, "expect C++ 2014"); +// +// The purpose of this part is to ensure NVCC's host compiler also supports +// the standard version. See 'cuda-cpp14.cu'. +// +// https://en.cppreference.com/w/cpp/preprocessor/replace#Predefined_macros +// +static_assert(__cplusplus >= 201402L, "expect C++ 2014 for host compiler"); -auto make_message_cpp() -> std::string{ - return fmt::format("host compiler\t: __cplusplus == {}", __cplusplus); +auto make_message_cpp() -> std::string { + return fmt::format("host compiler \t: __cplusplus == {}", __cplusplus); } diff --git a/test/cuda-test/cuda-cpp14.cu b/test/cuda-test/cuda-cpp14.cu index 11078b63..3d2aef90 100644 --- a/test/cuda-test/cuda-cpp14.cu +++ b/test/cuda-test/cuda-cpp14.cu @@ -1,16 +1,31 @@ // -// nvcc -x cu -std=c++14 ..\test\cuda-test\include_test.cu -I"../include" -Xcompiler /Zc:__cplusplus -l"fmtd" -L"../build/Debug" +// Direct NVCC command line example: +// +// nvcc.exe ./cuda-cpp14.cu -x cu -I"../include" -l"fmtd" -L"../build/Debug" \ +// -std=c++14 -Xcompiler /std:c++14 -Xcompiler /Zc:__cplusplus // // -// Ensure that we are using the expected standard +// Ensure that we are using the latest C++ standard for NVCC +// The version is C++14 +// +// https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#c-cplusplus-language-support // https://en.cppreference.com/w/cpp/preprocessor/replace#Predefined_macros // static_assert(__cplusplus >= 201402L, "expect C++ 2014 for nvcc"); -#if defined(__CUDACC__) -# define FMT_DEPRECATED +// +// https://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html#nvcc-identification-macro +// +// __NVCC__ is for NVCC compiler +// __CUDACC__ is for CUDA(.cu) source code +// +// Since we don't know the actual case in this header, checking both macro +// will prevent possible pitfalls ... +// +#if defined(__NVCC__) || defined(__CUDACC__) +# define FMT_DEPRECATED #endif #include @@ -22,11 +37,11 @@ using namespace std; extern auto make_message_cpp() -> std::string; extern auto make_message_cuda() -> std::string; -int main(int, char*[]){ - cout << make_message_cuda() << endl; - cout << make_message_cpp() << endl; +int main(int, char*[]) { + cout << make_message_cuda() << endl; + cout << make_message_cpp() << endl; } -auto make_message_cuda() -> std::string{ - return fmt::format("nvcc \t: __cplusplus == {}", __cplusplus); +auto make_message_cuda() -> std::string { + return fmt::format("nvcc compiler \t: __cplusplus == {}", __cplusplus); }