SYCL NBNXM offload support
[alexxy/gromacs.git] / src / testutils / TestMacros.cmake
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2011,2012,2013,2014,2015 by the GROMACS development team.
5 # Copyright (c) 2016,2017,2018,2019,2020 by the GROMACS development team.
6 # Copyright (c) 2021, by the GROMACS development team, led by
7 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 # and including many others, as listed in the AUTHORS file in the
9 # top-level source directory and at http://www.gromacs.org.
10 #
11 # GROMACS is free software; you can redistribute it and/or
12 # modify it under the terms of the GNU Lesser General Public License
13 # as published by the Free Software Foundation; either version 2.1
14 # of the License, or (at your option) any later version.
15 #
16 # GROMACS is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 # Lesser General Public License for more details.
20 #
21 # You should have received a copy of the GNU Lesser General Public
22 # License along with GROMACS; if not, see
23 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25 #
26 # If you want to redistribute modifications to GROMACS, please
27 # consider that scientific software is very special. Version
28 # control is crucial - bugs must be traceable. We will be happy to
29 # consider code for inclusion in the official distribution, but
30 # derived work must not be called official GROMACS. Details are found
31 # in the README & COPYING files - if they are missing, get the
32 # official version at http://www.gromacs.org.
33 #
34 # To help us fund GROMACS development, we humbly ask that you cite
35 # the research papers on the package. Check out http://www.gromacs.org.
36
37 include(CMakeParseArguments)
38 include(gmxClangCudaUtils)
39
40 function (gmx_add_unit_test_library NAME)
41     if (GMX_BUILD_UNITTESTS AND BUILD_TESTING)
42         add_library(${NAME} STATIC ${UNITTEST_TARGET_OPTIONS} ${ARGN})
43         gmx_target_compile_options(${NAME})
44         target_compile_definitions(${NAME} PRIVATE HAVE_CONFIG_H)
45         target_include_directories(${NAME} SYSTEM BEFORE PRIVATE ${PROJECT_SOURCE_DIR}/src/external/thread_mpi/include)
46         target_link_libraries(${NAME} PRIVATE testutils gmock)
47         if(GMX_CLANG_TIDY)
48             set_target_properties(${NAME} PROPERTIES CXX_CLANG_TIDY
49                 "${CLANG_TIDY_EXE};-warnings-as-errors=*;-header-filter=.*")
50         endif()
51         if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER "7")
52             target_compile_options(${NAME} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-Weverything ${IGNORED_CLANG_ALL_WARNINGS} -Wno-gnu-zero-variadic-macro-arguments -Wno-zero-as-null-pointer-constant -Wno-missing-variable-declarations>)
53         endif()
54
55     endif()
56 endfunction ()
57
58 # This function creates a GoogleTest test executable for a module.  It
59 # hides all the complexity of how to treat different source files
60 # under different configuration conditions. It should be extended
61 # if we ever support another GPU compilation approach.
62 #
63 # It can be called with extra options and arguments:
64 #   MPI
65 #     To trigger the ctest runner to run this test with multiple ranks
66 #   HARDWARE_DETECTION
67 #     To trigger the test executable setup code to run hardware detection
68 #   CPP_SOURCE_FILES          file1.cpp file2.cpp ...
69 #     All the normal C++ .cpp source files
70 #   GPU_CPP_SOURCE_FILES  file1.cpp file2.cpp ...
71 #     All the C++ .cpp source files that are always needed, but must be
72 #     compiled in the way that suits GMX_GPU.
73 #   CUDA_CU_SOURCE_FILES      file1.cu  file2.cu  ...
74 #     All the normal CUDA .cu source files
75 #   OPENCL_CPP_SOURCE_FILES   file1.cpp file2.cpp ...
76 #     All the other C++ .cpp source files needed only with OpenCL
77 #   SYCL_CPP_SOURCE_FILES   file1.cpp file2.cpp ...
78 #     All the C++ .cpp source files needed only with SYCL
79 #   NON_GPU_CPP_SOURCE_FILES  file1.cpp file2.cpp ...
80 #     All the other C++ .cpp source files needed only with neither OpenCL nor CUDA nor SYCL
81 function (gmx_add_gtest_executable EXENAME)
82     if (GMX_BUILD_UNITTESTS AND BUILD_TESTING)
83         set(_options MPI HARDWARE_DETECTION)
84         set(_multi_value_keywords
85             CPP_SOURCE_FILES
86             CUDA_CU_SOURCE_FILES
87             GPU_CPP_SOURCE_FILES
88             OPENCL_CPP_SOURCE_FILES
89             SYCL_CPP_SOURCE_FILES
90             NON_GPU_CPP_SOURCE_FILES
91             )
92         cmake_parse_arguments(ARG "${_options}" "" "${_multi_value_keywords}" ${ARGN})
93
94         file(RELATIVE_PATH _input_files_path ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
95         set(_temporary_files_path "${CMAKE_CURRENT_BINARY_DIR}/Testing/Temporary")
96         file(MAKE_DIRECTORY ${_temporary_files_path})
97         # Note that the quotation marks in the next line form part of
98         # the defined symbol, so that the macro replacement in the
99         # source file is as a string.
100         # These are only needed for unittest_main.cpp, but for simplicity used
101         # for the whole target (since there may be multiple executables in the
102         # same directory, it is not straightforward to use a source file
103         # property).
104         set(EXTRA_COMPILE_DEFINITIONS
105             TEST_DATA_PATH="${_input_files_path}"
106             TEST_TEMP_PATH="${_temporary_files_path}")
107         if (ARG_MPI)
108             list(APPEND EXTRA_COMPILE_DEFINITIONS
109                  TEST_USES_MPI=true)
110         endif()
111         if (ARG_HARDWARE_DETECTION)
112             list(APPEND EXTRA_COMPILE_DEFINITIONS
113                  TEST_USES_HARDWARE_DETECTION=true)
114         endif()
115
116         if (GMX_GPU_CUDA AND NOT GMX_CLANG_CUDA)
117             # Work around FindCUDA that prevents using target_link_libraries()
118             # with keywords otherwise...
119             set(CUDA_LIBRARIES PRIVATE ${CUDA_LIBRARIES})
120             cuda_add_executable(${EXENAME} ${UNITTEST_TARGET_OPTIONS}
121                 ${ARG_CPP_SOURCE_FILES}
122                 ${ARG_CUDA_CU_SOURCE_FILES}
123                 ${ARG_GPU_CPP_SOURCE_FILES})
124         else()
125             add_executable(${EXENAME} ${UNITTEST_TARGET_OPTIONS}
126                 ${ARG_CPP_SOURCE_FILES})
127         endif()
128
129         if (GMX_GPU_CUDA)
130             if (GMX_CLANG_CUDA)
131                 target_sources(${EXENAME} PRIVATE
132                     ${ARG_CUDA_CU_SOURCE_FILES}
133                     ${ARG_GPU_CPP_SOURCE_FILES})
134                 set_source_files_properties(${ARG_GPU_CPP_SOURCE_FILES} PROPERTIES CUDA_SOURCE_PROPERTY_FORMAT OBJ)
135                 gmx_compile_cuda_file_with_clang(${ARG_CUDA_CU_SOURCE_FILES})
136                 gmx_compile_cuda_file_with_clang(${ARG_GPU_CPP_SOURCE_FILES})
137                 if(ARG_CUDA_CU_SOURCE_FILES OR ARG_GPU_CPP_SOURCE_FILES)
138                     target_link_libraries(${EXENAME} PRIVATE ${GMX_EXTRA_LIBRARIES})
139                 endif()
140             endif()
141         elseif (GMX_GPU_OPENCL)
142             target_sources(${EXENAME} PRIVATE ${ARG_OPENCL_CPP_SOURCE_FILES} ${ARG_GPU_CPP_SOURCE_FILES})
143             if(ARG_OPENCL_CPP_SOURCE_FILES OR ARG_GPU_CPP_SOURCE_FILES)
144                 target_link_libraries(${EXENAME} PRIVATE ${OpenCL_LIBRARIES})
145             endif()
146         elseif (GMX_GPU_SYCL)
147             target_sources(${EXENAME} PRIVATE ${ARG_SYCL_CPP_SOURCE_FILES} ${ARG_GPU_CPP_SOURCE_FILES})
148             if(ARG_SYCL_CPP_SOURCE_FILES OR ARG_GPU_CPP_SOURCE_FILES)
149                 add_sycl_to_target(
150                     TARGET ${EXENAME}
151                     SOURCES ${ARG_SYCL_CPP_SOURCE_FILES} ${ARG_GPU_CPP_SOURCE_FILES}
152                     )
153             endif()
154         else()
155             target_sources(${EXENAME} PRIVATE ${ARG_NON_GPU_CPP_SOURCE_FILES} ${ARG_GPU_CPP_SOURCE_FILES})
156         endif()
157
158         gmx_target_compile_options(${EXENAME})
159         target_compile_definitions(${EXENAME} PRIVATE HAVE_CONFIG_H ${EXTRA_COMPILE_DEFINITIONS})
160         target_include_directories(${EXENAME} SYSTEM BEFORE PRIVATE ${PROJECT_SOURCE_DIR}/src/external/thread_mpi/include)
161         # Permit GROMACS code to include externally developed headers,
162         # such as the functionality from the nonstd project that we
163         # use for gmx::compat::optional. These are included as system
164         # headers so that no warnings are issued from them.
165         target_include_directories(${EXENAME} SYSTEM PRIVATE ${PROJECT_SOURCE_DIR}/src/external)
166         if(CYGWIN)
167             # Ensure GoogleTest headers can find POSIX things needed
168             target_compile_definitions(${EXENAME} PRIVATE _POSIX_C_SOURCE=200809L)
169         endif()
170
171         target_link_libraries(${EXENAME} PRIVATE
172             testutils common libgromacs gmock
173             ${GMX_COMMON_LIBRARIES} ${GMX_EXE_LINKER_FLAGS})
174
175         if(GMX_CLANG_TIDY)
176             set_target_properties(${EXENAME} PROPERTIES CXX_CLANG_TIDY
177                 "${CLANG_TIDY_EXE};-warnings-as-errors=*;-header-filter=.*")
178         endif()
179         if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER "7")
180             target_compile_options(${EXENAME} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-Weverything ${IGNORED_CLANG_ALL_WARNINGS} -Wno-gnu-zero-variadic-macro-arguments -Wno-zero-as-null-pointer-constant -Wno-missing-variable-declarations>)
181         endif()
182         # clang-3.6 warns about a number of issues that are not reported by more modern compilers
183         # and we know they are not real issues. So we only check that it can compile without error
184         # but ignore all warnings.
185         if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION MATCHES "^3\.6")
186             target_compile_options(${EXENAME} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-w>)
187         endif()
188     endif()
189 endfunction()
190
191 # This function can be called with extra options and arguments:
192 #   OPENMP_THREADS <N>    declares the requirement to run the test binary with N OpenMP
193 #                           threads (when supported by the build configuration)
194 #   MPI_RANKS <N>         declares the requirement to run the test binary with N ranks
195 #   INTEGRATION_TEST      requires the use of the IntegrationTest label in CTest
196 #   SLOW_TEST             requires the use of the SlowTest label in CTest, and
197 #                         increase the length of the ctest timeout.
198 #   IGNORE_LEAKS          Skip some memory safety checks.
199 #
200 # TODO When a test case needs it, generalize the MPI_RANKS mechanism so
201 # that ctest can run the test binary over a range of numbers of MPI
202 # ranks.
203 function (gmx_register_gtest_test NAME EXENAME)
204     if (GMX_BUILD_UNITTESTS AND BUILD_TESTING)
205         set(_options INTEGRATION_TEST SLOW_TEST IGNORE_LEAKS)
206         set(_one_value_args MPI_RANKS OPENMP_THREADS)
207         cmake_parse_arguments(ARG "${_options}" "${_one_value_args}" "" ${ARGN})
208         set(_xml_path ${CMAKE_BINARY_DIR}/Testing/Temporary/${NAME}.xml)
209         set(_labels GTest)
210         set(_timeout 30)
211         if (ARG_INTEGRATION_TEST)
212             list(APPEND _labels IntegrationTest)
213             # Slow build configurations should have longer timeouts.
214             # Both OpenCL (from JIT) and ThreadSanitizer (from how it
215             # checks) can take signficantly more time than other
216             # configurations.
217             if (GMX_GPU_OPENCL OR GMX_GPU_SYCL)
218                 set(_timeout 240)
219             elseif (${CMAKE_BUILD_TYPE} STREQUAL TSAN)
220                 set(_timeout 300)
221             else()
222                 set(_timeout 120)
223             endif()
224         elseif (ARG_SLOW_TEST)
225             list(APPEND _labels SlowTest)
226             set(_timeout 480)
227         else()
228             list(APPEND _labels UnitTest)
229             gmx_get_test_prefix_cmd(_prefix_cmd)
230         endif()
231         if (ARG_IGNORE_LEAKS)
232             gmx_get_test_prefix_cmd(_prefix_cmd IGNORE_LEAKS)
233         endif ()
234         set(_cmd ${_prefix_cmd} $<TARGET_FILE:${EXENAME}>)
235         if (ARG_OPENMP_THREADS)
236             if (GMX_OPENMP)
237                 list(APPEND _cmd -ntomp ${ARG_OPENMP_THREADS})
238             endif()
239         endif()
240         if (ARG_MPI_RANKS)
241             if (NOT GMX_CAN_RUN_MPI_TESTS)
242                 return()
243             endif()
244             list(APPEND _labels MpiTest)
245             if (GMX_MPI)
246                 set(_cmd
247                     ${MPIEXEC} ${MPIEXEC_NUMPROC_FLAG} ${ARG_MPI_RANKS}
248                     ${MPIEXEC_PREFLAGS} ${_cmd} ${MPIEXEC_POSTFLAGS})
249             elseif (GMX_THREAD_MPI)
250                 list(APPEND _cmd -ntmpi ${ARG_MPI_RANKS})
251             endif()
252         endif()
253         add_test(NAME ${NAME}
254                  COMMAND ${_cmd} --gtest_output=xml:${_xml_path})
255         set_tests_properties(${NAME} PROPERTIES LABELS "${_labels}")
256         set_tests_properties(${NAME} PROPERTIES TIMEOUT ${_timeout})
257         add_dependencies(tests ${EXENAME})
258     endif()
259 endfunction ()
260
261 function (gmx_add_unit_test NAME EXENAME)
262     gmx_add_gtest_executable(${EXENAME} ${ARGN})
263     gmx_register_gtest_test(${NAME} ${EXENAME})
264 endfunction()
265
266 function (gmx_add_mpi_unit_test NAME EXENAME RANKS)
267     if (GMX_MPI OR (GMX_THREAD_MPI AND GTEST_IS_THREADSAFE))
268         gmx_add_gtest_executable(${EXENAME} MPI ${ARGN})
269         gmx_register_gtest_test(${NAME} ${EXENAME} MPI_RANKS ${RANKS})
270     endif()
271 endfunction()