Implement PME solve in SYCL
[alexxy/gromacs.git] / src / gromacs / gpu_utils / sycl_kernel_utils.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35
36 #ifndef GMX_GPU_UTILS_SYCL_KERNEL_UTILS_H
37 #define GMX_GPU_UTILS_SYCL_KERNEL_UTILS_H
38
39 #include "gmxsycl.h"
40
41 /*! \file
42  *  \brief SYCL kernel helper functions.
43  *
44  *  \author Andrey Alekseenko <al42and@gmail.com>
45  */
46
47 //! \brief Full warp active thread mask used in CUDA warp-level primitives.
48 static constexpr unsigned int c_cudaFullWarpMask = 0xffffffff;
49
50 /*! \brief Convenience wrapper to do atomic addition to a global buffer.
51  */
52 template<typename T, sycl_2020::memory_scope MemoryScope = sycl_2020::memory_scope::device>
53 static inline void atomicFetchAdd(T& val, const T delta)
54 {
55     sycl_2020::atomic_ref<T, sycl_2020::memory_order::relaxed, MemoryScope, cl::sycl::access::address_space::global_space> ref(
56             val);
57     ref.fetch_add(delta);
58 }
59
60 /*! \brief Convenience wrapper to do atomic loads from a global buffer.
61  */
62 template<typename T, sycl_2020::memory_scope MemoryScope = sycl_2020::memory_scope::device>
63 static inline T atomicLoad(T& val)
64 {
65     sycl_2020::atomic_ref<T, sycl_2020::memory_order::relaxed, MemoryScope, cl::sycl::access::address_space::global_space> ref(
66             val);
67     return ref.load();
68 }
69
70
71 /*! \brief Issue an intra sub-group barrier.
72  *
73  * Equivalent with CUDA's \c syncwarp(c_cudaFullWarpMask).
74  *
75  */
76 template<int Dim>
77 static inline void subGroupBarrier(const cl::sycl::nd_item<Dim> itemIdx)
78 {
79 #if GMX_SYCL_HIPSYCL
80     cl::sycl::group_barrier(itemIdx.get_sub_group(), cl::sycl::memory_scope::sub_group);
81 #else
82     itemIdx.get_sub_group().barrier();
83 #endif
84 }
85
86 namespace sycl_2020
87 {
88 #if GMX_SYCL_HIPSYCL
89 __device__ __host__ static inline float shift_left(sycl_2020::sub_group,
90                                                    float                                var,
91                                                    sycl_2020::sub_group::linear_id_type delta)
92 {
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);
100 #        else
101 #            error "Unsupported hipSYCL target"
102 #        endif
103 #    else
104     // Should never be called
105     GMX_UNUSED_VALUE(var);
106     GMX_UNUSED_VALUE(delta);
107     assert(false);
108     return NAN;
109 #    endif
110 }
111 #elif GMX_SYCL_DPCPP
112 static inline float shift_left(sycl_2020::sub_group sg, float var, sycl_2020::sub_group::linear_id_type delta)
113 {
114     return sg.shuffle_down(var, delta);
115 }
116 #endif
117
118 #if GMX_SYCL_HIPSYCL
119 __device__ __host__ static inline float shift_right(sycl_2020::sub_group,
120                                                     float                                var,
121                                                     sycl_2020::sub_group::linear_id_type delta)
122 {
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);
130 #        else
131 #            error "Unsupported hipSYCL target"
132 #        endif
133 #    else
134     // Should never be called
135     assert(false);
136     GMX_UNUSED_VALUE(var);
137     GMX_UNUSED_VALUE(delta);
138     return NAN;
139 #    endif
140 }
141 #elif GMX_SYCL_DPCPP
142 static inline float shift_right(sycl_2020::sub_group sg, float var, sycl_2020::sub_group::linear_id_type delta)
143 {
144     return sg.shuffle_up(var, delta);
145 }
146 #endif
147
148 #if GMX_SYCL_HIPSYCL
149 /*! \brief Polyfill for sycl::isfinite missing from hipSYCL
150  *
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)
155 {
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);
163 #        else
164 #            error "Unsupported hipSYCL target"
165 #        endif
166 #    else
167     // Should never be called
168     assert(false);
169     GMX_UNUSED_VALUE(value);
170     return false;
171 #    endif
172 }
173 #elif GMX_SYCL_DPCPP
174 template<typename Real>
175 static inline bool isfinite(Real value)
176 {
177     return cl::sycl::isfinite(value);
178 }
179
180 #endif
181
182 #if GMX_SYCL_HIPSYCL
183
184 /*! \brief Polyfill for sycl::vec::load buggy in hipSYCL
185  *
186  * Loads from the address \c ptr offset in elements of type T by
187  * NumElements * offset, into the components of \c v.
188  *
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)
195 {
196     for (int i = 0; i < NumElements; ++i)
197     {
198         (*v)[i] = ptr.get()[offset * NumElements + i];
199     }
200 }
201
202 /*! \brief Polyfill for sycl::vec::store buggy in hipSYCL
203  *
204  * Loads from the address \c ptr offset in elements of type T by
205  * NumElements * offset, into the components of \c v.
206  *
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,
211                                 size_t                               offset,
212                                 cl::sycl::multi_ptr<T, AddressSpace> ptr)
213 {
214     for (int i = 0; i < NumElements; ++i)
215     {
216         ptr.get()[offset * NumElements + i] = v[i];
217     }
218 }
219
220 #elif GMX_SYCL_DPCPP
221
222 /*! \brief Polyfill for sycl::vec::load buggy in hipSYCL
223  *
224  * Loads from the address \c ptr offset in elements of type T by
225  * NumElements * offset, into the components of \c v.
226  *
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)
233 {
234     v->load(offset, ptr);
235 }
236
237 /*! \brief Polyfill for sycl::vec::store buggy in hipSYCL
238  *
239  * Loads from the address \c ptr offset in elements of type T by
240  * NumElements * offset, into the components of \c v.
241  *
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,
246                                 size_t offset,
247                                 cl::sycl::multi_ptr<T, AddressSpace> ptr)
248 {
249     v.store(offset, ptr);
250 }
251
252 #endif
253
254 } // namespace sycl_2020
255
256 #endif /* GMX_GPU_UTILS_SYCL_KERNEL_UTILS_H */