Skip to content

Commit 1401716

Browse files
authored
[cmake] Allow users to link against onemath_<domain>_<backend> targets (#627)
1 parent 44c0d53 commit 1401716

File tree

38 files changed

+385
-412
lines changed

38 files changed

+385
-412
lines changed

docs/using_onemath_with_cmake.rst

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Using oneMath in your project with CMake
66
The CMake build tool can help you use oneMath in your own project. Instead of
77
manually linking and including directories, you can use the CMake targets
88
exported by the oneMath project. You can use oneMath in one of two forms, with
9-
the target names depending on the approach taken:
9+
the target names depending on the approach taken:
1010

1111
* you can use a previously installed copy, either from a binary distribution or
1212
built from source. This can be imported using CMake's ``find_package``
@@ -33,14 +33,16 @@ For example:
3333
find_package(oneMath REQUIRED)
3434
target_link_libraries(myTarget PRIVATE ONEMATH::onemath)
3535
36-
Different targets can be used depending on the requirements of oneMath.
37-
To link against the entire library, the ``ONEMATH::onemath`` target should be used.
38-
For specific domains, ``ONEMATH::onemath_<domain>`` should be used.
39-
And for specific backends, ``ONEMATH::onemath_<domain>_<backend>`` should be used.
36+
Different targets can be used depending on the requirements of oneMath.
37+
To link against the entire library with run-time dispatching, the
38+
``ONEMATH::onemath`` target should be used.
39+
For specific backends with compile-time dispatching,
40+
``ONEMATH::onemath_<domain>_<backend>`` should be used.
4041

4142
When using a binary, it may be useful to know the backends that were enabled
42-
during the build. To check for the existence of backends, CMake's ``if(TARGET
43-
<target>)`` construct can be used. For example, with the ``cufft`` backend:
43+
during the build. To check for the existence of backends, CMake's
44+
``if(TARGET <target>)`` construct can be used. For example, with the ``cufft``
45+
backend:
4446

4547
.. code-block:: cmake
4648
@@ -81,10 +83,8 @@ The build parameters should be appropriately set before
8183
:ref:`building_the_project_with_adaptivecpp`.
8284

8385
To link against the main library with run-time dispatching, use the target
84-
``onemath``. To link against particular domains, use the target
85-
``onemath_<domain>``. For example, ``onemath_blas`` or ``onemath_dft``. To link
86-
against particular backends (as required for static dispatch of oneAPI calls to
87-
a particular backend), use the target ``onemath_<domain>_<backend>``. For
88-
example, ``onemath_dft_cufft``.
86+
``onemath``. To link against particular backends with compile-time dispatching,
87+
use the target ``onemath_<domain>_<backend>``. For example,
88+
``onemath_dft_cufft``.
8989

9090
.. _FetchContent: https://cmake.org/cmake/help/latest/module/FetchContent.html

examples/blas/compile_time_dispatching/level3/CMakeLists.txt

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,31 @@
1717
# SPDX-License-Identifier: Apache-2.0
1818
#===============================================================================
1919

20-
#Build object from all sources
21-
set(BLAS_CT_SOURCES "")
22-
if(ENABLE_MKLCPU_BACKEND AND ENABLE_CUBLAS_BACKEND)
23-
list(APPEND BLAS_CT_SOURCES "gemm_usm_mklcpu_cublas")
20+
# The example is written for the MKLCPU and CUBLAS backends
21+
if(NOT (ENABLE_MKLCPU_BACKEND AND ENABLE_CUBLAS_BACKEND))
22+
return()
2423
endif()
2524

26-
foreach(blas_ct_source ${BLAS_CT_SOURCES})
27-
add_executable(example_${domain}_${blas_ct_source} ${blas_ct_source}.cpp)
28-
target_include_directories(example_${domain}_${blas_ct_source}
29-
PUBLIC ${PROJECT_SOURCE_DIR}/examples/include
30-
PUBLIC ${PROJECT_SOURCE_DIR}/include
31-
PUBLIC ${CMAKE_BINARY_DIR}/bin
32-
)
25+
set(EXAMPLE_TARGET example_blas_gemm_usm_mklcpu_cublas)
3326

34-
if(domain STREQUAL "blas" AND ENABLE_MKLCPU_BACKEND AND ENABLE_CUBLAS_BACKEND)
35-
add_dependencies(example_${domain}_${blas_ct_source} onemath_${domain}_mklcpu onemath_${domain}_cublas)
36-
list(APPEND ONEMATH_LIBRARIES_${domain} onemath_${domain}_mklcpu onemath_${domain}_cublas)
37-
endif()
27+
# External applications should use find_package or FetchContent to include oneMath first.
28+
# See https://github.com/uxlfoundation/oneMath/blob/develop/docs/using_onemath_with_cmake.rst
3829

