From 55bbfb0909d752b15c2ae5495118001555c617f6 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Thu, 26 Oct 2023 15:36:24 +0200 Subject: [PATCH] Use IntelSYCL cmake script --- CMakeLists.txt | 10 +- dpnp/CMakeLists.txt | 34 +- .../cmake/Modules/IntelSYCLConfig.cmake | 360 ++++++++++++++++++ dpnp/backend/extensions/lapack/CMakeLists.txt | 11 +- .../extensions/sycl_ext/CMakeLists.txt | 7 +- dpnp/backend/extensions/vm/CMakeLists.txt | 7 +- 6 files changed, 401 insertions(+), 28 deletions(-) create mode 100644 dpnp/backend/cmake/Modules/IntelSYCLConfig.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 02b15ba98c3..46e988bdcbe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,8 +1,4 @@ -cmake_minimum_required(VERSION 3.21...3.26 FATAL_ERROR) - -if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.24") - cmake_policy(SET CMP0135 NEW) -endif() +cmake_minimum_required(VERSION 3.21...3.27 FATAL_ERROR) project(dpnp DESCRIPTION "NumPy-like API accelerated by SYCL." @@ -27,7 +23,7 @@ endif() set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${DPCTL_MODULE_PATH}) -find_package(IntelDPCPP REQUIRED) +find_package(IntelSYCL REQUIRED PATHS ${CMAKE_SOURCE_DIR}/dpnp/backend/cmake/Modules NO_DEFAULT_PATH) find_package(TBB QUIET) if(TBB_FOUND) find_package(TBB REQUIRED) @@ -77,7 +73,7 @@ FetchContent_Declare( ) FetchContent_MakeAvailable(pybind11) -find_package(PythonExtensions REQUIRED) +find_package(Python REQUIRED COMPONENTS Development.Module) find_package(NumPy REQUIRED) set(CYTHON_FLAGS "-t -w \"${CMAKE_SOURCE_DIR}\"") diff --git a/dpnp/CMakeLists.txt b/dpnp/CMakeLists.txt index df4971cfa38..ebaf1d7b0ef 100644 --- a/dpnp/CMakeLists.txt +++ b/dpnp/CMakeLists.txt @@ -1,37 +1,45 @@ function(build_dpnp_cython_ext _trgt _src _dest) + set(options SYCL) + cmake_parse_arguments(BUILD_DPNP_EXT "${options}" "" "" ${ARGN}) add_cython_target(${_trgt} ${_src} CXX OUTPUT_VAR _generated_src) message(STATUS "Using ${_trgt}") - add_library(${_trgt} MODULE ${_generated_src}) + + Python_add_library(${_trgt} MODULE WITH_SOABI ${_generated_src}) set(_trgt_deps "${_trgt}_deps") add_custom_target(${_trgt_deps} DEPENDS ${_src}) add_dependencies(${_trgt} ${_trgt_deps}) + + if (BUILD_DPNP_EXT_SYCL) + add_sycl_to_target(TARGET ${_trgt} SOURCES ${_generated_src}) + endif() + if (DPNP_GENERATE_COVERAGE) target_compile_definitions(${_trgt} PRIVATE CYTHON_TRACE=1 CYTHON_TRACE_NOGIL=1) + target_compile_options(${_trgt} PRIVATE "-fno-sycl-use-footer") endif() - target_compile_definitions(${_trgt} PRIVATE NPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION) + # NumPy + target_compile_definitions(${_trgt} PRIVATE NPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION) target_include_directories(${_trgt} PRIVATE ${NumPy_INCLUDE_DIR}) + # Dpctl target_include_directories(${_trgt} PRIVATE ${Dpctl_INCLUDE_DIR}) target_link_directories(${_trgt} PRIVATE ${Dpctl_INCLUDE_DIR}/..) - target_link_libraries(${_trgt} DPCTLSyclInterface) + target_link_libraries(${_trgt} PRIVATE DPCTLSyclInterface) set(_linker_options "LINKER:${DPNP_LDFLAGS}") target_link_options(${_trgt} PRIVATE ${_linker_options}) - python_extension_module(${_trgt}) if (DPNP_GENERATE_COVERAGE) set(_copy_cxx_trgt "${_trgt}_copy_cxx") add_custom_target( - ${_copy_cxx_trgt} ALL - COMMAND ${CMAKE_COMMAND} - -DSOURCE_FILE=${_generated_src} - -DDEST=${CMAKE_CURRENT_SOURCE_DIR} - -P ${CMAKE_SOURCE_DIR}/dpnp/cmake/copy_existing.cmake - DEPENDS ${_trgt} - VERBATIM - COMMENT "Copying Cython-generated source for target ${_trgt} to dpnp source layout" + ${_copy_cxx_trgt} ALL COMMAND ${CMAKE_COMMAND} + -DSOURCE_FILE=${_generated_src} + -DDEST=${CMAKE_CURRENT_SOURCE_DIR} + -P ${CMAKE_SOURCE_DIR}/dpnp/cmake/copy_existing.cmake + DEPENDS ${_trgt} + VERBATIM COMMENT "Copying Cython-generated source for target ${_trgt} to dpnp source layout" ) endif() install(TARGETS ${_trgt} LIBRARY DESTINATION ${_dest}) @@ -39,7 +47,7 @@ endfunction() function(build_dpnp_cython_ext_with_backend _trgt _src _dest) build_dpnp_cython_ext(${_trgt} ${_src} ${_dest}) - target_link_libraries(${_trgt} dpnp_backend_library) + target_link_libraries(${_trgt} PRIVATE dpnp_backend_library) if (UNIX) set_target_properties(${_trgt} PROPERTIES INSTALL_RPATH "$ORIGIN/..") endif() diff --git a/dpnp/backend/cmake/Modules/IntelSYCLConfig.cmake b/dpnp/backend/cmake/Modules/IntelSYCLConfig.cmake new file mode 100644 index 00000000000..45e038b324d --- /dev/null +++ b/dpnp/backend/cmake/Modules/IntelSYCLConfig.cmake @@ -0,0 +1,360 @@ +# +# Modifications, Copyright (C) 2023 Intel Corporation +# +# This software and the related documents are Intel copyrighted materials, and +# your use of them is governed by the express license under which they were +# provided to you ("License"). Unless the License provides otherwise, you may not +# use, modify, copy, publish, distribute, disclose or transmit this software or +# the related documents without Intel's prior written permission. +# +# This software and the related documents are provided as is, with no express +# or implied warranties, other than those that are expressly stated in the +# License. +# +# Distributed under the OSI-approved BSD 3-Clause License. See accompanying +# file Copyright.txt or https://cmake.org/licensing for details. + +#[=======================================================================[.rst: +IntelSYCLConfig +------- + +Library to verify SYCL compatability of CMAKE_CXX_COMPILER +and passes relevant compiler flags. + +Result Variables +^^^^^^^^^^^^^^^^ + +This will define the following variables: + +``IntelSYCL_FOUND`` + True if the system has the SYCL library. +``SYCL_LANGUAGE_VERSION`` + The SYCL language spec version by Compiler. +``SYCL_INCLUDE_DIR`` + Include directories needed to use SYCL. +``SYCL_IMPLEMENTATION_ID`` + The SYCL compiler variant. +``SYCL_FLAGS`` + SYCL specific flags for the compiler. + +``IntelSYCL::SYCL_CXX`` + Target for using Intel SYCL (DPC++). The following properties are defined + for the target: ``INTERFACE_COMPILE_OPTIONS``, ``INTERFACE_LINK_OPTIONS``, + ``INTERFACE_INCLUDE_DIRECTORIES``, and ``INTERFACE_LINK_DIRECTORIES`` + +Cache Variables +^^^^^^^^^^^^^^^ + +The following cache variable may also be set: + +``SYCL_LANGUAGE_VERSION`` + The SYCL language spec version by Compiler. + + +.. Note:: + + 1. User needs to set -DCMAKE_CXX_COMPILER or environment of + CXX pointing to SYCL compatible compiler ( eg: icx, clang++, icpx) + + + 2. Add this package to user's Cmake config file. + + .. code-block:: cmake + + find_package(IntelSYCL REQUIRED) + + 3. Add sources to target through add_sycl_to_target() + + .. code-block:: cmake + + # Compile specific sources for SYCL and build target for SYCL + add_executable(target_proj A.cpp B.cpp offload1.cpp offload2.cpp) + add_sycl_to_target(TARGET target_proj SOURCES offload1.cpp offload2.cpp) + +#]=======================================================================] + +include(${CMAKE_ROOT}/Modules/FindPackageHandleStandardArgs.cmake) + +find_package(PkgConfig QUIET) +if(PKG_CONFIG_FOUND) + # TODO add dependency package module checks, if any +endif() + + +# TODO: can't use find_program to override the CMAKE_CXX_COMPILER as +# Platform/ files are executed, potentially for a different compiler. +# Safer approach is to make user to define CMAKE_CXX_COMPILER. + +string(COMPARE EQUAL "${CMAKE_CXX_COMPILER}" "" nocmplr) +if(nocmplr) + set(IntelSYCL_FOUND False) + set(SYCL_REASON_FAILURE "SYCL: CMAKE_CXX_COMPILER not set!!") + set(IntelSYCL_NOT_FOUND_MESSAGE "${SYCL_REASON_FAILURE}") +endif() + +# Check if a Compiler ID is being set. project() should be set prior to find_package() + +if("x${CMAKE_CXX_COMPILER_ID}" STREQUAL "x") + set(IntelSYCL_FOUND False) + set(SYCL_REASON_FAILURE "CMake CXX Compiler family is not set. Please make sure find_package(IntelSYCL) is called after project()!!") + set(IntelSYCL_NOT_FOUND_MESSAGE "${SYCL_REASON_FAILURE}") + return() +endif() + +# Check for known compiler family that supports SYCL + +if( NOT "x${CMAKE_CXX_COMPILER_ID}" STREQUAL "xClang" AND + NOT "x${CMAKE_CXX_COMPILER_ID}" STREQUAL "xIntelLLVM") + set(IntelSYCL_FOUND False) + set(SYCL_REASON_FAILURE "Unsupported compiler family ${CMAKE_CXX_COMPILER_ID} and compiler ${CMAKE_CXX_COMPILER}!!") + set(IntelSYCL_NOT_FOUND_MESSAGE "${SYCL_REASON_FAILURE}") + return() +endif() + +# Assume that CXX Compiler supports SYCL and then test to verify. +set(SYCL_COMPILER ${CMAKE_CXX_COMPILER}) + +# Function to write a test case to verify SYCL features. + +function(SYCL_FEATURE_TEST_WRITE src) + + set(pp_if "#if") + set(pp_endif "#endif") + + set(SYCL_TEST_CONTENT "") + string(APPEND SYCL_TEST_CONTENT "#include \nusing namespace std;\n") + string(APPEND SYCL_TEST_CONTENT "int main(){\n") + + # Feature tests goes here + + string(APPEND SYCL_TEST_CONTENT "${pp_if} defined(SYCL_LANGUAGE_VERSION)\n") + string(APPEND SYCL_TEST_CONTENT "cout << \"SYCL_LANGUAGE_VERSION=\"<