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 Feb 28, 2022
1 parent 2d39f1a commit 244deaa
Show file tree
Hide file tree
Showing 4 changed files with 624 additions and 12 deletions.
72 changes: 60 additions & 12 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,62 @@
cmake_minimum_required(VERSION 3.9)
cmake_minimum_required(VERSION 3.20)

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 autotest)
if (NOT ALIAS_STANDARD_FUNCTION_NAMES)
list(APPEND test_targets test_suite)
set(targets_needing_config_h test_suite)
endif()


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)
if (NOT ALIAS_STANDARD_FUNCTION_NAMES)
target_link_libraries(cuda_test_suite_device printf_cuda)
set_target_properties(cuda_test_suite_device PROPERTIES CUDA_SEPARABLE_COMPILATION YES)
list(APPEND targets_needing_config_h cuda_test_suite_host)
# target_compile_options(cuda_test_suite_device PRIVATE -G -g)
endif()
endif()

foreach(tgt ${test_targets})
add_executable(${tgt} "${tgt}.cpp")
set_target_properties(
Expand Down Expand Up @@ -75,20 +122,21 @@ foreach(tgt ${test_targets})
target_compile_options(${tgt} PRIVATE -ffat-lto-objects)
endif()
endif()

endforeach()

if (NOT ALIAS_STANDARD_FUNCTION_NAMES)
# 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}"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/../src/>"
)
# These following is necessary, since thee targets applying printf_config.h
# do not actually use the compiled library - they includes the library's source .c file;
# so we need to make sure it's accessible to them
foreach(tgt ${targets_needing_config_h})
target_compile_definitions(test_suite PRIVATE PRINTF_INCLUDE_CONFIG_H)
target_include_directories(
${tgt}
PRIVATE
"${GENERATED_INCLUDE_DIR}"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/../src/>"
)
endforeach()
add_test(
NAME "${PROJECT_NAME}.test_suite"
COMMAND "test_suite" # ${TEST_RUNNER_PARAMS}
Expand Down
Loading

0 comments on commit 244deaa

Please sign in to comment.