39-
target_link_libraries(example_${domain}_${blas_ct_source} PUBLIC
40-
${ONEMATH_LIBRARIES_${domain}}
41-
ONEMATH::SYCL::SYCL
42-
)
30+
# Create a CMake target with one source file
31+
add_executable(${EXAMPLE_TARGET} gemm_usm_mklcpu_cublas.cpp)
4332

44-
# Register example as ctest
45-
add_test(NAME ${domain}/EXAMPLE/CT/${blas_ct_source} COMMAND example_${domain}_${blas_ct_source})
33+
# Linking against onemath_blas_mklcpu and onemath_blas_cublas in CMake will add the required include directories and dependencies.
34+
# One can also link against `onemath_blas` to link against all the blas backends built.
35+
# These targets should only be used for compile-time dispatching.
36+
target_link_libraries(${EXAMPLE_TARGET} PUBLIC
37+
onemath_blas_mklcpu
38+
onemath_blas_cublas
39+
)
4640

47-
endforeach(blas_ct_source)
41+
# Include directories specific to the examples
42+
target_include_directories(${EXAMPLE_TARGET} PUBLIC
43+
${PROJECT_SOURCE_DIR}/examples/include
44+
)
45+
46+
# Register example as ctest
47+
add_test(NAME blas/EXAMPLE/CT/gemm_usm_mklcpu_cublas COMMAND ${EXAMPLE_TARGET})

examples/blas/run_time_dispatching/level3/CMakeLists.txt

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@
2020
# NOTE: user needs to set env var ONEAPI_DEVICE_SELECTOR to use runtime example without specifying backend in CMake
2121
# $ENV{ONEAPI_DEVICE_SELECTOR}
2222

23-
24-
# Build object from all example sources
25-
set(BLAS_RT_SOURCES "gemm_usm")
26-
2723
# Set up for the right backend for run-time dispatching examples
2824
# If users build more than one backend (i.e. mklcpu and mklgpu, or mklcpu and CUDA), they may need to
2925
# overwrite ONEAPI_DEVICE_SELECTOR in their environment to run on the desired backend
@@ -56,32 +52,27 @@ endif()
5652

5753
message(STATUS "ONEAPI_DEVICE_SELECTOR will be set to the following value(s): [${DEVICE_FILTERS}] for run-time dispatching examples")
5854

59-
foreach(blas_rt_source ${BLAS_RT_SOURCES})
60-
add_executable(example_${domain}_${blas_rt_source} ${blas_rt_source}.cpp)
61-
target_include_directories(example_${domain}_${blas_rt_source}
62-
PUBLIC ${PROJECT_SOURCE_DIR}/examples/include
63-
PUBLIC ${PROJECT_SOURCE_DIR}/include
64-
PUBLIC ${CMAKE_BINARY_DIR}/bin
65-
)
55+
set(EXAMPLE_TARGET example_blas_gemm_usm)
6656

67-
add_dependencies(example_${domain}_${blas_rt_source} onemath)
57+
# External applications should use find_package or FetchContent to include oneMath first.
58+
# See https://github.com/uxlfoundation/oneMath/blob/develop/docs/using_onemath_with_cmake.rst
6859

69-
if (USE_ADD_SYCL_TO_TARGET_INTEGRATION)
70-
add_sycl_to_target(TARGET example_${domain}_${blas_rt_source} SOURCES ${BLAS_RT_SOURCES})
71-
endif()
60+
# Create a CMake target with one source file
61+
add_executable(${EXAMPLE_TARGET} gemm_usm.cpp)
7262

73-
target_link_libraries(example_${domain}_${blas_rt_source} PUBLIC
74-
onemath
75-
ONEMATH::SYCL::SYCL
76-
${CMAKE_DL_LIBS}
77-
)
63+
# Linking against onemath in CMake will add the required include directories and dependencies.
64+
# This target should only be used for runtime dispatching.
65+
target_link_libraries(${EXAMPLE_TARGET} PUBLIC onemath)
7866

