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 Access mode to use for atomic accessors.
49 * Intel DPCPP compiler has \c sycl::atomic_ref, but has no \c sycl::atomic_fetch_add for floats.
50 * However, \c atomic_ref can not be constructed from \c sycl::atomic, so we can not use
51 * atomic accessors. Thus, we use \c mode::read_write accessors and \c atomic_ref.
53 * hipSYCL does not have \c sycl::atomic_ref, but has \c sycl::atomic_fetch_add for floats, which
54 * requires using atomic accessors. Thus, we use \c mode::atomic accessors.
56 * The \ref atomicFetchAdd function could be used for doing operations on such accessors.
58 static constexpr auto mode_atomic = GMX_SYCL_DPCPP ? cl::sycl::access::mode::read_write :
59 /* GMX_SYCL_HIPSYCL */ cl::sycl::access::mode::atomic;
61 // \brief Full warp active thread mask used in CUDA warp-level primitives.
62 static constexpr unsigned int c_cudaFullWarpMask = 0xffffffff;
64 /*! \brief Convenience wrapper to do atomic addition to a global buffer.
66 * The implementation differences between DPCPP and hipSYCL are explained in \ref mode_atomic.
68 template<class IndexType>
69 static inline void atomicFetchAdd(DeviceAccessor<float, mode_atomic> acc, const IndexType idx, const float val)
72 sycl_2020::atomic_ref<float, sycl_2020::memory_order::relaxed, sycl_2020::memory_scope::device, cl::sycl::access::address_space::global_space>
73 fout_atomic(acc[idx]);
74 fout_atomic.fetch_add(val);
75 #elif GMX_SYCL_HIPSYCL
76 # ifdef SYCL_DEVICE_ONLY
77 /* While there is support for float atomics on device, the host implementation uses
78 * Clang's __atomic_fetch_add intrinsic, that, at least in Clang 11, does not support
79 * floats. Luckily, we don't want to run on host. */
80 // The pragmas below can be removed once we switch to sycl::atomic
81 # pragma clang diagnostic push
82 # pragma clang diagnostic ignored "-Wdeprecated-declarations"
83 acc[idx].fetch_add(val);
84 # pragma clang diagnostic push
86 GMX_ASSERT(false, "hipSYCL host codepath not supported");
87 GMX_UNUSED_VALUE(val);
88 GMX_UNUSED_VALUE(acc);
89 GMX_UNUSED_VALUE(idx);
94 /* \brief Issue an intra sub-group barrier.
96 * Equivalent with CUDA syncwarp(c_cudaFullWarpMask).
99 static inline void subGroupBarrier(const cl::sycl::nd_item<1> itemIdx)
102 cl::sycl::group_barrier(itemIdx.get_sub_group(), cl::sycl::memory_scope::sub_group);
104 itemIdx.get_sub_group().barrier();
111 __device__ __host__ static inline float shift_left(sycl_2020::sub_group,
113 sycl_2020::sub_group::linear_id_type delta)
115 // No sycl::sub_group::shift_left / shuffle_down in hipSYCL yet
116 # ifdef SYCL_DEVICE_ONLY
117 # if defined(HIPSYCL_PLATFORM_CUDA) && defined(__HIPSYCL_ENABLE_CUDA_TARGET__)
118 return __shfl_down_sync(c_cudaFullWarpMask, var, delta);
119 # elif defined(HIPSYCL_PLATFORM_ROCM) && defined(__HIPSYCL_ENABLE_HIP_TARGET__)
120 // Do we need more ifdefs? https://github.com/ROCm-Developer-Tools/HIP/issues/1491
121 return __shfl_down(var, delta);
123 # error "Unsupported hipSYCL target"
126 // Should never be called
127 GMX_UNUSED_VALUE(var);
128 GMX_UNUSED_VALUE(delta);
134 static inline float shift_left(sycl_2020::sub_group sg, float var, sycl_2020::sub_group::linear_id_type delta)
136 return sg.shuffle_down(var, delta);
141 __device__ __host__ static inline float shift_right(sycl_2020::sub_group,
143 sycl_2020::sub_group::linear_id_type delta)
145 // No sycl::sub_group::shift_right / shuffle_up in hipSYCL yet
146 # ifdef SYCL_DEVICE_ONLY
147 # if defined(HIPSYCL_PLATFORM_CUDA) && defined(__HIPSYCL_ENABLE_CUDA_TARGET__)
148 return __shfl_up_sync(c_cudaFullWarpMask, var, delta);
149 # elif defined(HIPSYCL_PLATFORM_ROCM) && defined(__HIPSYCL_ENABLE_HIP_TARGET__)
150 // Do we need more ifdefs? https://github.com/ROCm-Developer-Tools/HIP/issues/1491
151 return __shfl_up(var, delta);
153 # error "Unsupported hipSYCL target"
156 // Should never be called
158 GMX_UNUSED_VALUE(var);
159 GMX_UNUSED_VALUE(delta);
164 static inline float shift_right(sycl_2020::sub_group sg, float var, sycl_2020::sub_group::linear_id_type delta)
166 return sg.shuffle_up(var, delta);
169 } // namespace sycl_2020
171 #endif /* GMX_GPU_UTILS_SYCL_KERNEL_UTILS_H */