Created SIMD module
[alexxy/gromacs.git] / src / gromacs / mdlib / nbnxn_kernels / nbnxn_kernel_simd_utils.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013, 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 #ifndef _nbnxn_kernel_simd_utils_h_
36 #define _nbnxn_kernel_simd_utils_h_
37
38 /*! \brief Provides hardware-specific utility routines for the SIMD kernels.
39  *
40  * Defines all functions, typedefs, constants and macros that have
41  * explicit dependencies on the j-cluster size, precision, or SIMD
42  * width. This includes handling diagonal, Newton and topology
43  * exclusions.
44  *
45  * The functionality which depends on the j-cluster size is:
46  *   LJ-parameter lookup
47  *   force table lookup
48  *   energy group pair energy storage
49  */
50
51 #if !defined GMX_NBNXN_SIMD_2XNN && !defined GMX_NBNXN_SIMD_4XN
52 #error "Must define an NBNxN kernel flavour before including NBNxN kernel utility functions"
53 #endif
54
55 #ifdef GMX_SIMD_REFERENCE_PLAIN_C
56
57 /* Align a stack-based thread-local working array. */
58 static gmx_inline int *
59 prepare_table_load_buffer(const int *array)
60 {
61     return NULL;
62 }
63
64 #include "nbnxn_kernel_simd_utils_ref.h"
65
66 #else /* GMX_SIMD_REFERENCE_PLAIN_C */
67
68 #ifdef GMX_X86_SSE2
69 /* Include x86 SSE2 compatible SIMD functions */
70
71 /* Set the stride for the lookup of the two LJ parameters from their
72  * (padded) array. We use the minimum supported SIMD memory alignment.
73  */
74 #if defined GMX_DOUBLE
75 static const int nbfp_stride = 2;
76 #else
77 static const int nbfp_stride = 4;
78 #endif
79
80 /* Align a stack-based thread-local working array. Table loads on
81  * full-width AVX_256 use the array, but other implementations do
82  * not. */
83 static gmx_inline int *
84 prepare_table_load_buffer(const int *array)
85 {
86 #if defined GMX_X86_AVX_256 && !defined GMX_USE_HALF_WIDTH_SIMD_HERE
87     return gmx_simd_align_int(array);
88 #else
89     return NULL;
90 #endif
91 }
92
93 #if defined GMX_X86_AVX_256 && !defined GMX_USE_HALF_WIDTH_SIMD_HERE
94
95 /* With full AVX-256 SIMD, half SIMD-width table loads are optimal */
96 #if GMX_SIMD_WIDTH_HERE == 8
97 #define TAB_FDV0
98 #endif
99
100 #ifdef GMX_DOUBLE
101 #include "nbnxn_kernel_simd_utils_x86_256d.h"
102 #else  /* GMX_DOUBLE */
103 #include "nbnxn_kernel_simd_utils_x86_256s.h"
104 #endif /* GMX_DOUBLE */
105
106 #else  /* defined GMX_X86_AVX_256 && !defined GMX_USE_HALF_WIDTH_SIMD_HERE */
107
108 /* We use the FDV0 table layout when we can use aligned table loads */
109 #if GMX_SIMD_WIDTH_HERE == 4
110 #define TAB_FDV0
111 #endif
112
113 #ifdef GMX_DOUBLE
114 #include "nbnxn_kernel_simd_utils_x86_128d.h"
115 #else  /* GMX_DOUBLE */
116 #include "nbnxn_kernel_simd_utils_x86_128s.h"
117 #endif /* GMX_DOUBLE */
118
119 #endif /* defined GMX_X86_AVX_256 && !defined GMX_USE_HALF_WIDTH_SIMD_HERE */
120
121 #else  /* GMX_X86_SSE2 */
122
123 #if GMX_SIMD_WIDTH_HERE > 4
124 static const int nbfp_stride = 4;
125 #else
126 static const int nbfp_stride = GMX_SIMD_WIDTH_HERE;
127 #endif
128
129 /* We use the FDV0 table layout when we can use aligned table loads */
130 #if GMX_SIMD_WIDTH_HERE == 4
131 #define TAB_FDV0
132 #endif
133
134 #ifdef GMX_CPU_ACCELERATION_IBM_QPX
135 #include "nbnxn_kernel_simd_utils_ibm_qpx.h"
136 #endif /* GMX_CPU_ACCELERATION_IBM_QPX */
137
138 #endif /* GMX_X86_SSE2 */
139 #endif /* GMX_SIMD_REFERENCE_PLAIN_C */
140
141
142 #ifdef UNROLLJ
143 /* Add energy register to possibly multiple terms in the energy array */
144 static inline void add_ener_grp(gmx_mm_pr e_S, real *v, const int *offset_jj)
145 {
146     int jj;
147
148     /* We need to balance the number of store operations with
149      * the rapidly increases number of combinations of energy groups.
150      * We add to a temporary buffer for 1 i-group vs 2 j-groups.
151      */
152     for (jj = 0; jj < (UNROLLJ/2); jj++)
153     {
154         gmx_mm_pr v_S;
155
156         v_S = gmx_load_pr(v+offset_jj[jj]+jj*GMX_SIMD_WIDTH_HERE);
157         gmx_store_pr(v+offset_jj[jj]+jj*GMX_SIMD_WIDTH_HERE, gmx_add_pr(v_S, e_S));
158     }
159 }
160 #endif
161
162 #if defined GMX_NBNXN_SIMD_2XNN && defined UNROLLJ
163 /* As add_ener_grp, but for two groups of UNROLLJ/2 stored in
164  * a single SIMD register.
165  */
166 static inline void
167 add_ener_grp_halves(gmx_mm_pr e_S, real *v0, real *v1, const int *offset_jj)
168 {
169     gmx_mm_hpr e_S0, e_S1;
170     int        jj;
171
172     gmx_pr_to_2hpr(e_S, &e_S0, &e_S1);
173
174     for (jj = 0; jj < (UNROLLJ/2); jj++)
175     {
176         gmx_mm_hpr v_S;
177
178         gmx_load_hpr(&v_S, v0+offset_jj[jj]+jj*GMX_SIMD_WIDTH_HERE/2);
179         gmx_store_hpr(v0+offset_jj[jj]+jj*GMX_SIMD_WIDTH_HERE/2, gmx_add_hpr(v_S, e_S0));
180     }
181     for (jj = 0; jj < (UNROLLJ/2); jj++)
182     {
183         gmx_mm_hpr v_S;
184
185         gmx_load_hpr(&v_S, v1+offset_jj[jj]+jj*GMX_SIMD_WIDTH_HERE/2);
186         gmx_store_hpr(v1+offset_jj[jj]+jj*GMX_SIMD_WIDTH_HERE/2, gmx_add_hpr(v_S, e_S1));
187     }
188 }
189 #endif
190
191 #endif /* _nbnxn_kernel_simd_utils_h_ */