79-
# Register example as ctest
80-
foreach(device_filter ${DEVICE_FILTERS})
81-
add_test(NAME ${domain}/EXAMPLE/RT/${blas_rt_source}/${device_filter} COMMAND example_${domain}_${blas_rt_source})
82-
set_property(TEST ${domain}/EXAMPLE/RT/${blas_rt_source}/${device_filter} PROPERTY
83-
ENVIRONMENT LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/lib:$ENV{LD_LIBRARY_PATH}
84-
ENVIRONMENT ONEAPI_DEVICE_SELECTOR=${device_filter})
85-
endforeach(device_filter)
67+
# Include directories specific to the examples
68+
target_include_directories(${EXAMPLE_TARGET} PUBLIC
69+
${PROJECT_SOURCE_DIR}/examples/include
70+
)
8671

87-
endforeach(blas_rt_source)
72+
# Register example as ctest for each device
73+
foreach(device_filter ${DEVICE_FILTERS})
74+
add_test(NAME blas/EXAMPLE/RT/gemm_usm/${device_filter} COMMAND ${EXAMPLE_TARGET})
75+
# Set ONEAPI_DEVICE_SELECTOR environment variable to select a device at runtime
76+
set_property(TEST blas/EXAMPLE/RT/gemm_usm/${device_filter} PROPERTY
77+
ENVIRONMENT ONEAPI_DEVICE_SELECTOR=${device_filter})
78+
endforeach(device_filter)

examples/dft/compile_time_dispatching/CMakeLists.txt

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,35 @@
1717
# SPDX-License-Identifier: Apache-2.0
1818
#===============================================================================
1919

20-
#Build object from all sources
21-
set(DFT_CT_SOURCES "")
22-
if (ENABLE_MKLCPU_BACKEND AND ENABLE_CUFFT_BACKEND)
23-
list(APPEND DFT_CT_SOURCES "complex_fwd_usm_mklcpu_cufft")
20+
# The example is written for the MKLCPU and CUFFT backends
21+
if(NOT (ENABLE_MKLCPU_BACKEND AND ENABLE_CUFFT_BACKEND))
22+
return()
2423
endif()
2524

26-
include(WarningsUtils)
25+
set(EXAMPLE_TARGET example_dft_complex_fwd_usm_mklcpu_cufft)
2726

28-
foreach(dft_ct_source ${DFT_CT_SOURCES})
29-
set(EXAMPLE_NAME example_${domain}_${dft_ct_source})
30-
add_executable(${EXAMPLE_NAME} ${dft_ct_source}.cpp)
31-
target_include_directories(${EXAMPLE_NAME}
32-
PUBLIC ${PROJECT_SOURCE_DIR}/examples/include
33-
PUBLIC ${CMAKE_BINARY_DIR}/bin
34-
)
27+
# External applications should use find_package or FetchContent to include oneMath first.
28+
# See https://github.com/uxlfoundation/oneMath/blob/develop/docs/using_onemath_with_cmake.rst
3529

36-
if(domain STREQUAL "dft" AND ENABLE_MKLCPU_BACKEND AND ENABLE_CUFFT_BACKEND)
37-
add_dependencies(${EXAMPLE_NAME} onemath_${domain}_mklcpu onemath_${domain}_cufft)
38-
list(APPEND ONEMATH_LIBRARIES_${domain} onemath_${domain}_mklcpu onemath_${domain}_cufft)
39-
endif()
30+
# Create a CMake target with one source file
31+
add_executable(${EXAMPLE_TARGET} complex_fwd_usm_mklcpu_cufft.cpp)
4032

41-
target_link_libraries(${EXAMPLE_NAME} PUBLIC
42-
${ONEMATH_LIBRARIES_${domain}}
43-
onemath_warnings
44-
)
33+
# Linking against onemath_dft_mklcpu and onemath_dft_cufft in CMake will add the required include directories and dependencies.
34+
# One can also link against `onemath_dft` to link against all the dft backends built.
35+
# These targets should only be used for compile-time dispatching.
36+
target_link_libraries(${EXAMPLE_TARGET} PUBLIC
37+
onemath_dft_mklcpu
38+
onemath_dft_cufft
39+
)
4540

46-
# Register example as ctest
47-
add_test(NAME dft/EXAMPLE/CT/${dft_ct_source} COMMAND ${EXAMPLE_NAME})
41+
# Include directories specific to the examples
42+
target_include_directories(${EXAMPLE_TARGET} PUBLIC
43+
${PROJECT_SOURCE_DIR}/examples/include
44+
)
4845

49-
endforeach(dft_ct_source)
46+
# Enable warnings
47+
include(WarningsUtils)
48+
target_link_libraries(${EXAMPLE_TARGET} PRIVATE onemath_warnings)
5049

