Skip to content

Commit

Permalink
CMake: add support for SUNDIALS3
Browse files Browse the repository at this point in the history
  • Loading branch information
mic84 committed Mar 13, 2019
1 parent b070fbd commit 40068ae
Show file tree
Hide file tree
Showing 12 changed files with 411 additions and 140 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
cmake_minimum_required (VERSION 3.5)
cmake_minimum_required (VERSION 3.13)

project (AMReX)


enable_language (C)
enable_language (CXX)
enable_language (Fortran)
Expand Down
2 changes: 2 additions & 0 deletions Docs/sphinx_documentation/source/BuildingAMReX.rst
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ below.
+------------------------------+-------------------------------------------------+-------------+-----------------+
| BLITZ_INSTALL_DIR | Path to Blitz installation directory | | user-defined |
+------------------------------+-------------------------------------------------+-------------+-----------------+
| ENABLE_SUNDIALS | Enable SUNDIALS3 interfaces | OFF | ON, OFF |
+------------------------------+-------------------------------------------------+-------------+-----------------+
.. raw:: latex

\end{center}
Expand Down
4 changes: 4 additions & 0 deletions Src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ if (ENABLE_SENSEI_INSITU)
include (Extern/SENSEI/CMakeLists.txt)
endif ()

if (ENABLE_SUNDIALS)
include(Extern/SUNDIALS3/CMakeLists.txt)
endif ()


#
# Here we generate AMReX_BuildInfo.cpp
Expand Down
32 changes: 32 additions & 0 deletions Src/Extern/SUNDIALS3/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
set(_sundials_components nvecserial;cvode;arkode)

if (ENABLE_OMP)
list(APPEND _sundials_components nvecopenmp)
endif ()

if (ENABLE_CUDA)
list(APPEND _sundials_components nveccuda)
endif ()

if (ENABLE_FORTRAN_INTERFACES)
add_sources(fnvector_serial.f90 fnvector_serial_fprefix.f90)
add_sources(fsunlinsol_dense.f90 fsunmat_dense.f90)

# Arkcode interfaces
add_sources(arkode_interface.f90 farkode.f90)

# CVODe interfaces
add_sources(cvode_interface.f90 fcvode.f90 )
endif ()

#
# We link to libraries and always include nvecserial (in case app code needs it)
#
find_package(SUNDIALS REQUIRED COMPONENTS ${_sundials_components})
foreach(_comp ${_sundials_components})
target_link_libraries(amrex PUBLIC SUNDIALS::${_comp})
endforeach ()
target_compile_definitions(amrex PUBLIC AMREX_USE_SUNDIALS_3x4x)



16 changes: 16 additions & 0 deletions Tools/CMake/AMReXConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ set(AMREX_BUILD_TYPE @CMAKE_BUILD_TYPE@)
#
set(AMREX_GIT_VERSION \"@AMREX_GIT_VERSION@\")

#
# Add AMReX modules to app code CMake
#
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "@CMAKE_INSTALL_PREFIX@/Tools/CMake/")

#
# Configuration options
#
Expand Down Expand Up @@ -50,6 +55,10 @@ set(AMREX_ENABLE_COMM_PROFILE @ENABLE_COMM_PROFILE@)
set(AMREX_ENABLE_BACKTRACE @ENABLE_BACKTRACE@)
set(AMREX_ENABLE_PROFPARSER @ENABLE_PROFPARSER@)

# SUNDIALS support
set(AMREX_ENABLE_SUNDIALS @ENABLE_SUNDIALS@)

# Find dependencies if needed
if (AMREX_ENABLE_3D_NODAL_MLMG)
# External libs
set(BLITZ_INSTALL_PREFIX @BLITZ_INSTALL_PREFIX@)
Expand All @@ -69,6 +78,13 @@ if (AMREX_ENABLE_3D_NODAL_MLMG)
endif ()
endif ()

if (AMREX_ENABLE_SUNDIALS)
include(CMakeFindDependencyMacro)
find_dependency(SUNDIALS COMPONENTS nvecserial cvode arkode REQUIRED )
#if (TARGET SUNDIALS::nvecserial)
message("TARGET EXISTS")
#endif ()
endif ()


# This two lines are suggested on the internet
Expand Down
6 changes: 6 additions & 0 deletions Tools/CMake/AMReX_Options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ else ()
set(ENABLE_3D_NODAL_MLMG OFF CACHE INTERNAL "Enable 3D nodal MLMG")
endif ()

