SYCL: Avoid using no_init read accessor in rocFFT
[alexxy/gromacs.git] / src / gromacs / simd / tests / simd_vector_operations.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2014,2015,2017,2018,2019,2020, 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 "gmxpre.h"
36
37 #include <cmath>
38
39 #include "gromacs/simd/simd.h"
40 #include "gromacs/simd/vector_operations.h"
41
42 #include "data.h"
43 #include "simd.h"
44
45 #if GMX_SIMD
46
47 namespace gmx
48 {
49 namespace test
50 {
51 namespace
52 {
53
54 /*! \cond internal */
55 /*! \addtogroup module_simd */
56 /*! \{ */
57
58 #    if GMX_SIMD_HAVE_REAL
59
60 /*! \internal \brief Test fixture for vector operations tests (identical to the generic \ref SimdTest) */
61 typedef SimdTest SimdVectorOperationsTest;
62
63 TEST_F(SimdVectorOperationsTest, iprod)
64 {
65     SimdReal aX       = rSimd_c0c1c2;
66     SimdReal aY       = rSimd_c3c4c5;
67     SimdReal aZ       = rSimd_c6c7c8;
68     SimdReal bX       = rSimd_c3c0c4;
69     SimdReal bY       = rSimd_c4c6c8;
70     SimdReal bZ       = rSimd_c7c2c3;
71     SimdReal iprodRef = setSimdRealFrom3R(
72             c0 * c3 + c3 * c4 + c6 * c7, c1 * c0 + c4 * c6 + c7 * c2, c2 * c4 + c5 * c8 + c8 * c3);
73
74     setUlpTol(2);
75     GMX_EXPECT_SIMD_REAL_NEAR(iprodRef, iprod(aX, aY, aZ, bX, bY, bZ));
76 }
77
78 TEST_F(SimdVectorOperationsTest, norm2)
79 {
80     SimdReal simdX    = rSimd_c0c1c2;
81     SimdReal simdY    = rSimd_c3c4c5;
82     SimdReal simdZ    = rSimd_c6c7c8;
83     SimdReal norm2Ref = setSimdRealFrom3R(
84             c0 * c0 + c3 * c3 + c6 * c6, c1 * c1 + c4 * c4 + c7 * c7, c2 * c2 + c5 * c5 + c8 * c8);
85
86     setUlpTol(2);
87     GMX_EXPECT_SIMD_REAL_NEAR(norm2Ref, norm2(simdX, simdY, simdZ));
88 }
89
90 TEST_F(SimdVectorOperationsTest, cprod)
91 {
92     SimdReal aX = rSimd_c0c1c2;
93     SimdReal aY = rSimd_c3c4c5;
94     SimdReal aZ = rSimd_c6c7c8;
95     SimdReal bX = rSimd_c3c0c4;
96     SimdReal bY = rSimd_c4c6c8;
97     SimdReal bZ = rSimd_c7c2c3;
98     // The SIMD version might use FMA. If we don't force FMA for the reference value, the compiler is free to use FMA
99     // for either product. If the compiler uses FMA for one product and the SIMD version uses FMA for the other, the
100     // rounding error of each product adds up and the total possible ulp-error is 12.
101     SimdReal refcX = setSimdRealFrom3R(
102             std::fma(-c6, c4, c3 * c7), std::fma(-c7, c6, c4 * c2), std::fma(-c8, c8, c5 * c3));
103     SimdReal refcY = setSimdRealFrom3R(
104             std::fma(-c0, c7, c6 * c3), std::fma(-c1, c2, c7 * c0), std::fma(-c2, c3, c8 * c4));
105     SimdReal refcZ = setSimdRealFrom3R(
106             std::fma(-c3, c3, c0 * c4), std::fma(-c4, c0, c1 * c6), std::fma(-c5, c4, c2 * c8));
107     SimdReal cX, cY, cZ;
108
109     // The test assumes that cprod uses FMA on architectures which have FMA so that the compiler
110     // can't choose which product is computed with FMA.
111     cprod(aX, aY, aZ, bX, bY, bZ, &cX, &cY, &cZ);
112
113     // The test values cannot be computed without FMA for the case that SIMD has no FMA. Even if no
114     // explicit FMA were used, the compiler could choose to use FMA. This causes up to 6upl error
115     // because of the product is up to 6 times larger than the final result after the difference.
116     setUlpTol(GMX_SIMD_HAVE_FMA ? ulpTol_ : 6);
117
118     GMX_EXPECT_SIMD_REAL_NEAR(refcX, cX);
119     GMX_EXPECT_SIMD_REAL_NEAR(refcY, cY);
120     GMX_EXPECT_SIMD_REAL_NEAR(refcZ, cZ);
121 }
122
123 #    endif // GMX_SIMD_HAVE_REAL
124
125 /*! \} */
126 /*! \endcond */
127
128 } // namespace
129 } // namespace test
130 } // namespace gmx
131
132 #endif // GMX_SIMD