FindSYCLToolkit.cmake 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # This will define the following variables:
  2. # SYCL_FOUND : True if the system has the SYCL library.
  3. # SYCL_INCLUDE_DIR : Include directories needed to use SYCL.
  4. # SYCL_LIBRARY_DIR : The path to the SYCL library.
  5. # SYCL_LIBRARY : SYCL library fullname.
  6. # SYCL_COMPILER_VERSION : SYCL compiler version.
  7. include(FindPackageHandleStandardArgs)
  8. set(SYCL_ROOT "")
  9. if(DEFINED ENV{SYCL_ROOT})
  10. set(SYCL_ROOT $ENV{SYCL_ROOT})
  11. elseif(DEFINED ENV{CMPLR_ROOT})
  12. set(SYCL_ROOT $ENV{CMPLR_ROOT})
  13. else()
  14. # Use the default path to ensure proper linking with torch::xpurt when the user is working with libtorch.
  15. if(CMAKE_SYSTEM_NAME MATCHES "Linux")
  16. set(SYCL_ROOT "/opt/intel/oneapi/compiler/latest")
  17. elseif(CMAKE_SYSTEM_NAME MATCHES "Windows")
  18. set(SYCL_ROOT "C:/Program Files (x86)/Intel/oneAPI/compiler/latest")
  19. endif()
  20. if(NOT EXISTS ${SYCL_ROOT})
  21. set(SYCL_ROOT "")
  22. endif()
  23. endif()
  24. string(COMPARE EQUAL "${SYCL_ROOT}" "" nosyclfound)
  25. if(nosyclfound)
  26. set(SYCL_FOUND False)
  27. set(SYCL_REASON_FAILURE "SYCL library not set!!")
  28. set(SYCL_NOT_FOUND_MESSAGE "${SYCL_REASON_FAILURE}")
  29. return()
  30. endif()
  31. # Find SYCL compiler executable.
  32. find_program(
  33. SYCL_COMPILER
  34. NAMES icx
  35. PATHS "${SYCL_ROOT}"
  36. PATH_SUFFIXES bin bin64
  37. NO_DEFAULT_PATH
  38. )
  39. function(parse_sycl_compiler_version version_number)
  40. # Execute the SYCL compiler with the --version flag to match the version string.
  41. execute_process(COMMAND ${SYCL_COMPILER} --version OUTPUT_VARIABLE SYCL_VERSION_STRING)
  42. string(REGEX REPLACE "Intel\\(R\\) (.*) Compiler ([0-9]+\\.[0-9]+\\.[0-9]+) (.*)" "\\2"
  43. SYCL_VERSION_STRING_MATCH ${SYCL_VERSION_STRING})
  44. string(REPLACE "." ";" SYCL_VERSION_LIST ${SYCL_VERSION_STRING_MATCH})
  45. # Split the version number list into major, minor, and patch components.
  46. list(GET SYCL_VERSION_LIST 0 VERSION_MAJOR)
  47. list(GET SYCL_VERSION_LIST 1 VERSION_MINOR)
  48. list(GET SYCL_VERSION_LIST 2 VERSION_PATCH)
  49. # Calculate the version number in the format XXXXYYZZ, using the formula (major * 10000 + minor * 100 + patch).
  50. math(EXPR VERSION_NUMBER_MATCH "${VERSION_MAJOR} * 10000 + ${VERSION_MINOR} * 100 + ${VERSION_PATCH}")
  51. set(${version_number} "${VERSION_NUMBER_MATCH}" PARENT_SCOPE)
  52. endfunction()
  53. if(SYCL_COMPILER)
  54. parse_sycl_compiler_version(SYCL_COMPILER_VERSION)
  55. endif()
  56. if(NOT SYCL_COMPILER_VERSION)
  57. set(SYCL_FOUND False)
  58. set(SYCL_REASON_FAILURE "Cannot parse sycl compiler version to get SYCL_COMPILER_VERSION!")
  59. set(SYCL_NOT_FOUND_MESSAGE "${SYCL_REASON_FAILURE}")
  60. return()
  61. endif()
  62. # Find include path from binary.
  63. find_file(
  64. SYCL_INCLUDE_DIR
  65. NAMES include
  66. HINTS ${SYCL_ROOT}
  67. NO_DEFAULT_PATH
  68. )
  69. # Find include/sycl path from include path.
  70. find_file(
  71. SYCL_INCLUDE_SYCL_DIR
  72. NAMES sycl
  73. HINTS ${SYCL_ROOT}/include/
  74. NO_DEFAULT_PATH
  75. )
  76. # Due to the unrecognized compilation option `-fsycl` in other compiler.
  77. list(APPEND SYCL_INCLUDE_DIR ${SYCL_INCLUDE_SYCL_DIR})
  78. # Find library directory from binary.
  79. find_file(
  80. SYCL_LIBRARY_DIR
  81. NAMES lib lib64
  82. HINTS ${SYCL_ROOT}
  83. NO_DEFAULT_PATH
  84. )
  85. # Define the old version of SYCL toolkit that is compatible with the current version of PyTorch.
  86. set(PYTORCH_2_5_SYCL_TOOLKIT_VERSION 20249999)
  87. # By default, we use libsycl.so on Linux and sycl.lib on Windows as the SYCL library name.
  88. if (SYCL_COMPILER_VERSION VERSION_LESS_EQUAL PYTORCH_2_5_SYCL_TOOLKIT_VERSION)
  89. # Don't use if(WIN32) here since this requires cmake>=3.25 and file is installed
  90. # and used by other projects.
  91. # See: https://cmake.org/cmake/help/v3.25/variable/LINUX.html
  92. if(CMAKE_SYSTEM_NAME MATCHES "Windows")
  93. # On Windows, the SYCL library is named sycl7.lib until PYTORCH_2_5_SYCL_TOOLKIT_VERSION.
  94. # sycl.lib is supported in the later version.
  95. set(sycl_lib_suffix "7")
  96. endif()
  97. endif()
  98. # Find SYCL library fullname.
  99. find_library(
  100. SYCL_LIBRARY
  101. NAMES "sycl${sycl_lib_suffix}"
  102. HINTS ${SYCL_LIBRARY_DIR}
  103. NO_DEFAULT_PATH
  104. )
  105. # Find OpenCL library fullname, which is a dependency of oneDNN.
  106. find_library(
  107. OCL_LIBRARY
  108. NAMES OpenCL
  109. HINTS ${SYCL_LIBRARY_DIR}
  110. NO_DEFAULT_PATH
  111. )
  112. if((NOT SYCL_LIBRARY) OR (NOT OCL_LIBRARY))
  113. set(SYCL_FOUND False)
  114. set(SYCL_REASON_FAILURE "SYCL library is incomplete!!")
  115. set(SYCL_NOT_FOUND_MESSAGE "${SYCL_REASON_FAILURE}")
  116. return()
  117. endif()
  118. find_package_handle_standard_args(
  119. SYCL
  120. FOUND_VAR SYCL_FOUND
  121. REQUIRED_VARS SYCL_INCLUDE_DIR SYCL_LIBRARY_DIR SYCL_LIBRARY
  122. REASON_FAILURE_MESSAGE "${SYCL_REASON_FAILURE}"
  123. VERSION_VAR SYCL_COMPILER_VERSION
  124. )