How to try_run MPI code

All,

I recently found that Open MPI 5.0.0 has an issue with Intel Fortran and -init=snan (at least for us, see Crash on MPI_Init with Open MPI 5.0.0, Intel Fortran, and -init=snan · Issue #12113 · open-mpi/ompi · GitHub). So far I can get away with using Open MPI 4.1.6 and all is well, but at some point, we might need Open MPI 5.0.0 for something … :man_shrugging:

My first thought was to try and detect the Open MPI version by querying ompi_info or something like that but that got pretty convoluted. So, I thought “We use try_compile a few times to see if an MPI stack correctly supports some MPI calls (due to MPICH bugs, say), so maybe I can use try_run”.

So based on previous help, I whipped up:

cmake_minimum_required (VERSION 3.27)
project(testflag Fortran)
include(CMakePrintHelpers)
set(SNAN_FLAG "-init=snan")
find_package(MPI REQUIRED COMPONENTS Fortran)
try_run(
   RUN_RESULT_VAR COMPILE_RESULT_VAR
   ${CMAKE_CURRENT_BINARY_DIR}/test ${CMAKE_CURRENT_LIST_DIR}/just_init.F90
   LINK_LIBRARIES MPI::MPI_Fortran
   COMPILE_DEFINITIONS "${SNAN_FLAG}"
   COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT
   RUN_OUTPUT_STDOUT_VARIABLE RUN_OUTPUT_STDOUT
   RUN_OUTPUT_STDERR_VARIABLE RUN_OUTPUT_STDERR
   )

cmake_print_variables(COMPILE_RESULT_VAR)
cmake_print_variables(COMPILE_OUTPUT)
cmake_print_variables(RUN_RESULT_VAR)
cmake_print_variables(RUN_OUTPUT_STDOUT)
cmake_print_variables(RUN_OUTPUT_STDERR)

and from its run I can see it’s getting the flag in there:

-- COMPILE_RESULT_VAR="TRUE"
-- COMPILE_OUTPUT="Change Dir: '/home/mathomp4/F90Files/CMakeMPITryRun/build/test/CMakeFiles/CMakeTmp'

Run Build Command(s): /gpfsm/dulocal15/sles15/other/cmake/3.27.4/gcc-7.5.0-2/bin/cmake -E env VERBOSE=1 /usr/bin/gmake -f Makefile cmTC_2852c/fast
/usr/bin/gmake  -f CMakeFiles/cmTC_2852c.dir/build.make CMakeFiles/cmTC_2852c.dir/build
gmake[1]: Entering directory '/gpfsm/dhome/mathomp4/F90Files/CMakeMPITryRun/build/test/CMakeFiles/CMakeTmp'
Building Fortran object CMakeFiles/cmTC_2852c.dir/just_init.F90.o
/usr/local/intel/oneapi/2021/compiler/2022.1.0/linux/bin/intel64/ifort  -I/gpfsm/dswdev/gmao_SIteam/MPI/openmpi/5.0.0-SLES15/intel-2021.6.0/include -I/gpfsm/dswdev/gmao_SIteam/MPI/openmpi/5.0.0-SLES15/intel-2021.6.0/lib -init=snan -c /home/mathomp4/F90Files/CMakeMPITryRun/just_init.F90 -o CMakeFiles/cmTC_2852c.dir/just_init.F90.o
Linking Fortran executable cmTC_2852c
/gpfsm/dulocal15/sles15/other/cmake/3.27.4/gcc-7.5.0-2/bin/cmake -E cmake_link_script CMakeFiles/cmTC_2852c.dir/link.txt --verbose=1
/usr/local/intel/oneapi/2021/compiler/2022.1.0/linux/bin/intel64/ifort CMakeFiles/cmTC_2852c.dir/just_init.F90.o -o cmTC_2852c  -Wl,-rpath,/gpfsm/dswdev/gmao_SIteam/MPI/openmpi/5.0.0-SLES15/intel-2021.6.0/lib /gpfsm/dswdev/gmao_SIteam/MPI/openmpi/5.0.0-SLES15/intel-2021.6.0/lib/libmpi_usempif08.so /gpfsm/dswdev/gmao_SIteam/MPI/openmpi/5.0.0-SLES15/intel-2021.6.0/lib/libmpi_usempi_ignore_tkr.so /gpfsm/dswdev/gmao_SIteam/MPI/openmpi/5.0.0-SLES15/intel-2021.6.0/lib/libmpi_mpifh.so /gpfsm/dswdev/gmao_SIteam/MPI/openmpi/5.0.0-SLES15/intel-2021.6.0/lib/libmpi.so
gmake[1]: Leaving directory '/gpfsm/dhome/mathomp4/F90Files/CMakeMPITryRun/build/test/CMakeFiles/CMakeTmp'

"

but the run phase is…empty:

-- RUN_RESULT_VAR="0"
-- RUN_OUTPUT_STDOUT=""
-- RUN_OUTPUT_STDERR=""

So, I’m doing something wrong. If nothing else, I might expect success as if you don’t run with mpirun -np 1 it actually works:

$ mpifort -init=snan just_init.F90 && ./a.out
$ mpifort -init=snan just_init.F90 && mpirun -np 1 ./a.out
forrtl: error (65): floating invalid
Image              PC                Routine            Line        Source
a.out              000000000040A41B  for__signal_handl     Unknown  Unknown
libpthread-2.31.s  00001487C13FC910  Unknown               Unknown  Unknown
...

Any ideas on what I’m doing wrong?

Or is this just all moot and try_run can’t run MPI anyway so…

It looks like try_run might need an option to select how to run the built executable. A LAUNCHER keyword seems like it would be useful.

1 Like

Can you please file an issue requesting it so that it can be tracked?

Done: https://gitlab.kitware.com/cmake/cmake/-/issues/25451