Add hipSYCL sanity check
authorAndrey Alekseenko <al42and@gmail.com>
Wed, 11 Aug 2021 10:12:21 +0000 (10:12 +0000)
committerAndrey Alekseenko <al42and@gmail.com>
Wed, 11 Aug 2021 10:12:21 +0000 (10:12 +0000)
cmake/HipSyclTest/CMakeLists.txt [new file with mode: 0644]
cmake/HipSyclTest/main.cpp [new file with mode: 0644]
cmake/gmxManageSYCL.cmake

diff --git a/cmake/HipSyclTest/CMakeLists.txt b/cmake/HipSyclTest/CMakeLists.txt
new file mode 100644 (file)
index 0000000..d7c444a
--- /dev/null
@@ -0,0 +1,52 @@
+#
+# This file is part of the GROMACS molecular simulation package.
+#
+# Copyright (c) 2021, by the GROMACS development team, led by
+# Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
+# and including many others, as listed in the AUTHORS file in the
+# top-level source directory and at http://www.gromacs.org.
+#
+# GROMACS is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public License
+# as published by the Free Software Foundation; either version 2.1
+# of the License, or (at your option) any later version.
+#
+# GROMACS is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with GROMACS; if not, see
+# http://www.gnu.org/licenses, or write to the Free Software Foundation,
+# Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
+#
+# If you want to redistribute modifications to GROMACS, please
+# consider that scientific software is very special. Version
+# control is crucial - bugs must be traceable. We will be happy to
+# consider code for inclusion in the official distribution, but
+# derived work must not be called official GROMACS. Details are found
+# in the README & COPYING files - if they are missing, get the
+# official version at http://www.gromacs.org.
+#
+# To help us fund GROMACS development, we humbly ask that you cite
+# the research papers on the package. Check out http://www.gromacs.org.
+
+project(HipSyclTest)
+cmake_minimum_required(VERSION 3.0)
+find_package(hipsycl REQUIRED)
+
+# By default, this project will fail compilation if it can't use hipSYCL compiler.
+# By defining CHECK_x_TARGET we can also make it fail if the specified backend is not among the compilation targets.
+
+if (CHECK_CUDA_TARGET)
+    add_compile_definitions(CHECK_CUDA_TARGET)
+endif()
+if (CHECK_HIP_TARGET)
+    add_compile_definitions(CHECK_HIP_TARGET)
+endif()
+if (CHECK_LEVELZERO_TARGET)
+    add_compile_definitions(CHECK_LEVELZERO_TARGET)
+endif()
+add_executable(HipSyclTest main.cpp)
+add_sycl_to_target(TARGET HipSyclTest SOURCES main.cpp)
diff --git a/cmake/HipSyclTest/main.cpp b/cmake/HipSyclTest/main.cpp
new file mode 100644 (file)
index 0000000..0ff32ab
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * This file is part of the GROMACS molecular simulation package.
+ *
+ * Copyright (c) 2021, by the GROMACS development team, led by
+ * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
+ * and including many others, as listed in the AUTHORS file in the
+ * top-level source directory and at http://www.gromacs.org.
+ *
+ * GROMACS is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1
+ * of the License, or (at your option) any later version.
+ *
+ * GROMACS is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GROMACS; if not, see
+ * http://www.gnu.org/licenses, or write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
+ *
+ * If you want to redistribute modifications to GROMACS, please
+ * consider that scientific software is very special. Version
+ * control is crucial - bugs must be traceable. We will be happy to
+ * consider code for inclusion in the official distribution, but
+ * derived work must not be called official GROMACS. Details are found
+ * in the README & COPYING files - if they are missing, get the
+ * official version at http://www.gromacs.org.
+ *
+ * To help us fund GROMACS development, we humbly ask that you cite
+ * the research papers on the package. Check out http://www.gromacs.org.
+ */
+
+// Always fail if we are not compiling with hipSYCL
+#if !defined(__HIPSYCL__)
+#    error "__HIPSYCL__ macro not defined. Please check your hipSYCL installation."
+#endif
+
+/* Next, we optionally check three backends.
+ * If CHECK_CUDA_TARGET is defined:
+ *  - If we are not compiling for CUDA (because we did not specify CUDA devices among targets),
+ *  this test compilation will fail.
+ *  - If we are compiling for CUDA, the compilation will proceed.
+ * Same for HIP and LevelZero.
+ *
+ * This allows us to compile this test file with different -DCHECK_x_TARGET flags to see which
+ * backends we are compiling for and report it to CMake.
+ * */
+
+#if defined(CHECK_CUDA_TARGET) && !defined(__HIPSYCL_ENABLE_CUDA_TARGET__)
+#    error "CUDA target not enabled";
+#endif
+
+#if defined(CHECK_HIP_TARGET) && !defined(__HIPSYCL_ENABLE_HIP_TARGET__)
+#    error "HIP target not enabled";
+#endif
+
+#if defined(CHECK_LEVELZERO_TARGET) && !defined(__HIPSYCL_ENABLE_SPIRV_TARGET__)
+#    error "LevelZero (SPIR-V) target not enabled"
+#endif
+
+int main() {}
index cf5117453655637dc57b61e6abb156950ef57b7a..0263c6615bd93150ccccf14f9b9bbdb99f13b920 100644 (file)
@@ -50,16 +50,121 @@ endif()
 
 include(gmxFindFlagsForSource)
 
