9694949fa94e7dd6b3d0b32d0906f3997ce5d2eb
[alexxy/gromacs.git] / src / gromacs / nbnxm / kernels_simd_2xmm / kernel_common.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014,2015,2018,2019, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 #include "gromacs/pbcutil/ishift.h"
36 #include "gromacs/simd/simd.h"
37 #include "gromacs/simd/simd_math.h"
38 #include "gromacs/simd/vector_operations.h"
39 #ifdef CALC_COUL_EWALD
40 #    include "gromacs/math/utilities.h"
41 #endif
42
43 #include "config.h"
44
45 #include <cstdint>
46
47 #if !GMX_SIMD_HAVE_HSIMD_UTIL_REAL
48 #    error "Half-simd utility operations are required for the 2xNN kernels"
49 #endif
50
51 #ifndef GMX_SIMD_J_UNROLL_SIZE
52 #    error "Need to define GMX_SIMD_J_UNROLL_SIZE before including the 2xnn kernel common header file"
53 #endif
54
55 #define UNROLLI 4
56 #define UNROLLJ (GMX_SIMD_REAL_WIDTH / GMX_SIMD_J_UNROLL_SIZE)
57
58 static_assert(UNROLLI == c_nbnxnCpuIClusterSize, "UNROLLI should match the i-cluster size");
59
60 /* The stride of all the atom data arrays is equal to half the SIMD width */
61 #define STRIDE UNROLLJ
62
63 #if !defined GMX_NBNXN_SIMD_2XNN && !defined GMX_NBNXN_SIMD_4XN
64 #    error "Must define an NBNxN kernel flavour before including NBNxN kernel utility functions"
65 #endif
66
67 // We use the FDV0 tables for width==4 (when we can load it in one go), or if we don't have any unaligned loads
68 #if GMX_SIMD_REAL_WIDTH == 4 || !GMX_SIMD_HAVE_GATHER_LOADU_BYSIMDINT_TRANSPOSE_REAL
69 #    define TAB_FDV0
70 #endif
71
72 #if defined UNROLLJ
73 /* As add_ener_grp, but for two groups of UNROLLJ/2 stored in
74  * a single SIMD register.
75  */
76 static inline void add_ener_grp_halves(gmx::SimdReal e_S, real* v0, real* v1, const int* offset_jj)
77 {
78     for (int jj = 0; jj < (UNROLLJ / 2); jj++)
79     {
80         incrDualHsimd(v0 + offset_jj[jj] + jj * GMX_SIMD_REAL_WIDTH / 2,
81                       v1 + offset_jj[jj] + jj * GMX_SIMD_REAL_WIDTH / 2, e_S);
82     }
83 }
84 #endif
85
86 #if GMX_SIMD_HAVE_INT32_LOGICAL
87 typedef gmx::SimdInt32 SimdBitMask;
88 #else
89 typedef gmx::SimdReal SimdBitMask;
90 #endif
91
92
93 static inline void gmx_simdcall gmx_load_simd_2xnn_interactions(int            excl,
94                                                                 SimdBitMask    filter_S0,
95                                                                 SimdBitMask    filter_S2,
96                                                                 gmx::SimdBool* interact_S0,
97                                                                 gmx::SimdBool* interact_S2)
98 {
99     using namespace gmx;
100 #if GMX_SIMD_HAVE_INT32_LOGICAL
101     SimdInt32 mask_pr_S(excl);
102     *interact_S0 = cvtIB2B(testBits(mask_pr_S & filter_S0));
103     *interact_S2 = cvtIB2B(testBits(mask_pr_S & filter_S2));
104 #elif GMX_SIMD_HAVE_LOGICAL
105     union {
106 #    if GMX_DOUBLE
107         std::int64_t i;
108 #    else
109         std::int32_t i;
110 #    endif
111         real         r;
112     } conv;
113
114     conv.i = excl;
115     SimdReal mask_pr_S(conv.r);
116
117     *interact_S0 = testBits(mask_pr_S & filter_S0);
118     *interact_S2 = testBits(mask_pr_S & filter_S2);
119 #endif
120 }
121
122 /* All functionality defines are set here, except for:
123  * CALC_ENERGIES, ENERGY_GROUPS which are defined before.
124  * CHECK_EXCLS, which is set just before including the inner loop contents.
125  * The combination rule defines, LJ_COMB_GEOM or LJ_COMB_LB are currently
126  * set before calling the kernel function. We might want to move that
127  * to inside the n-loop and have a different combination rule for different
128  * ci's, as no combination rule gives a 50% performance hit for LJ.
129  */
130
131 /* We always calculate shift forces, because it's cheap anyhow */
132 #define CALC_SHIFTFORCES
133
134 /* Assumes all LJ parameters are identical */
135 /* #define FIX_LJ_C */