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