+# Return all current CMake variables with name starting with "hipsycl" (case-insensitive).
+# Result is in the form of a list of flags ("-Dfoo=bar;-Dbaz=true").
+# Semicolons in values are escaped (needed for HIPSYCL_TARGETS).
+function(_getHipSyclCmakeFlags RETURN_VAR)
+    get_cmake_property(_VARS VARIABLES)
+    list (SORT _VARS)
+    set(RESULT "")
+    foreach (_VARNAME ${_VARS})
+            string(TOLOWER "${_VARNAME}" _VARNAME_LOWER)
+            if (${_VARNAME_LOWER} MATCHES "^hipsycl")
+                # Escape semicolon. The number of backslashes was determined empirically.
+                string(REPLACE ";" "\\\\\\;" _VARVALUE "${${_VARNAME}}")
+                list(APPEND
+                  RESULT
+                  -D${_VARNAME}=${_VARVALUE}
+                )
+            endif()
+    endforeach()
+    set("${RETURN_VAR}" ${RESULT} PARENT_SCOPE)
+endfunction()
+
 if(GMX_SYCL_HIPSYCL)
-    if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
-        message(FATAL_ERROR "hipSYCL can only be built with Clang++ compiler")
-    endif()
     set(HIPSYCL_CLANG "${CMAKE_CXX_COMPILER}")
     # -Wno-unknown-cuda-version because Clang-11 complains about CUDA 11.0-11.2, despite working fine with them.
     # -Wno-unknown-attributes because hipSYCL does not support reqd_sub_group_size (because it can only do some sub group sizes).
-    # --hipsycl-explicit-multipass is needed when building for both CUDA and HIP.
-    set(HIPSYCL_SYCLCC_EXTRA_ARGS "-Wno-unknown-cuda-version -Wno-unknown-attributes --hipsycl-explicit-multipass")
+    set(HIPSYCL_SYCLCC_EXTRA_ARGS "-Wno-unknown-cuda-version -Wno-unknown-attributes")
+
+    # Must be called before find_package to capture all user-set CMake variables, but not those set automatically
+    _getHipSyclCmakeFlags(_ALL_HIPSYCL_CMAKE_FLAGS)
+
     find_package(hipsycl REQUIRED)
