Generalize constraints on MPI rank counts for tests
[alexxy/gromacs.git] / tests / CMakeLists.txt
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2012,2013,2014,2015,2016, The GROMACS development team.
5 # Copyright (c) 2017,2018,2019,2020,2021, 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 set(REGRESSIONTEST_PATH "" CACHE PATH "Directory containing regressiontests")
37 mark_as_advanced(REGRESSIONTEST_PATH)
38 option(REGRESSIONTEST_DOWNLOAD
39     "Automatically download regressiontests. Tests can be run with ctest." OFF)
40
41 if(REGRESSIONTEST_DOWNLOAD)
42     if (NOT SOURCE_IS_SOURCE_DISTRIBUTION)
43         set(REGRESSIONTEST_URL https://gitlab.com/gromacs/gromacs-regressiontests/-/archive/${REGRESSIONTEST_BRANCH}/gromacs-regressiontests-${REGRESSIONTEST_BRANCH}.tar.gz)
44         # REGRESSIONTEST_PATH for dev trees is set later based on the dirname found in the tar
45     else()
46         set(REGRESSIONTEST_URL https://ftp.gromacs.org/regressiontests/regressiontests-${REGRESSIONTEST_VERSION}.tar.gz)
47         set(REGRESSIONTEST_PATH
48             "${CMAKE_CURRENT_BINARY_DIR}/regressiontests-${REGRESSIONTEST_VERSION}"
49             CACHE PATH "Path to auto-downloaded regressiontests" FORCE)
50     endif()
51     set(REGRESSIONTEST_FILE "${CMAKE_CURRENT_BINARY_DIR}/regressiontests.tgz")
52     message("Downloading: ${REGRESSIONTEST_URL}")
53     file(DOWNLOAD ${REGRESSIONTEST_URL} "${REGRESSIONTEST_FILE}" SHOW_PROGRESS STATUS status LOG log)
54     list(GET status 0 status_code)
55     list(GET status 1 status_string)
56
57     if(NOT status_code EQUAL 0)
58         message(FATAL_ERROR "error: downloading '${REGRESSIONTEST_URL}' failed
59 status_code: ${status_code}
60 status_string: ${status_string}
61 log: ${log}")
62     endif()
63     if (SOURCE_IS_SOURCE_DISTRIBUTION)
64         file(MD5 ${REGRESSIONTEST_FILE} COMPUTED_MD5SUM)
65         if(NOT ${REGRESSIONTEST_MD5SUM} STREQUAL ${COMPUTED_MD5SUM})
66             message(FATAL_ERROR "Download of regressiontests failed. Expected MD5 of ${REGRESSIONTEST_MD5SUM} but download has ${COMPUTED_MD5SUM}")
67         endif()
68     else()
69         # Extract the REGRESSIONTEST_PATH from the tar when using dev tree.
70         execute_process(COMMAND ${CMAKE_COMMAND} -E tar tf "${REGRESSIONTEST_FILE}"
71                 RESULT_VARIABLE _tar_tf_res
72                 OUTPUT_VARIABLE _tar_tf_out)
73         if (${_tar_tf_res} EQUAL 0)
74             string(REGEX REPLACE "/\n.*$" "" _regressiontest_dir "${_tar_tf_out}")
75             set(REGRESSIONTEST_PATH "${CMAKE_CURRENT_BINARY_DIR}/${_regressiontest_dir}"
76                 CACHE PATH "Path to auto-downloaded regressiontests" FORCE)
77         else()
78             message(FATAL_ERROR "Failed to list the contents of the downloaded tarball: ${REGRESSIONTEST_FILE}")
79         endif()
80     endif()
81
82     file(REMOVE_RECURSE "${REGRESSIONTEST_PATH}") #delete potential prior folder
83     execute_process(COMMAND ${CMAKE_COMMAND} -E tar xf "${REGRESSIONTEST_FILE}"
84         WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
85     if(NOT EXISTS ${REGRESSIONTEST_PATH}/gmxtest.pl)
86         message(FATAL_ERROR "Download incorrect. Doesn't contain required gmxtest.pl")
87     endif()
88     set(REGRESSIONTEST_DOWNLOAD OFF CACHE BOOL "Tests already downloaded. Set to yes to download again" FORCE)
89 endif()
90
91 if(REGRESSIONTEST_PATH AND (CMAKE_CROSSCOMPILING OR CMAKE_CONFIGURATION_TYPES))
92     # TODO: It would be nicer to do these checks before potentially downloading the tests.
93     # Cross-compiling toolchains require us to compile both front-end and
94     # back-end binaries to run gmxtest.pl.
95     message(WARNING
96         "With cross-compiling or multi-configuration generators (e.g. Visual Studio), running regressiontests from build system is not supported. Please run gmxtest.pl directly.")
97     set(REGRESSIONTEST_PATH OFF CACHE BOOL
98         "With cross-compiling or multi-configuration generators, running regressiontests from build system is not supported." FORCE)
99 endif()
100
101 if(REGRESSIONTEST_PATH)
102     if(NOT EXISTS ${REGRESSIONTEST_PATH}/gmxtest.pl)
103         message(FATAL_ERROR
104             "REGRESSIONTEST_PATH invalid. The path needs to contain gmxtest.pl.")
105     endif()
106
107     # gmxtests target builds all binaries required for running gmxtest
108     add_custom_target(gmxtests DEPENDS gmx)
109     add_dependencies(run-ctest gmxtests)
110     add_dependencies(run-ctest-nophys gmxtests)
111
112     if(GMX_DOUBLE)
113         list(APPEND ARGS -double)
114     endif()
115     if(GMX_LIB_MPI AND NOT MPIEXEC) # find_program failed
116         message(WARNING
117             "Please set MPIEXEC. Otherwise mpirun is assumed for running tests.")
118     endif()
119     if(DEFINED ENV{CI_JOB_ID} AND GMX_OPENMP)
120         set(GMX_REGRESSIONTEST_OMP_THREADS 2 CACHE STRING
121             "Number of OpenMP threads to use with the regression tests")
122         mark_as_advanced(GMX_REGRESSIONTEST_OMP_THREADS)
123         list(APPEND ARGS -ntomp ${GMX_REGRESSIONTEST_OMP_THREADS})
124     endif()
125     if(GMX_LIB_MPI)
126         set(GMX_TEST_NUMBER_PROCS 8 CACHE STRING "Number of processors used for testing")
127         mark_as_advanced(GMX_TEST_NUMBER_PROCS)
128         list(APPEND ARGS -np ${GMX_TEST_NUMBER_PROCS})
129         if(MPIEXEC)
130             list(APPEND ARGS -mpirun ${MPIEXEC})
131         endif()
132         #We should use MPIEXEC_NUMPROC_FLAG but gmxtest.pl doesn't let us pass it
133     endif()
134     if(GMX_BINARY_SUFFIX)
135         list(APPEND ARGS -suffix ${GMX_BINARY_SUFFIX})
136     endif()
137     #crosscompile is only used to disable checking whether binaries work
138     #given that we know they are there and that mdrun might not be executable
139     #(e.g. Cray) we enable it.
140     list(APPEND ARGS -crosscompile)
141
142     set(REGRESSIONTEST_EXTRA_ARGS "" CACHE STRING 
143         "Extra arguments passed to gmxtest")
144     mark_as_advanced(REGRESSIONTEST_EXTRA_ARGS)
145     list(APPEND ARGS ${REGRESSIONTEST_EXTRA_ARGS})
146
147     if(DEFINED ENV{CI_JOB_ID} AND GMX_THREAD_MPI)
148         set(GMX_REGRESSIONTEST_THREAD_MPI_THREADS 2 CACHE STRING
149         "Number of thread MPI threads to use for testing")
150         mark_as_advanced(GMX_REGRESSIONTEST_THREAD_MPI_THREADS)
151         list(APPEND ARGS -nt ${GMX_REGRESSIONTEST_THREAD_MPI_THREADS})
152     endif()
153     list(APPEND ARGS -noverbose -nosuffix)
154
155     if(GMX_NATIVE_WINDOWS)
156         set(PATH_SEPARATOR "\\;")
157         #replacing \ with / shouldn't be neccessary. But otherwise "..\bin\;c:\.."
158         #gets turned into "...\bin\\c:\.." don't know why and don't have a better
159         #workaround. This workaround doesn't hurt.
160         string(REPLACE "\\" "/" PATH "$ENV{PATH}")
161         #protect ; (don't treat as list)
162         string(REPLACE ";" "\\;" PATH "${PATH}")
163     else()
164         set(PATH_SEPARATOR ":")
165         set(PATH "$ENV{PATH}")
166     endif()
167
168     foreach(FOLDER bin lib) #lib folders might be needed for
169         #e.g. DLLs. For GMX paths native ("\") is needed for GMXLIB detection
170         file(TO_NATIVE_PATH "${CMAKE_BINARY_DIR}/${FOLDER}" DIR)
171         set(PATH "${DIR}${PATH_SEPARATOR}${PATH}")
172     endforeach()
173
174     find_program(PERL_EXECUTABLE NAMES "perl")
175     mark_as_advanced(PERL_EXECUTABLE)
176
177     if (NOT PERL_EXECUTABLE)
178         message(FATAL_ERROR "Perl not found. Install perl, set PERL_EXECUTABLE to the perl location, or unset REGRESSIONTEST_PATH to disable testing.")
179     endif()
180
181     #currently not testing tools because they don't contain any useful tests
182     foreach(subtest complex freeenergy rotation essentialdynamics)
183         add_test(NAME regressiontests/${subtest}
184             #windows requires the command to be perl and not the script
185             COMMAND perl "${REGRESSIONTEST_PATH}/gmxtest.pl" ${subtest} ${ARGS})
186         set_tests_properties(regressiontests/${subtest} PROPERTIES
187             ENVIRONMENT "PATH=${PATH}")
188     endforeach()
189 else()
190     gmx_add_missing_tests_notice("Regression tests have not been run. If you want to run them from the build system, get the correct version of the regression tests package and set REGRESSIONTEST_PATH in CMake to point to it, or set REGRESSIONTEST_DOWNLOAD=ON.")
191 endif()
192
193
194 #
195 # Physical validation tests are opt-in via -DGMX_PHYSICAL_VALIDATION=ON
196 #
197 if(GMX_PHYSICAL_VALIDATION)
198     include(FindPythonModule)
199     # physical validation suite is distributed with the source
200     set(PHYSVALTEST_SOURCE_PATH
201             "${CMAKE_CURRENT_SOURCE_DIR}/physicalvalidation")
202     #       CACHE PATH "Source directory containing physical validation tests.")
203     if(NOT EXISTS ${PHYSVALTEST_SOURCE_PATH}/gmx_physicalvalidation.py)
204         message(FATAL_ERROR
205             "GMX_PHYSICAL_VALIDATION set, but physical validation script not found in ${PHYSVALTEST_SOURCE_PATH}.")
206     endif()
207
208     if(CMAKE_CROSSCOMPILING OR CMAKE_CONFIGURATION_TYPES)
209         # The following comment is copied from regression tests:
210         #     Cross-compiling toolchains require us to compile both front-end and
211         #     back-end binaries to run gmxtest.pl.
212         # TODO: Look into the details of this.
213         # For now, turn it off - our python-gmx interface is probably not that stable for special cases anyway
214         message(WARNING
215                 "With cross-compiling or multi-configuration generators (e.g. Visual Studio),\
216                 running physical validation tests from build system is not supported.\
217                 Please run physicalvalidation.py directly.")
218         set(GMX_PHYSICAL_VALIDATION OFF CACHE BOOL
219                 "With cross-compiling or multi-configuration generators, running physical validation tests from build\
220                 system is not supported." FORCE)
221
222     else()
223         #
224         # Making sure gmx is built before running physical validation tests
225         #
226         add_dependencies(run-ctest-phys gmx)
227         add_dependencies(run-ctest gmx)
228
229         #
230         # Determine arguments passed to physicalvalidation.py
231         #
232         set(PARGS "")
233         list(APPEND PARGS --wd ${CMAKE_CURRENT_BINARY_DIR}/physicalvalidation)
234         list(APPEND PARGS --bindir ${CMAKE_BINARY_DIR}/bin)
235
236         if(GMX_LIB_MPI AND NOT MPIEXEC) # find_program failed
237             message(WARNING
238                 "Please set MPIEXEC. Otherwise mpirun is assumed for running tests.")
239         endif()
240         if(GMX_LIB_MPI)
241             # define number of processors in analogy to regression tests?
242             if(MPIEXEC)
243                 list(APPEND ARGS --mpicmd ${MPIEXEC})
244             else()
245                 list(APPEND ARGS --mpicmd mpirun)
246             endif()
247         endif()
248         if(GMX_LIB_MPI)
249             message(FATAL_ERROR
250                 "Physical validation using MPI not supported.")
251         endif()
252
253         if(GMX_BINARY_SUFFIX)
254             list(APPEND PARGS --suffix ${GMX_BINARY_SUFFIX})
255         endif()
256
257         #
258         # Give possibility to add args via cache
259         #
260         set(PHYSVALTEST_EXTRA_ARGS "" CACHE STRING
261             "Extra arguments passed to phystest")
262         mark_as_advanced(PHYSVALTEST_EXTRA_ARGS)
263         list(APPEND PARGS ${PHYSVALTEST_EXTRA_ARGS})
264
265         #
266         # The following lines are directly copied from regression tests.
267         # They seem to be applicable to physical validation tests as well.
268         #
269         if(GMX_NATIVE_WINDOWS)
270             set(PATH_SEPARATOR "\\;")
271             #replacing \ with / shouldn't be neccessary. But otherwise "..\bin\;c:\.."
272             #gets turned into "...\bin\\c:\.." don't know why and don't have a better
273             #workaround. This workaround doesn't hurt.
274             string(REPLACE "\\" "/" PATH "$ENV{PATH}")
275             #protect ; (don't treat as list)
276             string(REPLACE ";" "\\;" PATH "${PATH}")
277         else()
278             set(PATH_SEPARATOR ":")
279             set(PATH "$ENV{PATH}")
280         endif()
281
282         foreach(FOLDER bin lib) #lib folders might be needed for
283                                 #e.g. DLLs. For GMX paths native ("\") is needed for GMXLIB detection
284             file(TO_NATIVE_PATH "${CMAKE_BINARY_DIR}/${FOLDER}" DIR)
285             set(PATH "${DIR}${PATH_SEPARATOR}${PATH}")
286         endforeach()
287         #
288         # End copied from regression tests.
289         #
290
291         if (NOT Python3_Interpreter_FOUND)
292             message(FATAL_ERROR
293                     "Python not found. Physical validation requires python. \
294                      Install python, set Python3_ROOT_DIR or PYTHON_EXECUTABLE to a valid location, \
295                      or set GMX_PHYSICAL_VALIDATION=OFF to disable the physical validation tests.")
296         endif()
297         #
298         foreach(module numpy scipy pymbar) # add further modules if necessary
299             find_python_module(${module})
300             string(TOUPPER ${module} module_upper)
301             if(NOT PYTHONMODULE_${module_upper})
302                 message(WARNING
303                         "Python module ${module} not found. Physical validation relies on ${module}. Make sure\
304                         ${module} can be found by PYTHON_EXECUTABLE, or set GMX_PHYSICAL_VALIDATION=OFF to disable\
305                         the physical validation tests.")
306             endif()
307         endforeach()
308
309         #
310         # Hook in our own tests
311         # Read them from json file to make every system a separate test
312         #
313         if (GMX_DOUBLE)
314             set(PHYSVALTEST_JSON "${PHYSVALTEST_SOURCE_PATH}/systems_d.json")
315         else()
316             set(PHYSVALTEST_JSON "${PHYSVALTEST_SOURCE_PATH}/systems.json")
317         endif()
318         file(STRINGS "${PHYSVALTEST_JSON}" json)
319         string(REPLACE "\"" "" json ${json})
320         string(REPLACE "," "" json ${json})
321         string(REPLACE " " "" json ${json})
322         string(REPLACE ";" "<<>>" json ${json})
323         string(REPLACE "[" "" json ${json})
324         string(REPLACE "]" "" json ${json})
325         string(REPLACE "{" "" json ${json})
326         string(REPLACE "}" "" json ${json})
327         string(REPLACE "<<>>" ";" json ${json})
328         foreach(line ${json})
329             if("${line}" MATCHES "name:")
330                 string(REPLACE "name:" "" testname ${line})
331                 add_test(NAME physicalvalidationtests/${testname}
332                         COMMAND ${PYTHON_EXECUTABLE} "${PHYSVALTEST_SOURCE_PATH}/gmx_physicalvalidation.py" "${PHYSVALTEST_JSON}" -s ${testname} -a ${PARGS})
333                 set_tests_properties(physicalvalidationtests/${testname} PROPERTIES
334                         ENVIRONMENT "PATH=${PATH}"
335                         LABELS "PhysicalValidationTest")
336             endif()
337         endforeach()
338
339         #
340         # Create prepare and run targets while all variables are set
341         # Will be referenced in CheckTarget.cmake
342         #
343         # "check-phys-prepare" prepares the systems needed for physical validation for external running
344         add_custom_target(check-phys-prepare
345                           COMMAND ${PYTHON_EXECUTABLE} "${PHYSVALTEST_SOURCE_PATH}/gmx_physicalvalidation.py" "${PHYSVALTEST_JSON}" -p ${PARGS}
346                           COMMENT "Preparing systems for physical validation"
347                           DEPENDS gmx)
348         # "run-physval-sims" prepares and runs the systems needed for physical validation
349         add_custom_target(run-physval-sims
350                           COMMAND ${PYTHON_EXECUTABLE} "${PHYSVALTEST_SOURCE_PATH}/gmx_physicalvalidation.py" "${PHYSVALTEST_JSON}" -r ${PARGS}
351                           COMMENT "Preparing and running systems for physical validation"
352                           DEPENDS gmx)
353     endif()
354 else()
355     #
356     # Create dummy prepare and run targets
357     # Will be referenced in CheckTarget.cmake
358     #
359     # "check-phys-prepare" prepares the systems needed for physical validation for external running
360     add_custom_target(check-phys-prepare
361                       COMMAND ${CMAKE_COMMAND} -E echo "NOTE: You called the target `check-phys-prepare`, but ran cmake with\
362  `-DGMX_PHYSICAL_VALIDATION=OFF`. The physical validation tests are therefore unavailable,\
363  and this target is not doing anything."
364                       COMMENT "No physical validation" VERBATIM)
365     # "run-physval-sims" prepares and runs the systems needed for physical validation
366     add_custom_target(run-physval-sims
367                       COMMAND ${CMAKE_COMMAND} -E echo "NOTE: You called the target `run-physval-sims`, but ran cmake with\
368  `-DGMX_PHYSICAL_VALIDATION=OFF`. The physical validation tests are therefore unavailable,\
369  and this target is not doing anything."
370                       COMMENT "No physical validation" VERBATIM)
371 endif()
372
373 gmx_create_missing_tests_notice_target()
374