2b07b927a7649288778546911ef69f5fb26ca5f3
[alexxy/gromacs.git] / src / gromacs / gpu_utils / gmxsycl.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2020,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 /*! \libinternal \file
36  * \brief
37  * Wraps the complexity of including SYCL in GROMACS.
38  *
39  * The __SYCL_COMPILER_VERSION macro is used to identify Intel DPCPP compiler.
40  * See https://github.com/intel/llvm/pull/2998 for better proposal.
41  *
42  * Intel SYCL headers use symbol DIM as a template parameter, which gets broken by macro DIM defined
43  * in gromacs/math/vectypes.h. Here, we include the SYCL header while temporary undefining this macro.
44  * See https://github.com/intel/llvm/issues/2981.
45  *
46  * Different compilers, at the time of writing, have different names for some of the proposed features
47  * of the SYCL2020 standard. For uniformity, they are all aliased in our custom sycl_2020 namespace.
48  *
49  * \inlibraryapi
50  */
51
52 #ifndef GMX_GPU_UTILS_GMXSYCL_H
53 #define GMX_GPU_UTILS_GMXSYCL_H
54
55 #include "config.h"
56
57 // For hipSYCL, we need to activate floating-point atomics
58 #if GMX_SYCL_HIPSYCL
59 #    define HIPSYCL_EXT_FP_ATOMICS
60 #    pragma clang diagnostic push
61 #    pragma clang diagnostic ignored "-Wunused-variable"
62 #    pragma clang diagnostic ignored "-Wunused-parameter"
63 #    pragma clang diagnostic ignored "-Wmissing-noreturn"
64 #    pragma clang diagnostic ignored "-Wshadow-field"
65 #    pragma clang diagnostic ignored "-Wctad-maybe-unsupported"
66 #    pragma clang diagnostic ignored "-Wdeprecated-copy-dtor"
67 #    pragma clang diagnostic ignored "-Winconsistent-missing-destructor-override"
68 #    pragma clang diagnostic ignored "-Wunused-template"
69 #    pragma clang diagnostic ignored "-Wsign-compare"
70 #    pragma clang diagnostic ignored "-Wundefined-reinterpret-cast"
71 #    pragma clang diagnostic ignored "-Wdeprecated-copy"
72 #    pragma clang diagnostic ignored "-Wnewline-eof"
73 #    pragma clang diagnostic ignored "-Wextra-semi"
74 #    pragma clang diagnostic ignored "-Wsuggest-override"
75 #    pragma clang diagnostic ignored "-Wsuggest-destructor-override"
76 #    pragma clang diagnostic ignored "-Wgcc-compat"
77 #endif
78
79
80 #ifdef DIM
81 #    if DIM != 3
82 #        error "The workaround here assumes we use DIM=3."
83 #    else
84 #        undef DIM
85 #        include <CL/sycl.hpp>
86 #        define DIM 3
87 #    endif
88 #else
89 #    include <CL/sycl.hpp>
90 #endif
91
92 #if GMX_SYCL_HIPSYCL
93 #    pragma clang diagnostic pop
94 #endif
95
96 /* Exposing Intel-specific extensions in a manner compatible with SYCL2020 provisional spec.
97  * Despite ICPX (up to 2021.3.0 at the least) having SYCL_LANGUAGE_VERSION=202001,
98  * some parts of the spec are still in custom sycl::ONEAPI namespace (sycl::ext::oneapi in beta versions),
99  * and some functions have different names. To make things easier to upgrade
100  * in the future, this thin layer is added.
101  * */
102 namespace sycl_2020
103 {
104 namespace detail
105 {
106 #if GMX_SYCL_DPCPP
107 // Confirmed to work for 2021.1-beta10 (20201005) to 2021.3.0 (20210619).
108 // Deprecated in favor of sycl::ext::oneapi on 20210717 in https://github.com/intel/llvm/commit/d703f578.
109 namespace origin = cl::sycl::ONEAPI;
110 #elif GMX_SYCL_HIPSYCL
111 namespace origin = cl::sycl;
112 #else
113 #    error "Unsupported version of SYCL compiler"
114 #endif
115 } // namespace detail
116
117 using detail::origin::memory_order;
118 using detail::origin::memory_scope;
119 using detail::origin::plus;
120 using detail::origin::sub_group;
121
122 #if GMX_SYCL_DPCPP
123 using detail::origin::atomic_ref;
124 template<typename... Args>
125 bool group_any_of(Args&&... args)
126 {
127     return detail::origin::any_of(std::forward<Args>(args)...);
128 }
129 template<typename... Args>
130 auto group_reduce(Args&&... args) -> decltype(detail::origin::reduce(std::forward<Args>(args)...))
131 {
132     return detail::origin::reduce(std::forward<Args>(args)...);
133 }
134 #elif GMX_SYCL_HIPSYCL
135 using detail::origin::atomic_ref;
136 using detail::origin::group_any_of;
137 using detail::origin::group_reduce;
138 #else
139 #    error "Unsupported SYCL compiler"
140 #endif
141
142 } // namespace sycl_2020
143
144 #endif