192a1ffd59005160a934b504d21905d9b87c347b
[alexxy/gromacs.git] / admin / containers / utility.py
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2020,2021, by the GROMACS development team, led by
5 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 # and including many others, as listed in the AUTHORS file in the
7 # top-level source directory and at http://www.gromacs.org.
8 #
9 # GROMACS is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public License
11 # as published by the Free Software Foundation; either version 2.1
12 # of the License, or (at your option) any later version.
13 #
14 # GROMACS is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # Lesser General Public License for more details.
18 #
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with GROMACS; if not, see
21 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23 #
24 # If you want to redistribute modifications to GROMACS, please
25 # consider that scientific software is very special. Version
26 # control is crucial - bugs must be traceable. We will be happy to
27 # consider code for inclusion in the official distribution, but
28 # derived work must not be called official GROMACS. Details are found
29 # in the README & COPYING files - if they are missing, get the
30 # official version at http://www.gromacs.org.
31 #
32 # To help us fund GROMACS development, we humbly ask that you cite
33 # the research papers on the package. Check out http://www.gromacs.org.
34
35 """A utility module to help manage the matrix of configurations for CI testing and build containers.
36
37 When called as a stand alone script, prints a Docker image name based on the
38 command line arguments. The Docker image name is of the form used in the GROMACS
39 CI pipeline jobs.
40
41 Example::
42
43     $ python3 -m utility --llvm --doxygen
44     gromacs/ci-ubuntu-20.04-llvm-7-docs
45
46 See Also:
47     :file:`buildall.sh`
48
49 As a module, provides importable argument parser and docker image name generator.
50
51 Note that the parser is created with ``add_help=False`` to make it friendly as a
52 parent parser, but this means that you must derive a new parser from it if you
53 want to see the full generated command line help.
54
55 Example::
56
57     import utility.parser
58     # utility.parser does not support `-h` or `--help`
59     parser = argparse.ArgumentParser(
60         description='GROMACS CI image creation script',
61         parents=[utility.parser])
62     # ArgumentParser(add_help=True) is default, so parser supports `-h` and `--help`
63
64 See Also:
65     :file:`scripted_gmx_docker_builds.py`
66
67 Authors:
68     * Paul Bauer <paul.bauer.q@gmail.com>
69     * Eric Irrgang <ericirrgang@gmail.com>
70     * Joe Jordan <e.jjordan12@gmail.com>
71     * Mark Abraham <mark.j.abraham@gmail.com>
72
73 """
74
75 import argparse
76
77
78 parser = argparse.ArgumentParser(description='GROMACS CI image slug options.', add_help=False)
79 """A parent parser for tools referencing image parameters.
80
81 This argparse parser is defined for convenience and may be used to partially initialize
82 parsers for tools.
83
84 .. warning:: Do not modify this parser.
85
86     Instead, inherit from it with the *parents* argument to :py:class:`argparse.ArgumentParser`
87 """
88
89 parser.add_argument('--cmake', nargs='*', type=str, default=['3.16.3', '3.17.2', '3.18.4'], # new minimum required versions
90                     help='Selection of CMake version to provide to base image')
91
92 compiler_group = parser.add_mutually_exclusive_group()
93 compiler_group.add_argument('--gcc', type=int, nargs='?', const=7, default=7,
94                             help='Select GNU compiler tool chain. (Default) '
95                                  'Some checking is implemented to avoid incompatible combinations')
96 compiler_group.add_argument('--llvm', type=str, nargs='?', const='7', default=None,
97                             help='Select LLVM compiler tool chain. '
98                                  'Some checking is implemented to avoid incompatible combinations')
99 # TODO currently the installation merely gets the latest beta version of oneAPI,
100 # not a specific version. GROMACS probably doesn't need to address that until
101 # oneAPI makes an official release. Also, the resulting container is a mix
102 # of packages with different betaXY version numbers, which hopefully works and
103 # is what Intel intends...
104 compiler_group.add_argument('--oneapi', type=str, nargs='?', const="2021.1.1", default=None,
105                             help='Select Intel oneAPI package version.')
106
107 linux_group = parser.add_mutually_exclusive_group()
108 linux_group.add_argument('--ubuntu', type=str, nargs='?', const='20.04', default='20.04',
109                          help='Select Ubuntu Linux base image. (default: ubuntu 20.04)')
110 linux_group.add_argument('--centos', type=str, nargs='?', const='7', default=None,
111                          help='Select Centos Linux base image.')
112
113 parser.add_argument('--cuda', type=str, nargs='?', const='11.0', default=None,
114                     help='Select a CUDA version for a base Linux image from NVIDIA.')
115
116 parser.add_argument('--mpi', type=str, nargs='?', const='openmpi', default=None,
117                     help='Enable MPI (default disabled) and optionally select distribution (default: openmpi)')
118
119 parser.add_argument('--tsan', type=str, nargs='?', const='llvm', default=None,
120                     help='Build special compiler versions with TSAN OpenMP support')
121
122 parser.add_argument('--hipsycl', type=str, nargs='?', default=None,
123                     help='Select hipSYCL repository tag/commit/branch.')
124
125 parser.add_argument('--rocm', type=str, nargs='?', const='debian', default=None,
126                     help='Select AMD compute engine version.')
127
128 parser.add_argument('--intel-compute-runtime', type=str, nargs='?', default=None,
129                     help='Select Intel Compute Runtime version.')
130
131 parser.add_argument('--clfft', type=str, nargs='?', const='master', default=None,
132                     help='Add external clFFT libraries to the build image')
133
134 parser.add_argument('--doxygen', type=str, nargs='?', const='1.8.5', default=None,
135                     help='Add doxygen environment for documentation builds. Also adds other requirements needed for final docs images.')
136
137 # Supported Python versions for maintained branches.
138 _python_versions = ['3.6.10', '3.7.7', '3.8.2', '3.9.1']
139 parser.add_argument('--venvs', nargs='*', type=str, default=_python_versions,
140                     help='List of Python versions ("major.minor.patch") for which to install venvs. '
141                          'Default: {}'.format(' '.join(_python_versions)))
142
143
144 def image_name(configuration: argparse.Namespace) -> str:
145     """Generate docker image name.
146
147     Image names have the form ``ci-<slug>``, where the configuration slug has the form::
148
149         <distro>-<version>-<compiler>-<major version>[-<gpusdk>-<version>][-<use case>]
150
151     This function also applies an appropriate Docker image repository prefix.
152
153     Arguments:
154         configuration: Docker image configuration as described by the parsed arguments.
155
156     """
157     elements = []
158     for distro in ('centos', 'ubuntu'):
159         version = getattr(configuration, distro, None)
160         if version is not None:
161             elements.append(distro + '-' + version)
162             break
163     for compiler in ('llvm', 'gcc'):
164         version = getattr(configuration, compiler, None)
165         if version is not None:
166             elements.append(compiler + '-' + str(version).split('.')[0])
167             break
168     for gpusdk in ('cuda', 'hipsycl'):
169         version = getattr(configuration, gpusdk, None)
170         if version is not None:
171             elements.append(gpusdk + '-' + version)
172     if configuration.oneapi is not None:
173         elements.append('oneapi-' + configuration.oneapi)
174     if configuration.intel_compute_runtime is not None:
175         elements.append('intel-' + configuration.intel_compute_runtime)
176     if configuration.rocm is not None:
177         elements.append('rocm-' + configuration.rocm)
178
179     # Check for special cases
180     # The following attribute keys indicate the image is built for the named
181     # special use case.
182     cases = {'doxygen': 'docs',
183              'tsan': 'tsan'}
184     for attr in cases:
185         value = getattr(configuration, attr, None)
186         if value is not None:
187             elements.append(cases[attr])
188     slug = '-'.join(elements)
189     # we are using the GitLab container registry to store the images
190     # to get around issues with pulling them repeatedly from DockerHub
191     # and running into the image pull limitation there.
192     return 'registry.gitlab.com/gromacs/gromacs/ci-' + slug
193
194
195 if __name__ == "__main__":
196     args = argparse.ArgumentParser(parents=[parser]).parse_args()
197     print(image_name(args))