dd95c9128fbb97b44ad9128fa1bc6859c9f6ab11
[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, 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         unsigned int u32;
63         float        f32;
64     } next, expected, current;
65     current.f32 = *addr;
66     do
67     {
68         expected.f32 = current.f32;
69         next.f32     = expected.f32 + val;
70         current.u32  = atomic_cmpxchg((volatile __local unsigned int*)addr, expected.u32, next.u32);
71     } while (current.u32 != expected.u32);
72 }
73
74 /* \brief Atomically increment the float3 value at address addr (in local memory) with value val.
75  *
76  * Note that the thee components of the float3 addr are incremented sequentially.
77  *
78  * The adrress should be a local memory pointer.
79  *
80  * \param[in]   addr    The address where the data to be added to resides
81  * \param[in]   val     The value to increment with
82  */
83 gmx_opencl_inline void atomicAdd_l_f3(__local float3* addr, float3 val)
84 {
85     atomicAdd_l_f(((__local float*)(addr)), val.x);
86     atomicAdd_l_f(((__local float*)(addr)) + 1, val.y);
87     atomicAdd_l_f(((__local float*)(addr)) + 2, val.z);
88 }
89
90 /* \brief Atomically increment the float value at address addr (in global memory) with value val.
91  *
92  * The address addr should be a global memory pointer.
93  *
94  * \param[in]   addr    The address where the data to be added to resides
95  * \param[in]   val     The value to increment with
96  */
97 gmx_opencl_inline void atomicAdd_g_f(volatile __global float* addr, float val)
98 {
99     union {
100         unsigned int u32;
101         float        f32;
102     } next, expected, current;
103     current.f32 = *addr;
104     do
105     {
106         expected.f32 = current.f32;
107         next.f32     = expected.f32 + val;
108         current.u32 = atomic_cmpxchg((volatile __global unsigned int*)addr, expected.u32, next.u32);
109     } while (current.u32 != expected.u32);
110 }
111
112 /* \brief Atomically increment the float3 value at address addr (in local memory) with value val.
113  *
114  * Note that the thee components of the float3 addr are incremented sequentially.
115  * On the host float3 is used, but on the device float1 because f3 translates to f4 and messes up memory indexing.
116  *
117  * The adrress addr should be a local memory pointer.
118  *
119  * \param[in]   addr    The address where the data to be added to resides
120  * \param[in]   val     The value to increment with
121  */
122 gmx_opencl_inline void atomicAdd_g_f3(__global float* addr, const float3 val)
123 {
124     atomicAdd_g_f(addr, val.x);
125     atomicAdd_g_f(addr + 1, val.y);
126     atomicAdd_g_f(addr + 2, val.z);
127 }
128
129 #endif /* GMX_GPU_UTILS_DEVICE_UTILS_CLH */