50+
# Register example as ctest
51+
add_test(NAME dft/EXAMPLE/CT/complex_fwd_usm_mklcpu_cufft COMMAND ${EXAMPLE_TARGET})

examples/dft/run_time_dispatching/CMakeLists.txt

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,11 @@
1818
#===============================================================================
1919

2020
# NOTE: user needs to set env var ONEAPI_DEVICE_SELECTOR to use runtime example (no need to specify backend when building with CMake)
21-
include(WarningsUtils)
22-
2321

24-
# Build object from all example sources
25-
set(DFT_RT_SOURCES "")
2622
# Set up for the right backend for run-time dispatching examples
2723
# If users build more than one backend (i.e. mklcpu and mklgpu, or mklcpu and CUDA), they may need to
2824
# overwrite ONEAPI_DEVICE_SELECTOR in their environment to run on the desired backend
2925
set(DEVICE_FILTERS "")
30-
if(ENABLE_MKLGPU_BACKEND OR ENABLE_MKLCPU_BACKEND OR ENABLE_CUFFT_BACKEND OR ENABLE_ROCFFT_BACKEND OR ENABLE_PORTFFT_BACKEND)
31-
list(APPEND DFT_RT_SOURCES "real_fwd_usm")
32-
endif()
33-
3426
if(ENABLE_MKLGPU_BACKEND)
3527
list(APPEND DEVICE_FILTERS "level_zero:gpu")
3628
endif()
@@ -49,33 +41,31 @@ endif()
4941

5042
message(STATUS "ONEAPI_DEVICE_SELECTOR will be set to the following value(s): [${DEVICE_FILTERS}] for run-time dispatching examples")
5143

52-
foreach(dft_rt_sources ${DFT_RT_SOURCES})
53-
add_executable(example_${domain}_${dft_rt_sources} ${dft_rt_sources}.cpp)
54-
target_include_directories(example_${domain}_${dft_rt_sources}
55-
PUBLIC ${PROJECT_SOURCE_DIR}/examples/include
56-
PUBLIC ${PROJECT_SOURCE_DIR}/include
57-
PUBLIC ${CMAKE_BINARY_DIR}/bin
58-
)
44+
set(EXAMPLE_TARGET example_dft_real_fwd_usm)
45+
46+
# External applications should use find_package or FetchContent to include oneMath first.
47+
# See https://github.com/uxlfoundation/oneMath/blob/develop/docs/using_onemath_with_cmake.rst
5948

60-
add_dependencies(example_${domain}_${dft_rt_sources} onemath)
49+
# Create a CMake target with one source file
50+
add_executable(${EXAMPLE_TARGET} real_fwd_usm.cpp)
6151

62-
if (USE_ADD_SYCL_TO_TARGET_INTEGRATION)
63-
add_sycl_to_target(TARGET example_${domain}_${dft_rt_sources} SOURCES ${DFT_RT_SOURCES})
64-
endif()
52+
# Linking against onemath in CMake will add the required include directories and dependencies.
53+
# This target should only be used for runtime dispatching.
54+
target_link_libraries(${EXAMPLE_TARGET} PUBLIC onemath)
6555

66-
target_link_libraries(example_${domain}_${dft_rt_sources}
67-
PUBLIC onemath
68-
PUBLIC ONEMATH::SYCL::SYCL
69-
PUBLIC ${CMAKE_DL_LIBS}
70-
PRIVATE onemath_warnings
71-
)
56+
# Include directories specific to the examples
57+
target_include_directories(${EXAMPLE_TARGET} PUBLIC
58+
${PROJECT_SOURCE_DIR}/examples/include
59+
)
7260

73-
# Register example as ctest
74-
foreach(device_filter ${DEVICE_FILTERS})
75-
add_test(NAME ${domain}/EXAMPLE/RT/${dft_rt_sources}/${device_filter} COMMAND example_${domain}_${dft_rt_sources})
76-
set_property(TEST ${domain}/EXAMPLE/RT/${dft_rt_sources}/${device_filter} PROPERTY
77-
ENVIRONMENT LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/lib:$ENV{LD_LIBRARY_PATH}
78-
ENVIRONMENT ONEAPI_DEVICE_SELECTOR=${device_filter})
79-
endforeach(device_filter)
61+
# Enable warnings
62+
include(WarningsUtils)
63+
target_link_libraries(${EXAMPLE_TARGET} PRIVATE onemath_warnings)
8064

