Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CMake: Use LAPACK imported target #542

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cmake/SUNDIALSConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ if("@ENABLE_KOKKOS_KERNELS@" AND NOT TARGET Kokkos::kokkoskernels)
find_dependency(KokkosKernels PATHS "@KokkosKernels_DIR@")
endif()

if("@ENABLE_LAPACK@" AND NOT TARGET LAPACK::LAPACK)
find_dependency(LAPACK)
endif()

if("@ENABLE_PETSC@" AND NOT TARGET SUNDIALS::PETSC)
add_library(SUNDIALS::PETSC INTERFACE IMPORTED)
target_link_libraries(SUNDIALS::PETSC INTERFACE "@PETSC_LIBRARIES@")
Expand Down
4 changes: 2 additions & 2 deletions cmake/SundialsBuildOptionsPost.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,14 @@ list(APPEND SUNDIALS_BUILD_LIST "BUILD_SUNLINSOL_KOKKOSDENSE")
sundials_option(
BUILD_SUNLINSOL_LAPACKBAND BOOL
"Build the SUNLINSOL_LAPACKBAND module (requires LAPACK)" ON
DEPENDS_ON ENABLE_LAPACK LAPACK_WORKS
DEPENDS_ON ENABLE_LAPACK
ADVANCED)
list(APPEND SUNDIALS_BUILD_LIST "BUILD_SUNLINSOL_LAPACKBAND")

sundials_option(
BUILD_SUNLINSOL_LAPACKDENSE BOOL
"Build the SUNLINSOL_LAPACKDENSE module (requires LAPACK)" ON
DEPENDS_ON ENABLE_LAPACK LAPACK_WORKS
DEPENDS_ON ENABLE_LAPACK
ADVANCED)
list(APPEND SUNDIALS_BUILD_LIST "BUILD_SUNLINSOL_LAPACKDENSE")

Expand Down
117 changes: 37 additions & 80 deletions cmake/tpl/SundialsLapack.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -26,41 +26,34 @@
# Section 1: Include guard
# -----------------------------------------------------------------------------

if(NOT DEFINED SUNDIALS_LAPACK_INCLUDED)
set(SUNDIALS_LAPACK_INCLUDED)
else()
return()
endif()
include_guard(GLOBAL)

# -----------------------------------------------------------------------------
# Section 2: Check to make sure options are compatible
# -----------------------------------------------------------------------------

# LAPACK does not support extended precision
if(ENABLE_LAPACK AND SUNDIALS_PRECISION MATCHES "EXTENDED")
print_error("LAPACK is not compatible with ${SUNDIALS_PRECISION} precision")
message(
FATAL_ERROR "LAPACK is not compatible with ${SUNDIALS_PRECISION} precision")
endif()

# -----------------------------------------------------------------------------
# Section 3: Find the TPL
# -----------------------------------------------------------------------------

# If LAPACK libraries are undefined, try to find them.
if(NOT LAPACK_LIBRARIES)
find_package(LAPACK REQUIRED)
endif()
find_package(LAPACK REQUIRED)

# If we have the LAPACK libraries, display progress message.
if(LAPACK_LIBRARIES)
message(STATUS "Looking for LAPACK libraries... OK")
set(LAPACK_FOUND TRUE)
# get path to LAPACK library to use in generated makefiles for examples, if
# LAPACK_LIBRARIES contains multiple items only use the path of the first entry
list(LENGTH LAPACK_LIBRARIES len)
if(len EQUAL 1)
get_filename_component(LAPACK_LIBRARY_DIR ${LAPACK_LIBRARIES} PATH)
else()
message(STATUS "Looking for LAPACK libraries... FAILED")
set(LAPACK_FOUND FALSE)
list(GET LAPACK_LIBRARIES 0 TMP_LAPACK_LIBRARIES)
get_filename_component(LAPACK_LIBRARY_DIR ${TMP_LAPACK_LIBRARIES} PATH)
endif()

message(STATUS "LAPACK_LIBRARIES: ${LAPACK_LIBRARIES}")

# -----------------------------------------------------------------------------
# Section 4: Test the TPL
# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -302,33 +295,17 @@ if(NEED_FORTRAN_NAME_MANGLING)

