2 * This file is part of the GROMACS molecular simulation package.
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.
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.
37 * \brief OpenCL device-side utilities.
39 * Implements device-side macros and inline functions to be used in OpenCL kernels.
41 * \author Szilárd Páll <pall.szilard@gmail.com>
42 * \ingroup module_gpu_utils
45 #ifndef GMX_GPU_UTILS_DEVICE_UTILS_CLH
46 #define GMX_GPU_UTILS_DEVICE_UTILS_CLH
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
52 /* \brief Atomically increment the float value at address addr (in local memory) with value val.
54 * The adrress addr should be a local memory pointer.
56 * \param[in] addr The address where the data to be added to resides
57 * \param[in] val The value to increment with
59 gmx_opencl_inline void atomicAdd_l_f(volatile __local float* addr, float val)
64 } next, expected, current;
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);
74 /* \brief Atomically increment the float3 value at address addr (in local memory) with value val.
76 * Note that the thee components of the float3 addr are incremented sequentially.
78 * The adrress should be a local memory pointer.
80 * \param[in] addr The address where the data to be added to resides
81 * \param[in] val The value to increment with
83 gmx_opencl_inline void atomicAdd_l_f3(__local float3* addr, float3 val)
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);
90 /* \brief Atomically increment the float value at address addr (in global memory) with value val.
92 * The address addr should be a global memory pointer.
94 * \param[in] addr The address where the data to be added to resides
95 * \param[in] val The value to increment with
97 gmx_opencl_inline void atomicAdd_g_f(volatile __global float* addr, float val)
102 } next, expected, current;
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);
112 /* \brief Atomically increment the float3 value at address addr (in local memory) with value val.
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.
117 * The adrress addr should be a local memory pointer.
119 * \param[in] addr The address where the data to be added to resides
120 * \param[in] val The value to increment with
122 gmx_opencl_inline void atomicAdd_g_f3(__global float* addr, const float3 val)
124 atomicAdd_g_f(addr, val.x);
125 atomicAdd_g_f(addr + 1, val.y);
126 atomicAdd_g_f(addr + 2, val.z);
129 #endif /* GMX_GPU_UTILS_DEVICE_UTILS_CLH */