Skip to content

Commit ddac145

Browse files
Eyal Rozenbergeyalroz
authored andcommitted
Fixes #13, regards #12:
* 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.
1 parent 4226e22 commit ddac145

4 files changed

Lines changed: 629 additions & 12 deletions

File tree

test/CMakeLists.txt

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,61 @@
1-
cmake_minimum_required(VERSION 3.9)
1+
cmake_minimum_required(VERSION 3.20)
22

33
enable_language(CXX)
44

5+
option(BUILD_CUDA_TESTS "Include tests for use of the printf library with CUDA host-side and device-side code" OFF)
6+
57
set(test_targets autotest)
68
if (NOT ALIAS_STANDARD_FUNCTION_NAMES)
79
list(APPEND test_targets test_suite)
10+
set(targets_needing_config_h test_suite)
811
endif()
912

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

17+
if(BUILD_CUDA_TESTS)
18+
enable_language(CUDA)
19+
list(APPEND CMAKE_CUDA_FLAGS "--extended-lambda --expt-relaxed-constexpr -Xcudafe --display_error_number")
20+
cmake_policy(SET CMP0104 OLD)
21+
include(FindCUDA/select_compute_arch)
22+
list(APPEND cuda_test_targets cuda_test_suite_host cuda_test_suite_device)
23+
if((NOT DEFINED CUDA_ARCH_FLAGS) OR ("${CUDA_ARCH_FLAGS}" STREQUAL ""))
24+
cuda_select_nvcc_arch_flags(CUDA_ARCH_FLAGS_1 Auto)
25+
set(CUDA_ARCH_FLAGS ${CUDA_ARCH_FLAGS} CACHE STRING "CUDA -gencode parameters")
26+
string(REPLACE ";" " " CUDA_ARCH_FLAGS_STR "${CUDA_ARCH_FLAGS}")
27+
else()
28+
set(CUDA_ARCH_FLAGS_STR "${CUDA_ARCH_FLAGS}")
29+
endif()
30+
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} ${CUDA_ARCH_FLAGS_STR}")
31+
32+
foreach(tgt ${cuda_test_targets})
33+
string(REPLACE "cuda_" "cuda/" source_file_prefix ${tgt})
34+
add_executable(${tgt} "${source_file_prefix}.cu")
35+
target_include_directories(${tgt} PRIVATE "$<BUILD_INTERFACE:${GENERATED_INCLUDE_DIR}>")
36+
set_target_properties(
37+
${tgt}
38+
PROPERTIES
39+
CUDA_STANDARD 11
40+
CUDA_STANDARD_REQUIRED YES
41+
CUDA_EXTENSIONS NO
42+
)
43+
if (TEST_WITH_NON_STANDARD_FORMAT_STRINGS)
44+
target_compile_definitions(${tgt} PRIVATE TEST_WITH_NON_STANDARD_FORMAT_STRINGS)
45+
endif()
46+
add_test(NAME "${PROJECT_NAME}.${tgt}" COMMAND "${tgt}")
47+
endforeach()
48+
endif()
49+
50+
if(BUILD_CUDA_TESTS)
51+
if (NOT ALIAS_STANDARD_FUNCTION_NAMES)
52+
target_link_libraries(cuda_test_suite_device printf_cuda)
53+
set_target_properties(cuda_test_suite_device PROPERTIES CUDA_SEPARABLE_COMPILATION YES)
54+
list(APPEND targets_needing_config_h cuda_test_suite_host)
55+
# target_compile_options(cuda_test_suite_device PRIVATE -G -g)
56+
endif()
57+
endif()
58+
1359
foreach(tgt ${test_targets})
1460
add_executable(${tgt} "${tgt}.cpp")
1561
set_target_properties(
@@ -75,20 +121,21 @@ foreach(tgt ${test_targets})
75121
target_compile_options(${tgt} PRIVATE -ffat-lto-objects)
76122
endif()
77123
endif()
78-
79124
endforeach()
80125

81126
if (NOT ALIAS_STANDARD_FUNCTION_NAMES)
82-
# These two lines are necessary, since the test suite does not actually use the
83-
# compiled library - it includes the library's source .c file; and that means we
84-
# need to include the generated config.h file.
85-
target_compile_definitions(test_suite PRIVATE PRINTF_INCLUDE_CONFIG_H)
86-
target_include_directories(
87-
test_suite
88-
PRIVATE
89-
"${GENERATED_INCLUDE_DIR}"
90-
"$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/../src/>"
91-
)
127+
# These following is necessary, since thee targets applying printf_config.h
128+
# do not actually use the compiled library - they includes the library's source .c file;
129+
# so we need to make sure it's accessible to them
130+
foreach(tgt ${targets_needing_config_h})
131+
target_compile_definitions(test_suite PRIVATE PRINTF_INCLUDE_CONFIG_H)
132+
target_include_directories(
133+
${tgt}
134+
PRIVATE
135+
"${GENERATED_INCLUDE_DIR}"
136+
"$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/../src/>"
137+
)
138+
endforeach()
92139
add_test(
93140
NAME "${PROJECT_NAME}.test_suite"
94141
COMMAND "test_suite" # ${TEST_RUNNER_PARAMS}

0 commit comments

Comments
 (0)