endif()

# If we have the LAPACK libraries, determine if they work.
if(LAPACK_LIBRARIES AND (NOT LAPACK_WORKS))
# Create the LapackTest directory
set(LapackTest_DIR ${PROJECT_BINARY_DIR}/LapackTest)
file(MAKE_DIRECTORY ${LapackTest_DIR})
# Try building a simple test
if(NOT LAPACK_WORKS)

# Create a CMakeLists.txt file
file(
WRITE ${LapackTest_DIR}/CMakeLists.txt
"CMAKE_MINIMUM_REQUIRED(VERSION ${CMAKE_VERSION})\n"
"PROJECT(ltest C)\n"
"SET(CMAKE_VERBOSE_MAKEFILE ON)\n"
"SET(CMAKE_BUILD_TYPE \"${CMAKE_BUILD_TYPE}\")\n"
"SET(CMAKE_C_COMPILER \"${CMAKE_C_COMPILER}\")\n"
"SET(CMAKE_C_STANDARD \"${CMAKE_C_STANDARD}\")\n"
"SET(CMAKE_C_FLAGS \"${CMAKE_C_FLAGS}\")\n"
"SET(CMAKE_C_FLAGS_RELEASE \"${CMAKE_C_FLAGS_RELEASE}\")\n"
"SET(CMAKE_C_FLAGS_DEBUG \"${CMAKE_C_FLAGS_DEBUG}\")\n"
"SET(CMAKE_C_FLAGS_RELWITHDEBUGINFO \"${CMAKE_C_FLAGS_RELWITHDEBUGINFO}\")\n"
"SET(CMAKE_C_FLAGS_MINSIZE \"${CMAKE_C_FLAGS_MINSIZE}\")\n"
"ADD_EXECUTABLE(ltest ltest.c)\n"
"TARGET_LINK_LIBRARIES(ltest ${LAPACK_LIBRARIES})\n")

# Create a C source file which calls a Blas function (dcopy) and an Lapack
# function (dgetrf)
message(CHECK_START "Testing LAPACK")

# Create the test directory
set(LAPACK_TEST_DIR ${PROJECT_BINARY_DIR}/LAPACK_TEST)

# Create a C source file calling a BLAS (dcopy) and LAPACK (dgetrf) function
file(
WRITE ${LapackTest_DIR}/ltest.c
WRITE ${LAPACK_TEST_DIR}/test.c
"${LAPACK_MANGLE_MACRO1}\n"
"#define dcopy_f77 SUNDIALS_LAPACK_FUNC(dcopy, DCOPY)\n"
"#define dgetrf_f77 SUNDIALS_LAPACK_FUNC(dgetrf, DGETRF)\n"
Expand All @@ -340,49 +317,29 @@ if(LAPACK_LIBRARIES AND (NOT LAPACK_WORKS))
"double y=1.0;\n"
"dcopy_f77(&n, &x, &n, &y, &n);\n"
"dgetrf_f77(&n, &n, &x, &n, &n, &n);\n"
"return(0);\n"
"return 0;\n"
"}\n")

# Attempt to build and link the "ltest" executable
# Attempt to build and link the test executable, pass --debug-trycompile to
# the cmake command to save build files for debugging
try_compile(
COMPILE_OK ${LapackTest_DIR}
${LapackTest_DIR} ltest
COMPILE_OK ${LAPACK_TEST_DIR}
${LAPACK_TEST_DIR}/test.c
LINK_LIBRARIES LAPACK::LAPACK
OUTPUT_VARIABLE COMPILE_OUTPUT)

# To ensure we do not use stuff from the previous attempts, we must remove the
# CMakeFiles directory.
file(REMOVE_RECURSE ${LapackTest_DIR}/CMakeFiles)

# Process test result
# Check the result
if(COMPILE_OK)
message(STATUS "Checking if LAPACK works with SUNDIALS... OK")
set(LAPACK_WORKS
TRUE
CACHE BOOL "LAPACK works with SUNDIALS as configured" FORCE)

