SYCL: Avoid using no_init read accessor in rocFFT
[alexxy/gromacs.git] / src / gromacs / gpu_utils / device_utils.clh
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2018,2019,2021, 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
36 /*! \internal \file
37  *  \brief OpenCL device-side utilities.
38  *
39  *  Implements device-side macros and inline functions to be used in OpenCL kernels.
40  *
41  *  \author Szilárd Páll <pall.szilard@gmail.com>
42  *  \ingroup module_gpu_utils
43  */
44
45 #ifndef GMX_GPU_UTILS_DEVICE_UTILS_CLH
46 #define GMX_GPU_UTILS_DEVICE_UTILS_CLH
47
48 ///! As far as we know this should be enough to convince OpenCL C compilers
49 //   to inline, but if needed, customization should be done here.
50 #define gmx_opencl_inline static inline
51
52 /* \brief Atomically increment the float value at address addr (in local memory) with value val.
53  *
54  * The adrress addr should be a local memory pointer.
55  *
56  * \param[in]   addr    The address where the data to be added to resides
57  * \param[in]   val     The value to increment with
58  */
59 gmx_opencl_inline void atomicAdd_l_f(volatile __local float* addr, float val)
60 {
61     union
62     {
63         unsigned int u32;
64         float        f32;
65     } next, expected, current;
66     current.f32 = *addr;
67     do
68     {
69         expected.f32 = current.f32;
70         next.f32     = expected.f32 + val;
71         current.u32  = atomic_cmpxchg((volatile __local unsigned int*)addr, expected.u32, next.u32);
72     } while (current.u32 != expected.u32);
73 }
74
75 /* \brief Atomically increment the float3 value at address addr (in local memory) with value val.
76  *
77  * Note that the thee components of the float3 addr are incremented sequentially.
78  *
79  * The adrress should be a local memory pointer.
80  *
81  * \param[in]   addr    The address where the data to be added to resides
82  * \param[in]   val     The value to increment with
83  */
84 gmx_opencl_inline void atomicAdd_l_f3(__local float3* addr, float3 val)
85 {
86     atomicAdd_l_f(((__local float*)(addr)), val.x);
87     atomicAdd_l_f(((__local float*)(addr)) + 1, val.y);
88     atomicAdd_l_f(((__local float*)(addr)) + 2, val.z);
89 }
90
91 /* \brief Atomically increment the float value at address addr (in global memory) with value val.
92  *
93  * The address addr should be a global memory pointer.
94  *
95  * \param[in]   addr    The address where the data to be added to resides
96  * \param[in]   val     The value to increment with
97  */
98 gmx_opencl_inline void atomicAdd_g_f(volatile __global float* addr, float val)
99 {
100     union
101     {
102         unsigned int u32;
103         float        f32;
104     } next, expected, current;
105     current.f32 = *addr;
106     do
107     {
108         expected.f32 = current.f32;
109         next.f32     = expected.f32 + val;
110         current.u32 = atomic_cmpxchg((volatile __global unsigned int*)addr, expected.u32, next.u32);
111     } while (current.u32 != expected.u32);
112 }
113
114 /* \brief Atomically increment the float3 value at address addr (in local memory) with value val.
115  *
116  * Note that the thee components of the float3 addr are incremented sequentially.
117  * On the host float3 is used, but on the device float1 because f3 translates to f4 and messes up memory indexing.
118  *
119  * The adrress addr should be a local memory pointer.
120  *
121  * \param[in]   addr    The address where the data to be added to resides
122  * \param[in]   val     The value to increment with
123  */
124 gmx_opencl_inline void atomicAdd_g_f3(__global float* addr, const float3 val)
125 {
126     atomicAdd_g_f(addr, val.x);
127     atomicAdd_g_f(addr + 1, val.y);
128     atomicAdd_g_f(addr + 2, val.z);
129 }
130
131 #endif /* GMX_GPU_UTILS_DEVICE_UTILS_CLH */