2 * This file is part of the GROMACS molecular simulation package.
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.
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)
65 } next, expected, current;
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);
75 /* \brief Atomically increment the float3 value at address addr (in local memory) with value val.
77 * Note that the thee components of the float3 addr are incremented sequentially.
79 * The adrress should be a local memory pointer.
81 * \param[in] addr The address where the data to be added to resides
82 * \param[in] val The value to increment with
84 gmx_opencl_inline void atomicAdd_l_f3(__local float3* addr, float3 val)
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);
91 /* \brief Atomically increment the float value at address addr (in global memory) with value val.
93 * The address addr should be a global memory pointer.
95 * \param[in] addr The address where the data to be added to resides
96 * \param[in] val The value to increment with
98 gmx_opencl_inline void atomicAdd_g_f(volatile __global float* addr, float val)
104 } next, expected, current;
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);
114 /* \brief Atomically increment the float3 value at address addr (in local memory) with value val.
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.
119 * The adrress addr should be a local memory pointer.
121 * \param[in] addr The address where the data to be added to resides
122 * \param[in] val The value to increment with
124 gmx_opencl_inline void atomicAdd_g_f3(__global float* addr, const float3 val)
126 atomicAdd_g_f(addr, val.x);
127 atomicAdd_g_f(addr + 1, val.y);
128 atomicAdd_g_f(addr + 2, val.z);
131 #endif /* GMX_GPU_UTILS_DEVICE_UTILS_CLH */