# get path to LAPACK library to use in generated makefiles for examples, if
# LAPACK_LIBRARIES contains multiple items only use the path of the first
# entry
list(LENGTH LAPACK_LIBRARIES len)
if(len EQUAL 1)
get_filename_component(LAPACK_LIBRARY_DIR ${LAPACK_LIBRARIES} PATH)
else()
list(GET LAPACK_LIBRARIES 0 TMP_LAPACK_LIBRARIES)
get_filename_component(LAPACK_LIBRARY_DIR ${TMP_LAPACK_LIBRARIES} PATH)
endif()
else(COMPILE_OK)
set(LAPACK_WORKS
FALSE
CACHE BOOL "LAPACK does not work with SUNDIALS as configured" FORCE)
message(STATUS "Checking if LAPACK works with SUNDIALS... FAILED")
message(STATUS "Check output: ")
message("${COMPILE_OUTPUT}")
message(FATAL_ERROR "SUNDIALS interface to LAPACK is not functional.")
message(CHECK_PASS "success")
else()
message(CHECK_FAIL "failed")
file(WRITE ${LAPACK_TEST_DIR}/compile.out "${COMPILE_OUTPUT}")
message(
FATAL_ERROR
"Could not compile LAPACK test. Check output in ${LAPACK_TEST_DIR}/compile.out"
)
endif()

elseif(LAPACK_LIBRARIES AND LAPACK_WORKS)
message(
STATUS
"Skipped LAPACK tests, assuming LAPACK works with SUNDIALS. Set LAPACK_WORKS=FALSE to (re)run compatibility test."
)
else()
message(STATUS "Skipped LAPACK test. Set LAPACK_WORKS=FALSE to test.")
endif()
52 changes: 23 additions & 29 deletions cmake/tpl/SundialsTPL.cmake.template
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@
# Section 1: Include guard
# -----------------------------------------------------------------------------

if(NOT DEFINED SUNDIALS_<TPL>_INCLUDED)
set(SUNDIALS_<TPL>_INCLUDED)
else()
return()
endif()
include_guard(GLOBAL)

# -----------------------------------------------------------------------------
# Section 2: Check to make sure options are compatible
Expand All @@ -46,38 +42,36 @@ find_package(<TPL> REQUIRED)
# Section 4: Test the TPL
# -----------------------------------------------------------------------------

if(<TPL>_FOUND AND (NOT <TPL>_WORKS))
# Do any checks which don't require compilation first.
# Do any checks which don't require compilation first.

# Create the <TPL>_TEST directory
set(<TPL>_TEST_DIR ${PROJECT_BINARY_DIR}/<TPL>_TEST)
file(MAKE_DIRECTORY ${<TPL>_TEST_DIR})
# Try building a simple test
if(NOT <TPL>_WORKS)

# Create a CMakeLists.txt file
file(WRITE ${<TPL>_TEST_DIR}/CMakeLists.txt "")
message(CHECK_START "Testing <TPL>")

# Create a C source file
file(WRITE ${<TPL>_TEST_DIR}/ltest.c "")
# Create the test directory
set(<TPL>_TEST_DIR ${PROJECT_BINARY_DIR}/<TPL>_TEST)

# To ensure we do not use stuff from the previous attempts,
# we must remove the CMakeFiles directory.
file(REMOVE_RECURSE ${<TPL>_TEST_DIR}/CMakeFiles)
# Create a C source file
file(WRITE ${<TPL>_TEST_DIR}/test.c
"int main(void) {\n"
"return 0;\n"
"}\n")

# Attempt to build and link the "ltest" executable
try_compile(COMPILE_OK ${<TPL>_TEST_DIR} ${<TPL>_TEST_DIR} ltest
OUTPUT_VARIABLE COMPILE_OUTPUT)
# Attempt to build and link the test executable, pass --debug-trycompile to
# the cmake command to save build files for debugging
try_compile(COMPILE_OK ${<TPL>_TEST_DIR} ${<TPL>_TEST_DIR}/test.c
LINK_LIBRARIES <TPL target> OUTPUT_VARIABLE COMPILE_OUTPUT)

