Merge branch release-2021 into master
[alexxy/gromacs.git] / cmake / gmxManageSYCL.cmake
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 # OpenCL required version: 1.2 or newer
36 set(REQUIRED_SYCL_MIN_VERSION_MAJOR 1)
37 set(REQUIRED_SYCL_MIN_VERSION_MINOR 2)
38 set(REQUIRED_SYCL_MIN_VERSION ${REQUIRED_SYCL_MIN_VERSION_MAJOR}.${REQUIRED_SYCL_MIN_VERSION_MINOR})
39
40 set(GMX_GPU_SYCL ON)
41
42 # CMake issue tracking the efforts to make a universal upstream module:
43 # https://gitlab.kitware.com/cmake/cmake/-/issues/21711
44
45 option(GMX_SYCL_HIPSYCL "Use hipSYCL instead of Intel/Clang for SYCL compilation" OFF)
46
47 if(GMX_DOUBLE)
48     message(FATAL_ERROR "SYCL acceleration is not available in double precision")
49 endif()
50
51 include(gmxFindFlagsForSource)
52
53 if(GMX_SYCL_HIPSYCL)
54     if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
55         message(FATAL_ERROR "hipSYCL can only be built with Clang++ compiler")
56     endif()
57     set(HIPSYCL_CLANG "${CMAKE_CXX_COMPILER}")
58     # -Wno-unknown-cuda-version because Clang-11 complains about CUDA 11.0-11.2, despite working fine with them.
59     # -Wno-unknown-attributes because hipSYCL does not support reqd_sub_group_size (because it can only do some sub group sizes).
60     # --hipsycl-explicit-multipass is needed when building for both CUDA and HIP.
61     set(HIPSYCL_SYCLCC_EXTRA_ARGS "-Wno-unknown-cuda-version -Wno-unknown-attributes --hipsycl-explicit-multipass")
62     find_package(hipsycl REQUIRED)
63 else()
64     if(CMAKE_CXX_COMPILER MATCHES "dpcpp")
65         # At least Intel dpcpp defaults to having SYCL enabled for all code. This leads to two problems:
66         #
67         # 1. Compiles take ~3x longer, since every file has to be compiled for multiple targets.
68         # 2. We get a ton of warnings for the device-specific pass when the compiler sees our SIMD code.
69         #
70         # To avoid this, we attempt to find a flag to disable SYCL for non-SYCL files. Unfortunately,
71         # when using gmx_find_flag_for_source() that includes calling check_cxx_compiler_flag(),
72         # this in turn exposes a bug in dpcpp, where an object file compiles with -fno-sycl leads to
73         # a failed link stage (when the same flag is not used). Since none of this is critical, we handle
74         # it by merely checking if it works to compile a source fils with this flag, and choking if SYCL
75         # is still enabled.
76     
77         if(NOT CHECK_DISABLE_SYCL_CXX_FLAGS_QUIETLY)
78             message(STATUS "Checking for flags to disable SYCL")
79         endif()
80     
81         gmx_check_source_compiles_with_flags(
82             "int main() { return 0; }"
83             "-fno-sycl"
84             "CXX"
85             DISABLE_SYCL_CXX_FLAGS_RESULT)
86     
87         if(DISABLE_SYCL_CXX_FLAGS_RESULT)
88             set(DISABLE_SYCL_CXX_FLAGS "-fno-sycl")
89         endif()
90         if(NOT CHECK_DISABLE_SYCL_CXX_FLAGS_QUIETLY)
91             if(DISABLE_SYCL_CXX_FLAGS_RESULT)
92                 message(STATUS "Checking for flags to disable SYCL - -fno-sycl")
93             else()
94                 message(WARNING "Cannot find flags to disable SYCL for non-SYCL hardware-specific C++ code. Expect many warnings, but they are likely benign.")
95             endif()
96             set(CHECK_DISABLE_SYCL_CXX_FLAGS_QUIETLY 1 CACHE INTERNAL "Keep quiet on future calls to detect no-SYCL flags" FORCE)
97         endif()
98     endif()
99     
100     # Find the flags to enable (or re-enable) SYCL with Intel extensions. In case we turned it off above,
101     # it's important that we check the combination of both flags, to make sure the second one re-enables SYCL.
102     if(NOT CHECK_SYCL_CXX_FLAGS_QUIETLY)
103         message(STATUS "Checking for flags to enable SYCL")
104     endif()
105     gmx_find_flag_for_source(SYCL_CXX_FLAGS_RESULT
106         "#include <CL/sycl.hpp>
107          namespace sycl = cl::sycl;
108          int main(){
109              sycl::queue q(sycl::default_selector{});
110              return 0;
111          }
112          " "CXX" DISABLE_SYCL_CXX_FLAGS SYCL_CXX_FLAGS "-fsycl -fsycl-device-code-split=per_kernel")
113     
114     if(NOT CHECK_SYCL_CXX_FLAGS_QUIETLY)
115         if(SYCL_CXX_FLAGS_RESULT)
116             message(STATUS "Checking for flags to enable SYCL - ${SYCL_CXX_FLAGS}")
117         endif()
118         set(CHECK_SYCL_CXX_FLAGS_QUIETLY 1 CACHE INTERNAL "Keep quiet on future calls to detect SYCL flags" FORCE)
119     endif()
120     
121     if(NOT SYCL_CXX_FLAGS_RESULT)
122         message(FATAL_ERROR "Cannot compile with SYCL Intel compiler. Try a different compiler or disable SYCL.")
123     endif()
124
125     # Add function wrapper similar to the one used by ComputeCPP and hipSYCL
126     function(add_sycl_to_target)
127         cmake_parse_arguments(
128             PARSE_ARGV 0 # No positional arguments
129             ARGS # Prefix for the resulting variables
130             "" # No options
131             "TARGET" # One-value keyword
132             "SOURCES" # Multi-value keyword
133         )
134         set_source_files_properties(${ARGS_SOURCES} PROPERTIES COMPILE_FLAGS "${SYCL_CXX_FLAGS}")
135         target_link_libraries(${ARGS_TARGET} PRIVATE ${SYCL_CXX_FLAGS})
136     endfunction(add_sycl_to_target)
137 endif()