0567e1d81244f74ac7d799cfb3f98d32845c145b
[alexxy/gromacs.git] / src / gromacs / gmxlib / nonbonded / nonbonded.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014,2015,2017,2018, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 #include "gmxpre.h"
38
39 #include "nonbonded.h"
40
41 #include "config.h"
42
43 #include <cassert>
44 #include <cstdio>
45 #include <cstdlib>
46
47 #include "thread_mpi/threads.h"
48
49 #include "gromacs/gmxlib/nrnb.h"
50 #include "gromacs/gmxlib/nonbonded/nb_free_energy.h"
51 #include "gromacs/gmxlib/nonbonded/nb_generic.h"
52 #include "gromacs/gmxlib/nonbonded/nb_generic_cg.h"
53 #include "gromacs/gmxlib/nonbonded/nb_kernel.h"
54 #include "gromacs/listed-forces/bonded.h"
55 #include "gromacs/math/utilities.h"
56 #include "gromacs/math/vec.h"
57 #include "gromacs/mdtypes/enerdata.h"
58 #include "gromacs/mdtypes/forcerec.h"
59 #include "gromacs/mdtypes/md_enums.h"
60 #include "gromacs/mdtypes/mdatom.h"
61 #include "gromacs/mdtypes/nblist.h"
62 #include "gromacs/pbcutil/ishift.h"
63 #include "gromacs/pbcutil/mshift.h"
64 #include "gromacs/pbcutil/pbc.h"
65 #include "gromacs/tables/forcetable.h"
66 #include "gromacs/utility/arraysize.h"
67 #include "gromacs/utility/basedefinitions.h"
68 #include "gromacs/utility/cstringutil.h"
69 #include "gromacs/utility/fatalerror.h"
70 #include "gromacs/utility/smalloc.h"
71
72 /* Different default (c) and SIMD instructions interaction-specific kernels */
73 #if !GMX_CLANG_ANALYZER
74 #include "gromacs/gmxlib/nonbonded/nb_kernel_c/nb_kernel_c.h"
75 #endif
76
77 #if GMX_SIMD_X86_SSE2 && !GMX_DOUBLE
78 #    include "gromacs/gmxlib/nonbonded/nb_kernel_sse2_single/nb_kernel_sse2_single.h"
79 #endif
80 #if GMX_SIMD_X86_SSE4_1 && !GMX_DOUBLE
81 #    include "gromacs/gmxlib/nonbonded/nb_kernel_sse4_1_single/nb_kernel_sse4_1_single.h"
82 #endif
83 #if GMX_SIMD_X86_AVX_128_FMA && !GMX_DOUBLE
84 #    include "gromacs/gmxlib/nonbonded/nb_kernel_avx_128_fma_single/nb_kernel_avx_128_fma_single.h"
85 #endif
86 #if (GMX_SIMD_X86_AVX_256 || GMX_SIMD_X86_AVX2_256) && !GMX_DOUBLE
87 #    include "gromacs/gmxlib/nonbonded/nb_kernel_avx_256_single/nb_kernel_avx_256_single.h"
88 #endif
89 #if GMX_SIMD_X86_SSE2 && GMX_DOUBLE
90 #    include "gromacs/gmxlib/nonbonded/nb_kernel_sse2_double/nb_kernel_sse2_double.h"
91 #endif
92 #if GMX_SIMD_X86_SSE4_1 && GMX_DOUBLE
93 #    include "gromacs/gmxlib/nonbonded/nb_kernel_sse4_1_double/nb_kernel_sse4_1_double.h"
94 #endif
95 #if GMX_SIMD_X86_AVX_128_FMA && GMX_DOUBLE
96 #    include "gromacs/gmxlib/nonbonded/nb_kernel_avx_128_fma_double/nb_kernel_avx_128_fma_double.h"
97 #endif
98 #if (GMX_SIMD_X86_AVX_256 || GMX_SIMD_X86_AVX2_256) && GMX_DOUBLE
99 #    include "gromacs/gmxlib/nonbonded/nb_kernel_avx_256_double/nb_kernel_avx_256_double.h"
100 #endif
101 #if GMX_SIMD_SPARC64_HPC_ACE && GMX_DOUBLE
102 #    include "gromacs/gmxlib/nonbonded/nb_kernel_sparc64_hpc_ace_double/nb_kernel_sparc64_hpc_ace_double.h"
103 #endif
104
105
106 static tMPI_Thread_mutex_t nonbonded_setup_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
107 static gmx_bool            nonbonded_setup_done  = FALSE;
108
109
110 void
111 gmx_nonbonded_setup(t_forcerec *   fr,
112                     gmx_bool       bGenericKernelOnly)
113 {
114     tMPI_Thread_mutex_lock(&nonbonded_setup_mutex);
115     /* Here we are guaranteed only one thread made it. */
116     if (!nonbonded_setup_done)
117     {
118         if (!bGenericKernelOnly)
119         {
120             /* Add the generic kernels to the structure stored statically in nb_kernel.c */
121 #if !GMX_CLANG_ANALYZER
122             nb_kernel_list_add_kernels(kernellist_c, kernellist_c_size);
123 #endif
124
125             if (!(fr != nullptr && !fr->use_simd_kernels))
126             {
127                 /* Add interaction-specific kernels for different architectures */
128                 /* Single precision */
129 #if GMX_SIMD_X86_SSE2 && !GMX_DOUBLE
130                 nb_kernel_list_add_kernels(kernellist_sse2_single, kernellist_sse2_single_size);
131 #endif
132 #if GMX_SIMD_X86_SSE4_1 && !GMX_DOUBLE
133                 nb_kernel_list_add_kernels(kernellist_sse4_1_single, kernellist_sse4_1_single_size);
134 #endif
135 #if GMX_SIMD_X86_AVX_128_FMA && !GMX_DOUBLE
136                 nb_kernel_list_add_kernels(kernellist_avx_128_fma_single, kernellist_avx_128_fma_single_size);
137 #endif
138 #if (GMX_SIMD_X86_AVX_256 || GMX_SIMD_X86_AVX2_256) && !GMX_DOUBLE
139                 nb_kernel_list_add_kernels(kernellist_avx_256_single, kernellist_avx_256_single_size);
140 #endif
141                 /* Double precision */
142 #if GMX_SIMD_X86_SSE2 && GMX_DOUBLE
143                 nb_kernel_list_add_kernels(kernellist_sse2_double, kernellist_sse2_double_size);
144 #endif
145 #if GMX_SIMD_X86_SSE4_1 && GMX_DOUBLE
146                 nb_kernel_list_add_kernels(kernellist_sse4_1_double, kernellist_sse4_1_double_size);
147 #endif
148 #if GMX_SIMD_X86_AVX_128_FMA && GMX_DOUBLE
149                 nb_kernel_list_add_kernels(kernellist_avx_128_fma_double, kernellist_avx_128_fma_double_size);
150 #endif
151 #if (GMX_SIMD_X86_AVX_256 || GMX_SIMD_X86_AVX2_256) && GMX_DOUBLE
152                 nb_kernel_list_add_kernels(kernellist_avx_256_double, kernellist_avx_256_double_size);
153 #endif
154 #if GMX_SIMD_SPARC64_HPC_ACE && GMX_DOUBLE
155                 nb_kernel_list_add_kernels(kernellist_sparc64_hpc_ace_double, kernellist_sparc64_hpc_ace_double_size);
156 #endif
157                 ; /* empty statement to avoid a completely empty block */
158             }
159         }
160         /* Create a hash for faster lookups */
161         nb_kernel_list_hash_init();
162
163         nonbonded_setup_done = TRUE;
164     }
165     tMPI_Thread_mutex_unlock(&nonbonded_setup_mutex);
166 }
167
168
169
170 void
171 gmx_nonbonded_set_kernel_pointers(FILE *log, t_nblist *nl, gmx_bool bElecAndVdwSwitchDiffers)
172 {
173     const char *     elec;
174     const char *     elec_mod;
175     const char *     vdw;
176     const char *     vdw_mod;
177     const char *     geom;
178     const char *     other;
179
180     struct
181     {
182         const char *  arch;
183         int           simd_padding_width;
184     }
185     arch_and_padding[] =
186     {
187         /* Single precision */
188 #if (GMX_SIMD_X86_AVX_256 || GMX_SIMD_X86_AVX2_256) && !GMX_DOUBLE
189         { "avx_256_single", 8 },
190 #endif
191 #if GMX_SIMD_X86_AVX_128_FMA && !GMX_DOUBLE
192         { "avx_128_fma_single", 4 },
193 #endif
194 #if GMX_SIMD_X86_SSE4_1 && !GMX_DOUBLE
195         { "sse4_1_single", 4 },
196 #endif
197 #if GMX_SIMD_X86_SSE2 && !GMX_DOUBLE
198         { "sse2_single", 4 },
199 #endif
200         /* Double precision */
201 #if (GMX_SIMD_X86_AVX_256 || GMX_SIMD_X86_AVX2_256) && GMX_DOUBLE
202         { "avx_256_double", 4 },
203 #endif
204 #if GMX_SIMD_X86_AVX_128_FMA && GMX_DOUBLE
205         /* Sic. Double precision 2-way SIMD does not require neighbor list padding,
206          * since the kernels execute a loop unrolled a factor 2, followed by
207          * a possible single odd-element epilogue.
208          */
209         { "avx_128_fma_double", 1 },
210 #endif
211 #if GMX_SIMD_X86_SSE2 && GMX_DOUBLE
212         /* No padding - see comment above */
213         { "sse2_double", 1 },
214 #endif
215 #if GMX_SIMD_X86_SSE4_1 && GMX_DOUBLE
216         /* No padding - see comment above */
217         { "sse4_1_double", 1 },
218 #endif
219 #if GMX_SIMD_SPARC64_HPC_ACE && GMX_DOUBLE
220         /* No padding - see comment above */
221         { "sparc64_hpc_ace_double", 1 },
222 #endif
223         { "c", 1 },
224     };
225     int              narch = asize(arch_and_padding);
226     int              i;
227
228     if (!nonbonded_setup_done)
229     {
230         /* We typically call this setup routine before starting timers,
231          * but if that has not been done for whatever reason we do it now.
232          */
233         gmx_nonbonded_setup(nullptr, FALSE);
234     }
235
236     /* Not used yet */
237     other = "";
238
239     nl->kernelptr_vf = nullptr;
240     nl->kernelptr_v  = nullptr;
241     nl->kernelptr_f  = nullptr;
242
243     elec     = gmx_nbkernel_elec_names[nl->ielec];
244     elec_mod = eintmod_names[nl->ielecmod];
245     vdw      = gmx_nbkernel_vdw_names[nl->ivdw];
246     vdw_mod  = eintmod_names[nl->ivdwmod];
247     geom     = gmx_nblist_geometry_names[nl->igeometry];
248
249     if (nl->type == GMX_NBLIST_INTERACTION_FREE_ENERGY)
250     {
251         nl->kernelptr_vf       = reinterpret_cast<void *>(gmx_nb_free_energy_kernel);
252         nl->kernelptr_f        = reinterpret_cast<void *>(gmx_nb_free_energy_kernel);
253         nl->simd_padding_width = 1;
254     }
255     else if (!gmx_strcasecmp_min(geom, "CG-CG"))
256     {
257         nl->kernelptr_vf       = reinterpret_cast<void *>(gmx_nb_generic_cg_kernel);
258         nl->kernelptr_f        = reinterpret_cast<void *>(gmx_nb_generic_cg_kernel);
259         nl->simd_padding_width = 1;
260     }
261     else
262     {
263         /* Try to find a specific kernel first */
264
265         for (i = 0; i < narch && nl->kernelptr_vf == nullptr; i++)
266         {
267             nl->kernelptr_vf       = reinterpret_cast<void *>(nb_kernel_list_findkernel(log, arch_and_padding[i].arch, elec, elec_mod, vdw, vdw_mod, geom, other, "PotentialAndForce"));
268             nl->simd_padding_width = arch_and_padding[i].simd_padding_width;
269         }
270         for (i = 0; i < narch && nl->kernelptr_f == nullptr; i++)
271         {
272             nl->kernelptr_f        = reinterpret_cast<void *>(nb_kernel_list_findkernel(log, arch_and_padding[i].arch, elec, elec_mod, vdw, vdw_mod, geom, other, "Force"));
273             nl->simd_padding_width = arch_and_padding[i].simd_padding_width;
274
275             /* If there is not force-only optimized kernel, is there a potential & force one? */
276             if (nl->kernelptr_f == nullptr)
277             {
278                 nl->kernelptr_f        = reinterpret_cast<void *>(nb_kernel_list_findkernel(nullptr, arch_and_padding[i].arch, elec, elec_mod, vdw, vdw_mod, geom, other, "PotentialAndForce"));
279                 nl->simd_padding_width = arch_and_padding[i].simd_padding_width;
280             }
281         }
282
283         /* For now, the accelerated kernels cannot handle the combination of switch functions for both
284          * electrostatics and VdW that use different switch radius or switch cutoff distances
285          * (both of them enter in the switch function calculation). This would require
286          * us to evaluate two completely separate switch functions for every interaction.
287          * Instead, we disable such kernels by setting the pointer to NULL.
288          * This will cause the generic kernel (which can handle it) to be called instead.
289          *
290          * Note that we typically already enable tabulated coulomb interactions for this case,
291          * so this is mostly a safe-guard to make sure we call the generic kernel if the
292          * tables are disabled.
293          */
294         if ((nl->ielec != GMX_NBKERNEL_ELEC_NONE) && (nl->ielecmod == eintmodPOTSWITCH) &&
295             (nl->ivdw  != GMX_NBKERNEL_VDW_NONE)  && (nl->ivdwmod  == eintmodPOTSWITCH) &&
296             bElecAndVdwSwitchDiffers)
297         {
298             nl->kernelptr_vf = nullptr;
299             nl->kernelptr_f  = nullptr;
300         }
301
302         /* Give up, pick a generic one instead.
303          * We only do this for particle-particle kernels; by leaving the water-optimized kernel
304          * pointers to NULL, the water optimization will automatically be disabled for this interaction.
305          */
306         if (nl->kernelptr_vf == nullptr && !gmx_strcasecmp_min(geom, "Particle-Particle"))
307         {
308             nl->kernelptr_vf       = reinterpret_cast<void *>(gmx_nb_generic_kernel);
309             nl->kernelptr_f        = reinterpret_cast<void *>(gmx_nb_generic_kernel);
310             nl->simd_padding_width = 1;
311             if (debug)
312             {
313                 fprintf(debug,
314                         "WARNING - Slow generic NB kernel used for neighborlist with\n"
315                         "    Elec: '%s', Modifier: '%s'\n"
316                         "    Vdw:  '%s', Modifier: '%s'\n",
317                         elec, elec_mod, vdw, vdw_mod);
318             }
319         }
320     }
321 }
322
323 void do_nonbonded(const t_forcerec  *fr,
324                   rvec               x[],
325                   rvec               f_shortrange[],
326                   const t_mdatoms   *mdatoms,
327                   const t_blocka    *excl,
328                   gmx_grppairener_t *grppener,
329                   t_nrnb            *nrnb,
330                   real              *lambda,
331                   real              *dvdl,
332                   int                nls,
333                   int                eNL,
334                   int                flags)
335 {
336     t_nblist *        nlist;
337     int               n, n0, n1, i, i0, i1;
338     t_nblists *       nblists;
339     nb_kernel_data_t  kernel_data;
340     nb_kernel_t *     kernelptr = nullptr;
341     rvec *            f;
342
343     kernel_data.flags                   = flags;
344     kernel_data.exclusions              = excl;
345     kernel_data.lambda                  = lambda;
346     kernel_data.dvdl                    = dvdl;
347
348     if (fr->bAllvsAll)
349     {
350         gmx_incons("All-vs-all kernels have not been implemented in version 4.6");
351     }
352
353     if (eNL >= 0)
354     {
355         i0 = eNL;
356         i1 = i0+1;
357     }
358     else
359     {
360         i0 = 0;
361         i1 = eNL_NR;
362     }
363
364     if (nls >= 0)
365     {
366         n0 = nls;
367         n1 = nls+1;
368     }
369     else
370     {
371         n0 = 0;
372         n1 = fr->nnblists;
373     }
374
375     for (n = n0; (n < n1); n++)
376     {
377         nblists = &fr->nblists[n];
378
379         /* Tabulated kernels hard-code a lot of assumptions about the
380          * structure of these tables, but that's not worth fixing with
381          * the group scheme due for removal soon. As a token
382          * improvement, this assertion will stop code segfaulting if
383          * someone assumes that extending the group-scheme table-type
384          * enumeration is something that GROMACS supports. */
385         static_assert(etiNR == 3, "");
386
387         kernel_data.table_elec              = nblists->table_elec;
388         kernel_data.table_vdw               = nblists->table_vdw;
389         kernel_data.table_elec_vdw          = nblists->table_elec_vdw;
390
391         {
392             {
393                 /* Short-range */
394                 if (!(flags & GMX_NONBONDED_DO_SR))
395                 {
396                     continue;
397                 }
398                 kernel_data.energygrp_elec          = grppener->ener[egCOULSR];
399                 kernel_data.energygrp_vdw           = grppener->ener[fr->bBHAM ? egBHAMSR : egLJSR];
400                 nlist = nblists->nlist_sr;
401                 f                                   = f_shortrange;
402             }
403
404             for (i = i0; (i < i1); i++)
405             {
406                 if (nlist[i].nri > 0)
407                 {
408                     if (flags & GMX_NONBONDED_DO_POTENTIAL)
409                     {
410                         /* Potential and force */
411                         kernelptr = reinterpret_cast<nb_kernel_t *>(nlist[i].kernelptr_vf);
412                     }
413                     else
414                     {
415                         /* Force only, no potential */
416                         kernelptr = reinterpret_cast<nb_kernel_t *>(nlist[i].kernelptr_f);
417                     }
418
419                     if (nlist[i].type != GMX_NBLIST_INTERACTION_FREE_ENERGY && (flags & GMX_NONBONDED_DO_FOREIGNLAMBDA))
420                     {
421                         /* We don't need the non-perturbed interactions */
422                         continue;
423                     }
424                     /* Neighborlists whose kernelptr==NULL will always be empty */
425                     if (kernelptr != nullptr)
426                     {
427                         (*kernelptr)(&(nlist[i]), x, f, const_cast<t_forcerec*>(fr),
428                                      const_cast<t_mdatoms*>(mdatoms), &kernel_data, nrnb);
429                     }
430                     else
431                     {
432                         gmx_fatal(FARGS, "Non-empty neighborlist does not have any kernel pointer assigned.");
433                     }
434                 }
435             }
436         }
437     }
438 }