readability-implicit-bool-conversion 1/2
[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 #include "gromacs/gmxlib/nonbonded/nb_kernel_c/nb_kernel_c.h"
74
75 #if GMX_SIMD_X86_SSE2 && !GMX_DOUBLE
76 #    include "gromacs/gmxlib/nonbonded/nb_kernel_sse2_single/nb_kernel_sse2_single.h"
77 #endif
78 #if GMX_SIMD_X86_SSE4_1 && !GMX_DOUBLE
79 #    include "gromacs/gmxlib/nonbonded/nb_kernel_sse4_1_single/nb_kernel_sse4_1_single.h"
80 #endif
81 #if GMX_SIMD_X86_AVX_128_FMA && !GMX_DOUBLE
82 #    include "gromacs/gmxlib/nonbonded/nb_kernel_avx_128_fma_single/nb_kernel_avx_128_fma_single.h"
83 #endif
84 #if (GMX_SIMD_X86_AVX_256 || GMX_SIMD_X86_AVX2_256) && !GMX_DOUBLE
85 #    include "gromacs/gmxlib/nonbonded/nb_kernel_avx_256_single/nb_kernel_avx_256_single.h"
86 #endif
87 #if GMX_SIMD_X86_SSE2 && GMX_DOUBLE
88 #    include "gromacs/gmxlib/nonbonded/nb_kernel_sse2_double/nb_kernel_sse2_double.h"
89 #endif
90 #if GMX_SIMD_X86_SSE4_1 && GMX_DOUBLE
91 #    include "gromacs/gmxlib/nonbonded/nb_kernel_sse4_1_double/nb_kernel_sse4_1_double.h"
92 #endif
93 #if GMX_SIMD_X86_AVX_128_FMA && GMX_DOUBLE
94 #    include "gromacs/gmxlib/nonbonded/nb_kernel_avx_128_fma_double/nb_kernel_avx_128_fma_double.h"
95 #endif
96 #if (GMX_SIMD_X86_AVX_256 || GMX_SIMD_X86_AVX2_256) && GMX_DOUBLE
97 #    include "gromacs/gmxlib/nonbonded/nb_kernel_avx_256_double/nb_kernel_avx_256_double.h"
98 #endif
99 #if GMX_SIMD_SPARC64_HPC_ACE && GMX_DOUBLE
100 #    include "gromacs/gmxlib/nonbonded/nb_kernel_sparc64_hpc_ace_double/nb_kernel_sparc64_hpc_ace_double.h"
101 #endif
102
103
104 static tMPI_Thread_mutex_t nonbonded_setup_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
105 static gmx_bool            nonbonded_setup_done  = FALSE;
106
107
108 void
109 gmx_nonbonded_setup(t_forcerec *   fr,
110                     gmx_bool       bGenericKernelOnly)
111 {
112     tMPI_Thread_mutex_lock(&nonbonded_setup_mutex);
113     /* Here we are guaranteed only one thread made it. */
114     if (!nonbonded_setup_done)
115     {
116         if (!bGenericKernelOnly)
117         {
118             /* Add the generic kernels to the structure stored statically in nb_kernel.c */
119             nb_kernel_list_add_kernels(kernellist_c, kernellist_c_size);
120
121             if (!(fr != nullptr && !fr->use_simd_kernels))
122             {
123                 /* Add interaction-specific kernels for different architectures */
124                 /* Single precision */
125 #if GMX_SIMD_X86_SSE2 && !GMX_DOUBLE
126                 nb_kernel_list_add_kernels(kernellist_sse2_single, kernellist_sse2_single_size);
127 #endif
128 #if GMX_SIMD_X86_SSE4_1 && !GMX_DOUBLE
129                 nb_kernel_list_add_kernels(kernellist_sse4_1_single, kernellist_sse4_1_single_size);
130 #endif
131 #if GMX_SIMD_X86_AVX_128_FMA && !GMX_DOUBLE
132                 nb_kernel_list_add_kernels(kernellist_avx_128_fma_single, kernellist_avx_128_fma_single_size);
133 #endif
134 #if (GMX_SIMD_X86_AVX_256 || GMX_SIMD_X86_AVX2_256) && !GMX_DOUBLE
135                 nb_kernel_list_add_kernels(kernellist_avx_256_single, kernellist_avx_256_single_size);
136 #endif
137                 /* Double precision */
138 #if GMX_SIMD_X86_SSE2 && GMX_DOUBLE
139                 nb_kernel_list_add_kernels(kernellist_sse2_double, kernellist_sse2_double_size);
140 #endif
141 #if GMX_SIMD_X86_SSE4_1 && GMX_DOUBLE
142                 nb_kernel_list_add_kernels(kernellist_sse4_1_double, kernellist_sse4_1_double_size);
143 #endif
144 #if GMX_SIMD_X86_AVX_128_FMA && GMX_DOUBLE
145                 nb_kernel_list_add_kernels(kernellist_avx_128_fma_double, kernellist_avx_128_fma_double_size);
146 #endif
147 #if (GMX_SIMD_X86_AVX_256 || GMX_SIMD_X86_AVX2_256) && GMX_DOUBLE
148                 nb_kernel_list_add_kernels(kernellist_avx_256_double, kernellist_avx_256_double_size);
149 #endif
150 #if GMX_SIMD_SPARC64_HPC_ACE && GMX_DOUBLE
151                 nb_kernel_list_add_kernels(kernellist_sparc64_hpc_ace_double, kernellist_sparc64_hpc_ace_double_size);
152 #endif
153                 ; /* empty statement to avoid a completely empty block */
154             }
155         }
156         /* Create a hash for faster lookups */
157         nb_kernel_list_hash_init();
158
159         nonbonded_setup_done = TRUE;
160     }
161     tMPI_Thread_mutex_unlock(&nonbonded_setup_mutex);
162 }
163
164
165
166 void
167 gmx_nonbonded_set_kernel_pointers(FILE *log, t_nblist *nl, gmx_bool bElecAndVdwSwitchDiffers)
168 {
169     const char *     elec;
170     const char *     elec_mod;
171     const char *     vdw;
172     const char *     vdw_mod;
173     const char *     geom;
174     const char *     other;
175
176     struct
177     {
178         const char *  arch;
179         int           simd_padding_width;
180     }
181     arch_and_padding[] =
182     {
183         /* Single precision */
184 #if (GMX_SIMD_X86_AVX_256 || GMX_SIMD_X86_AVX2_256) && !GMX_DOUBLE
185         { "avx_256_single", 8 },
186 #endif
187 #if GMX_SIMD_X86_AVX_128_FMA && !GMX_DOUBLE
188         { "avx_128_fma_single", 4 },
189 #endif
190 #if GMX_SIMD_X86_SSE4_1 && !GMX_DOUBLE
191         { "sse4_1_single", 4 },
192 #endif
193 #if GMX_SIMD_X86_SSE2 && !GMX_DOUBLE
194         { "sse2_single", 4 },
195 #endif
196         /* Double precision */
197 #if (GMX_SIMD_X86_AVX_256 || GMX_SIMD_X86_AVX2_256) && GMX_DOUBLE
198         { "avx_256_double", 4 },
199 #endif
200 #if GMX_SIMD_X86_AVX_128_FMA && GMX_DOUBLE
201         /* Sic. Double precision 2-way SIMD does not require neighbor list padding,
202          * since the kernels execute a loop unrolled a factor 2, followed by
203          * a possible single odd-element epilogue.
204          */
205         { "avx_128_fma_double", 1 },
206 #endif
207 #if GMX_SIMD_X86_SSE2 && GMX_DOUBLE
208         /* No padding - see comment above */
209         { "sse2_double", 1 },
210 #endif
211 #if GMX_SIMD_X86_SSE4_1 && GMX_DOUBLE
212         /* No padding - see comment above */
213         { "sse4_1_double", 1 },
214 #endif
215 #if GMX_SIMD_SPARC64_HPC_ACE && GMX_DOUBLE
216         /* No padding - see comment above */
217         { "sparc64_hpc_ace_double", 1 },
218 #endif
219         { "c", 1 },
220     };
221     int              narch = asize(arch_and_padding);
222     int              i;
223
224     if (!nonbonded_setup_done)
225     {
226         /* We typically call this setup routine before starting timers,
227          * but if that has not been done for whatever reason we do it now.
228          */
229         gmx_nonbonded_setup(nullptr, FALSE);
230     }
231
232     /* Not used yet */
233     other = "";
234
235     nl->kernelptr_vf = nullptr;
236     nl->kernelptr_v  = nullptr;
237     nl->kernelptr_f  = nullptr;
238
239     elec     = gmx_nbkernel_elec_names[nl->ielec];
240     elec_mod = eintmod_names[nl->ielecmod];
241     vdw      = gmx_nbkernel_vdw_names[nl->ivdw];
242     vdw_mod  = eintmod_names[nl->ivdwmod];
243     geom     = gmx_nblist_geometry_names[nl->igeometry];
244
245     if (nl->type == GMX_NBLIST_INTERACTION_FREE_ENERGY)
246     {
247         nl->kernelptr_vf       = reinterpret_cast<void *>(gmx_nb_free_energy_kernel);
248         nl->kernelptr_f        = reinterpret_cast<void *>(gmx_nb_free_energy_kernel);
249         nl->simd_padding_width = 1;
250     }
251     else if (!gmx_strcasecmp_min(geom, "CG-CG"))
252     {
253         nl->kernelptr_vf       = reinterpret_cast<void *>(gmx_nb_generic_cg_kernel);
254         nl->kernelptr_f        = reinterpret_cast<void *>(gmx_nb_generic_cg_kernel);
255         nl->simd_padding_width = 1;
256     }
257     else
258     {
259         /* Try to find a specific kernel first */
260
261         for (i = 0; i < narch && nl->kernelptr_vf == nullptr; i++)
262         {
263             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"));
264             nl->simd_padding_width = arch_and_padding[i].simd_padding_width;
265         }
266         for (i = 0; i < narch && nl->kernelptr_f == nullptr; i++)
267         {
268             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"));
269             nl->simd_padding_width = arch_and_padding[i].simd_padding_width;
270
271             /* If there is not force-only optimized kernel, is there a potential & force one? */
272             if (nl->kernelptr_f == nullptr)
273             {
274                 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"));
275                 nl->simd_padding_width = arch_and_padding[i].simd_padding_width;
276             }
277         }
278
279         /* For now, the accelerated kernels cannot handle the combination of switch functions for both
280          * electrostatics and VdW that use different switch radius or switch cutoff distances
281          * (both of them enter in the switch function calculation). This would require
282          * us to evaluate two completely separate switch functions for every interaction.
283          * Instead, we disable such kernels by setting the pointer to NULL.
284          * This will cause the generic kernel (which can handle it) to be called instead.
285          *
286          * Note that we typically already enable tabulated coulomb interactions for this case,
287          * so this is mostly a safe-guard to make sure we call the generic kernel if the
288          * tables are disabled.
289          */
290         if ((nl->ielec != GMX_NBKERNEL_ELEC_NONE) && (nl->ielecmod == eintmodPOTSWITCH) &&
291             (nl->ivdw  != GMX_NBKERNEL_VDW_NONE)  && (nl->ivdwmod  == eintmodPOTSWITCH) &&
292             bElecAndVdwSwitchDiffers)
293         {
294             nl->kernelptr_vf = nullptr;
295             nl->kernelptr_f  = nullptr;
296         }
297
298         /* Give up, pick a generic one instead.
299          * We only do this for particle-particle kernels; by leaving the water-optimized kernel
300          * pointers to NULL, the water optimization will automatically be disabled for this interaction.
301          */
302         if (nl->kernelptr_vf == nullptr && !gmx_strcasecmp_min(geom, "Particle-Particle"))
303         {
304             nl->kernelptr_vf       = reinterpret_cast<void *>(gmx_nb_generic_kernel);
305             nl->kernelptr_f        = reinterpret_cast<void *>(gmx_nb_generic_kernel);
306             nl->simd_padding_width = 1;
307             if (debug)
308             {
309                 fprintf(debug,
310                         "WARNING - Slow generic NB kernel used for neighborlist with\n"
311                         "    Elec: '%s', Modifier: '%s'\n"
312                         "    Vdw:  '%s', Modifier: '%s'\n",
313                         elec, elec_mod, vdw, vdw_mod);
314             }
315         }
316     }
317 }
318
319 void do_nonbonded(const t_forcerec  *fr,
320                   rvec               x[],
321                   rvec               f_shortrange[],
322                   const t_mdatoms   *mdatoms,
323                   const t_blocka    *excl,
324                   gmx_grppairener_t *grppener,
325                   t_nrnb            *nrnb,
326                   real              *lambda,
327                   real              *dvdl,
328                   int                nls,
329                   int                eNL,
330                   int                flags)
331 {
332     t_nblist *        nlist;
333     int               n, n0, n1, i, i0, i1;
334     t_nblists *       nblists;
335     nb_kernel_data_t  kernel_data;
336     nb_kernel_t *     kernelptr = nullptr;
337     rvec *            f;
338
339     kernel_data.flags                   = flags;
340     kernel_data.exclusions              = excl;
341     kernel_data.lambda                  = lambda;
342     kernel_data.dvdl                    = dvdl;
343
344     if (fr->bAllvsAll)
345     {
346         gmx_incons("All-vs-all kernels have not been implemented in version 4.6");
347     }
348
349     if (eNL >= 0)
350     {
351         i0 = eNL;
352         i1 = i0+1;
353     }
354     else
355     {
356         i0 = 0;
357         i1 = eNL_NR;
358     }
359
360     if (nls >= 0)
361     {
362         n0 = nls;
363         n1 = nls+1;
364     }
365     else
366     {
367         n0 = 0;
368         n1 = fr->nnblists;
369     }
370
371     for (n = n0; (n < n1); n++)
372     {
373         nblists = &fr->nblists[n];
374
375         /* Tabulated kernels hard-code a lot of assumptions about the
376          * structure of these tables, but that's not worth fixing with
377          * the group scheme due for removal soon. As a token
378          * improvement, this assertion will stop code segfaulting if
379          * someone assumes that extending the group-scheme table-type
380          * enumeration is something that GROMACS supports. */
381         static_assert(etiNR == 3, "");
382
383         kernel_data.table_elec              = nblists->table_elec;
384         kernel_data.table_vdw               = nblists->table_vdw;
385         kernel_data.table_elec_vdw          = nblists->table_elec_vdw;
386
387         {
388             {
389                 /* Short-range */
390                 if (!(flags & GMX_NONBONDED_DO_SR))
391                 {
392                     continue;
393                 }
394                 kernel_data.energygrp_elec          = grppener->ener[egCOULSR];
395                 kernel_data.energygrp_vdw           = grppener->ener[fr->bBHAM ? egBHAMSR : egLJSR];
396                 nlist = nblists->nlist_sr;
397                 f                                   = f_shortrange;
398             }
399
400             for (i = i0; (i < i1); i++)
401             {
402                 if (nlist[i].nri > 0)
403                 {
404                     if (flags & GMX_NONBONDED_DO_POTENTIAL)
405                     {
406                         /* Potential and force */
407                         kernelptr = reinterpret_cast<nb_kernel_t *>(nlist[i].kernelptr_vf);
408                     }
409                     else
410                     {
411                         /* Force only, no potential */
412                         kernelptr = reinterpret_cast<nb_kernel_t *>(nlist[i].kernelptr_f);
413                     }
414
415                     if (nlist[i].type != GMX_NBLIST_INTERACTION_FREE_ENERGY && (flags & GMX_NONBONDED_DO_FOREIGNLAMBDA))
416                     {
417                         /* We don't need the non-perturbed interactions */
418                         continue;
419                     }
420                     /* Neighborlists whose kernelptr==NULL will always be empty */
421                     if (kernelptr != nullptr)
422                     {
423                         (*kernelptr)(&(nlist[i]), x, f, fr, mdatoms, &kernel_data, nrnb);
424                     }
425                     else
426                     {
427                         gmx_fatal(FARGS, "Non-empty neighborlist does not have any kernel pointer assigned.");
428                     }
429                 }
430             }
431         }
432     }
433 }