Skip to content

Commit

Permalink
FindHDF5: correct HDF5-MPI
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed May 3, 2024
1 parent db00a5b commit 261a440
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
47 changes: 36 additions & 11 deletions cmake/FindHDF5.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ set(${outvar} ${_v} PARENT_SCOPE)

endfunction(pop_flag)

macro(find_mpi)
# non-cache set by FindMPI are not visible outside function -- need macro just to see within that function
set(mpi_comp C)
if(Fortran IN_LIST HDF5_FIND_COMPONENTS)
list(APPEND mpi_comp Fortran)
endif()
if(HDF5_FIND_REQUIRED)
find_package(MPI COMPONENTS ${mpi_comp} REQUIRED)
else()
find_package(MPI COMPONENTS ${mpi_comp})
endif()

endmacro(find_mpi)


macro(detect_config)

Expand Down Expand Up @@ -121,8 +135,8 @@ check_symbol_exists(H5_HAVE_PARALLEL ${h5_conf} HDF5_HAVE_PARALLEL)
set(HDF5_parallel_FOUND false)

if(HDF5_HAVE_PARALLEL)
find_mpi()
if(NOT MPI_FOUND)
message(WARNING "user CMakeLists.txt must do find_package(MPI) if using parallel HDF5")
return()
endif()

Expand Down Expand Up @@ -638,10 +652,11 @@ endfunction(hdf5_c_wrap)

function(check_c_links)

list(INSERT CMAKE_REQUIRED_LIBRARIES 0 ${HDF5_C_LIBRARIES})
list(PREPEND CMAKE_REQUIRED_LIBRARIES ${HDF5_C_LIBRARIES})
set(CMAKE_REQUIRED_INCLUDES ${HDF5_C_INCLUDE_DIR})

if(HDF5_parallel_FOUND)
find_mpi()

list(APPEND CMAKE_REQUIRED_INCLUDES ${MPI_C_INCLUDE_DIRS})
list(APPEND CMAKE_REQUIRED_LIBRARIES ${MPI_C_LIBRARIES})
Expand Down Expand Up @@ -687,10 +702,11 @@ endfunction(check_c_links)

function(check_fortran_links)

list(INSERT CMAKE_REQUIRED_LIBRARIES 0 ${HDF5_Fortran_LIBRARIES} ${HDF5_C_LIBRARIES})
list(PREPEND CMAKE_REQUIRED_LIBRARIES ${HDF5_Fortran_LIBRARIES} ${HDF5_C_LIBRARIES})
set(CMAKE_REQUIRED_INCLUDES ${HDF5_Fortran_INCLUDE_DIR} ${HDF5_C_INCLUDE_DIR})

if(HDF5_parallel_FOUND)
find_mpi()

list(APPEND CMAKE_REQUIRED_INCLUDES ${MPI_Fortran_INCLUDE_DIRS})
list(APPEND CMAKE_REQUIRED_LIBRARIES ${MPI_Fortran_LIBRARIES})
Expand Down Expand Up @@ -727,6 +743,15 @@ endfunction(check_fortran_links)

function(check_hdf5_link)

# HDF5 bug #3663 for HDF5 1.14.2, 1.14.3, ...?
# https://github.com/HDFGroup/hdf5/issues/3663
if(WIN32 AND CMAKE_Fortran_COMPILER_ID MATCHES "^Intel")
if(HDF5_VERSION MATCHES "1.14.[2-3]")
message(VERBOSE "FindHDF5: applying workaround for HDF5 bug #3663 with Intel oneAPI on Windows")
list(APPEND CMAKE_REQUIRED_LIBRARIES shlwapi)
endif()
endif()

if(NOT HDF5_C_FOUND)
return()
endif()
Expand Down Expand Up @@ -774,10 +799,10 @@ endif()

set(hdf5_lsuf lib hdf5/lib) # need explicit "lib" for self-built HDF5
if(NOT HDF5_ROOT)
list(INSERT hdf5_lsuf 0 hdf5/openmpi hdf5/mpich) # Ubuntu
list(INSERT hdf5_lsuf 0 openmpi/lib mpich/lib) # CentOS
list(PREPEND hdf5_lsuf hdf5/openmpi hdf5/mpich) # Ubuntu
list(PREPEND hdf5_lsuf openmpi/lib mpich/lib) # CentOS
if(NOT parallel IN_LIST HDF5_FIND_COMPONENTS)
list(INSERT hdf5_lsuf 0 hdf5/serial) # Ubuntu
list(PREPEND hdf5_lsuf hdf5/serial) # Ubuntu
endif()
endif()

Expand All @@ -792,13 +817,13 @@ else()
endif()

# Ubuntu
list(INSERT hdf5_isuf 0 hdf5/openmpi hdf5/mpich)
list(INSERT hdf5_msuf 0 hdf5/openmpi hdf5/mpich)
list(PREPEND hdf5_isuf hdf5/openmpi hdf5/mpich)
list(PREPEND hdf5_msuf hdf5/openmpi hdf5/mpich)

if(NOT parallel IN_LIST HDF5_FIND_COMPONENTS)
# Ubuntu
list(INSERT hdf5_isuf 0 hdf5/serial)
list(INSERT hdf5_msuf 0 hdf5/serial)
list(PREPEND hdf5_isuf hdf5/serial)
list(PREPEND hdf5_msuf hdf5/serial)
endif()

if(CMAKE_SYSTEM_PROCESSOR MATCHES "(x86_64|AMD64)")
Expand All @@ -810,7 +835,7 @@ endif()
if(NOT HDF5_ROOT AND CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
# CentOS paths
if(parallel IN_LIST HDF5_FIND_COMPONENTS)
list(INSERT hdf5_msuf 0 gfortran/modules/openmpi gfortran/modules/mpich)
list(PREPEND hdf5_msuf gfortran/modules/openmpi gfortran/modules/mpich)
else()
list(APPEND hdf5_msuf gfortran/modules)
endif()
Expand Down
6 changes: 4 additions & 2 deletions scripts/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ if(hdf5_parallel)
set(MPI_ROOT $ENV{MPI_ROOT})
endif()

set(MPI_DETERMINE_LIBRARY_VERSION true)
find_package(MPI COMPONENTS C REQUIRED)
message(STATUS "MPI_C: ${MPI_C_LIBRARY_VERSION_STRING}")

if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND MPI_ROOT)
set(ld_path $ENV{LD_LIBRARY_PATH})
cmake_path(CONVERT "${ld_path}" TO_CMAKE_PATH_LIST ld_path NORMALIZE)
cmake_path(CONVERT "${MPI_ROOT}" TO_CMAKE_PATH_LIST MPI_ROOT NORMALIZE)

find_package(MPI COMPONENTS C REQUIRED)

include(${PROJECT_SOURCE_DIR}/../cmake/check_mpi.cmake)
check_mpi_version()

Expand Down

0 comments on commit 261a440

Please sign in to comment.