# Process test result
# Check the result
if(COMPILE_OK)
message(STATUS "Checking if <TPL> works with SUNDIALS... OK")
set(<TPL>_WORKS TRUE CACHE BOOL "<TPL> works with SUNDIALS as configured" FORCE)
message(CHECK_PASS "success")
else()
message(STATUS "Checking if <TPL> works with SUNDIALS... FAILED")
message(STATUS "Check output: ")
message("${COMPILE_OUTPUT}")
message(FATAL_ERROR "SUNDIALS interface to <TPL> is not functional.")
message(CHECK_FAIL "failed")
file(WRITE ${<TPL>_TEST_DIR}/compile.out "${COMPILE_OUTPUT}")
message(FATAL_ERROR "Could not compile <TPL> test. Check output in ${<TPL>_TEST_DIR}/compile.out")
endif()

elseif(<TPL>_FOUND AND <TPL>_WORKS)
message(STATUS "Skipped <TPL> tests, assuming <TPL> works with SUNDIALS.")
else()
message(STATUS "Skipped <TPL> test. Set <TPL>_WORKS=FALSE to test.")
endif()
4 changes: 2 additions & 2 deletions doc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ DIRS = arkode/guide arkode/examples \
kinsol/guide

# prefix with desired command to create unique dependencies/targets
LATEXDIRS = $(DIRS:%=latexpdf-%)
LATEXDIRS = $(DIRS:%=latexpdf-%) latexpdf-install_guide
HTMLDIRS = $(DIRS:%=html-%) html-superbuild
CLEANDIRS = $(DIRS:%=clean-%) clean-superbuild
CLEANDIRS = $(DIRS:%=clean-%) clean-superbuild clean-install_guide

latexpdf: $(LATEXDIRS)
$(LATEXDIRS):
Expand Down
3 changes: 0 additions & 3 deletions examples/arkode/F2003_serial/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,6 @@ if(BUILD_SUNLINSOL_LAPACKDENSE)
set(SUNLINSOLLAPACK_LIBS sundials_sunlinsollapackdense
sundials_fsunlinsollapackdense_mod)

# LAPACK libraries
list(APPEND SUNLINSOLLAPACK_LIBS ${LAPACK_LIBRARIES})

foreach(example_tuple ${FARKODE_examples_LAPACK})

# parse the example tuple
Expand Down
3 changes: 0 additions & 3 deletions examples/cvode/F2003_serial/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,6 @@ if(BUILD_SUNLINSOL_LAPACKBAND AND BUILD_SUNLINSOL_LAPACKDENSE)
set(SUNLINSOLLAPACK_LIBS sundials_sunlinsollapackdense
sundials_fsunlinsollapackdense_mod)

# LAPACK libraries
list(APPEND SUNLINSOLLAPACK_LIBS ${LAPACK_LIBRARIES})

foreach(example_tuple ${FCVODE_examples_LAPACK})

# parse the example tuple
Expand Down
3 changes: 0 additions & 3 deletions examples/cvode/serial/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,6 @@ if(BUILD_SUNLINSOL_LAPACKBAND AND BUILD_SUNLINSOL_LAPACKDENSE)
set(SUNLINSOLLAPACK_LIBS sundials_sunlinsollapackband
sundials_sunlinsollapackdense)

# LAPACK libraries
list(APPEND SUNLINSOLLAPACK_LIBS ${LAPACK_LIBRARIES})

foreach(example_tuple ${CVODE_examples_BL})

# parse the example tuple
Expand Down
3 changes: 0 additions & 3 deletions examples/cvodes/serial/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,6 @@ if(BUILD_SUNLINSOL_LAPACKBAND AND BUILD_SUNLINSOL_LAPACKDENSE)
set(SUNLINSOLLAPACK_LIBS sundials_sunlinsollapackband
sundials_sunlinsollapackdense)

# LAPACK libraries
list(APPEND SUNLINSOLLAPACK_LIBS ${LAPACK_LIBRARIES})

foreach(example_tuple ${CVODES_examples_BL})

