2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2014, 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.
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.
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.
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.
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.
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.
41 * Separate test of SIMD load/store, before we use them in the SIMD test classes.
43 * Simple tests without using any classes/utilities, so we can use load/store
44 * functions inside our test utilities after this has passed.
48 * - gmx_simd_align_r(),gmx_simd_align_i(),gmx_simd4_align_r(),
49 * - gmx_simd_load_r(),gmx_simd_store_r(),gmx_simd_loadu_r(),gmx_simd_storeu_r()
50 * - gmx_simd_load_i(),gmx_simd_store_i(), gmx_simd_loadu_i(),gmx_simd_storeu_i()
51 * - gmx_simd4_load_r(),gmx_simd4_store_r(), gmx_simd4_loadu_r(),gmx_simd4_storeu_r()
53 * \author Erik Lindahl <erik.lindahl@scilifelab.se>
54 * \ingroup module_simd
57 #include <gtest/gtest.h>
59 #include "gromacs/legacyheaders/types/simple.h"
61 #include "gromacs/simd/simd.h"
67 /*! \addtogroup module_simd */
70 TEST(SimdBootstrapTest, gmxSimdAlign)
72 #ifdef GMX_SIMD_HAVE_REAL
73 real rdata[GMX_SIMD_REAL_WIDTH*2];
74 for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
76 EXPECT_EQ(((size_t)gmx_simd_align_r(&rdata[i]) & (GMX_SIMD_REAL_WIDTH*sizeof(real)-1)), (size_t)0);
79 #ifdef GMX_SIMD_HAVE_INT32
80 int idata[GMX_SIMD_INT32_WIDTH*2];
81 for (int i = 0; i < GMX_SIMD_INT32_WIDTH; i++)
83 EXPECT_EQ(((size_t)gmx_simd_align_i(&idata[i]) & (GMX_SIMD_INT32_WIDTH*sizeof(int)-1)), (size_t)0);
88 /*! \brief Generic routine to test load & store of SIMD, and check for side effects.
90 * The tests for load, store, unaligned load and unaligned store both for
91 * real and int are pretty much similar, so we use a template function with
92 * additional function pointers for the actual load/store calls. This would
93 * be more hacking to turn into a class, since the SIMD functionality uses
94 * macros rather than functions that can be overloaded.
96 template <typename T, typename TSimd> void
97 simdLoadStoreTester(TSimd simdLoadFn(T* mem), void simdStoreFn(T* mem, TSimd),
98 T * simdAlignFn(T *mem),
99 const int loadOffset, const int storeOffset, const int simdWidth)
101 /* We want simdWidth elements before the data to check we are not polluting
102 * memory. Then we need 2*simdWidth storage to be able to extract an aligned
103 * pointer, another simdWidth elements so we can create (deliberately)
104 * offset un-aligned pointers, and finally simdWidth elements at the end
105 * to test we are not polluting memory there either. Sum=5*simdWidth!
107 std::vector<T> src(simdWidth*5);
108 std::vector<T> dst(simdWidth*5);
109 // Make sure we have memory to check both before and after the test pointers
110 T * pCopySrc = simdAlignFn(&src[0]) + simdWidth + loadOffset;
111 T * pCopyDst = simdAlignFn(&dst[0]) + simdWidth + storeOffset;
114 for (i = 0; i < simdWidth*5; i++)
120 simdStoreFn(pCopyDst, simdLoadFn(pCopySrc));
122 for (i = 0; i < simdWidth; i++)
124 EXPECT_EQ(pCopySrc[i], pCopyDst[i]) << "SIMD load or store not moving data correctly for element " << i;
127 for (i = 0; i < simdWidth*5; i++)
129 EXPECT_EQ(src[i], (T)(1+i)) << "Side effect on source memory, i = " << i;
130 if (&dst[0]+i < pCopyDst || &dst[0]+i >= pCopyDst+simdWidth)
132 EXPECT_EQ(dst[i], (T)(-1-i)) << "Side effect on destination memory, i = " << i;
137 #ifdef GMX_SIMD_HAVE_REAL
138 //! Wrapper for SIMD macro to load aligned floating-point data.
139 gmx_simd_real_t wrapperSimdLoadR(real *m)
141 return gmx_simd_load_r(m);
143 //! Wrapper for SIMD macro to store to aligned floating-point data.
144 void wrapperSimdStoreR(real *m, gmx_simd_real_t s)
146 gmx_simd_store_r(m, s);
149 TEST(SimdBootstrapTest, gmxSimdLoadStoreR)
151 simdLoadStoreTester(wrapperSimdLoadR, wrapperSimdStoreR, gmx_simd_align_r, 0, 0, GMX_SIMD_REAL_WIDTH);
154 # ifdef GMX_SIMD_HAVE_LOADU
155 //! Wrapper for SIMD macro to load unaligned floating-point data.
156 gmx_simd_real_t WrapperSimdLoadUR(real *m)
158 return gmx_simd_loadu_r(m);
161 TEST(SimdBootstrapTest, gmxSimdLoadUR)
163 for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
165 simdLoadStoreTester(WrapperSimdLoadUR, wrapperSimdStoreR, gmx_simd_align_r, i, 0, GMX_SIMD_REAL_WIDTH);
170 # ifdef GMX_SIMD_HAVE_STOREU
171 //! Wrapper for SIMD macro to store to unaligned floating-point data.
172 void WrapperSimdStoreUR(real *m, gmx_simd_real_t s)
174 gmx_simd_storeu_r(m, s);
177 TEST(SimdBootstrapTest, gmxSimdStoreUR)
179 for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
181 simdLoadStoreTester(wrapperSimdLoadR, WrapperSimdStoreUR, gmx_simd_align_r, 0, i, GMX_SIMD_REAL_WIDTH);
187 #ifdef GMX_SIMD_HAVE_INT32
188 // Tests for gmx_simd_int32_t load & store operations
190 //! Wrapper for SIMD macro to load aligned integer data.
191 gmx_simd_int32_t wrapperSimdLoadI(int *m)
193 return gmx_simd_load_i(m);
195 //! Wrapper for SIMD macro to store to aligned integer data.
196 void wrapperSimdStoreI(int *m, gmx_simd_int32_t s)
198 gmx_simd_store_i(m, s);
201 TEST(SimdBootstrapTest, gmxSimdLoadStoreI)
203 simdLoadStoreTester(wrapperSimdLoadI, wrapperSimdStoreI, gmx_simd_align_i, 0, 0, GMX_SIMD_INT32_WIDTH);
206 # ifdef GMX_SIMD_HAVE_LOADU
207 //! Wrapper for SIMD macro to load unaligned integer data.
208 gmx_simd_int32_t wrapperSimdLoadUI(int *m)
210 return gmx_simd_loadu_i(m);
213 TEST(SimdBootstrapTest, gmxSimdLoadUI)
215 for (int i = 0; i < GMX_SIMD_INT32_WIDTH; i++)
217 simdLoadStoreTester(wrapperSimdLoadUI, wrapperSimdStoreI, gmx_simd_align_i, i, 0, GMX_SIMD_INT32_WIDTH);
222 # ifdef GMX_SIMD_HAVE_STOREU
223 //! Wrapper for SIMD macro to store to unaligned integer data.
224 void wrapperSimdStoreUI(int *m, gmx_simd_int32_t s)
226 gmx_simd_storeu_i(m, s);
229 TEST(SimdBootstrapTest, gmxSimdStoreUI)
231 for (int i = 0; i < GMX_SIMD_INT32_WIDTH; i++)
233 simdLoadStoreTester(wrapperSimdLoadI, wrapperSimdStoreUI, gmx_simd_align_i, 0, i, GMX_SIMD_INT32_WIDTH);
239 #ifdef GMX_SIMD4_HAVE_REAL
240 /* Tests for gmx_simd4_real_t load & store operations. Define wrapper functions
241 * for the SIMD instructions that are typically implemented as macros.
244 /*! \brief Separate load/store tester function for SIMD4.
246 * Due to the way SIMD variables
247 * are implemented as deep internal data, some compilers treat them as
248 * float/double with special prefixes. Unfortunately, this means that some C++
249 * compilers think an 8-wide normal real SIMD and a 4-wide SIMD4 real type
250 * cannot be overloaded (e.g. with gcc using 256-bit AVX single precision).
252 template <typename T, typename TSimd> void
253 simd4LoadStoreTester(TSimd simd4LoadFn(T* mem), void simd4StoreFn(T* mem, TSimd),
254 T * simd4AlignFn(T *mem),
255 const int loadOffset, const int storeOffset)
257 /* We want simdWidth elements before the data to check we are not polluting
258 * memory. Then we need 2*simdWidth storage to be able to extract an aligned
259 * pointer, another simdWidth elements so we can create (deliberately)
260 * offset un-aligned pointers, and finally simdWidth elements at the end
261 * to test we are not polluting memory there either. Sum=5*simdWidth!
263 T src[GMX_SIMD4_WIDTH*5];
264 T dst[GMX_SIMD4_WIDTH*5];
265 // Make sure we have memory to check both before and after the test pointers
266 T * pCopySrc = simd4AlignFn(src) + GMX_SIMD4_WIDTH + loadOffset;
267 T * pCopyDst = simd4AlignFn(dst) + GMX_SIMD4_WIDTH + storeOffset;
270 for (i = 0; i < GMX_SIMD4_WIDTH*5; i++)
276 simd4StoreFn(pCopyDst, simd4LoadFn(pCopySrc));
278 for (i = 0; i < GMX_SIMD4_WIDTH; i++)
280 EXPECT_EQ(pCopySrc[i], pCopyDst[i]) << "SIMD4 load or store not moving data correctly for element " << i;
283 for (i = 0; i < GMX_SIMD4_WIDTH*5; i++)
285 EXPECT_EQ(src[i], (T)(1+i)) << "Side effect on source memory, i = " << i;
286 if (dst+i < pCopyDst || dst+i >= pCopyDst+GMX_SIMD4_WIDTH)
288 EXPECT_EQ(dst[i], (T)(-1-i)) << "Side effect on destination memory, i = " << i;
293 //! Wrapper for SIMD4 macro to load aligned floating-point data.
294 gmx_simd4_real_t wrapperSimd4LoadR(real *m)
296 return gmx_simd4_load_r(m);
298 //! Wrapper for SIMD4 macro to store to aligned floating-point data.
299 void wrapperSimd4StoreR(real *m, gmx_simd4_real_t s)
301 gmx_simd4_store_r(m, s);
304 TEST(SimdBootstrapTest, gmxSimd4LoadStoreR)
306 simd4LoadStoreTester(wrapperSimd4LoadR, wrapperSimd4StoreR, gmx_simd4_align_r, 0, 0);
309 # ifdef GMX_SIMD_HAVE_LOADU
310 //! Wrapper for SIMD4 macro to load unaligned floating-point data.
311 gmx_simd4_real_t WrapperSimd4LoadUR(real *m)
313 return gmx_simd4_loadu_r(m);
316 TEST(SimdBootstrapTest, gmxSimd4LoadUR)
318 for (int i = 0; i < GMX_SIMD4_WIDTH; i++)
320 simd4LoadStoreTester(WrapperSimd4LoadUR, wrapperSimd4StoreR, gmx_simd4_align_r, i, 0);
325 # ifdef GMX_SIMD_HAVE_STOREU
326 //! Wrapper for SIMD4 macro to store to unaligned floating-point data.
327 void WrapperSimd4StoreUR(real *m, gmx_simd4_real_t s)
329 gmx_simd4_storeu_r(m, s);
332 TEST(SimdBootstrapTest, gmxSimd4StoreUR)
334 for (int i = 0; i < GMX_SIMD4_WIDTH; i++)
336 simd4LoadStoreTester(wrapperSimd4LoadR, WrapperSimd4StoreUR, gmx_simd4_align_r, 0, i);