+    # Ensure the try_compile projects below find the same hipsycl)
+    list(APPEND _ALL_HIPSYCL_CMAKE_FLAGS -Dhipsycl_DIR=${hipsycl_DIR})
+
+    # If the user-set CMake variables change (e.g. because the user
+    # changed HIPSYCL_TARGETS), then the try_compile tests below need
+    # to be re-run. Set and use an internal cache variable to detect
+    # the change and set a flag to rerun the tests.
+    if (DEFINED GMX_ALL_HIPSYCL_CMAKE_FLAGS_COPY AND "${GMX_ALL_HIPSYCL_CMAKE_FLAGS_COPY}" STREQUAL "${_ALL_HIPSYCL_CMAKE_FLAGS}")
+        set(_rerun_hipsycl_try_compile_tests FALSE)
+    else()
+        # The new value should over-write the previous copy
+        set(GMX_ALL_HIPSYCL_CMAKE_FLAGS_COPY ${_ALL_HIPSYCL_CMAKE_FLAGS} CACHE INTERNAL "Store the list of CMake variables needed for hipSYCL compilation test projects")
+        set(_rerun_hipsycl_try_compile_tests TRUE)
+    endif()
+
+    # Does the hipSYCL compiler work at all for the given targets?
+    if (NOT DEFINED GMX_HIPSYCL_COMPILATION_WORKS OR _rerun_hipsycl_try_compile_tests)
+        message(STATUS "Checking for valid hipSYCL compiler")
+        try_compile(GMX_HIPSYCL_COMPILATION_WORKS "${CMAKE_BINARY_DIR}/CMakeTmpHipSyclTest" "${CMAKE_SOURCE_DIR}/cmake/HipSyclTest/" "HipSyclTest"
+          OUTPUT_VARIABLE _HIPSYCL_COMPILATION_OUTPUT
+          CMAKE_FLAGS
+            ${_ALL_HIPSYCL_CMAKE_FLAGS})
+        file(REMOVE_RECURSE "${CMAKE_BINARY_DIR}/CMakeTmpHipSyclTest")
+        if(GMX_HIPSYCL_COMPILATION_WORKS)
+            message(STATUS "Checking for valid hipSYCL compiler - Success")
+        endif()
+    endif()
+    if (NOT GMX_HIPSYCL_COMPILATION_WORKS)
+        message(FATAL_ERROR "hipSYCL compiler not working:\n${_HIPSYCL_COMPILATION_OUTPUT}")
+    endif()
+
+    # Does hipSYCL compilation target CUDA devices?
+    if(NOT DEFINED GMX_HIPSYCL_HAVE_CUDA_TARGET OR _rerun_hipsycl_try_compile_tests)
+        message(STATUS "Checking for hipSYCL CUDA target")
+        try_compile(GMX_HIPSYCL_HAVE_CUDA_TARGET "${CMAKE_BINARY_DIR}/CMakeTmpHipSyclTest" "${CMAKE_SOURCE_DIR}/cmake/HipSyclTest/" "HipSyclTest"
+          CMAKE_FLAGS
+            -DCHECK_CUDA_TARGET=ON
+            ${_ALL_HIPSYCL_CMAKE_FLAGS})
+        file(REMOVE_RECURSE "${CMAKE_BINARY_DIR}/CMakeTmpHipSyclTest")
+        if(GMX_HIPSYCL_HAVE_CUDA_TARGET)
+            message(STATUS "Checking for hipSYCL CUDA target - Success")
+        else()
+            message(STATUS "Checking for hipSYCL CUDA target - Failed")
+        endif()
+    endif()
+
+    # Does hipSYCL compilation target HIP devices?
+    if(NOT DEFINED GMX_HIPSYCL_HAVE_HIP_TARGET OR _rerun_hipsycl_try_compile_tests)
+        message(STATUS "Checking for hipSYCL HIP target")
+        try_compile(GMX_HIPSYCL_HAVE_HIP_TARGET "${CMAKE_BINARY_DIR}/CMakeTmpHipSyclTest" "${CMAKE_SOURCE_DIR}/cmake/HipSyclTest/" "HipSyclTest"
+          CMAKE_FLAGS
+            -DCHECK_HIP_TARGET=ON
+            ${_ALL_HIPSYCL_CMAKE_FLAGS})
+        file(REMOVE_RECURSE "${CMAKE_BINARY_DIR}/CMakeTmpHipSyclTest")
+        if(GMX_HIPSYCL_HAVE_HIP_TARGET)
+            message(STATUS "Checking for hipSYCL HIP target - Success")
+        else()
+            message(STATUS "Checking for hipSYCL HIP target - Failed")
+        endif()
+    endif()
+
+    # Does hipSYCL compilation target Intel Level0 devices?
+    if(NOT DEFINED GMX_HIPSYCL_HAVE_LEVELZERO_TARGET OR _rerun_hipsycl_try_compile_tests)
+        message(STATUS "Checking for hipSYCL LevelZero target")
+        try_compile(GMX_HIPSYCL_HAVE_LEVELZERO_TARGET "${CMAKE_BINARY_DIR}/CMakeTmpHipSyclTest" "${CMAKE_SOURCE_DIR}/cmake/HipSyclTest/" "HipSyclTest"
+          CMAKE_FLAGS
+            -DCHECK_LEVELZERO_TARGET=ON
+            ${_ALL_HIPSYCL_CMAKE_FLAGS})
+        file(REMOVE_RECURSE "${CMAKE_BINARY_DIR}/CMakeTmpHipSyclTest")
+        if(GMX_HIPSYCL_HAVE_LEVELZERO_TARGET)
+            message(STATUS "Checking for hipSYCL LevelZero target - Success")
+            message(WARNING "GROMACS does not support LevelZero backend of hipSYCL")
+        else()
+            message(STATUS "Checking for hipSYCL LevelZero target - Failed")
+        endif()
+    endif()
+
+    if(NOT GMX_HIPSYCL_HAVE_CUDA_TARGET AND NOT GMX_HIPSYCL_HAVE_HIP_TARGET)
+        message(WARNING "hipSYCL has no GPU targets set! Please, specify target hardware with -DHIPSYCL_TARGETS CMake option")
+    endif()
+    if(GMX_HIPSYCL_HAVE_CUDA_TARGET AND GMX_HIPSYCL_HAVE_HIP_TARGET)
+        message(FATAL_ERROR "hipSYCL cannot have both CUDA and HIP targets active! This would require explicit multipass mode which both decreases performance on NVIDIA devices and has been removed in clang 12. Compile only for either CUDA or HIP targets.")
+    endif()
+    unset(_rerun_hipsycl_try_compile_tests)
 else()
     if(CMAKE_CXX_COMPILER MATCHES "dpcpp")
         # At least Intel dpcpp defaults to having SYCL enabled for all code. This leads to two problems: