Skip to content

Commit

Permalink
Merge pull request #15283 from masterleinad/cmake_kokkos_targets
Browse files Browse the repository at this point in the history
Use Kokkos CMake targets
  • Loading branch information
tamiko committed Jun 9, 2023
2 parents 3e82abc + a6e9364 commit 915f5ad
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 49 deletions.
3 changes: 3 additions & 0 deletions cmake/macros/macro_copy_target_properties.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,19 @@ function(copy_target_properties _destination_target)

get_target_property(_values ${_entry} INTERFACE_COMPILE_DEFINITIONS)
if(NOT "${_values}" MATCHES "-NOTFOUND")
strip_known_generator_expressions(_values)
list(APPEND _compile_definitions ${_values})
endif()

get_target_property(_values ${_entry} INTERFACE_COMPILE_OPTIONS)
if(NOT "${_values}" MATCHES "-NOTFOUND")
strip_known_generator_expressions(_values)
list(APPEND _compile_options ${_values})
endif()

get_target_property(_values ${_entry} INTERFACE_LINK_OPTIONS)
if(NOT "${_values}" MATCHES "-NOTFOUND")
strip_known_generator_expressions(_values)
list(APPEND _link_options ${_values})
endif()
endwhile()
Expand Down
10 changes: 9 additions & 1 deletion cmake/macros/macro_strip_known_generator_expressions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
#

macro(strip_known_generator_expressions _variable)
string(REGEX REPLACE "^\\$<LINK_ONLY:(.*)>$" "\\1" ${_variable} "${${_variable}}")
set(generator_expressions
"\\$<LINK_ONLY:([^>]*)>"
"\\$<\\$<LINK_LANGUAGE:CXX>:([^>]*)>"
"\\$<\\$<COMPILE_LANGUAGE:CXX>:([^>]*)>"
)

foreach(expression ${generator_expressions})
string(REGEX REPLACE ${expression} "\\1" ${_variable} "${${_variable}}")
endforeach()
endmacro()

61 changes: 13 additions & 48 deletions cmake/modules/FindDEAL_II_KOKKOS.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -32,64 +32,29 @@ if(DEAL_II_TRILINOS_WITH_KOKKOS OR DEAL_II_PETSC_WITH_KOKKOS)
# Let deal.II know that we have found Kokkos
set(KOKKOS_FOUND ON)
else()

# silence a warning when including FindKOKKOS.cmake
set(CMAKE_CXX_EXTENSIONS OFF)
find_package(Kokkos 3.7.0 QUIET
HINTS ${KOKKOS_DIR} ${Kokkos_DIR} $ENV{Kokkos_DIR}
)

if(Kokkos_FOUND)
# We are only interested in Kokkos if it is not part of Trilinos
if(TARGET Kokkos::kokkos AND TARGET Kokkos::kokkoscore)
get_property(KOKKOS_INSTALL_INCLUDE_DIR TARGET Kokkos::kokkos PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
get_property(KOKKOS_EXTRA_LD_FLAGS_FULL TARGET Kokkos::kokkoscore PROPERTY INTERFACE_LINK_OPTIONS)
string(REGEX REPLACE "\\$<\\$<LINK_LANGUAGE:CXX>:([^>]*)>" "\\1" KOKKOS_EXTRA_LD_FLAGS "${KOKKOS_EXTRA_LD_FLAGS_FULL}")
string(REPLACE ";" " " KOKKOS_EXTRA_LD_FLAGS "${KOKKOS_EXTRA_LD_FLAGS}")
get_property(KOKKOS_COMPILE_FLAGS_FULL TARGET Kokkos::kokkoscore PROPERTY INTERFACE_COMPILE_OPTIONS)
string(REGEX REPLACE "\\$<\\$<COMPILE_LANGUAGE:CXX>:([^>]*)>" "\\1" KOKKOS_COMPILE_FLAGS "${KOKKOS_COMPILE_FLAGS_FULL}")
string(REPLACE ";" " " KOKKOS_COMPILE_FLAGS "${KOKKOS_COMPILE_FLAGS}")

# Kokkos links transitively with OpenMP so that we need to find OpenMP again.
# Since we are not using target_link_libraries, we have to extract the compile flag manually.
if(Kokkos_VERSION VERSION_GREATER_EQUAL 4.0.00 AND Kokkos_ENABLE_OPENMP)
string(APPEND KOKKOS_COMPILE_FLAGS " ${OpenMP_CXX_FLAGS}")
endif()
set(KOKKOS_FOUND ${Kokkos_FOUND})

# We need to disable SIMD vectorization for CUDA device code.
# Otherwise, nvcc compilers from version 9 on will emit an error message like:
# "[...] contains a vector, which is not supported in device code". We
# would like to set the variable in check_01_cpu_feature but at that point
# we don't know if CUDA support is enabled in Kokkos
if(Kokkos_ENABLE_CUDA)
set(DEAL_II_VECTORIZATION_WIDTH_IN_BITS 0)
# Require lambda support to use Kokkos as a backend
KOKKOS_CHECK(OPTIONS CUDA_LAMBDA)
endif()

deal_ii_find_library(KOKKOS_CORE_LIBRARY
NAMES kokkoscore
HINTS ${KOKKOS_DIR}/lib ${Kokkos_DIR}/lib $ENV{Kokkos_DIR}/lib
PATH_SUFFIXES
lib${LIB_SUFFIX} lib64 lib
)

deal_ii_find_library(KOKKOS_CONTAINERS_LIBRARY
NAMES kokkoscontainers
HINTS ${KOKKOS_DIR}/lib ${Kokkos_DIR}/lib $ENV{Kokkos_DIR}/lib
PATH_SUFFIXES
lib${LIB_SUFFIX} lib64 lib
)
else()
set(Kokkos_FOUND FALSE)
if(Kokkos_FOUND)
# We need to disable SIMD vectorization for CUDA device code.
# Otherwise, nvcc compilers from version 9 on will emit an error message like:
# "[...] contains a vector, which is not supported in device code". We
# would like to set the variable in check_01_cpu_feature but at that point
# we don't know if CUDA support is enabled in Kokkos
if(Kokkos_ENABLE_CUDA)
set(DEAL_II_VECTORIZATION_WIDTH_IN_BITS 0)
# Require lambda support to use Kokkos as a backend
KOKKOS_CHECK(OPTIONS CUDA_LAMBDA)
endif()
endif()

set(_target Kokkos::kokkos)
process_feature(KOKKOS
LIBRARIES REQUIRED KOKKOS_CORE_LIBRARY KOKKOS_CONTAINERS_LIBRARY
INCLUDE_DIRS REQUIRED KOKKOS_INSTALL_INCLUDE_DIR
CXX_FLAGS OPTIONAL KOKKOS_COMPILE_FLAGS
LINKER_FLAGS OPTIONAL KOKKOS_EXTRA_LD_FLAGS
CLEAR KOKKOS_DIR KOKKOS_CORE_LIBRARY KOKKOS_CONTAINERS_LIBRARY
TARGETS REQUIRED _target
)
endif()

0 comments on commit 915f5ad

Please sign in to comment.