81-
endforeach()
65+
# Register example as ctest
66+
foreach(device_filter ${DEVICE_FILTERS})
67+
add_test(NAME dft/EXAMPLE/RT/real_fwd_usm/${device_filter} COMMAND ${EXAMPLE_TARGET})
68+
# Set ONEAPI_DEVICE_SELECTOR environment variable to select a device at runtime
69+
set_property(TEST dft/EXAMPLE/RT/real_fwd_usm/${device_filter} PROPERTY
70+
ENVIRONMENT ONEAPI_DEVICE_SELECTOR=${device_filter})
71+
endforeach(device_filter)

examples/lapack/compile_time_dispatching/CMakeLists.txt

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,31 @@
1717
# SPDX-License-Identifier: Apache-2.0
1818
#===============================================================================
1919

20-
#Build object from all sources
21-
set(LAPACK_CT_SOURCES "")
22-
if(ENABLE_MKLCPU_BACKEND AND ENABLE_CUSOLVER_BACKEND)
23-
list(APPEND LAPACK_CT_SOURCES "getrs_usm_mklcpu_cusolver")
20+
# The example is written for the MKLCPU and CUSOLVER backends
21+
if(NOT (ENABLE_MKLCPU_BACKEND AND ENABLE_CUSOLVER_BACKEND))
22+
return()
2423
endif()
2524

26-
if(domain STREQUAL "lapack" AND ENABLE_MKLCPU_BACKEND)
27-
find_library(OPENCL_LIBRARY NAMES OpenCL)
28-
message(STATUS "Found OpenCL: ${OPENCL_LIBRARY}")
29-
endif()
25+
set(EXAMPLE_TARGET example_lapack_getrs_usm_mklcpu_cusolver)
26+
27+
# External applications should use find_package or FetchContent to include oneMath first.
28+
# See https://github.com/uxlfoundation/oneMath/blob/develop/docs/using_onemath_with_cmake.rst
29+
30+
# Create a CMake target with one source file
31+
add_executable(${EXAMPLE_TARGET} getrs_usm_mklcpu_cusolver.cpp)
32+
33+
# Linking against onemath_lapack_mklcpu and onemath_lapack_cusolver in CMake will add the required include directories and dependencies.
34+
# One can also link against `onemath_lapack` to link against all the lapack backends built.
35+
# These targets should only be used for compile-time dispatching.
36+
target_link_libraries(${EXAMPLE_TARGET} PUBLIC
37+
onemath_lapack_mklcpu
38+
onemath_lapack_cusolver
39+
)
40+
41+
# Include directories specific to the examples
42+
target_include_directories(${EXAMPLE_TARGET} PUBLIC
43+
${PROJECT_SOURCE_DIR}/examples/include
44+
)
3045

31-
foreach(lapack_ct_source ${LAPACK_CT_SOURCES})
32-
add_executable(example_${domain}_${lapack_ct_source} ${lapack_ct_source}.cpp)
33-
target_include_directories(example_${domain}_${lapack_ct_source}
34-
PUBLIC ${PROJECT_SOURCE_DIR}/examples/include
35-
PUBLIC ${PROJECT_SOURCE_DIR}/include
36-
PUBLIC ${CMAKE_BINARY_DIR}/bin
37-
)
38-
if(domain STREQUAL "lapack" AND ENABLE_MKLCPU_BACKEND AND ENABLE_CUSOLVER_BACKEND)
39-
add_dependencies(example_${domain}_${lapack_ct_source} onemath_${domain}_mklcpu onemath_${domain}_cusolver)
40-
list(APPEND ONEMATH_LIBRARIES_${domain} onemath_${domain}_mklcpu onemath_${domain}_cusolver)
41-
target_link_libraries(example_${domain}_${lapack_ct_source} PUBLIC ${OPENCL_LIBRARY})
42-
endif()
43-
target_link_libraries(example_${domain}_${lapack_ct_source} PUBLIC
44-
${ONEMATH_LIBRARIES_${domain}}
45-
ONEMATH::SYCL::SYCL
46-
)
47-
# Register example as ctest
48-
add_test(NAME ${domain}/EXAMPLE/CT/${lapack_ct_source} COMMAND example_${domain}_${lapack_ct_source})
49-
endforeach(lapack_ct_source)
46+
# Register example as ctest
47+
add_test(NAME lapack/EXAMPLE/CT/getrs_usm_mklcpu_cusolver COMMAND ${EXAMPLE_TARGET})

0 commit comments

Comments
 (0)