Skip to content

Commit

Permalink
Fixes #13, regards #12:
Browse files Browse the repository at this point in the history
* Updated the compiler directives for suppressing relevant warnings to also properly work with NVCC.
* Test suite "replicated" for CUDA host-side compilation (by including the `.cpp` file from the `.cu` file)
* Test suite adapted for CUDA device-side testing - using some invocation wrappering machinery. It's a bit crude but it does the job.
* `CMakeLists.txt` for the tests reworked to support CUDA targets, architecture auto-detection, etc.

Caveat: At the moment, CUDA testing is only performed using single-block, single-thread grids.
  • Loading branch information
Eyal Rozenberg authored and eyalroz committed Oct 25, 2021
1 parent fedd3ee commit 811286a
Show file tree
Hide file tree
Showing 4 changed files with 1,254 additions and 11 deletions.
60 changes: 52 additions & 8 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,56 @@
cmake_minimum_required(VERSION 3.9)
cmake_minimum_required(VERSION 3.18)

enable_language(CXX)

option(BUILD_CUDA_TESTS "Include tests for use of the printf library with CUDA host-side and device-side code" OFF)

set(test_targets test_suite autotest)

set(targets_needing_config_h test_suite)

option(TEST_WITH_NON_STANDARD_FORMAT_STRINGS "Include tests using non-standard-compliant format strings?" ON)
# ... don't worry, we'll suppress the compiler warnings for those.

if(BUILD_CUDA_TESTS)
enable_language(CUDA)
list(APPEND CMAKE_CUDA_FLAGS "--extended-lambda --expt-relaxed-constexpr -Xcudafe --display_error_number")
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.18)
cmake_policy(SET CMP0104 OLD)
endif()
include(FindCUDA/select_compute_arch)
list(APPEND cuda_test_targets cuda_test_suite_host cuda_test_suite_device)
if((NOT DEFINED CUDA_ARCH_FLAGS) OR ("${CUDA_ARCH_FLAGS}" STREQUAL ""))
cuda_select_nvcc_arch_flags(CUDA_ARCH_FLAGS_1 Auto)
set(CUDA_ARCH_FLAGS ${CUDA_ARCH_FLAGS} CACHE STRING "CUDA -gencode parameters")
string(REPLACE ";" " " CUDA_ARCH_FLAGS_STR "${CUDA_ARCH_FLAGS}")
else()
set(CUDA_ARCH_FLAGS_STR "${CUDA_ARCH_FLAGS}")
endif()
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} ${CUDA_ARCH_FLAGS_STR}")

foreach(tgt ${cuda_test_targets})
string(REPLACE "cuda_" "cuda/" source_file_prefix ${tgt})
add_executable(${tgt} "${source_file_prefix}.cu")
target_include_directories(${tgt} PRIVATE "$<BUILD_INTERFACE:${GENERATED_INCLUDE_DIR}>")
set_target_properties(
${tgt}
PROPERTIES
CUDA_STANDARD 11
CUDA_STANDARD_REQUIRED YES
CUDA_EXTENSIONS NO
)
if (TEST_WITH_NON_STANDARD_FORMAT_STRINGS)
target_compile_definitions(${tgt} PRIVATE TEST_WITH_NON_STANDARD_FORMAT_STRINGS)
endif()
endforeach()
endif()

if(BUILD_CUDA_TESTS)
target_link_libraries(cuda_test_suite_device cuda_printf)
set_target_properties(cuda_test_suite_device PROPERTIES CUDA_SEPARABLE_COMPILATION YES)
list(APPEND targets_needing_config_h cuda_test_suite_host)
endif()

foreach(tgt ${test_targets})
add_executable(${tgt} "${tgt}.cpp")
set_target_properties(
Expand All @@ -17,10 +61,9 @@ foreach(tgt ${test_targets})
CXX_EXTENSIONS NO
)

if (TEST_WITH_NON_STANDARD_FORMAT_STRINGS)
target_compile_definitions(${tgt} PRIVATE TEST_WITH_NON_STANDARD_FORMAT_STRINGS)
endif()

if (TEST_WITH_NON_STANDARD_FORMAT_STRINGS)
target_compile_definitions(${tgt} PRIVATE TEST_WITH_NON_STANDARD_FORMAT_STRINGS)
endif()

if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
target_compile_options(test_suite PRIVATE /W4)
Expand Down Expand Up @@ -54,14 +97,15 @@ foreach(tgt ${test_targets})
target_compile_options(${tgt} PRIVATE -ffat-lto-objects)
endif()
endif()

endforeach()

# These two lines are necessary, since the test suite does not actually use the
# compiled library - it includes the library's source .c file; and that means we
# need to include the generated config.h file.
target_compile_definitions(test_suite PRIVATE PRINTF_INCLUDE_CONFIG_H)
target_include_directories(test_suite PRIVATE "${GENERATED_INCLUDE_DIR}")
foreach(tgt ${targets_needing_config_h})
target_compile_definitions(${tgt} PRIVATE PRINTF_INCLUDE_CONFIG_H)
target_include_directories(${tgt} PRIVATE "${GENERATED_INCLUDE_DIR}")
endforeach()
add_test(
NAME "${PROJECT_NAME}.test_suite"
COMMAND "test_suite" # ${TEST_RUNNER_PARAMS}
Expand Down
Loading

0 comments on commit 811286a

Please sign in to comment.