3 # This file is part of the GROMACS molecular simulation package.
5 # Copyright (c) 2013,2014,2015,2017,2018,2019, 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.
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.
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.
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.
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.
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.
36 # This script is used by the GROMACS developers to build most of the
37 # files from which the nbnxn kernels are compiled. It is not called at
38 # CMake time, and users should never need to use it. It currently
39 # works for nbnxn kernel structure types 2xnn and 4xn. The generated
40 # files are versions of the *.pre files in this directory, customized
41 # for the kernel structure type and/or the detailed kernel type. These
44 # A single header file that declares all the kernel functions for
45 # this nbnxn kernel structure type and a function pointer table.
47 # Many C kernel files, each defining a single kernel function. These
48 # functions can take a noticeable time to compile, and should tend
49 # to be in seperate files to take advantage of make-time
52 # This script should be run from the directory in which it is
53 # located. The generated files are located in ../simd_<type>. There
54 # are three other files in those locations that are not generated. These
57 # setup logic peculiar to the kernel structure type but common to
58 # all the kernels within that type, and
60 # the logic for the outer and inner loops of the kernels, as
61 # customized by numerous preprocessor defines to suit the hardware
64 # Note that while functions for both nbnxn kernel structures are
65 # compiled and built into an mdrun executable, because that executable
66 # is not portable, only the functions for the useful nbnxn kernel
67 # structure for the hardware selected at CMake time contain real
68 # kernel logic. A run-time error occurs if an inappropriate kernel
69 # dispatcher function is called (but that is normally impossible).
74 os.chdir(os.path.dirname(os.path.abspath(__file__)))
75 import collections # Requires Python 2.7
76 sys.path.append('../../../../../admin')
77 from copyright import create_copyright_header
79 FileHeader = create_copyright_header('2012,2013,2014,2015,2019')
81 * Note: this file was generated by the Verlet kernel generator for
87 def read_kernel_template(filename):
88 with open(filename, "r") as TemplateFile:
89 TemplateText = TemplateFile.read()
90 copyright_re = r'/\*\n \* This file is part of the GROMACS molecular simulation package\.\n( \*.*\n)* \*/\n'
91 match = re.match(copyright_re, TemplateText)
93 TemplateText = TemplateText[match.end():]
96 # The dict order must match the order of an enumeration in
97 # nbnxn_kernel_simd_template.c.pre
98 ElectrostaticsDict = collections.OrderedDict()
99 ElectrostaticsDict['ElecRF'] = { 'define' : '#define CALC_COUL_RF' }
100 ElectrostaticsDict['ElecQSTab'] = { 'define' : '#define CALC_COUL_TAB' }
101 ElectrostaticsDict['ElecQSTabTwinCut'] = { 'define' : '#define CALC_COUL_TAB\n#define VDW_CUTOFF_CHECK /* Use twin-range cut-off */' }
102 ElectrostaticsDict['ElecEw'] = { 'define' : '#define CALC_COUL_EWALD' }
103 ElectrostaticsDict['ElecEwTwinCut'] = { 'define' : '#define CALC_COUL_EWALD\n#define VDW_CUTOFF_CHECK /* Use twin-range cut-off */' }
105 # The dict order must match the order of a C enumeration.
106 VdwTreatmentDict = collections.OrderedDict()
107 VdwTreatmentDict['VdwLJCombGeom'] = { 'define' : '#define LJ_CUT\n#define LJ_COMB_GEOM' }
108 VdwTreatmentDict['VdwLJCombLB'] = { 'define' : '#define LJ_CUT\n#define LJ_COMB_LB' }
109 VdwTreatmentDict['VdwLJ'] = { 'define' : '#define LJ_CUT\n/* Use full LJ combination matrix */' }
110 VdwTreatmentDict['VdwLJFSw'] = { 'define' : '#define LJ_FORCE_SWITCH\n/* Use full LJ combination matrix */' }
111 VdwTreatmentDict['VdwLJPSw'] = { 'define' : '#define LJ_POT_SWITCH\n/* Use full LJ combination matrix */' }
112 VdwTreatmentDict['VdwLJEwCombGeom'] = { 'define' : '#define LJ_CUT\n#define LJ_EWALD_GEOM\n/* Use full LJ combination matrix + geometric rule for the grid correction */' }
114 # This is OK as an unordered dict
115 EnergiesComputationDict = {
117 'function type' : 'nbk_func_noener',
118 'define' : '/* Will not calculate energies */',
121 'function type' : 'nbk_func_ener',
122 'define' : '#define CALC_ENERGIES',
125 'function type' : 'nbk_func_ener',
126 'define' : '#define CALC_ENERGIES\n#define ENERGY_GROUPS',
130 # This is OK as an unordered dict
131 VerletKernelTypeDict = {
133 'Define' : 'GMX_NBNXN_SIMD_2XNN',
134 'WidthSetup' : '/* Include the full-width SIMD macros */\n',
135 'WidthCheck' : ('#if !(GMX_SIMD_REAL_WIDTH == 8 || GMX_SIMD_REAL_WIDTH == 16)\n' \
136 '#error "unsupported SIMD width"\n' \
141 'Define' : 'GMX_NBNXN_SIMD_4XN',
143 'WidthCheck' : ('#if !(GMX_SIMD_REAL_WIDTH == 2 || GMX_SIMD_REAL_WIDTH == 4 || GMX_SIMD_REAL_WIDTH == 8)\n' \
144 '#error "unsupported SIMD width"\n' \
150 KernelsHeaderTemplate = read_kernel_template("nbnxn_kernel_simd_template.h.pre")
152 # For each Verlet kernel type, write two kinds of files:
153 # a header file defining the functions for all the kernels and
154 # the kernel function lookup table
155 # for each kernel, a file defining the single C function for that kernel
156 for type in VerletKernelTypeDict:
157 DirName = "../simd_{0}".format(type)
158 KernelNamePrefix = 'nbnxn_kernel'
159 KernelsName = "{0}_simd_{1}".format(KernelNamePrefix,type)
160 KernelsHeaderFileName = "{0}.h".format(KernelsName,type)
161 KernelsHeaderPathName = "gromacs/mdlib/nbnxn_kernels/simd_{0}/{1}".format(type,KernelsHeaderFileName)
162 KernelFunctionLookupTable = {}
163 KernelDeclarations = ''
164 KernelTemplate = read_kernel_template("{0}_kernel.cpp.pre".format(KernelsName))
166 # Loop over all kernels
167 for ener in EnergiesComputationDict:
168 KernelFunctionLookupTable[ener] = '{\n'
169 for elec in ElectrostaticsDict:
170 KernelFunctionLookupTable[ener] += ' {\n'
171 for ljtreat in VdwTreatmentDict:
172 KernelName = ('{0}_{1}_{2}_{3}_{4}'
173 .format(KernelNamePrefix,elec,ljtreat,ener,type))
175 # Declare the kernel function
176 KernelDeclarations += ('{1:21} {0};\n'
178 EnergiesComputationDict[ener]['function type']))
180 # Write the file with the kernel definition
181 with open('{0}/{1}.cpp'.format(DirName,KernelName), 'w') as kernelfp:
182 kernelfp.write(FileHeader.format(type))
183 kernelfp.write(KernelTemplate
184 .format(VerletKernelTypeDict[type]['Define'],
185 ElectrostaticsDict[elec]['define'],
186 VdwTreatmentDict[ljtreat]['define'],
187 EnergiesComputationDict[ener]['define'],
188 KernelsHeaderPathName,
190 " " * (len(KernelName) + 1),
191 VerletKernelTypeDict[type]['UnrollSize'],
195 # Enter the kernel function in the lookup table
196 KernelFunctionLookupTable[ener] += ' {0},\n'.format(KernelName)
198 KernelFunctionLookupTable[ener] += ' },\n'
199 KernelFunctionLookupTable[ener] += '};\n'
200 KernelDeclarations += '\n'
202 # Write the header file that declares all the kernel
203 # functions for this type
204 with open('{0}/{1}'.format(DirName,KernelsHeaderFileName),'w') as fp:
205 fp.write(FileHeader.format(type))
206 fp.write(KernelsHeaderTemplate
207 .format(KernelDeclarations,
209 KernelFunctionLookupTable['F'],
210 KernelFunctionLookupTable['VF'],
211 KernelFunctionLookupTable['VgrpF'])