Skip to content

Commit d619604

Browse files
committed
[ENH] refactor of parallel hdf5 detection
1 parent 3fdab2d commit d619604

2 files changed

Lines changed: 53 additions & 49 deletions

File tree

CMakeLists.txt

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ include(ReplaceIdefixSource)
4242
include(AddIdefixSource)
4343
include(SetIdefixProperty)
4444
include(SetRequiredBuildSettingsForGCC8)
45+
include(CheckHdf5ParallelSupport)
4546

4647
#Idefix requires Cuda Lambdas (experimental)
4748
if(Kokkos_ENABLE_CUDA)
@@ -130,55 +131,8 @@ if(Idefix_HDF5)
130131
message(STATUS "Found HDF5 include directories: ${HDF5_INCLUDE_DIRS}")
131132
target_include_directories(idefix PUBLIC "${HDF5_INCLUDE_DIRS}")
132133
if(Idefix_MPI)
133-
include(CheckCSourceCompiles)
134-
135-
# Some CMake/HDF5 combinations do not reliably set HDF5_IS_PARALLEL
136-
# (e.g. when HDF5 is found via its CMake config package rather than the
137-
# FindHDF5 module). Combine metadata checks with a compile+link probe for
138-
# the MPI-IO symbols to reliably detect parallel HDF5.
139-
set(_idefix_hdf5_is_parallel FALSE)
140-
if(HDF5_IS_PARALLEL OR HDF5_C_IS_PARALLEL)
141-
set(_idefix_hdf5_is_parallel TRUE)
142-
endif()
143-
144-
if(NOT _idefix_hdf5_is_parallel AND DEFINED HDF5_C_COMPILER_EXECUTABLE)
145-
execute_process(
146-
COMMAND "${HDF5_C_COMPILER_EXECUTABLE}" -showconfig
147-
OUTPUT_VARIABLE _idefix_hdf5_showconfig
148-
ERROR_QUIET
149-
)
150-
if(_idefix_hdf5_showconfig MATCHES "Parallel HDF5:[ \t]*yes")
151-
set(_idefix_hdf5_is_parallel TRUE)
152-
endif()
153-
endif()
154-
155-
set(_idefix_saved_required_includes "${CMAKE_REQUIRED_INCLUDES}")
156-
set(_idefix_saved_required_libraries "${CMAKE_REQUIRED_LIBRARIES}")
157-
158-
set(CMAKE_REQUIRED_INCLUDES ${HDF5_INCLUDE_DIRS} ${MPI_C_INCLUDE_DIRS})
159-
set(CMAKE_REQUIRED_LIBRARIES ${_idefix_hdf5_link_items} MPI::MPI_C)
160-
check_c_source_compiles(
161-
"#include <mpi.h>
162-
#include <hdf5.h>
163-
int main(void) {
164-
hid_t plist = H5Pcreate(H5P_FILE_ACCESS);
165-
H5Pset_fapl_mpio(plist, MPI_COMM_WORLD, MPI_INFO_NULL);
166-
H5Pclose(plist);
167-
return 0;
168-
}"
169-
IDEFIX_HDF5_HAS_MPI_IO
170-
)
171-
172-
set(CMAKE_REQUIRED_INCLUDES "${_idefix_saved_required_includes}")
173-
set(CMAKE_REQUIRED_LIBRARIES "${_idefix_saved_required_libraries}")
174-
unset(_idefix_saved_required_includes)
175-
unset(_idefix_saved_required_libraries)
176-
177-
if(IDEFIX_HDF5_HAS_MPI_IO)
178-
set(_idefix_hdf5_is_parallel TRUE)
179-
endif()
180-
181-
if(NOT _idefix_hdf5_is_parallel)
134+
CheckHdf5ParallelSupport("${_idefix_hdf5_link_items}")
135+
if(NOT IDEFIX_HDF5_IS_PARALLEL)
182136
message(FATAL_ERROR "Parallel HDF5 required for Idefix_MPI but the found HDF5 library does not support it")
183137
endif()
184138
endif()
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
include(CheckCSourceCompiles)
2+
3+
# Check whether the HDF5 library found by find_package(HDF5) supports parallel
4+
# (MPI-IO) access, and store the result in IDEFIX_HDF5_IS_PARALLEL in the
5+
# caller's scope.
6+
#
7+
# Usage: CheckHdf5ParallelSupport(<hdf5_link_items>)
8+
# where <hdf5_link_items> is the list of HDF5 targets/libraries to link against.
9+
#
10+
# Some CMake/HDF5 combinations do not reliably set HDF5_IS_PARALLEL (e.g. when
11+
# HDF5 is found via its CMake config package rather than the FindHDF5 module).
12+
# Combine metadata checks with a compile+link probe for the MPI-IO symbols to
13+
# reliably detect parallel HDF5.
14+
function(CheckHdf5ParallelSupport hdf5_link_items)
15+
set(_idefix_hdf5_is_parallel FALSE)
16+
if(HDF5_IS_PARALLEL OR HDF5_C_IS_PARALLEL)
17+
set(_idefix_hdf5_is_parallel TRUE)
18+
endif()
19+
20+
if(NOT _idefix_hdf5_is_parallel AND DEFINED HDF5_C_COMPILER_EXECUTABLE)
21+
execute_process(
22+
COMMAND "${HDF5_C_COMPILER_EXECUTABLE}" -showconfig
23+
OUTPUT_VARIABLE _idefix_hdf5_showconfig
24+
ERROR_QUIET
25+
)
26+
if(_idefix_hdf5_showconfig MATCHES "Parallel HDF5:[ \t]*yes")
27+
set(_idefix_hdf5_is_parallel TRUE)
28+
endif()
29+
endif()
30+
31+
set(CMAKE_REQUIRED_INCLUDES ${HDF5_INCLUDE_DIRS} ${MPI_C_INCLUDE_DIRS})
32+
set(CMAKE_REQUIRED_LIBRARIES ${hdf5_link_items} MPI::MPI_C)
33+
check_c_source_compiles(
34+
"#include <mpi.h>
35+
#include <hdf5.h>
36+
int main(void) {
37+
hid_t plist = H5Pcreate(H5P_FILE_ACCESS);
38+
H5Pset_fapl_mpio(plist, MPI_COMM_WORLD, MPI_INFO_NULL);
39+
H5Pclose(plist);
40+
return 0;
41+
}"
42+
IDEFIX_HDF5_HAS_MPI_IO
43+
)
44+
45+
if(IDEFIX_HDF5_HAS_MPI_IO)
46+
set(_idefix_hdf5_is_parallel TRUE)
47+
endif()
48+
49+
set(IDEFIX_HDF5_IS_PARALLEL ${_idefix_hdf5_is_parallel} PARENT_SCOPE)
50+
endfunction()

0 commit comments

Comments
 (0)