#
# External packages
#
option(ENABLE_SUNDIALS "Enable SUNDIALS3 interfaces" OFF)
print_option(ENABLE_SUNDIALS)

#
# This options are paths to external libraries installation directories
#
Expand Down
134 changes: 134 additions & 0 deletions Tools/CMake/FindSUNDIALS.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#[=======================================================================[:
FindSUNDIALS
-------
Finds the SUNDIALS libraries.
Imported Targets
^^^^^^^^^^^^^^^^
This module provides the following imported targets, if found:
``SUNDIALS::<comp>``
The <comp> library
where <comp> is the selected component.
If no component is specified, this module will search for ``nvecserial`` only.
Result Variables
^^^^^^^^^^^^^^^^
This will define the following variables:
``SUNDIALS_FOUND``
True if all the selected components have been found.
``SUNDIALS_VERSION``
The version of the SUNDIALS library which was found.
``SUNDIALS_<COMP>_INCLUDE_DIRS``
Include directories needed to use component <COMP>.
``SUNDIALS_<COMP>_LIBRARY``
Libraries needed to link to component <COMP>.
#]=======================================================================]

#
# Find package version
#
set(_version_file_name sundials_config.h)

find_path(_version_file_path
NAMES "${_version_file_name}"
PATH_SUFFIXES include;sundials )

if (_version_file_path)
file(STRINGS "${_version_file_path}/${_version_file_name}"
_strings_with_version_regex REGEX "SUNDIALS_VERSION")
list(GET _strings_with_version_regex 0 _version_string)
string(REGEX MATCHALL "[0-9]" _version "${_version_string}")
string(REPLACE ";" "." SUNDIALS_VERSION "${_version}")
endif ()

#
# Include directory is only the top level one, i.e. "include"
# Find path by using directory "sundials" as reference
#
find_path(_sundials_include_path NAMES sundials PATH_SUFFIXES include)

#
#
#
include(FindPackageHandleStandardArgs)

#
# Valid components
#
set(_valid_components
nvecserial
nvecparallel
nvecopenmp
nvecopenmpdev
nveccuda
cvode
arkode
)

#
# Search for a default library (nvecserial) or for all the
# required components
#
if (NOT SUNDIALS_FIND_COMPONENTS)
set(_sundials_findlist nvecserial )
else ()
set(_sundials_findlist ${SUNDIALS_FIND_COMPONENTS})
endif ()

list(REMOVE_DUPLICATES _sundials_findlist)

#
# Setup one imported target per component
#
set(_SUNDIALS_REQUIRED_VARS)
foreach(_comp IN LISTS _sundials_findlist)

string( TOLOWER "${_comp}" _comp_lower )
string( TOUPPER "${_comp}" _comp_upper )

# Check whether component is in valid list
if (NOT (${_comp_lower} IN_LIST _valid_components))
message(FATAL_ERROR "Invalid component ${_comp_lower}")
endif ()

# Include path is always the path to the top-level "include" directory in the install tree
# App codes should include headers by using relative paths
set(SUNDIALS_${_comp_upper}_INCLUDE_DIRS ${_sundials_include_path})
find_library(SUNDIALS_${_comp_upper}_LIBRARY NAMES sundials_${_comp_lower} PATH_SUFFIXES lib)

find_package_handle_standard_args(SUNDIALS_${_comp_upper}
REQUIRED_VARS
SUNDIALS_${_comp_upper}_LIBRARY
SUNDIALS_${_comp_upper}_INCLUDE_DIRS
VERSION_VAR SUNDIALS_VERSION
)

mark_as_advanced(SUNDIALS_${_comp_upper}_LIBRARY SUNDIALS_${_comp_upper}_INCLUDE_DIRS)

list(APPEND _SUNDIALS_REQUIRED_VARS "SUNDIALS_${_comp_upper}_FOUND")

# Create imported target
set(_target SUNDIALS::${_comp_lower})
if (SUNDIALS_${_comp_upper}_FOUND AND NOT TARGET ${_target})
add_library(${_target} UNKNOWN IMPORTED)
set_target_properties(${_target} PROPERTIES
IMPORTED_LOCATION "${SUNDIALS_${_comp_upper}_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${SUNDIALS_${_comp_upper}_INCLUDE_DIRS}"
)
endif ()

endforeach()

