Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import test for CUDA project #1285

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,21 @@
# endif
# endif
#endif

// Workaround broken [[deprecated]] in the Intel compiler.
#ifdef __INTEL_COMPILER
# define FMT_DEPRECATED_ALIAS
#else
# define FMT_DEPRECATED_ALIAS FMT_DEPRECATED
#endif

// Workaround broken [[deprecated]] for the NVCC (CUDA with C++14)
#if defined(__NVCC__) || defined(__CUDACC__)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest merging this into the above #ifdef:

#ifdef __INTEL_COMPILER || defined(__NVCC__) || defined(__CUDACC__)

Copy link
Contributor Author

@luncliff luncliff Aug 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. can I wrap the Intel compiler's macro in defined? (for consistencty?)

#if defined(__INTEL_COMPILER) || defined(__NVCC__) || defined(__CUDACC__)
#  define FMT_DEPRECATED_ALIAS 
#else 
#  define FMT_DEPRECATED_ALIAS FMT_DEPRECATED 
#endif

# define FMT_DEPRECATED_ALIAS
#else
# define FMT_DEPRECATED_ALIAS FMT_DEPRECATED
#endif

#ifndef FMT_BEGIN_NAMESPACE
# if FMT_HAS_FEATURE(cxx_inline_namespaces) || FMT_GCC_VERSION >= 404 || \
FMT_MSC_VER >= 1900
Expand Down
19 changes: 19 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,22 @@ if (FMT_PEDANTIC AND NOT WIN32)
"-DPEDANTIC_COMPILE_FLAGS=${PEDANTIC_COMPILE_FLAGS}"
"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
endif ()

#
# Activate CUDA related tests if we can find CUDA from CMake. This is optional.
# For version selection, see https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#cpp14-language-features
#
find_package(CUDA 9.0)
if(CUDA_FOUND)
add_test(cuda-test ${CMAKE_CTEST_COMMAND}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we not invoke cmake recursively and add cuda_add_executable etc. here? We already found the CUDA package here and don't need to find the FMT package since we are within fmt.

Copy link
Contributor Author

@luncliff luncliff Aug 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I intended was to show the full example for importing in CUDA project. test/CMakeLists.txt is quite long so I'm afraid it may confuse future readers.
We can consider 2 alternatives.

  • include(fmt-cuda-test.cmake) + add_test: we can consider include to shorten test/CMakeLists.txt
  • add_subdirectory + add_test: I think this is the best
#
# Activate CUDA related tests if we can find CUDA from CMake. This is optional.
# For version selection, see https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#cpp14-language-features
#
find_package(CUDA 9.0)
if(CUDA_FOUND)
  add_subdirectory(cuda-test)
  add_test(NAME cuda-test COMMAND fmt-in-cuda-test)
endif()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that add_subdirectory is a better option.

-C ${CMAKE_BUILD_TYPE}
--build-and-test
"${CMAKE_CURRENT_SOURCE_DIR}/cuda-test"
"${CMAKE_CURRENT_BINARY_DIR}/cuda-test"
--build-generator ${CMAKE_GENERATOR}
--build-makeprogram ${CMAKE_MAKE_PROGRAM}
--build-options
"-DFMT_DIR=${PROJECT_BINARY_DIR}"
"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
)
endif()
112 changes: 112 additions & 0 deletions test/cuda-test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@

#
# We can find some usecases which follows the guide of CMake.
# The way replaces 'find_package(CUDA)' to 'enable_language(CUDA)'.
# And let the CMake built-in functions to use NVCC.
#
# See: https://cmake.org/cmake/help/latest/module/FindCUDA.html#replacement
#
# However, such CMake versions are pretty high (3.10 or later).
# 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 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)'
#

# Follow the Root CMakeLists.txt
cmake_minimum_required(VERSION 3.1)
project(fmt-cuda-test LANGUAGES CXX)

# 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
find_package(CUDA REQUIRED)

#
# Update these when NVCC becomes ready for C++ 17 features
# https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#cpp14-language-features
#
set(CMAKE_CUDA_STANDARD 14)
set(CMAKE_CUDA_STANDARD_REQUIRED 14)

list(APPEND CUDA_NVCC_FLAGS "-std=c++14")
if(MSVC)
# this is the solution of pytorch
# https://github.com/pytorch/pytorch/pull/7118
list(APPEND CUDA_NVCC_FLAGS "-Xcompiler" "/std:c++14")
list(APPEND CUDA_NVCC_FLAGS "-Xcompiler" "/Zc:__cplusplus")
# for the reason of this -Xcompiler options, see below ...
endif()

#
# In this test, we will assume that
# 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
#
cuda_add_executable(fmt-in-cuda-test
cuda-cpp14.cu
cpp14.cc
)

#
# https://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html
#
set_target_properties(fmt-in-cuda-test
PROPERTIES
CXX_STANDARD 14 # Notice this is for C++ code
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems redundant since you set cxx_std_14 compiler feature.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will remove the part (CXX_STANDARD)

)
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
)
message(STATUS "cuda_standard: ${cuda_standard}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Case mismatch?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that was my mistake :)


get_target_property(cuda_standard_required
fmt-in-cuda-test CUDA_STANDARD_REQUIRED
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

)
message(STATUS "cuda_standard_required: ${cuda_standard_required}")

#
# https://cmake.org/cmake/help/latest/module/FindCUDA.html
#
# From the document, you can see "The default is to use no keyword"
#
target_link_libraries(fmt-in-cuda-test
# PUBLIC
fmt::fmt
)

if(MSVC)
#
# This part is for (non-CUDA) C++ code.
# MSVC can define incorrect '__cplusplus' macro.
# Fix for the issue is to use additional compiler flag.
#
# 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
/Zc:__cplusplus
/permissive-
)
endif()
14 changes: 14 additions & 0 deletions test/cuda-test/cpp14.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

#include <fmt/core.h>

//
// 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);
}
49 changes: 49 additions & 0 deletions test/cuda-test/cuda-cpp14.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

//
// 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 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");

//
// 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 ...
//
// --- this check is moved into the <fmt/core.h> ---
//
// #if defined(__NVCC__) || defined(__CUDACC__)
// # define FMT_DEPRECATED // suppress [[deprecated]] attribute
// #endif
#include <fmt/core.h>

#include <cuda.h>
#include <iostream>

using namespace std;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Let's just qualify cout.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it :)


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;
}

auto make_message_cuda() -> std::string {
return fmt::format("nvcc compiler \t: __cplusplus == {}", __cplusplus);
}