SYCL: Fully switch to atomic_ref
[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 static inline void subGroupBarrier(const cl::sycl::nd_item<1> itemIdx)
77 {
78 #if GMX_SYCL_HIPSYCL
79     cl::sycl::group_barrier(itemIdx.get_sub_group(), cl::sycl::memory_scope::sub_group);
80 #else
81     itemIdx.get_sub_group().barrier();
82 #endif
83 }
84
85 namespace sycl_2020
86 {
87 #if GMX_SYCL_HIPSYCL
88 __device__ __host__ static inline float shift_left(sycl_2020::sub_group,
89                                                    float                                var,
90                                                    sycl_2020::sub_group::linear_id_type delta)
91 {
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);
99 #        else
100 #            error "Unsupported hipSYCL target"
101 #        endif
102 #    else
103     // Should never be called
104     GMX_UNUSED_VALUE(var);
105     GMX_UNUSED_VALUE(delta);
106     assert(false);
107     return NAN;
108 #    endif
109 }
110 #elif GMX_SYCL_DPCPP
111 static inline float shift_left(sycl_2020::sub_group sg, float var, sycl_2020::sub_group::linear_id_type delta)
112 {
113     return sg.shuffle_down(var, delta);
114 }
115 #endif
116
117 #if GMX_SYCL_HIPSYCL
118 __device__ __host__ static inline float shift_right(sycl_2020::sub_group,
119                                                     float                                var,
120                                                     sycl_2020::sub_group::linear_id_type delta)
121 {
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);
129 #        else
130 #            error "Unsupported hipSYCL target"
131 #        endif
132 #    else
133     // Should never be called
134     assert(false);
135     GMX_UNUSED_VALUE(var);
136     GMX_UNUSED_VALUE(delta);
137     return NAN;
138 #    endif
139 }
140 #elif GMX_SYCL_DPCPP
141 static inline float shift_right(sycl_2020::sub_group sg, float var, sycl_2020::sub_group::linear_id_type delta)
142 {
143     return sg.shuffle_up(var, delta);
144 }
145 #endif
146 } // namespace sycl_2020
147
148 #endif /* GMX_GPU_UTILS_SYCL_KERNEL_UTILS_H */