# How to try\_run MPI code

**URL:** https://discourse.cmake.org/t/how-to-try-run-mpi-code/9516
**Category:** Code
**Created:** [November 27, 2023, 9:56pm UTC](https://discourse.cmake.org/t/how-to-try-run-mpi-code/9516 "2023-11-27T21:56:53Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![mathomp4](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mathomp4/32/176_2.png) [@mathomp4](https://discourse.cmake.org/u/mathomp4)
#### Post date: [November 27, 2023, 9:56pm UTC](https://discourse.cmake.org/t/how-to-try-run-mpi-code/9516/1 "2023-11-27T21:56:53Z")

</div>

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](https://github.com/open-mpi/ompi/issues/12113)). 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 … 🤷‍♂️

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](https://discourse.cmake.org/t/help-with-try-run-and-cmake-flags/2636), I whipped up:

```cmake
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:

```auto
-- 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:

```auto
-- 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:

```auto
$ 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…

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [November 28, 2023, 1:37am UTC](https://discourse.cmake.org/t/how-to-try-run-mpi-code/9516/2 "2023-11-28T01:37:28Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [November 29, 2023, 11:51am UTC](https://discourse.cmake.org/t/how-to-try-run-mpi-code/9516/3 "2023-11-29T11:51:35Z")

</div>

Can you please [file an issue](https://gitlab.kitware.com/cmake/cmake/-/issues) requesting it so that it can be tracked?

---

<div class="post-metadata">

### Author: ![mathomp4](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mathomp4/32/176_2.png) [@mathomp4](https://discourse.cmake.org/u/mathomp4)
#### Post date: [November 29, 2023, 12:36pm UTC](https://discourse.cmake.org/t/how-to-try-run-mpi-code/9516/4 "2023-11-29T12:36:17Z")

</div>

Done: [https://gitlab.kitware.com/cmake/cmake/-/issues/25451](https://gitlab.kitware.com/cmake/cmake/-/issues/25451)
