Skip to content

Commit 0a30b8b

Browse files
Use intel sycl (#1955)
* Use IntelSYCL instead of IntelDPCPP compiler CMake script * Matrix target needs sycl * Enable languages via CMake's project command * Compile nbody_f for fixed format Fortran * Remove warnings from nbody examples * add_fortran_example_with_mkl must link to MKL::MKL_DPCPP not MKL::MKL Change is also made to use MKL::MKL_SYCL if MKL 2024.0.0 is detected --------- Co-authored-by: Vikram Narayana <vikram.narayana@intel.com>
1 parent c11ce49 commit 0a30b8b

File tree

9 files changed

+77
-36
lines changed

9 files changed

+77
-36
lines changed

Publications/GPU-Opt-Guide/CMakeLists.txt

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,33 @@ cmake_minimum_required(VERSION 3.21)
22
option(BUILD_FORTRAN_EXAMPLES "Whether to build fortran examples" ON)
33
set(CMAKE_C_COMPILER icx)
44
set(CMAKE_CXX_COMPILER icpx)
5+
set(_languages C CXX)
6+
57
if (BUILD_FORTRAN_EXAMPLES)
8+
set(_languages ${_languages} Fortran)
69
set(CMAKE_Fortran_COMPILER ifx)
710
endif()
811

9-
project(GPUOptGuide)
10-
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
1112

12-
include(CheckLanguage)
13-
enable_testing()
13+
project(GPUOptGuide
14+
LANGUAGES ${_languages}
15+
DESCRIPTION "Code examples from Intel GPU Optimization guide")
1416

15-
find_package(IntelDPCPP REQUIRED)
17+
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
1618

17-
if (BUILD_FOTRAN_EXAMPLES)
18-
check_language(Fortran)
19-
if(CMAKE_Fortran_COMPILER)
20-
enable_language(Fortran)
21-
else()
22-
message(FATAL_ERROR "No Fortran support detected, but Fortran tests were requested. Install oneAPI HPC Toolkit.")
23-
endif()
24-
endif()
19+
find_package(IntelSYCL REQUIRED)
2520

2621
set(MKL_THREADING tbb_thread)
22+
set(MKL_INTERFACE "ilp64")
2723
set(DPCPP_COMPILER ON)
24+
25+
set(MKL_VERSION_2024 FALSE)
26+
find_package(MKL QUIET)
27+
if(MKL_FOUND)
28+
if(MKL_VERSION VERSION_GREATER_EQUAL "2024.0.0")
29+
set(MKL_VERSION_2024 TRUE)
30+
endif()
31+
endif()
2832
find_package(MKL REQUIRED)
2933

3034
string(CONCAT WARNING_CXX_FLAGS_STR
@@ -39,49 +43,77 @@ string(CONCAT WARNING_CXX_FLAGS_STR
3943
string(REPLACE " " ";" COMMON_CXX_FLAGS "${WARNING_CXX_FLAGS_STR}")
4044

4145
function(add_example_with_mkl name)
42-
add_executable(${name} ${name}.cpp)
46+
set(_sources ${name}.cpp)
47+
add_executable(${name} ${_sources})
48+
add_sycl_to_target(TARGET ${name} SOURCES ${_sources})
4349
target_compile_options(${name} PRIVATE ${COMMON_CXX_FLAGS})
44-
target_compile_options(${name} PRIVATE -fiopenmp -fopenmp-targets=spir64 -qmkl)
45-
target_link_libraries(${name} PRIVATE MKL::MKL_DPCPP)
46-
target_link_options(${name} PRIVATE -fiopenmp -fopenmp-targets=spir64 -qmkl -lOpenCL)
50+
if (MKL_VERSION_2024)
51+
target_link_libraries(${name} PUBLIC MKL::MKL_SYCL)
52+
else()
53+
target_link_libraries(${name} PUBLIC MKL::MKL_DPCPP)
54+
endif()
4755
add_test(NAME ${name} COMMAND ${name} ${ARGN})
4856
endfunction(add_example_with_mkl)
4957

5058
function(add_fortran_example_with_mkl name)
5159
if(CMAKE_Fortran_COMPILER)
52-
add_executable(${name} ${name}.f)
60+
set(_sources ${name}.f)
61+
add_executable(${name} ${_sources})
62+
add_sycl_to_target(TARGET ${name} SOURCES ${_sources})
5363
target_compile_options(${name} PRIVATE -warn all)
54-
target_compile_options(${name} PRIVATE -fiopenmp -fopenmp-targets=spir64 -qmkl -fpp -free)
64+
target_compile_options(${name} PRIVATE -fpp -free)
5565
set_target_properties(${name} PROPERTIES LINKER_LANGUAGE Fortran)
56-
target_link_libraries(${name} PUBLIC MKL::MKL_DPCPP)
57-
target_link_options(${name} PRIVATE -fiopenmp -fopenmp-targets=spir64 -qmkl -lOpenCL)
66+
if (MKL_VERSION_2024)
67+
target_link_libraries(${name} PUBLIC MKL::MKL_SYCL)
68+
else()
69+
target_link_libraries(${name} PUBLIC MKL::MKL_DPCPP)
70+
endif()
5871
add_test(NAME ${name} COMMAND ${name} ${ARGN})
5972
endif()
6073
endfunction(add_fortran_example_with_mkl)
6174

6275
function(add_example name)
63-
add_executable(${name} ${name}.cpp)
76+
set(_sources ${name}.cpp)
77+
add_executable(${name} ${_sources})
78+
add_sycl_to_target(TARGET ${name} SOURCES ${_sources})
6479
target_compile_options(${name} PRIVATE ${COMMON_CXX_FLAGS})
6580
target_link_options(${name} PRIVATE -fsycl-device-code-split=per_kernel)
6681
add_test(NAME ${name} COMMAND ${name} ${ARGN})
6782
endfunction(add_example)
6883

84+
function(add_openmp_example name)
85+
set(_sources ${name}.cpp)
86+
add_executable(${name} ${_sources})
87+
target_compile_options(${name} PRIVATE ${COMMON_CXX_FLAGS})
88+
add_test(NAME ${name} COMMAND ${name} ${ARGN})
89+
endfunction(add_openmp_example)
90+
6991
function(add_fortran_example name)
7092
if(CMAKE_Fortran_COMPILER)
71-
add_executable(${name} ${name}.f90)
93+
set(_sources ${name}.f90)
94+
add_executable(${name} ${_sources})
95+
add_sycl_to_target(TARGET ${name} SOURCES ${_sources})
7296
target_compile_options(${name} PRIVATE -warn all)
73-
target_compile_options(${name} PRIVATE -fiopenmp -fopenmp-targets=spir64)
7497
set_target_properties(${name} PROPERTIES LINKER_LANGUAGE Fortran)
75-
target_link_options(${name} PRIVATE -fiopenmp -fopenmp-targets=spir64)
7698
add_test(NAME ${name} COMMAND ${name} ${ARGN})
7799
endif()
78100
endfunction(add_fortran_example)
79101

102+
function(add_fixed_fortran_example name)
103+
if(CMAKE_Fortran_COMPILER)
104+
set(_sources ${name}.f)
105+
add_executable(${name} ${_sources})
106+
target_compile_options(${name} PRIVATE -warn all)
107+
set_target_properties(${name} PROPERTIES LINKER_LANGUAGE Fortran)
108+
add_test(NAME ${name} COMMAND ${name} ${ARGN})
109+
endif()
110+
endfunction(add_fixed_fortran_example)
111+
80112
function(add_mpi_example name)
81113
if(MPI_FOUND)
82-
add_executable(${name} ${name}.cpp)
83-
target_compile_options(${name} PRIVATE -O3 -fiopenmp -fopenmp-targets=spir64)
84-
target_link_options(${name} PRIVATE -O3 -fiopenmp -fopenmp-targets=spir64)
114+
set(_sources ${name}.cpp)
115+
add_executable(${name} ${_sources})
116+
add_sycl_to_target(TARGET ${name} SOURCES ${_sources})
85117
target_link_libraries(${name} PRIVATE MPI::MPI_CXX)
86118
add_test(NAME ${name} COMMAND ${name} ${ARGN})
87119
endif()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
add_mpi_example(omp_mpich)
2+
target_compile_options(omp_mpich PRIVATE -fiopenmp)
3+
target_link_options(omp_mpich PRIVATE -fiopenmp)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
add_example(nbody_c)
1+
add_openmp_example(nbody_c)

Publications/GPU-Opt-Guide/OpenMP/26_omp_prefetch/c/nbody_c.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void nbody_1d_cpu(float *c, float *a, float *b, int n1, int n2) {
7171
void clean_cache_gpu(double *d, int n) {
7272

7373
#pragma omp target teams distribute parallel for thread_limit(1024)
74-
for (unsigned i = 0; i < n; ++i)
74+
for (int i = 0; i < n; ++i)
7575
d[i] = i;
7676

7777
return;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
add_compile_options(-fopenmp-target-simd)
2-
add_example(nbody_c_simd)
2+
add_openmp_example(nbody_c_simd)

Publications/GPU-Opt-Guide/OpenMP/26_omp_prefetch/c_simd/nbody_c_simd.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ void nbody_1d_cpu(float *c, float *a, float *b, int n1, int n2) {
9090
void clean_cache_gpu(double *d, int n) {
9191

9292
#pragma omp target teams distribute parallel for thread_limit(1024)
93-
for (unsigned i = 0; i < n; ++i)
93+
for (int i = 0; i < n; ++i)
9494
d[i] = i;
9595

9696
return;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
add_compile_options(-fpconstant -fpp -ffast-math -fno-sycl-instrument-device-code)
2-
add_fortran_example_with_mkl(nbody_f)
1+
add_compile_options(-fpconstant -fpp -ffast-math)
2+
add_fixed_fortran_example(nbody_f)

Publications/GPU-Opt-Guide/OpenMP/26_omp_prefetch/fortran/nbody_f.f

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#define PREFETCH_HINT 4 ! 4 = prefetch to L1 and L3; 2 = prefetch to L3
88
#define TILE_SIZE 64
99

10+
module gpu_kernels
11+
contains
1012
subroutine nbody_1d_gpu(c, a, b, n1, n2)
1113
implicit none
1214
integer n1, n2
@@ -53,7 +55,7 @@ subroutine nbody_1d_cpu(c, a, b, n1, n2)
5355
implicit none
5456
integer n1, n2
5557
real a(0:n1), b(0:n2), c(0:n1)
56-
real dx, bb(0:TILE_SIZE), delta, r2, s0, s1, f
58+
real dx, delta, r2, s0, s1, f
5759
integer i,j
5860
real ma0, ma1, ma2, ma3, ma4, ma5, eps
5961
parameter (ma0=0.269327, ma1=-0.0750978, ma2=0.0114808)
@@ -86,7 +88,10 @@ subroutine clean_cache_gpu(d,n)
8688
!$omp end target teams distribute parallel do
8789
end subroutine
8890

91+
end module gpu_kernels
92+
8993
program nbody
94+
use gpu_kernels
9095
implicit none
9196
include 'omp_lib.h'
9297

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
add_executable(matrix matrix.cpp multiply.cpp)
1+
set(_sources matrix.cpp multiply.cpp)
2+
add_executable(matrix ${_sources})
3+
add_sycl_to_target(TARGET matrix SOURCES ${_sources})

0 commit comments

Comments
 (0)