|
1 | | -# Option to enable OneAPI |
2 | | -option(USE_ONEAPI "Enable support for Intel OneAPI" ON) |
| 1 | +# OneAPISupport.cmake |
| 2 | +# |
| 3 | +# IMPORTANT: The Intel DPC++/C++ compiler (icpx) must be selected BEFORE |
| 4 | +# CMake's project() call. Pass it on the command line: |
| 5 | +# cmake -DCMAKE_CXX_COMPILER=icpx ... |
| 6 | +# or source Intel's environment script first: |
| 7 | +# source /opt/intel/oneapi/setvars.sh && cmake -DCMAKE_CXX_COMPILER=icpx ... |
3 | 8 |
|
4 | 9 | if (USE_ONEAPI) |
5 | | - # Check for compiler and print debug info |
6 | | - include(CheckLanguage) |
7 | | - check_language(CXX) |
8 | | - |
9 | | - message(STATUS "CMAKE_CXX_COMPILER: ${CMAKE_CXX_COMPILER}") |
10 | | - message(STATUS "CMAKE_CXX_COMPILER_ID: ${CMAKE_CXX_COMPILER_ID}") |
11 | | - |
12 | | - # Explicitly check if the compiler is IntelLLVM (DPC++) |
13 | | - if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "IntelLLVM") |
14 | | - message(STATUS "OneAPI DPC++ compiler (IntelLLVM) detected: ${CMAKE_CXX_COMPILER}") |
| 10 | + # Verify that icpx (IntelLLVM) is actually the active CXX compiler. |
| 11 | + # CMAKE_CXX_COMPILER cannot be changed after project() has been called. |
| 12 | + if (NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "IntelLLVM") |
| 13 | + message(FATAL_ERROR |
| 14 | + "USE_ONEAPI requires the Intel DPC++/C++ compiler (icpx/icx).\n" |
| 15 | + " Detected: CMAKE_CXX_COMPILER_ID=${CMAKE_CXX_COMPILER_ID}" |
| 16 | + " (${CMAKE_CXX_COMPILER})\n" |
| 17 | + " Reconfigure with: -DCMAKE_CXX_COMPILER=icpx\n" |
| 18 | + " or source Intel's setvars.sh before running cmake.") |
| 19 | + endif() |
15 | 20 |
|
16 | | - # Set compiler flags |
17 | | - set(CMAKE_CXX_STANDARD 17) |
18 | | - set(CMAKE_CXX_STANDARD_REQUIRED ON) |
| 21 | + message(STATUS "OneAPI DPC++ compiler (IntelLLVM) detected: ${CMAKE_CXX_COMPILER}") |
19 | 22 |
|
20 | | - # Define a preprocessor directive for OneAPI support |
21 | | - add_definitions(-DUSE_ONEAPI) |
| 23 | + # -fsycl is required for SYCL device compilation |
| 24 | + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsycl") |
22 | 25 |
|
23 | | - # First try finding IntelSYCL (New recommended package) |
24 | | - set(IntelSYCL_DIR "/net/projects/tools/x86_64/rhel-8/intel-oneapi/2024.2/compiler/latest/lib/cmake/sycl") |
25 | | - find_package(IntelSYCL REQUIRED) |
| 26 | + add_definitions(-DUSE_ONEAPI) |
26 | 27 |
|
27 | | - if (IntelSYCL_FOUND) |
28 | | - message(STATUS "Intel OneAPI SYCL package found.") |
29 | | - set(COMMON_LINK_LIBRARIES ${COMMON_LINK_LIBRARIES} IntelSYCL::SYCL_CXX) |
| 28 | + # ------------------------------------------------------------------ |
| 29 | + # Locate the IntelSYCL CMake package. |
| 30 | + # Search order: |
| 31 | + # 1. User-supplied IntelSYCL_DIR cache variable |
| 32 | + # 2. CMPLR_ROOT environment variable (set by setvars.sh / modulefiles) |
| 33 | + # 3. ONEAPI_ROOT environment variable (set by setvars.sh) |
| 34 | + # 4. Derive from the compiler executable location |
| 35 | + # ------------------------------------------------------------------ |
| 36 | + if (NOT DEFINED IntelSYCL_DIR) |
| 37 | + if (DEFINED ENV{CMPLR_ROOT}) |
| 38 | + set(IntelSYCL_DIR "$ENV{CMPLR_ROOT}/lib/cmake/sycl" |
| 39 | + CACHE PATH "Path to IntelSYCL CMake config") |
| 40 | + elseif (DEFINED ENV{ONEAPI_ROOT}) |
| 41 | + set(IntelSYCL_DIR "$ENV{ONEAPI_ROOT}/compiler/latest/lib/cmake/sycl" |
| 42 | + CACHE PATH "Path to IntelSYCL CMake config") |
30 | 43 | else() |
31 | | - message(WARNING "IntelSYCL::SYCL target not found! Falling back to manual linking.") |
32 | | - |
33 | | - # Manually link the Intel SYCL library |
34 | | - set(SYCL_LIB_PATH "/net/projects/tools/x86_64/rhel-8/intel-oneapi/2024.2/compiler/2024.2/lib") |
| 44 | + # Fall back to a path relative to the compiler binary |
| 45 | + get_filename_component(_icpx_bindir "${CMAKE_CXX_COMPILER}" DIRECTORY) |
| 46 | + set(IntelSYCL_DIR "${_icpx_bindir}/../lib/cmake/sycl" |
| 47 | + CACHE PATH "Path to IntelSYCL CMake config") |
| 48 | + endif() |
| 49 | + endif() |
35 | 50 |
|
36 | | - # Correct OneAPI include path |
37 | | - set(SYCL_INCLUDE_PATH "/net/projects/tools/x86_64/rhel-8/intel-oneapi/2024.2/compiler/2024.2/include") |
| 51 | + find_package(IntelSYCL QUIET) |
38 | 52 |
|
39 | | - include_directories(${SYCL_INCLUDE_PATH}) |
40 | | - link_directories(${SYCL_LIB_PATH}) |
| 53 | + if (IntelSYCL_FOUND) |
| 54 | + message(STATUS "Intel OneAPI SYCL package found (${IntelSYCL_DIR})") |
| 55 | + set(COMMON_LINK_LIBRARIES ${COMMON_LINK_LIBRARIES} IntelSYCL::SYCL_CXX) |
| 56 | + else() |
| 57 | + message(WARNING |
| 58 | + "IntelSYCL CMake package not found (searched: ${IntelSYCL_DIR}).\n" |
| 59 | + "Falling back to manual SYCL library linking.") |
41 | 60 |
|
42 | | - set(COMMON_LINK_LIBRARIES ${COMMON_LINK_LIBRARIES} "${SYCL_LIB_PATH}/libsycl.so") |
| 61 | + # Derive SYCL lib/include paths with the same priority order |
| 62 | + if (DEFINED ENV{CMPLR_ROOT}) |
| 63 | + set(_sycl_root "$ENV{CMPLR_ROOT}") |
| 64 | + elseif (DEFINED ENV{ONEAPI_ROOT}) |
| 65 | + set(_sycl_root "$ENV{ONEAPI_ROOT}/compiler/latest") |
| 66 | + else() |
| 67 | + get_filename_component(_icpx_bindir "${CMAKE_CXX_COMPILER}" DIRECTORY) |
| 68 | + set(_sycl_root "${_icpx_bindir}/..") |
43 | 69 | endif() |
44 | 70 |
|
45 | | - # Force CMake to use DPC++ compiler |
46 | | - set(CMAKE_CXX_COMPILER icpx) |
47 | | - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsycl") |
| 71 | + set(SYCL_LIB_PATH "${_sycl_root}/lib") |
| 72 | + set(SYCL_INCLUDE_PATH "${_sycl_root}/include") |
48 | 73 |
|
49 | | - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I${SYCL_INCLUDE_PATH}") |
| 74 | + if (NOT EXISTS "${SYCL_LIB_PATH}/libsycl.so") |
| 75 | + message(FATAL_ERROR |
| 76 | + "Could not find libsycl.so under ${SYCL_LIB_PATH}.\n" |
| 77 | + "Set CMPLR_ROOT or ONEAPI_ROOT, or specify -DIntelSYCL_DIR=<path>.") |
| 78 | + endif() |
50 | 79 |
|
51 | | - else() |
52 | | - message(FATAL_ERROR "OneAPI DPC++ compiler not found. Detected CMAKE_CXX_COMPILER_ID=${CMAKE_CXX_COMPILER_ID}") |
| 80 | + include_directories(${SYCL_INCLUDE_PATH}) |
| 81 | + link_directories(${SYCL_LIB_PATH}) |
| 82 | + set(COMMON_LINK_LIBRARIES ${COMMON_LINK_LIBRARIES} "${SYCL_LIB_PATH}/libsycl.so") |
| 83 | + message(STATUS "Using manual SYCL library: ${SYCL_LIB_PATH}/libsycl.so") |
| 84 | + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I${SYCL_INCLUDE_PATH}") |
53 | 85 | endif() |
| 86 | + |
| 87 | + message(STATUS "OneAPI support enabled (flags: ${CMAKE_CXX_FLAGS})") |
54 | 88 | endif() |
0 commit comments