45831f715b4d792d2ab9955c8ec5b604e7944f66
[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 Access mode to use for atomic accessors.
48  *
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.
52  *
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.
55  *
56  * The \ref atomicFetchAdd function could be used for doing operations on such accessors.
57  */
58 static constexpr auto mode_atomic = GMX_SYCL_DPCPP ? cl::sycl::access::mode::read_write :
59                                                    /* GMX_SYCL_HIPSYCL */ cl::sycl::access::mode::atomic;
60
61 /*! \brief Convenience wrapper to do atomic addition to a global buffer.
62  *
63  * Skips sub-normal values.
64  *
65  * The implementation differences between DPCPP and hipSYCL are explained in \ref mode_atomic.
66  */
67 template<class IndexType>
68 static inline void atomicFetchAdd(DeviceAccessor<float, mode_atomic> acc, const IndexType idx, const float val)
69 {
70 #if GMX_SYCL_DPCPP
71     sycl_2020::atomic_ref<float, sycl_2020::memory_order::relaxed, sycl_2020::memory_scope::device, cl::sycl::access::address_space::global_space>
72             fout_atomic(acc[idx]);
73     fout_atomic.fetch_add(val);
74 #elif GMX_SYCL_HIPSYCL
75 #    ifdef SYCL_DEVICE_ONLY
76     /* While there is support for float atomics on device, the host implementation uses
77      * Clang's __atomic_fetch_add intrinsic, that, at least in Clang 11, does not support
78      * floats. Luckily, we don't want to run on host. */
79     acc[idx].fetch_add(val);
80 #    else
81     GMX_ASSERT(false, "hipSYCL host codepath not supported");
82     GMX_UNUSED_VALUE(val);
83     GMX_UNUSED_VALUE(acc);
84     GMX_UNUSED_VALUE(idx);
85 #    endif
86 #endif
87 }
88
89 namespace sycl_2020
90 {
91 #if GMX_SYCL_HIPSYCL
92 __device__ static inline float shift_left(sycl_2020::sub_group, float var, sycl_2020::sub_group::linear_id_type delta)
93 {
94     // No sycl::sub_group::shift_left / shuffle_down in hipSYCL yet
95 #    ifdef SYCL_DEVICE_ONLY
96 #        if defined(HIPSYCL_PLATFORM_CUDA) && defined(__HIPSYCL_ENABLE_CUDA_TARGET__)
97     static const unsigned int sc_cudaFullWarpMask = 0xffffffff;
98     return __shfl_down_sync(sc_cudaFullWarpMask, var, delta);
99 #        elif defined(HIPSYCL_PLATFORM_ROCM) && defined(__HIPSYCL_ENABLE_HIP_TARGET__)
100     // Do we need more ifdefs? https://github.com/ROCm-Developer-Tools/HIP/issues/1491
101     return __shfl_down(var, delta);
102 #        else
103 #            error "Unsupported hipSYCL target"
104 #        endif
105 #    else
106     // Should never be called
107     GMX_UNUSED_VALUE(var);
108     GMX_UNUSED_VALUE(delta);
109     assert(false);
110     return NAN;
111 #    endif
112 }
113 __host__ static inline float shift_left(sycl_2020::sub_group, float, sycl_2020::sub_group::linear_id_type)
114 {
115     // Should never be called
116     assert(false);
117     return NAN;
118 }
119 #elif GMX_SYCL_DPCPP
120 static inline float shift_left(sycl_2020::sub_group sg, float var, sycl_2020::sub_group::linear_id_type delta)
121 {
122     return sg.shuffle_down(var, delta);
123 }
124 #endif
125
126 #if GMX_SYCL_HIPSYCL
127 __device__ static inline float shift_right(sycl_2020::sub_group,
128                                            float                                var,
129                                            sycl_2020::sub_group::linear_id_type delta)
130 {
131     // No sycl::sub_group::shift_right / shuffle_up in hipSYCL yet
132 #    ifdef SYCL_DEVICE_ONLY
133 #        if defined(HIPSYCL_PLATFORM_CUDA) && defined(__HIPSYCL_ENABLE_CUDA_TARGET__)
134     static const unsigned int sc_cudaFullWarpMask = 0xffffffff;
135     return __shfl_up_sync(sc_cudaFullWarpMask, var, delta);
136 #        elif defined(HIPSYCL_PLATFORM_ROCM) && defined(__HIPSYCL_ENABLE_HIP_TARGET__)
137     // Do we need more ifdefs? https://github.com/ROCm-Developer-Tools/HIP/issues/1491
138     return __shfl_up(var, delta);
139 #        else
140 #            error "Unsupported hipSYCL target"
141 #        endif
142 #    else
143     // Should never be called
144     assert(false);
145     GMX_UNUSED_VALUE(var);
146     GMX_UNUSED_VALUE(delta);
147     return NAN;
148 #    endif
149 }
150 __host__ static inline float shift_right(sycl_2020::sub_group, float, sycl_2020::sub_group::linear_id_type)
151 {
152     // Should never be called
153     assert(false);
154     return NAN;
155 }
156 #elif GMX_SYCL_DPCPP
157 static inline float shift_right(sycl_2020::sub_group sg, float var, sycl_2020::sub_group::linear_id_type delta)
158 {
159     return sg.shuffle_up(var, delta);
160 }
161 #endif
162 } // namespace sycl_2020
163
164 #endif /* GMX_GPU_UTILS_SYCL_KERNEL_UTILS_H */