HDFGroup / HDFGroup/hdf5

Parallel HDF5 requests MPI_Fortran even if unneeded if language Fortran enabled.

Open
#6,366 0 comments 0 reactions 1 assignee Claimed by @jhendersonHDF View on GitHub
Component - Build Component - Parallel
Dominant language
C
Stars
988
Forks
355
Avg merge
4d 2h
Merged PRs (30d)
12

Description

**Describe the bug**
I work on a program that uses a mixture of C and Fortran. Only the C side of the code interacts with HDF5 or MPI. As a result, we typically don't enable Fortran support in our MPI and HDF5 builds.

If I build our code with parallel HDF5 2.1.0 or 2.1.1, however, the call to `find_dependency(MPI QUIET REQUIRED)`, [[here]](https://github.com/HDFGroup/hdf5/blob/f010df9fe6de12113a099e9a42230015d5ba7175/config/install/hdf5-config.cmake.in#L100) will see that we have the Fortran language enabled, and attempt to load `MPI::MPI_Fortran`.

Here is a minimum working example that shows this behavior. To trigger a failure requires a build of MPI without Fortran enabled, such as OpenMPI with the `--enable-mpi-fortran=no` configure option.

CMakeLists.txt:
```cmake
cmake_minimum_required(VERSION 3.27)

project(TestProject LANGUAGES C Fortran)

find_package(HDF5 NAMES hdf5 REQUIRED COMPONENTS C)

add_executable(test
test.c
test.f90
)

set_target_properties(test PROPERTIES LINKER_LANGUAGE C)

target_link_libraries(test PRIVATE hdf5-shared)
```

test.c:
```c
#include "hdf5.h"

extern double return_1();

int main(void) {
double fortran_value = return_1();
printf("Fortran function returned: %f\n", fortran_value);

// Create HDF5 file
hid_t file_id = H5Fcreate("test.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);

H5Fclose(file_id);
}
```

test.f90:
```fortran
function return_1() result(val) bind(C, name="return_1")
use iso_c_binding
implicit none
real(c_double) :: val

val = 1.0d0
end function return_1
```

On my machine, I get the output:
```
CMake Error at ${BUILDSYSTEM}/cmake/cmake-4.3.1/share/cmake-4.3/Modules/FindPackageHandleStandardArgs.cmake:290 (message):
Could NOT find MPI (missing: MPI_Fortran_FOUND) (found version "3.1")
Call Stack (most recent call first):
${BUILDSYSTEM}/cmake/cmake-4.3.1/share/cmake-4.3/Modules/FindPackageHandleStandardArgs.cmake:654 (_FPHSA_FAILURE_MESSAGE)
${BUILDSYSTEM}/cmake/cmake-4.3.1/share/cmake-4.3/Modules/FindMPI.cmake:2006 (find_package_handle_standard_args)
${BUILDSYSTEM}/cmake/cmake-4.3.1/share/cmake-4.3/Modules/CMakeFindDependencyMacro.cmake:93 (find_package)
${BUILDSYSTEM}/cmake/cmake-4.3.1/share/cmake-4.3/Modules/CMakeFindDependencyMacro.cmake:125 (__find_dependency_common)
${HOME}/HDF5/cmake/hdf5-config.cmake:124 (find_dependency)
CMakeLists.txt:5 (find_package)
```

Replacing `find_dependency(MPI QUIET REQUIRED)` with `find_dependency(MPI QUIET REQUIRED COMPONENTS C)` in `hdf5-config.cmake.in` appears to fix it, but I don't know enough of HDF5's architecture to know if this will cause problems.

**Expected behavior**
I expect a non-Fortran build of HDF5 to not need `MPI_Fortran`.

**Platform**
- HDF5 commit f010df9fe6de12113a099e9a42230015d5ba7175
- Linux, RHEL 8
- Intel 2024.2.1 (`icx` + `ifort`)
- CMake 4.3.1 + Ninja 1.13.2
- Built with `cmake .. -G Ninja -DHDF5_ENABLE_PARALLEL=ON -DHDF5_BUILD_FORTRAN=OFF`
- OpenMPI 5.0.10 with `--enable-mpi-fortran=no`

We have also tested 2.1.1 and 2.1.0 on various other systems, such as Windows with MS-MPI, which does not provide Fortran bindings, and have had similar issues.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.