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).
77 static inline void subGroupBarrier(const cl::sycl::nd_item<Dim> itemIdx)
80 cl::sycl::group_barrier(itemIdx.get_sub_group(), cl::sycl::memory_scope::sub_group);
82 itemIdx.get_sub_group().barrier();
89 __device__ __host__ static inline float shift_left(sycl_2020::sub_group,
91 sycl_2020::sub_group::linear_id_type delta)
93 // No sycl::sub_group::shift_left / shuffle_down in hipSYCL yet
94 # ifdef SYCL_DEVICE_ONLY
95 # if defined(HIPSYCL_PLATFORM_CUDA) && defined(__HIPSYCL_ENABLE_CUDA_TARGET__)
96 return __shfl_down_sync(c_cudaFullWarpMask, var, delta);
97 # elif defined(HIPSYCL_PLATFORM_ROCM) && defined(__HIPSYCL_ENABLE_HIP_TARGET__)
98 // Do we need more ifdefs? https://github.com/ROCm-Developer-Tools/HIP/issues/1491
99 return __shfl_down(var, delta);
101 # error "Unsupported hipSYCL target"
104 // Should never be called
105 GMX_UNUSED_VALUE(var);
106 GMX_UNUSED_VALUE(delta);
112 static inline float shift_left(sycl_2020::sub_group sg, float var, sycl_2020::sub_group::linear_id_type delta)
114 return sg.shuffle_down(var, delta);
119 __device__ __host__ static inline float shift_right(sycl_2020::sub_group,
121 sycl_2020::sub_group::linear_id_type delta)
123 // No sycl::sub_group::shift_right / shuffle_up in hipSYCL yet
124 # ifdef SYCL_DEVICE_ONLY
125 # if defined(HIPSYCL_PLATFORM_CUDA) && defined(__HIPSYCL_ENABLE_CUDA_TARGET__)
126 return __shfl_up_sync(c_cudaFullWarpMask, var, delta);
127 # elif defined(HIPSYCL_PLATFORM_ROCM) && defined(__HIPSYCL_ENABLE_HIP_TARGET__)
128 // Do we need more ifdefs? https://github.com/ROCm-Developer-Tools/HIP/issues/1491
129 return __shfl_up(var, delta);
131 # error "Unsupported hipSYCL target"
134 // Should never be called
136 GMX_UNUSED_VALUE(var);
137 GMX_UNUSED_VALUE(delta);
142 static inline float shift_right(sycl_2020::sub_group sg, float var, sycl_2020::sub_group::linear_id_type delta)
144 return sg.shuffle_up(var, delta);
149 /*! \brief Polyfill for sycl::isfinite missing from hipSYCL
151 * Does not follow GROMACS style because it should follow the name for
152 * which it is a polyfill. */
153 template<typename Real>
154 __device__ __host__ static inline bool isfinite(Real value)
156 // This is not yet implemented in hipSYCL pending
157 // https://github.com/illuhad/hipSYCL/issues/636
158 # ifdef SYCL_DEVICE_ONLY
159 # if defined(HIPSYCL_PLATFORM_CUDA) && defined(__HIPSYCL_ENABLE_CUDA_TARGET__)
160 return ::isfinite(value);
161 # elif defined(HIPSYCL_PLATFORM_ROCM) && defined(__HIPSYCL_ENABLE_HIP_TARGET__)
162 return ::isfinite(value);
164 # error "Unsupported hipSYCL target"
167 // Should never be called
169 GMX_UNUSED_VALUE(value);
174 template<typename Real>
175 static inline bool isfinite(Real value)
177 return cl::sycl::isfinite(value);
184 /*! \brief Polyfill for sycl::vec::load buggy in hipSYCL
186 * Loads from the address \c ptr offset in elements of type T by
187 * NumElements * offset, into the components of \c v.
189 * Can probably be removed when
190 * https://github.com/illuhad/hipSYCL/issues/647 is resolved. */
191 template<cl::sycl::access::address_space AddressSpace, typename T, int NumElements>
192 static inline void loadToVec(size_t offset,
193 cl::sycl::multi_ptr<const T, AddressSpace> ptr,
194 cl::sycl::vec<T, NumElements>* v)
196 for (int i = 0; i < NumElements; ++i)
198 (*v)[i] = ptr.get()[offset * NumElements + i];
202 /*! \brief Polyfill for sycl::vec::store buggy in hipSYCL
204 * Loads from the address \c ptr offset in elements of type T by
205 * NumElements * offset, into the components of \c v.
207 * Can probably be removed when
208 * https://github.com/illuhad/hipSYCL/issues/647 is resolved. */
209 template<cl::sycl::access::address_space AddressSpace, typename T, int NumElements>
210 static inline void storeFromVec(const cl::sycl::vec<T, NumElements>& v,
212 cl::sycl::multi_ptr<T, AddressSpace> ptr)
214 for (int i = 0; i < NumElements; ++i)
216 ptr.get()[offset * NumElements + i] = v[i];
222 /*! \brief Polyfill for sycl::vec::load buggy in hipSYCL
224 * Loads from the address \c ptr offset in elements of type T by
225 * NumElements * offset, into the components of \c v.
227 * Can probably be removed when
228 * https://github.com/illuhad/hipSYCL/issues/647 is resolved. */
229 template<cl::sycl::access::address_space AddressSpace, typename T, int NumElements>
230 static inline void loadToVec(size_t offset,
231 cl::sycl::multi_ptr<const T, AddressSpace> ptr,
232 cl::sycl::vec<T, NumElements>* v)
234 v->load(offset, ptr);
237 /*! \brief Polyfill for sycl::vec::store buggy in hipSYCL
239 * Loads from the address \c ptr offset in elements of type T by
240 * NumElements * offset, into the components of \c v.
242 * Can probably be removed when
243 * https://github.com/illuhad/hipSYCL/issues/647 is resolved. */
244 template<cl::sycl::access::address_space AddressSpace, typename T, int NumElements>
245 static inline void storeFromVec(const cl::sycl::vec<T, NumElements>& v,
247 cl::sycl::multi_ptr<T, AddressSpace> ptr)
249 v.store(offset, ptr);
254 } // namespace sycl_2020
256 #endif /* GMX_GPU_UTILS_SYCL_KERNEL_UTILS_H */