SYCL: Avoid using no_init read accessor in rocFFT
[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 set(GMX_CAN_RUN_MPI_TESTS 1)
41 if (GMX_MPI)
42     set(_an_mpi_variable_had_content 0)
43     foreach(VARNAME MPIEXEC MPIEXEC_NUMPROC_FLAG MPIEXEC_PREFLAGS MPIEXEC_POSTFLAGS)
44         # These variables need a valid value for the test to run
45         # and pass, but conceivably any of them might be valid
46         # with arbitrary (including empty) content. They can't be
47         # valid if they've been populated with the CMake
48         # find_package magic suffix/value "NOTFOUND", though.
49         if (${VARNAME} MATCHES ".*NOTFOUND")
50             gmx_add_missing_tests_notice("CMake variable ${VARNAME} was not detected to be a valid value. "
51                                          "To test GROMACS correctly, check the advice in the install guide.")
52             set(GMX_CAN_RUN_MPI_TESTS 0)
53         endif()
54         if (NOT VARNAME STREQUAL MPIEXEC AND ${VARNAME})
55             set(_an_mpi_variable_had_content 1)
56         endif()
57     endforeach()
58     if(_an_mpi_variable_had_content AND NOT MPIEXEC)
59         gmx_add_missing_tests_notice("CMake variable MPIEXEC must have a valid value if one of the other related "
60                                      "MPIEXEC variables does. To test GROMACS correctly, check the advice in the "
61                                      "install guide.")
62         set(GMX_CAN_RUN_MPI_TESTS 0)
63     endif()
64 elseif (NOT GMX_THREAD_MPI)
65     set(GMX_CAN_RUN_MPI_TESTS 0)
66 endif()
67
68 function (gmx_add_unit_test_library NAME)
69     if (GMX_BUILD_UNITTESTS AND BUILD_TESTING)
70         add_library(${NAME} STATIC ${UNITTEST_TARGET_OPTIONS} ${ARGN})
71         gmx_target_compile_options(${NAME})
72         target_compile_definitions(${NAME} PRIVATE HAVE_CONFIG_H)
73         target_include_directories(${NAME} SYSTEM BEFORE PRIVATE ${PROJECT_SOURCE_DIR}/src/external/thread_mpi/include)
74         target_link_libraries(${NAME} PRIVATE testutils gmock)
75         if(GMX_CLANG_TIDY)
76             set_target_properties(${NAME} PROPERTIES CXX_CLANG_TIDY
77                 "${CLANG_TIDY_EXE};-warnings-as-errors=*;-header-filter=.*")
78         endif()
79         gmx_warn_on_everything(${NAME})
80         if (HAS_WARNING_EVERYTHING)
81             # Some false positives exist produced by GoogleTest implementation
82             gmx_target_warning_suppression(${NAME} "-Wno-zero-as-null-pointer-constant" HAS_WARNING_NO_ZERO_AS_NULL_POINTER_CONSTANT)
83             gmx_target_warning_suppression(${NAME} "-Wno-gnu-zero-variadic-macro-arguments" HAS_WARNING_NO_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS)
84             # Use of GoogleMock can generate mock member functions that are unused
85             gmx_target_warning_suppression(${NAME} "-Wno-unused-member-function" HAS_WARNING_NO_UNUSED_MEMBER_FUNCTION)
86             if(GMX_GPU_CUDA)
87                 # CUDA headers target C, so use old-style casts that clang
88                 # warns about when it is the host compiler
89                 gmx_target_warning_suppression(${NAME} "-Wno-old-style-cast" HAS_NO_OLD_STYLE_CAST)
90             endif()
91         endif()
92     endif()
93 endfunction ()
94
95 # This function creates a GoogleTest test executable for a module.  It
96 # hides all the complexity of how to treat different source files
97 # under different configuration conditions. It should be extended
98 # if we ever support another GPU compilation approach.
99 #
100 # It can be called with extra options and arguments:
101 #   MPI
102 #     To trigger the ctest runner to run this test with multiple ranks
103 #   HARDWARE_DETECTION
104 #     To trigger the test executable setup code to run hardware detection
105 #   CPP_SOURCE_FILES          file1.cpp file2.cpp ...
106 #     All the normal C++ .cpp source files
107 #   GPU_CPP_SOURCE_FILES  file1.cpp file2.cpp ...
108 #     All the C++ .cpp source files that are always needed, but must be
109 #     compiled in the way that suits GMX_GPU.
110 #   CUDA_CU_SOURCE_FILES      file1.cu  file2.cu  ...
111 #     All the normal CUDA .cu source files
112 #   OPENCL_CPP_SOURCE_FILES   file1.cpp file2.cpp ...
113 #     All the other C++ .cpp source files needed only with OpenCL
114 #   SYCL_CPP_SOURCE_FILES   file1.cpp file2.cpp ...
115 #     All the C++ .cpp source files needed only with SYCL
116 #   NON_GPU_CPP_SOURCE_FILES  file1.cpp file2.cpp ...
117 #     All the other C++ .cpp source files needed only with neither OpenCL nor CUDA nor SYCL
118 function (gmx_add_gtest_executable EXENAME)
119     if (GMX_BUILD_UNITTESTS AND BUILD_TESTING)
120         set(_options MPI HARDWARE_DETECTION)
121         set(_multi_value_keywords
122             CPP_SOURCE_FILES
123             CUDA_CU_SOURCE_FILES
124             GPU_CPP_SOURCE_FILES
125             OPENCL_CPP_SOURCE_FILES
126             SYCL_CPP_SOURCE_FILES
127             NON_GPU_CPP_SOURCE_FILES
128             )
129         cmake_parse_arguments(ARG "${_options}" "" "${_multi_value_keywords}" ${ARGN})
130
131         file(RELATIVE_PATH _input_files_path ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
132         set(_temporary_files_path "${CMAKE_CURRENT_BINARY_DIR}/Testing/Temporary")
133         file(MAKE_DIRECTORY ${_temporary_files_path})
134         # Note that the quotation marks in the next line form part of
135         # the defined symbol, so that the macro replacement in the
136         # source file is as a string.
137         # These are only needed for unittest_main.cpp, but for simplicity used
138         # for the whole target (since there may be multiple executables in the
139         # same directory, it is not straightforward to use a source file
140         # property).
141         set(EXTRA_COMPILE_DEFINITIONS
142             TEST_DATA_PATH="${_input_files_path}"
143             TEST_TEMP_PATH="${_temporary_files_path}")
144         if (ARG_MPI)
145             list(APPEND EXTRA_COMPILE_DEFINITIONS
146                  TEST_USES_MPI=true)
147         endif()
148         if (ARG_HARDWARE_DETECTION)
149             list(APPEND EXTRA_COMPILE_DEFINITIONS
150                  TEST_USES_HARDWARE_DETECTION=true)
151         endif()
152
153         if (GMX_GPU_CUDA AND NOT GMX_CLANG_CUDA)
154             # Work around FindCUDA that prevents using target_link_libraries()
155             # with keywords otherwise...
156             set(CUDA_LIBRARIES PRIVATE ${CUDA_LIBRARIES})
157             cuda_add_executable(${EXENAME} ${UNITTEST_TARGET_OPTIONS}
158                 ${ARG_CPP_SOURCE_FILES}
159                 ${ARG_CUDA_CU_SOURCE_FILES}
160                 ${ARG_GPU_CPP_SOURCE_FILES})
161         else()
162             add_executable(${EXENAME} ${UNITTEST_TARGET_OPTIONS}
163                 ${ARG_CPP_SOURCE_FILES})
164         endif()
165
166         if (GMX_GPU_CUDA)
167             if (GMX_CLANG_CUDA)
168                 target_sources(${EXENAME} PRIVATE
169                     ${ARG_CUDA_CU_SOURCE_FILES}
170                     ${ARG_GPU_CPP_SOURCE_FILES})
171                 set_source_files_properties(${ARG_GPU_CPP_SOURCE_FILES} PROPERTIES CUDA_SOURCE_PROPERTY_FORMAT OBJ)
172                 gmx_compile_cuda_file_with_clang(${ARG_CUDA_CU_SOURCE_FILES})
173                 gmx_compile_cuda_file_with_clang(${ARG_GPU_CPP_SOURCE_FILES})
174                 if(ARG_CUDA_CU_SOURCE_FILES OR ARG_GPU_CPP_SOURCE_FILES)
175                     target_link_libraries(${EXENAME} PRIVATE ${GMX_EXTRA_LIBRARIES})
176                 endif()
177             endif()
178         elseif (GMX_GPU_OPENCL)
179             target_sources(${EXENAME} PRIVATE ${ARG_OPENCL_CPP_SOURCE_FILES} ${ARG_GPU_CPP_SOURCE_FILES})
180             if(ARG_OPENCL_CPP_SOURCE_FILES OR ARG_GPU_CPP_SOURCE_FILES)
181                 target_link_libraries(${EXENAME} PRIVATE ${OpenCL_LIBRARIES})
182             endif()
183         elseif (GMX_GPU_SYCL)
184             target_sources(${EXENAME} PRIVATE ${ARG_SYCL_CPP_SOURCE_FILES} ${ARG_GPU_CPP_SOURCE_FILES})
185             if(ARG_SYCL_CPP_SOURCE_FILES OR ARG_GPU_CPP_SOURCE_FILES)
186                 add_sycl_to_target(
187                     TARGET ${EXENAME}
188                     SOURCES ${ARG_SYCL_CPP_SOURCE_FILES} ${ARG_GPU_CPP_SOURCE_FILES}
189                     )
190             endif()
191         else()
192             target_sources(${EXENAME} PRIVATE ${ARG_NON_GPU_CPP_SOURCE_FILES} ${ARG_GPU_CPP_SOURCE_FILES})
193         endif()
194
195         gmx_target_compile_options(${EXENAME})
196         target_compile_definitions(${EXENAME} PRIVATE HAVE_CONFIG_H ${EXTRA_COMPILE_DEFINITIONS})
197         target_include_directories(${EXENAME} SYSTEM BEFORE PRIVATE ${PROJECT_SOURCE_DIR}/src/external/thread_mpi/include)
198         # Permit GROMACS code to include externally developed headers,
199         # such as the functionality from the nonstd project that we
200         # use for gmx::compat::optional. These are included as system
201         # headers so that no warnings are issued from them.
202         target_include_directories(${EXENAME} SYSTEM PRIVATE ${PROJECT_SOURCE_DIR}/src/external)
203         target_include_directories(${EXENAME} SYSTEM PRIVATE ${PROJECT_SOURCE_DIR}/src/external/muparser)
204         if(CYGWIN)
205             # Ensure GoogleTest headers can find POSIX things needed
206             target_compile_definitions(${EXENAME} PRIVATE _POSIX_C_SOURCE=200809L)
207         endif()
208
209         target_link_libraries(${EXENAME} PRIVATE
210             testutils common libgromacs gmock
211             ${GMX_COMMON_LIBRARIES} ${GMX_EXE_LINKER_FLAGS})
212
213         if(GMX_CLANG_TIDY)
214             set_target_properties(${EXENAME} PROPERTIES CXX_CLANG_TIDY
215                 "${CLANG_TIDY_EXE};-warnings-as-errors=*;-header-filter=.*")
216         endif()
217         gmx_warn_on_everything(${EXENAME})
218         if (HAS_WARNING_EVERYTHING)
219             # Some false positives exist produced by GoogleTest implementation
220             gmx_target_warning_suppression(${EXENAME} "-Wno-zero-as-null-pointer-constant" HAS_WARNING_NO_ZERO_AS_NULL_POINTER_CONSTANT)
221             gmx_target_warning_suppression(${EXENAME} "-Wno-gnu-zero-variadic-macro-arguments" HAS_WARNING_NO_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS)
222             # Use of GoogleMock can generate mock member functions that are unused
223             gmx_target_warning_suppression(${EXENAME} "-Wno-unused-member-function" HAS_WARNING_NO_UNUSED_MEMBER_FUNCTION)
224             if(GMX_GPU_CUDA)
225                 # CUDA headers target C, so use old-style casts that clang
226                 # warns about when it is the host compiler
227                 gmx_target_warning_suppression(${EXENAME} "-Wno-old-style-cast" HAS_NO_OLD_STYLE_CAST)
228             endif()
229         endif()
230     endif()
231 endfunction()
232
233 # This function can be called with extra options and arguments:
234 #   OPENMP_THREADS <N>    declares the requirement to run the test binary with N OpenMP
235 #                           threads (when supported by the build configuration)
236 #   MPI_RANKS <N>         declares the requirement to run the test binary with N ranks
237 #   INTEGRATION_TEST      requires the use of the IntegrationTest label in CTest
238 #   SLOW_TEST             requires the use of the SlowTest label in CTest, and
239 #                         increase the length of the ctest timeout.
240 #   IGNORE_LEAKS          Skip some memory safety checks.
241 #
242 # TODO When a test case needs it, generalize the MPI_RANKS mechanism so
243 # that ctest can run the test binary over a range of numbers of MPI
244 # ranks.
245 function (gmx_register_gtest_test NAME EXENAME)
246     if (GMX_BUILD_UNITTESTS AND BUILD_TESTING)
247         set(_options INTEGRATION_TEST SLOW_TEST IGNORE_LEAKS)
248         set(_one_value_args MPI_RANKS OPENMP_THREADS)
249         cmake_parse_arguments(ARG "${_options}" "${_one_value_args}" "" ${ARGN})
250         set(_xml_path ${CMAKE_BINARY_DIR}/Testing/Temporary/${NAME}.xml)
251         set(_labels GTest)
252         set(_timeout 30)
253         if (ARG_INTEGRATION_TEST)
254             list(APPEND _labels IntegrationTest)
255             # Slow build configurations should have longer timeouts.
256             # Both OpenCL (from JIT) and ThreadSanitizer (from how it
257             # checks) can take signficantly more time than other
258             # configurations.
259             if (GMX_GPU_OPENCL OR GMX_GPU_SYCL)
260                 set(_timeout 240)
261             elseif (${CMAKE_BUILD_TYPE} STREQUAL TSAN)
262                 set(_timeout 300)
263             else()
264                 set(_timeout 120)
265             endif()
266         elseif (ARG_SLOW_TEST)
267             list(APPEND _labels SlowTest)
268             set(_timeout 480)
269         else()
270             list(APPEND _labels UnitTest)
271             gmx_get_test_prefix_cmd(_prefix_cmd)
272         endif()
273         if (ARG_IGNORE_LEAKS)
274             gmx_get_test_prefix_cmd(_prefix_cmd IGNORE_LEAKS)
275         endif ()
276         set(_cmd ${_prefix_cmd} $<TARGET_FILE:${EXENAME}>)
277         if (ARG_OPENMP_THREADS)
278             if (GMX_OPENMP)
279                 list(APPEND _cmd -ntomp ${ARG_OPENMP_THREADS})
280             endif()
281         endif()
282         if (ARG_MPI_RANKS)
283             if (NOT GMX_CAN_RUN_MPI_TESTS)
284                 gmx_add_missing_tests_notice("Skipping ${NAME} because MPI tests are not available.")
285                 return()
286             endif()
287             list(APPEND _labels MpiTest)
288             if (GMX_MPI)
289                 set(_cmd
290                     ${MPIEXEC} ${MPIEXEC_NUMPROC_FLAG} ${ARG_MPI_RANKS}
291                     ${MPIEXEC_PREFLAGS} ${_cmd} ${MPIEXEC_POSTFLAGS})
292             elseif (GMX_THREAD_MPI)
293                 list(APPEND _cmd -ntmpi ${ARG_MPI_RANKS})
294             endif()
295         endif()
296         add_test(NAME ${NAME}
297                  COMMAND ${_cmd} --gtest_output=xml:${_xml_path})
298         set_tests_properties(${NAME} PROPERTIES LABELS "${_labels}")
299         set_tests_properties(${NAME} PROPERTIES TIMEOUT ${_timeout})
300         add_dependencies(tests ${EXENAME})
301     endif()
302 endfunction ()
303
304 function (gmx_add_unit_test NAME EXENAME)
305     gmx_add_gtest_executable(${EXENAME} ${ARGN})
306     gmx_register_gtest_test(${NAME} ${EXENAME})
307 endfunction()
308
309 function (gmx_add_mpi_unit_test NAME EXENAME RANKS)
310     if (GMX_MPI OR (GMX_THREAD_MPI AND GTEST_IS_THREADSAFE))
311         gmx_add_gtest_executable(${EXENAME} MPI ${ARGN})
312         gmx_register_gtest_test(${NAME} ${EXENAME} MPI_RANKS ${RANKS})
313     endif()
314 endfunction()