/*
 * This file is part of the GROMACS molecular simulation package.
 *
 * Copyright (c) 2012,2018, by the GROMACS development team, led by
 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
 * and including many others, as listed in the AUTHORS file in the
 * top-level source directory and at http://www.gromacs.org.
 *
 * GROMACS is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 *
 * GROMACS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with GROMACS; if not, see
 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
 *
 * If you want to redistribute modifications to GROMACS, please
 * consider that scientific software is very special. Version
 * control is crucial - bugs must be traceable. We will be happy to
 * consider code for inclusion in the official distribution, but
 * derived work must not be called official GROMACS. Details are found
 * in the README & COPYING files - if they are missing, get the
 * official version at http://www.gromacs.org.
 *
 * To help us fund GROMACS development, we humbly ask that you cite
 * the research papers on the package. Check out http://www.gromacs.org.
 */

/*! \internal \file
 *  \brief OpenCL device-side utilities.
 *
 *  Implements device-side macros and inline functions to be used in OpenCL kernels.
 *
 *  \author Szilárd Páll <pall.szilard@gmail.com>
 *  \ingroup module_gpu_utils
 */

#ifndef GMX_GPU_UTILS_DEVICE_UTILS_CLH
#define GMX_GPU_UTILS_DEVICE_UTILS_CLH

///! As far as we know this should be enough to convince OpenCL C compilers
//   to inline, but if needed, customization should be done here.
#define gmx_opencl_inline static inline

/* \brief Atomically increment the float value at address addr (in local memory) with value val.
 *
 * The adrress addr should be a local memory pointer.
 *
 * \param[in]   addr    The address where the data to be added to resides
 * \param[in]   val     The value to increment with
 */
gmx_opencl_inline
void atomicAdd_l_f(volatile __local float *addr, float val)
{
    union {
        unsigned int u32;
        float        f32;
    } next, expected, current;
    current.f32    = *addr;
    do
    {
        expected.f32 = current.f32;
        next.f32     = expected.f32 + val;
        current.u32  = atomic_cmpxchg( (volatile __local unsigned int *)addr, expected.u32, next.u32);
    }
    while (current.u32 != expected.u32);
}

/* \brief Atomically increment the float3 value at address addr (in local memory) with value val.
 *
 * Note that the thee components of the float3 addr are incremented sequentially.
 *
 * The adrress should be a local memory pointer.
 *
 * \param[in]   addr    The address where the data to be added to resides
 * \param[in]   val     The value to increment with
 */
gmx_opencl_inline void atomicAdd_l_f3(__local float3 *addr, float3 val)
{
    atomicAdd_l_f( ((__local float*)(addr)), val.x);
    atomicAdd_l_f( ((__local float*)(addr))+1, val.y);
    atomicAdd_l_f( ((__local float*)(addr))+2, val.z);
}

/* \brief Atomically increment the float value at address addr (in global memory) with value val.
 *
 * The address addr should be a global memory pointer.
 *
 * \param[in]   addr    The address where the data to be added to resides
 * \param[in]   val     The value to increment with
 */
gmx_opencl_inline void atomicAdd_g_f(volatile __global float *addr, float val)
{
    union {
        unsigned int u32;
        float        f32;
    } next, expected, current;
    current.f32    = *addr;
    do
    {
        expected.f32 = current.f32;
        next.f32     = expected.f32 + val;
        current.u32  = atomic_cmpxchg( (volatile __global unsigned int *)addr, expected.u32, next.u32);
    }
    while (current.u32 != expected.u32);
}

/* \brief Atomically increment the float3 value at address addr (in local memory) with value val.
 *
 * Note that the thee components of the float3 addr are incremented sequentially.
 * On the host float3 is used, but on the device float1 because f3 translates to f4 and messes up memory indexing.
 *
 * The adrress addr should be a local memory pointer.
 *
 * \param[in]   addr    The address where the data to be added to resides
 * \param[in]   val     The value to increment with
 */
gmx_opencl_inline void atomicAdd_g_f3(__global float *addr, const float3 val)
{
    atomicAdd_g_f(addr, val.x);
    atomicAdd_g_f(addr + 1, val.y);
    atomicAdd_g_f(addr + 2, val.z);
}

#endif /* GMX_GPU_UTILS_DEVICE_UTILS_CLH */