#
# Set
#
find_package_handle_standard_args(SUNDIALS
REQUIRED_VARS ${_SUNDIALS_REQUIRED_VARS}
VERSION_VAR SUNDIALS_VERSION
)
2 changes: 1 addition & 1 deletion Tutorials/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
set ( AMREX_TUTORIALS_DIR ${CMAKE_CURRENT_LIST_DIR} )

set ( AMREX_TUTORIALS_SUBDIRS Amr Basic ) # For now only Amr
set ( AMREX_TUTORIALS_SUBDIRS Amr Basic CVODE) # For now only Amr

prepend ( AMREX_TUTORIALS_SUBDIRS ${AMREX_TUTORIALS_DIR})

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#
# Does not work if amrex is built in shared mode
#
if (NOT (ENABLE_SUNDIALS) )
return()
endif ()

set( EXENAME "sundials3_cvode_cpp_ex1_serial.exe" )

add_executable( ${EXENAME} EXCLUDE_FROM_ALL "" )

target_sources( ${EXENAME} PRIVATE
SetIC.f90
main.cpp
myfunc_F.H
)

set_target_properties( ${EXENAME} PROPERTIES
INCLUDE_DIRECTORIES
"${CMAKE_CURRENT_LIST_DIR};${CMAKE_CURRENT_BINARY_DIR}/mod_files"
Fortran_MODULE_DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}/mod_files
RUNTIME_OUTPUT_DIRECTORY
${CMAKE_CURRENT_BINARY_DIR} )

target_link_libraries( ${EXENAME} amrex )

#
# Find input files
#
file( GLOB_RECURSE inputs LIST_DIRECTORIES false ${CMAKE_CURRENT_LIST_DIR}/input* )

#
# Copy input files to corresponding build dir
#
file( COPY ${inputs} DESTINATION ${CMAKE_CURRENT_BINARY_DIR} )

#
# Add to the "tutorial" target
#
add_tutorial(${EXENAME})
8 changes: 0 additions & 8 deletions Tutorials/CVODE/SUNDIALS3_cppversion/EX1_SERIAL_NVEC/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,11 @@
#include <AMReX_MultiFab.H>
#include <AMReX_Print.H>
#include <AMReX_PlotFileUtil.H>
#if !defined(BL_NO_FORT)
#include <AMReX_BaseFab_f.H>
#endif

#include <AMReX_BLFort.H>

#include <cvode/cvode.h> /* prototypes for CVODE fcts., consts. */
#include <sunlinsol/sunlinsol_spgmr.h> /* access to SPGMR SUNLinearSolver */
#include <cvode/cvode_spils.h> /* access to CVSpils interface */
#include <sundials/sundials_types.h> /* definition of type realtype */
#include <sundials/sundials_math.h> /* definition of ABS and EXP */

#include <nvector/nvector_cuda.h>
#include <nvector/nvector_serial.h>
#include "myfunc_F.H"

Expand Down
43 changes: 43 additions & 0 deletions Tutorials/CVODE/SUNDIALS3_finterface/EX1/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#
# Does not work if amrex is built in shared mode
#
if (NOT (ENABLE_SUNDIALS AND ENABLE_SUNDIALS_CVODE) )
return()
endif ()

set( EXENAME "sundials3_cvode_finterface_ex1.exe" )

add_executable( ${EXENAME} EXCLUDE_FROM_ALL "" )

target_sources( ${EXENAME} PRIVATE
integrate_ode.f90
main.cpp
myfunc_F.H
ode_mod.f90
)

set_target_properties( ${EXENAME} PROPERTIES
INCLUDE_DIRECTORIES
"${CMAKE_CURRENT_LIST_DIR};${CMAKE_CURRENT_BINARY_DIR}/mod_files"
Fortran_MODULE_DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}/mod_files
RUNTIME_OUTPUT_DIRECTORY
${CMAKE_CURRENT_BINARY_DIR} )

target_link_libraries( ${EXENAME} amrex )

#
# Find input files
#
file( GLOB_RECURSE inputs LIST_DIRECTORIES false ${CMAKE_CURRENT_LIST_DIR}/input* )

#
# Copy input files to corresponding build dir
#
file( COPY ${inputs} DESTINATION ${CMAKE_CURRENT_BINARY_DIR} )

#
# Add to the "tutorial" target
#
add_tutorial(${EXENAME})

Loading

0 comments on commit 40068ae

Please sign in to comment.