Merge "Merge branch 'release-4-6'"
[alexxy/gromacs.git] / src / gromacs / mdlib / nbnxn_kernels / nbnxn_kernel_file_generator / make_verlet_simd_kernel_files.py
1 #!/usr/bin/python
2 #
3 # This file is part of the GROMACS molecular simulation package.
4 #
5 # Copyright (c) 2013, by the GROMACS development team, led by
6 # David van der Spoel, Berk Hess, Erik Lindahl, and including many
7 # others, as listed in the AUTHORS file in the top-level source
8 # 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 # 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
42 # are:
43 #
44 #   A single header file that declares all the kernel functions for
45 #   this nbnxn kernel structure type, including the function that does
46 #   the dispatch via the function pointer table.
47 #
48 #   A single C kernel dispatcher file that defines the function that
49 #   decides at run time which kernel to call.
50 #
51 #   Many C kernel files, each defining a single kernel function. These
52 #   functions can take a noticeable time to compile, and should tend
53 #   to be in seperate files to take advantage of make-time
54 #   parallelism.
55 #
56 # This script should be run from the directory in which it is
57 # located. The generated files are located in ../simd_<type>. There
58 # are three other files in those locations that are not generated. These
59 # contain:
60 #
61 #   setup logic peculiar to the kernel structure type but common to
62 #   all the kernels within that type, and
63 #
64 #   the logic for the outer and inner loops of the kernels, as
65 #   customized by numerous preprocessor defines to suit the hardware
66 #   and kernel type.
67 #
68 # Note that while functions for both nbnxn kernel structures are
69 # compiled and built into an mdrun executable, because that executable
70 # is not portable, only the functions for the useful nbnxn kernel
71 # structure for the hardware selected at CMake time contain real
72 # kernel logic. A run-time error occurs if an inappropriate kernel
73 # dispatcher function is called (but that is normally impossible).
74
75 import sys
76 import os
77 import collections # Requires Python 2.7
78
79 FileHeader = \
80 '/*\n' \
81 ' * This file is part of the GROMACS molecular simulation package.\n' \
82 ' *\n' \
83 ' * Copyright (c) 2012,2013, by the GROMACS development team, led by\n' \
84 ' * David van der Spoel, Berk Hess, Erik Lindahl, and including many\n' \
85 ' * others, as listed in the AUTHORS file in the top-level source\n' \
86 ' * directory and at http://www.gromacs.org.\n' \
87 ' *\n' \
88 ' * GROMACS is free software; you can redistribute it and/or\n' \
89 ' * modify it under the terms of the GNU Lesser General Public License\n' \
90 ' * as published by the Free Software Foundation; either version 2.1\n' \
91 ' * of the License, or (at your option) any later version.\n' \
92 ' *\n' \
93 ' * GROMACS is distributed in the hope that it will be useful,\n' \
94 ' * but WITHOUT ANY WARRANTY; without even the implied warranty of\n' \
95 ' * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n' \
96 ' * Lesser General Public License for more details.\n' \
97 ' *\n' \
98 ' * You should have received a copy of the GNU Lesser General Public\n' \
99 ' * License along with GROMACS; if not, see\n' \
100 ' * http://www.gnu.org/licenses, or write to the Free Software Foundation,\n' \
101 ' * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.\n' \
102 ' *\n' \
103 ' * If you want to redistribute modifications to GROMACS, please\n' \
104 ' * consider that scientific software is very special. Version\n' \
105 ' * control is crucial - bugs must be traceable. We will be happy to\n' \
106 ' * consider code for inclusion in the official distribution, but\n' \
107 ' * derived work must not be called official GROMACS. Details are found\n' \
108 ' * in the README & COPYING files - if they are missing, get the\n' \
109 ' * official version at http://www.gromacs.org.\n' \
110 ' *\n' \
111 ' * To help us fund GROMACS development, we humbly ask that you cite\n' \
112 ' * the research papers on the package. Check out http://www.gromacs.org.\n' \
113 ' */\n' \
114 '/*\n' \
115 ' * Note: this file was generated by the Verlet kernel generator for\n' \
116 ' * kernel type {0}.\n' \
117 ' */\n\n'
118
119 # The dict order must match the order of an enumeration in
120 # nbnxn_kernel_simd_template.c.pre
121 ElectrostaticsDict = collections.OrderedDict()
122 ElectrostaticsDict['rf'] = { 'define' : '#define CALC_COUL_RF' }
123 ElectrostaticsDict['tab'] = { 'define' : '#define CALC_COUL_TAB' }
124 ElectrostaticsDict['tab_twin'] = { 'define' : '#define CALC_COUL_TAB\n#define VDW_CUTOFF_CHECK /* Use twin-range cut-off */' }
125 ElectrostaticsDict['ewald'] = { 'define' : '#define CALC_COUL_EWALD' }
126 ElectrostaticsDict['ewald_twin'] = { 'define' : '#define CALC_COUL_EWALD\n#define VDW_CUTOFF_CHECK /* Use twin-range cut-off */' }
127
128 # The dict order must match the order of a C enumeration.
129 LJCombinationRuleDict = collections.OrderedDict()
130 LJCombinationRuleDict['geom'] = { 'define' : '#define LJ_COMB_GEOM' }
131 LJCombinationRuleDict['lb'] = { 'define' : '#define LJ_COMB_LB' }
132 LJCombinationRuleDict['none'] = { 'define' : '/* Use no LJ combination rule */' }
133
134 # This is OK as an unordered dict
135 EnergiesComputationDict = {
136     'ener'    : {
137         'function type' : 'nbk_func_ener',
138         'define' : '#define CALC_ENERGIES',
139     },
140     'energrp' : {
141         'function type' : 'nbk_func_ener',
142         'define' : '#define CALC_ENERGIES\n#define ENERGY_GROUPS',
143     },
144     'noener'  : {
145         'function type' : 'nbk_func_noener',
146         'define' : '/* Will not calculate energies */',
147     },
148 }
149
150 # This is OK as an unordered dict
151 VerletKernelTypeDict = {
152     '2xnn' : {
153         'Define' : 'GMX_NBNXN_SIMD_2XNN',
154         'WidthSetup' : '/* Include the full-width SIMD macros */\n',
155         'WidthCheck' : ('#if !(GMX_SIMD_WIDTH_HERE == 8 || GMX_SIMD_WIDTH_HERE == 16)\n' \
156                         '#error "unsupported SIMD width"\n' \
157                         '#endif\n'),
158         'UnrollSize' : 2,
159     },
160     '4xn' : {
161         'Define' : 'GMX_NBNXN_SIMD_4XN',
162         'WidthSetup' : ('#ifdef GMX_NBNXN_HALF_WIDTH_SIMD\n' \
163                         '#define GMX_USE_HALF_WIDTH_SIMD_HERE\n' \
164                         '#endif\n'),
165         'WidthCheck' : ('#if !(GMX_SIMD_WIDTH_HERE == 2 || GMX_SIMD_WIDTH_HERE == 4 || GMX_SIMD_WIDTH_HERE == 8)\n' \
166                         '#error "unsupported SIMD width"\n' \
167                         '#endif\n'),
168         'UnrollSize' : 1,
169     },
170 }
171
172 with open ("nbnxn_kernel_simd_template.c.pre", "r") as KernelDispatcherTemplateFile:
173     KernelDispatcherTemplate = KernelDispatcherTemplateFile.read()
174
175 with open ("nbnxn_kernel_simd_template.h.pre", "r") as KernelsHeaderTemplateFile:
176     KernelsHeaderTemplate =  KernelsHeaderTemplateFile.read()
177
178 # For each Verlet kernel type, write three kinds of files:
179 #   a header file defining the functions for all the kernels,
180 #   a code file containing the kernel function lookup table and
181 #     the kernel dispatcher function
182 #   for each kernel, a file defining the single C function for that kernel
183 for type in VerletKernelTypeDict:
184     DirName = "../simd_{0}".format(type)
185     KernelNamePrefix = 'nbnxn_kernel_simd_{0}'.format(type)
186     KernelsHeaderFileName = "{0}.h".format(KernelNamePrefix)
187     KernelFunctionLookupTable = {}
188     KernelDeclarations = ''
189     with open ("{1}_kernel.c.pre".format(DirName,KernelNamePrefix), "r") as KernelTemplateFile:
190         KernelTemplate =  KernelTemplateFile.read()
191
192     # Loop over all kernels
193     for ener in EnergiesComputationDict:
194         KernelFunctionLookupTable[ener] = '{\n'
195         for elec in ElectrostaticsDict:
196             KernelFunctionLookupTable[ener] += '    {\n'
197             for ljcomb in LJCombinationRuleDict:
198                 KernelName = ('{0}_{1}_comb_{2}_{3}'
199                               .format(KernelNamePrefix,elec,ljcomb,ener))
200
201                 # Declare the kernel function
202                 KernelDeclarations += ('{1:21} {0};\n'
203                                        .format(KernelName,
204                                                EnergiesComputationDict[ener]['function type']))
205
206                 # Write the file with the kernel definition
207                 with open('{0}/{1}.c'.format(DirName,KernelName), 'w') as kernelfp:
208                     kernelfp.write(FileHeader.format(type))
209                     kernelfp.write(KernelTemplate
210                                    .format(VerletKernelTypeDict[type]['Define'],
211                                            ElectrostaticsDict[elec]['define'],
212                                            LJCombinationRuleDict[ljcomb]['define'],
213                                            EnergiesComputationDict[ener]['define'],
214                                            KernelsHeaderFileName,
215                                            KernelName,
216                                            " " * (len(KernelName) + 1),
217                                            VerletKernelTypeDict[type]['UnrollSize'],
218                                        )
219                                )
220
221                 # Enter the kernel function in the lookup table
222                 KernelFunctionLookupTable[ener] += '        {0},\n'.format(KernelName)
223
224             KernelFunctionLookupTable[ener] += '    },\n'
225         KernelFunctionLookupTable[ener] += '};\n'
226         KernelDeclarations += '\n'
227
228     # Write the header file that declares all the kernel
229     # functions for this type
230     with open('{0}/{1}'.format(DirName,KernelsHeaderFileName),'w') as fp:
231         fp.write(FileHeader.format(type))
232         fp.write(KernelsHeaderTemplate
233                  .format(KernelNamePrefix,
234                          " " * (len(KernelNamePrefix) + 1),
235                          KernelDeclarations))
236
237     # Write the file defining the kernel dispatcher
238     # function for this type
239     with open('{0}/{1}'.format(DirName,"{0}.c".format(KernelNamePrefix)),'w') as fp:
240         fp.write(FileHeader.format(type))
241         fp.write(KernelDispatcherTemplate
242                  .format(VerletKernelTypeDict[type]['Define'],
243                          VerletKernelTypeDict[type]['WidthSetup'],
244                          VerletKernelTypeDict[type]['WidthCheck'],
245                          VerletKernelTypeDict[type]['UnrollSize'],
246                          KernelsHeaderFileName,
247                          KernelNamePrefix,
248                          ' ' * (len(KernelNamePrefix)+1),
249                          KernelFunctionLookupTable['ener'],
250                          KernelFunctionLookupTable['energrp'],
251                          KernelFunctionLookupTable['noener'],
252                      )
253              )
254
255 sys.exit()