b787b9d79a94e90ab4a23e4f2b64f7da9695e350
[alexxy/gromacs.git] / src / gromacs / mdlib / nbnxn_kernels / simd_4xn / nbnxn_kernel_simd_4xn_common.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014,2015,2017,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/mdlib/nbnxn_consts.h"
36 #include "gromacs/pbcutil/ishift.h"
37 #include "gromacs/simd/simd.h"
38 #include "gromacs/simd/simd_math.h"
39 #include "gromacs/simd/vector_operations.h"
40 #include "gromacs/utility/basedefinitions.h"
41 #ifdef CALC_COUL_EWALD
42 #include "gromacs/math/utilities.h"
43 #endif
44
45 #include "config.h"
46
47 #ifndef GMX_SIMD_J_UNROLL_SIZE
48 #error "Need to define GMX_SIMD_J_UNROLL_SIZE before including the 4xn kernel common header file"
49 #endif
50
51 #define UNROLLI    4
52 #define UNROLLJ    (GMX_SIMD_REAL_WIDTH/GMX_SIMD_J_UNROLL_SIZE)
53
54 static_assert(UNROLLI == c_nbnxnCpuIClusterSize, "UNROLLI should match the i-cluster size");
55
56 /* The stride of all the atom data arrays is max(UNROLLI,unrollj) */
57 #if GMX_SIMD_REAL_WIDTH >= UNROLLI
58 #define STRIDE     (GMX_SIMD_REAL_WIDTH/GMX_SIMD_J_UNROLL_SIZE)
59 #else
60 #define STRIDE     (UNROLLI)
61 #endif
62
63
64 #if !defined GMX_NBNXN_SIMD_2XNN && !defined GMX_NBNXN_SIMD_4XN
65 #error "Must define an NBNxN kernel flavour before including NBNxN kernel utility functions"
66 #endif
67
68 // 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
69 #if GMX_SIMD_REAL_WIDTH == 4 || !GMX_SIMD_HAVE_GATHER_LOADU_BYSIMDINT_TRANSPOSE_REAL
70 #define TAB_FDV0
71 #endif
72
73
74 #ifdef UNROLLJ
75 /* Add energy register to possibly multiple terms in the energy array */
76 static inline void add_ener_grp(gmx::SimdReal e_S, real *v, const int *offset_jj)
77 {
78     using namespace gmx;
79     int jj;
80
81     /* We need to balance the number of store operations with
82      * the rapidly increases number of combinations of energy groups.
83      * We add to a temporary buffer for 1 i-group vs 2 j-groups.
84      */
85     for (jj = 0; jj < (UNROLLJ/2); jj++)
86     {
87         SimdReal v_S;
88
89         v_S = load<SimdReal>(v+offset_jj[jj]+jj*GMX_SIMD_REAL_WIDTH);
90         store(v+offset_jj[jj]+jj*GMX_SIMD_REAL_WIDTH, v_S + e_S);
91     }
92 }
93 #endif
94
95 #if GMX_SIMD_HAVE_INT32_LOGICAL
96 typedef gmx::SimdInt32    SimdBitMask;
97 #else
98 typedef gmx::SimdReal     SimdBitMask;
99 #endif
100
101 static inline void gmx_simdcall
102 gmx_load_simd_4xn_interactions(int                               excl,
103                                SimdBitMask gmx_unused            filter_S0,
104                                SimdBitMask gmx_unused            filter_S1,
105                                SimdBitMask gmx_unused            filter_S2,
106                                SimdBitMask gmx_unused            filter_S3,
107                                real gmx_unused                  *simd_interaction_array,
108                                gmx::SimdBool                    *interact_S0,
109                                gmx::SimdBool                    *interact_S1,
110                                gmx::SimdBool                    *interact_S2,
111                                gmx::SimdBool                    *interact_S3)
112 {
113     using namespace gmx;
114 #if GMX_SIMD_HAVE_INT32_LOGICAL
115     /* Load integer interaction mask */
116     SimdInt32 mask_pr_S(excl);
117     *interact_S0  = cvtIB2B(testBits( mask_pr_S & filter_S0 ));
118     *interact_S1  = cvtIB2B(testBits( mask_pr_S & filter_S1 ));
119     *interact_S2  = cvtIB2B(testBits( mask_pr_S & filter_S2 ));
120     *interact_S3  = cvtIB2B(testBits( mask_pr_S & filter_S3 ));
121 #elif GMX_SIMD_HAVE_LOGICAL
122     union
123     {
124 #if GMX_DOUBLE
125         std::int64_t i;
126 #else
127         std::int32_t i;
128 #endif
129         real         r;
130     } conv;
131
132     conv.i = excl;
133     SimdReal      mask_pr_S(conv.r);
134
135     *interact_S0  = testBits( mask_pr_S & filter_S0 );
136     *interact_S1  = testBits( mask_pr_S & filter_S1 );
137     *interact_S2  = testBits( mask_pr_S & filter_S2 );
138     *interact_S3  = testBits( mask_pr_S & filter_S3 );
139 #else
140     // Neither real or integer bitwise logical operations supported.
141     // Load masks from memory instead.
142     SimdReal      zero = setZero();
143     *interact_S0  = ( zero < load<SimdReal>( simd_interaction_array + GMX_SIMD_REAL_WIDTH*((excl >> (0 * UNROLLJ)) & 0xF) ) );
144     *interact_S1  = ( zero < load<SimdReal>( simd_interaction_array + GMX_SIMD_REAL_WIDTH*((excl >> (1 * UNROLLJ)) & 0xF) ) );
145     *interact_S2  = ( zero < load<SimdReal>( simd_interaction_array + GMX_SIMD_REAL_WIDTH*((excl >> (2 * UNROLLJ)) & 0xF) ) );
146     *interact_S3  = ( zero < load<SimdReal>( simd_interaction_array + GMX_SIMD_REAL_WIDTH*((excl >> (3 * UNROLLJ)) & 0xF) ) );
147 #endif
148 }
149
150 /* All functionality defines are set here, except for:
151  * CALC_ENERGIES, ENERGY_GROUPS which are defined before.
152  * CHECK_EXCLS, which is set just before including the inner loop contents.
153  * The combination rule defines, LJ_COMB_GEOM or LJ_COMB_LB are currently
154  * set before calling the kernel function. We might want to move that
155  * to inside the n-loop and have a different combination rule for different
156  * ci's, as no combination rule gives a 50% performance hit for LJ.
157  */
158
159 /* We always calculate shift forces, because it's cheap anyhow */
160 #define CALC_SHIFTFORCES
161
162 /* Assumes all LJ parameters are identical */
163 /* #define FIX_LJ_C */