# parse the example tuple
Expand Down
3 changes: 0 additions & 3 deletions examples/ida/serial/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,6 @@ if(BUILD_SUNLINSOL_LAPACKBAND AND BUILD_SUNLINSOL_LAPACKDENSE)
set(SUNLINSOLLAPACK_LIBS sundials_sunlinsollapackband
sundials_sunlinsollapackdense)

# LAPACK libraries
list(APPEND SUNLINSOLLAPACK_LIBS ${LAPACK_LIBRARIES})

foreach(example_tuple ${IDA_examples_BL})

# parse the example tuple
Expand Down
3 changes: 0 additions & 3 deletions examples/idas/serial/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,6 @@ if(BUILD_SUNLINSOL_LAPACKBAND AND BUILD_SUNLINSOL_LAPACKDENSE)
set(SUNLINSOLLAPACK_LIBS sundials_sunlinsollapackband
sundials_sunlinsollapackdense)

# LAPACK libraries
list(APPEND SUNLINSOLLAPACK_LIBS ${LAPACK_LIBRARIES})

foreach(example_tuple ${IDAS_examples_BL})

# parse the example tuple
Expand Down
3 changes: 0 additions & 3 deletions examples/kinsol/serial/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ if(BUILD_SUNLINSOL_LAPACKBAND AND BUILD_SUNLINSOL_LAPACKDENSE)
set(SUNLINSOLLAPACK_LIBS sundials_sunlinsollapackband
sundials_sunlinsollapackdense)

# LAPACK libraries
list(APPEND SUNLINSOLLAPACK_LIBS ${LAPACK_LIBRARIES})

foreach(example_tuple ${KINSOL_examples_BL})

# parse the example tuple
Expand Down
5 changes: 2 additions & 3 deletions examples/sunlinsol/lapackband/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ foreach(example_tuple ${sunlinsol_lapackband_examples})
set_target_properties(${example} PROPERTIES FOLDER "Examples")

# libraries to link against
target_link_libraries(
${example} sundials_nvecserial sundials_sunmatrixband
sundials_sunlinsollapackband ${LAPACK_LIBRARIES} ${EXE_EXTRA_LINK_LIBS})
target_link_libraries(${example} sundials_nvecserial sundials_sunmatrixband
sundials_sunlinsollapackband ${EXE_EXTRA_LINK_LIBS})
endif()

# check if example args are provided and set the test name
Expand Down
2 changes: 1 addition & 1 deletion examples/sunlinsol/lapackdense/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ foreach(example_tuple ${sunlinsol_lapackdense_examples})
# libraries to link against
target_link_libraries(
${example} sundials_nvecserial sundials_sunmatrixdense
sundials_sunlinsollapackdense ${LAPACK_LIBRARIES} ${EXE_EXTRA_LINK_LIBS})
sundials_sunlinsollapackdense ${EXE_EXTRA_LINK_LIBS})
endif()

# check if example args are provided and set the test name
Expand Down
2 changes: 1 addition & 1 deletion src/sunlinsol/lapackband/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ sundials_add_library(
INCLUDE_SUBDIR sunlinsol
LINK_LIBRARIES PUBLIC sundials_core
OBJECT_LIBRARIES
LINK_LIBRARIES PUBLIC sundials_sunmatrixband "${LAPACK_LIBRARIES}"
LINK_LIBRARIES PUBLIC sundials_sunmatrixband LAPACK::LAPACK
OUTPUT_NAME sundials_sunlinsollapackband
VERSION ${sunlinsollib_VERSION}
SOVERSION ${sunlinsollib_SOVERSION})
Expand Down
2 changes: 1 addition & 1 deletion src/sunlinsol/lapackdense/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ sundials_add_library(
INCLUDE_SUBDIR sunlinsol
LINK_LIBRARIES PUBLIC sundials_core
OBJECT_LIBRARIES
LINK_LIBRARIES PUBLIC sundials_sunmatrixdense "${LAPACK_LIBRARIES}"
LINK_LIBRARIES PUBLIC sundials_sunmatrixdense LAPACK::LAPACK
OUTPUT_NAME sundials_sunlinsollapackdense
VERSION ${sunlinsollib_VERSION}
SOVERSION ${sunlinsollib_SOVERSION})
Expand Down
Loading