3 # This file is part of the GROMACS molecular simulation package.
5 # Copyright (c) 2012,2013,2014, 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.
38 sys.path.append("../preprocessor")
39 sys.path.append("../../../../../admin")
40 from copyright import create_copyright_header
41 from gmxpreprocess import gmxpreprocess
43 # "The happiest programs are programs that write other programs."
46 # This script controls the generation of Gromacs nonbonded kernels.
48 # We no longer generate kernels on-the-fly, so this file is not run
49 # during a Gromacs compile - only when we need to update the kernels (=rarely).
51 # To maximize performance, each combination of interactions in Gromacs
52 # has a separate nonbonded kernel without conditionals in the code.
53 # To avoid writing hundreds of different routines for each architecture,
54 # we instead use a custom preprocessor so we can encode the conditionals
55 # and expand for-loops (e.g, for water-water interactions)
56 # from a general kernel template. While that file will contain quite a
57 # few preprocessor directives, it is still an order of magnitude easier
58 # to maintain than ~200 different kernels (not to mention it avoids bugs).
60 # To actually generate the kernels, this program iteratively calls the
61 # preprocessor with different define settings corresponding to all
62 # combinations of coulomb/van-der-Waals/geometry options.
64 # A main goal in the design was to make this new generator _general_. For
65 # this reason we have used a lot of different fields to identify a particular
66 # kernel and interaction. Basically, each kernel will have a name like
68 # nbkernel_ElecXX_VdwYY_GeomZZ_VF_QQ()
70 # Where XX/YY/ZZ/VF are strings to identify what the kernel computes.
72 # Elec/Vdw describe the type of interaction for electrostatics and van der Waals.
73 # The geometry settings correspond e.g. to water-water or water-particle kernels,
74 # and finally the VF setting is V,F,or VF depending on whether we calculate
75 # only the potential, only the force, or both of them. The final string (QQ)
76 # is the architecture/language/optimization of the kernel.
78 Arch = 'sparc64_hpc_ace_double'
80 # Explanation of the 'properties':
82 # It is cheap to compute r^2, and the kernels require various other functions of r for
83 # different kinds of interaction. Depending on the needs of the kernel and the available
84 # processor instructions, this will be done in different ways.
86 # 'rinv' means we need 1/r, which is calculated as 1/sqrt(r^2).
87 # 'rinvsq' means we need 1/(r*r). This is calculated as rinv*rinv if we already did rinv, otherwise 1/r^2.
88 # 'r' is similarly calculated as r^2*rinv when needed
89 # 'table' means the interaction is tabulated, in which case we will calculate a table index before the interaction
90 # 'shift' means the interaction will be modified by a constant to make it zero at the cutoff.
91 # 'cutoff' means the interaction is set to 0.0 outside the cutoff
94 FileHeader = create_copyright_header('2012,2013,2014')
96 * Note: this file was generated by the GROMACS """+Arch+""" kernel generator.
100 ###############################################
102 # Interactions and flags for them
103 ###############################################
104 ElectrostaticsList = {
106 'Coulomb' : ['rinv','rinvsq'],
107 'ReactionField' : ['rinv','rinvsq'],
108 'GeneralizedBorn' : ['rinv','r'],
109 'CubicSplineTable' : ['rinv','r','table'],
110 'Ewald' : ['rinv','rinvsq','r'],
114 ###############################################
116 # Interactions and flags for them
117 ###############################################
120 'LennardJones' : ['rinvsq'],
121 # 'Buckingham' : ['rinv','rinvsq','r'], # Disabled for sse4.1 to reduce number of kernels and simply the template
122 'CubicSplineTable' : ['rinv','r','table'],
123 'LJEwald' : ['rinv','rinvsq','r'],
127 ###############################################
129 # Different ways to adjust/modify interactions to conserve energy
130 ###############################################
133 'ExactCutoff' : ['exactcutoff'], # Zero the interaction outside the cutoff, used for reaction-field-zero
134 'PotentialShift' : ['shift','exactcutoff'],
135 'PotentialSwitch' : ['rinv','r','switch','exactcutoff']
139 ###############################################
140 # GEOMETRY COMBINATIONS
141 ###############################################
143 [ 'Particle' , 'Particle' ],
144 [ 'Water3' , 'Particle' ],
145 [ 'Water3' , 'Water3' ],
146 [ 'Water4' , 'Particle' ],
147 [ 'Water4' , 'Water4' ]
151 ###############################################
153 ###############################################
156 # 'Potential', # Not used yet
161 ###############################################
162 # GEOMETRY PROPERTIES
163 ###############################################
164 # Dictionaries with lists telling which interactions are present
165 # 1,2,3 means particles 1,2,3 (but not 0) have electrostatics!
166 GeometryElectrostatics = {
168 'Particle2' : [ 0 , 1 ],
169 'Particle3' : [ 0 , 1 , 2 ],
170 'Particle4' : [ 0 , 1 , 2 , 3 ],
171 'Water3' : [ 0 , 1 , 2 ],
172 'Water4' : [ 1 , 2 , 3 ]
177 'Particle2' : [ 0 , 1 ],
178 'Particle3' : [ 0 , 1 , 2 ],
179 'Particle4' : [ 0 , 1 , 2 , 3 ],
187 # Dictionary to abbreviate all strings (mixed from all the lists)
192 'ReactionField' : 'RF',
193 'GeneralizedBorn' : 'GB',
194 'CubicSplineTable' : 'CSTab',
195 'LennardJones' : 'LJ',
196 'Buckingham' : 'Bham',
198 'PotentialShift' : 'Sh',
199 'PotentialSwitch' : 'Sw',
200 'ExactCutoff' : 'Cut',
201 'PotentialAndForce' : 'VF',
213 ###############################################
215 ###############################################
217 # Return a string with the kernel name from current settings
218 def MakeKernelFileName(KernelElec,KernelElecMod,KernelVdw,KernelVdwMod,KernelGeom):
219 ElecStr = 'Elec' + Abbreviation[KernelElec]
220 if(KernelElecMod!='None'):
221 ElecStr = ElecStr + Abbreviation[KernelElecMod]
222 VdwStr = 'Vdw' + Abbreviation[KernelVdw]
223 if(KernelVdwMod!='None'):
224 VdwStr = VdwStr + Abbreviation[KernelVdwMod]
225 GeomStr = 'Geom' + Abbreviation[KernelGeom[0]] + Abbreviation[KernelGeom[1]]
226 return 'nb_kernel_' + ElecStr + '_' + VdwStr + '_' + GeomStr + '_' + Arch
228 def MakeKernelName(KernelElec,KernelElecMod,KernelVdw,KernelVdwMod,KernelGeom,KernelVF):
229 ElecStr = 'Elec' + Abbreviation[KernelElec]
230 if(KernelElecMod!='None'):
231 ElecStr = ElecStr + Abbreviation[KernelElecMod]
232 VdwStr = 'Vdw' + Abbreviation[KernelVdw]
233 if(KernelVdwMod!='None'):
234 VdwStr = VdwStr + Abbreviation[KernelVdwMod]
235 GeomStr = 'Geom' + Abbreviation[KernelGeom[0]] + Abbreviation[KernelGeom[1]]
236 VFStr = Abbreviation[KernelVF]
237 return 'nb_kernel_' + ElecStr + '_' + VdwStr + '_' + GeomStr + '_' + VFStr + '_' + Arch
239 # Return a string with a declaration to use for the kernel;
240 # this will be a sequence of string combinations as well as the actual function name
241 # Dont worry about field widths - that is just pretty-printing for the header!
242 def MakeKernelDecl(KernelName,KernelElec,KernelElecMod,KernelVdw,KernelVdwMod,KernelGeom,KernelOther,KernelVF):
243 KernelStr = '\"'+KernelName+'\"'
244 ArchStr = '\"'+Arch+'\"'
245 ElecStr = '\"'+KernelElec+'\"'
246 ElecModStr = '\"'+KernelElecMod+'\"'
247 VdwStr = '\"'+KernelVdw+'\"'
248 VdwModStr = '\"'+KernelVdwMod+'\"'
249 GeomStr = '\"'+KernelGeom[0]+KernelGeom[1]+'\"'
250 OtherStr = '\"'+KernelOther+'\"'
251 VFStr = '\"'+KernelVF+'\"'
253 ThisSpec = ArchStr+', '+ElecStr+', '+ElecModStr+', '+VdwStr+', '+VdwModStr+', '+GeomStr+', '+OtherStr+', '+VFStr
254 ThisDecl = ' { '+KernelName+', '+KernelStr+', '+ThisSpec+' }'
258 # Returns 1 if this kernel should be created, 0 if we should skip it
259 # This routine is not critical - it is not the end of the world if we create more kernels,
260 # but since the number is pretty large we save both space and compile-time by reducing it a bit.
261 def KeepKernel(KernelElec,KernelElecMod,KernelVdw,KernelVdwMod,KernelGeom,KernelVF):
263 # No need for kernels without interactions
264 if(KernelElec=='None' and KernelVdw=='None'):
267 # No need for modifiers without interactions
268 if((KernelElec=='None' and KernelElecMod!='None') or (KernelVdw=='None' and KernelVdwMod!='None')):
271 # No need for LJ-only water optimization, or water optimization with implicit solvent.
272 if('Water' in KernelGeom[0] and (KernelElec=='None' or 'GeneralizedBorn' in KernelElec)):
275 # Non-matching table settings are pointless
276 if( ('Table' in KernelElec) and ('Table' in KernelVdw) and KernelElec!=KernelVdw ):
279 # Try to reduce the number of different switch/shift options to get a reasonable number of kernels
280 # For electrostatics, reaction-field can use 'exactcutoff', and ewald can use switch or shift.
281 if(KernelElecMod=='ExactCutoff' and KernelElec!='ReactionField'):
283 if(KernelElecMod in ['PotentialShift','PotentialSwitch'] and KernelElec!='Ewald'):
285 # For Vdw, we support switch and shift for Lennard-Jones/Buckingham
286 if((KernelVdwMod=='ExactCutoff') or
287 (KernelVdwMod in ['PotentialShift','PotentialSwitch'] and KernelVdw not in ['LennardJones','Buckingham','LJEwald'])):
290 # For LJEwald, we only support shift
291 if(KernelVdw=='LJEwald' and KernelVdwMod=='PotentialSwitch'):
294 # Choose either switch or shift and don't mix them...
295 if((KernelElecMod=='PotentialShift' and KernelVdwMod=='PotentialSwitch') or
296 (KernelElecMod=='PotentialSwitch' and KernelVdwMod=='PotentialShift')):
299 # Don't use a Vdw kernel with a modifier if the electrostatics one does not have one
300 if(KernelElec!='None' and KernelElecMod=='None' and KernelVdwMod!='None'):
303 # Don't use an electrostatics kernel with a modifier if the vdw one does not have one,
304 # unless the electrostatics one is reaction-field with exact cutoff.
305 if(KernelVdw!='None' and KernelVdwMod=='None' and KernelElecMod!='None'):
306 if(KernelElec=='ReactionField' and KernelVdw!='CubicSplineTable'):
308 elif(KernelElec!='ReactionField'):
311 #Only do LJ-PME if we are also doing PME for electrostatics, or no electrostatics at all.
312 if(KernelVdw=='LJEwald' and KernelElec not in ['Ewald','None']):
320 # The preprocessor will automatically expand the interactions for water and other
321 # geometries inside the kernel, but to get this right we need to setup a couple
322 # of defines - we do them in a separate routine to keep the main loop clean.
324 # While this routine might look a bit complex it is actually quite straightforward,
325 # and the best news is that you wont have to modify _anything_ for a new geometry
326 # as long as you correctly define its Electrostatics/Vdw geometry in the lists above!
328 def SetDefines(KernelElec,KernelElecMod,KernelVdw,KernelVdwMod,KernelGeom,KernelVF,defines):
329 # What is the _name_ for the i/j group geometry?
330 igeometry = KernelGeom[0]
331 jgeometry = KernelGeom[1]
332 # define so we can access it in the source when the preprocessor runs
333 defines['GEOMETRY_I'] = igeometry
334 defines['GEOMETRY_J'] = jgeometry
336 # For the i/j groups, extract a python list of which sites have electrostatics
337 # For SPC/TIP3p this will be [1,1,1], while TIP4p (no elec on first site) will be [0,1,1,1]
338 ielec = GeometryElectrostatics[igeometry]
339 jelec = GeometryElectrostatics[jgeometry]
340 # Zero out the corresponding lists in case we dont do Elec
341 if(KernelElec=='None'):
345 # Extract similar interaction lists for Vdw interactions (example for SPC: [1,0,0])
346 iVdw = GeometryVdw[igeometry]
347 jVdw = GeometryVdw[jgeometry]
349 # Zero out the corresponding lists in case we dont do Vdw
350 if(KernelVdw=='None'):
354 # iany[] and jany[] contains lists of the particles actually used (for interactions) in this kernel
355 iany = list(set(ielec+iVdw)) # convert to+from set to make elements unique
356 jany = list(set(jelec+jVdw))
358 defines['PARTICLES_ELEC_I'] = ielec
359 defines['PARTICLES_ELEC_J'] = jelec
360 defines['PARTICLES_VDW_I'] = iVdw
361 defines['PARTICLES_VDW_J'] = jVdw
362 defines['PARTICLES_I'] = iany
363 defines['PARTICLES_J'] = jany
365 # elecij,Vdwij are sets with pairs of particles for which the corresponding interaction is done
366 # (and anyij again corresponds to either electrostatics or Vdw)
381 if [i,j] in elecij or [i,j] in Vdwij:
384 defines['PAIRS_IJ'] = anyij
386 # Make an 2d list-of-distance-properties-to-calculate for i,j
389 # Each element properties[i][j] is an empty list
390 properties = [ [ [] for j in range(0,nj) ] for i in range (0,ni) ]
391 # Add properties to each set
392 for i in range(0,ni):
393 for j in range(0,nj):
395 properties[i][j] = properties[i][j] + ['electrostatics'] + ElectrostaticsList[KernelElec] + ModifierList[KernelElecMod]
397 properties[i][j] = properties[i][j] + ['vdw'] + VdwList[KernelVdw] + ModifierList[KernelVdwMod]
398 # Add rinv if we need r
399 if 'r' in properties[i][j]:
400 properties[i][j] = properties[i][j] + ['rinv']
401 # Add rsq if we need rinv or rinsq
402 if 'rinv' in properties[i][j] or 'rinvsq' in properties[i][j]:
403 properties[i][j] = properties[i][j] + ['rsq']
405 defines['INTERACTION_FLAGS'] = properties
409 def PrintStatistics(ratio):
411 print '\rGenerating %s nonbonded kernels... %5.1f%%' % (Arch,ratio),
420 nelec = len(ElectrostaticsList)
422 nmod = len(ModifierList)
423 ngeom = len(GeometryNameList)
425 ntot = nelec*nmod*nVdw*nmod*ngeom
429 fpdecl = open('nb_kernel_' + Arch + '.c','w')
430 fpdecl.write( FileHeader )
431 fpdecl.write( '#ifndef nb_kernel_' + Arch + '_h\n' )
432 fpdecl.write( '#define nb_kernel_' + Arch + '_h\n\n' )
433 fpdecl.write( '#include "../nb_kernel.h"\n\n' )
435 for KernelElec in ElectrostaticsList:
436 defines['KERNEL_ELEC'] = KernelElec
438 for KernelElecMod in ModifierList:
439 defines['KERNEL_MOD_ELEC'] = KernelElecMod
441 for KernelVdw in VdwList:
442 defines['KERNEL_VDW'] = KernelVdw
444 for KernelVdwMod in ModifierList:
445 defines['KERNEL_MOD_VDW'] = KernelVdwMod
447 for KernelGeom in GeometryNameList:
450 KernelFilename = MakeKernelFileName(KernelElec,KernelElecMod,KernelVdw,KernelVdwMod,KernelGeom) + '.c'
451 fpkernel = open(KernelFilename,'w')
452 defines['INCLUDE_HEADER'] = 1 # Include header first time in new file
455 for KernelVF in VFList:
457 KernelName = MakeKernelName(KernelElec,KernelElecMod,KernelVdw,KernelVdwMod,KernelGeom,KernelVF)
459 defines['KERNEL_NAME'] = KernelName
460 defines['KERNEL_VF'] = KernelVF
462 # Check if this is a valid/sane/usable combination
463 if not KeepKernel(KernelElec,KernelElecMod,KernelVdw,KernelVdwMod,KernelGeom,KernelVF):
466 # The overall kernel settings determine what the _kernel_ calculates, but for the water
467 # kernels this does not mean that every pairwise interaction has e.g. Vdw interactions.
468 # This routine sets defines of what to calculate for each pair of particles in those cases.
469 SetDefines(KernelElec,KernelElecMod,KernelVdw,KernelVdwMod,KernelGeom,KernelVF,defines)
472 fpkernel.write( FileHeader )
474 gmxpreprocess('nb_kernel_template_' + Arch + '.pre', KernelName+'.tmp' , defines, force=1,contentType='C')
475 numKernels = numKernels + 1
477 defines['INCLUDE_HEADER'] = 0 # Header has been included once now
480 # Append temp file contents to the common kernelfile
481 fptmp = open(KernelName+'.tmp','r')
482 fpkernel.writelines(fptmp.readlines())
484 os.remove(KernelName+'.tmp')
486 # Add a declaration for this kernel
487 fpdecl.write('nb_kernel_t ' + KernelName + ';\n');
489 # Add declaration to the buffer
491 kerneldecl.append(MakeKernelDecl(KernelName,KernelElec,KernelElecMod,KernelVdw,KernelVdwMod,KernelGeom,KernelOther,KernelVF))
493 filesize = fpkernel.tell()
496 os.remove(KernelFilename)
498 PrintStatistics(cnt/ntot)
505 # Write out the list of settings and corresponding kernels to the declaration file
506 fpdecl.write( '\n\n' )
507 fpdecl.write( 'nb_kernel_info_t\n' )
508 fpdecl.write( ' kernellist_'+Arch+'[] =\n' )
509 fpdecl.write( '{\n' )
510 for decl in kerneldecl[0:-1]:
511 fpdecl.write( decl + ',\n' )
512 fpdecl.write( kerneldecl[-1] + '\n' )
513 fpdecl.write( '};\n\n' )
514 fpdecl.write( 'int\n' )
515 fpdecl.write( ' kernellist_'+Arch+'_size = sizeof(kernellist_'+Arch+')/sizeof(kernellist_'+Arch+'[0]);\n\n')
516 fpdecl.write( '#endif\n')