2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 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.
36 #ifndef GMX_GPU_UTILS_SYCL_KERNEL_UTILS_H
37 #define GMX_GPU_UTILS_SYCL_KERNEL_UTILS_H
42 * \brief SYCL kernel helper functions.
44 * \author Andrey Alekseenko <al42and@gmail.com>
47 //! \brief Full warp active thread mask used in CUDA warp-level primitives.
48 static constexpr unsigned int c_cudaFullWarpMask = 0xffffffff;
50 /*! \brief Convenience wrapper to do atomic addition to a global buffer.
52 template<typename T, sycl_2020::memory_scope MemoryScope = sycl_2020::memory_scope::device>
53 static inline void atomicFetchAdd(T& val, const T delta)
55 sycl_2020::atomic_ref<T, sycl_2020::memory_order::relaxed, MemoryScope, cl::sycl::access::address_space::global_space> ref(
60 /*! \brief Convenience wrapper to do atomic loads from a global buffer.
62 template<typename T, sycl_2020::memory_scope MemoryScope = sycl_2020::memory_scope::device>
63 static inline T atomicLoad(T& val)
65 sycl_2020::atomic_ref<T, sycl_2020::memory_order::relaxed, MemoryScope, cl::sycl::access::address_space::global_space> ref(
71 /*! \brief Issue an intra sub-group barrier.
73 * Equivalent with CUDA's \c syncwarp(c_cudaFullWarpMask).
76 static inline void subGroupBarrier(const cl::sycl::nd_item<1> itemIdx)
79 cl::sycl::group_barrier(itemIdx.get_sub_group(), cl::sycl::memory_scope::sub_group);
81 itemIdx.get_sub_group().barrier();
88 __device__ __host__ static inline float shift_left(sycl_2020::sub_group,
90 sycl_2020::sub_group::linear_id_type delta)
92 // No sycl::sub_group::shift_left / shuffle_down in hipSYCL yet
93 # ifdef SYCL_DEVICE_ONLY
94 # if defined(HIPSYCL_PLATFORM_CUDA) && defined(__HIPSYCL_ENABLE_CUDA_TARGET__)
95 return __shfl_down_sync(c_cudaFullWarpMask, var, delta);
96 # elif defined(HIPSYCL_PLATFORM_ROCM) && defined(__HIPSYCL_ENABLE_HIP_TARGET__)
97 // Do we need more ifdefs? https://github.com/ROCm-Developer-Tools/HIP/issues/1491
98 return __shfl_down(var, delta);
100 # error "Unsupported hipSYCL target"
103 // Should never be called
104 GMX_UNUSED_VALUE(var);
105 GMX_UNUSED_VALUE(delta);
111 static inline float shift_left(sycl_2020::sub_group sg, float var, sycl_2020::sub_group::linear_id_type delta)
113 return sg.shuffle_down(var, delta);
118 __device__ __host__ static inline float shift_right(sycl_2020::sub_group,
120 sycl_2020::sub_group::linear_id_type delta)
122 // No sycl::sub_group::shift_right / shuffle_up in hipSYCL yet
123 # ifdef SYCL_DEVICE_ONLY
124 # if defined(HIPSYCL_PLATFORM_CUDA) && defined(__HIPSYCL_ENABLE_CUDA_TARGET__)
125 return __shfl_up_sync(c_cudaFullWarpMask, var, delta);
126 # elif defined(HIPSYCL_PLATFORM_ROCM) && defined(__HIPSYCL_ENABLE_HIP_TARGET__)
127 // Do we need more ifdefs? https://github.com/ROCm-Developer-Tools/HIP/issues/1491
128 return __shfl_up(var, delta);
130 # error "Unsupported hipSYCL target"
133 // Should never be called
135 GMX_UNUSED_VALUE(var);
136 GMX_UNUSED_VALUE(delta);
141 static inline float shift_right(sycl_2020::sub_group sg, float var, sycl_2020::sub_group::linear_id_type delta)
143 return sg.shuffle_up(var, delta);
146 } // namespace sycl_2020
148 #endif /* GMX_GPU_UTILS_SYCL_KERNEL_UTILS_H */