Skip to content

Instantly share code, notes, and snippets.

@scivision
Last active May 5, 2024 17:37
Show Gist options
  • Save scivision/27a35a3ae69ea59959ac0693447d0764 to your computer and use it in GitHub Desktop.
Save scivision/27a35a3ae69ea59959ac0693447d0764 to your computer and use it in GitHub Desktop.
CMake MPI TEST_LAUNCHER

CMake CTest TEST_LAUNCHER

Standalone example of CMake TEST_LAUNCHER target property, often used for testing, especially with MPI.

cmake_minimum_required(VERSION 3.15)
project(mpitest LANGUAGES Fortran)
enable_testing()
find_package(MPI COMPONENTS Fortran REQUIRED)
include(TestMPILauncher.cmake)
add_executable(mpi_version mpi_version.f90)
target_link_libraries(mpi_version PRIVATE MPI::MPI_Fortran)
add_test(NAME MPIversion COMMAND mpi_version)
test_mpi_launcher(mpi_version MPIversion 1)
file(GENERATE OUTPUT .gitignore CONTENT "*")
program mpi_vers
! https://github.com/open-mpi/ompi/blob/master/examples/hello_usempif08.f90
use, intrinsic :: iso_fortran_env, only : compiler_version
use mpi_f08
implicit none
integer :: id, Nimg, vlen
character(MPI_MAX_LIBRARY_VERSION_STRING) :: version ! allocatable not ok
print *,compiler_version()
call MPI_INIT()
call MPI_COMM_RANK(MPI_COMM_WORLD, id)
call MPI_COMM_SIZE(MPI_COMM_WORLD, Nimg)
call MPI_GET_LIBRARY_VERSION(version, vlen)
print '(A,I3,A,I3,A)', 'MPI: Image ', id, ' / ', Nimg, ':', trim(version)
call MPI_FINALIZE()
end program
function(test_mpi_launcher target test Nworker)
if(NOT (DEFINED MPIEXEC_EXECUTABLE AND DEFINED MPIEXEC_NUMPROC_FLAG))
message(FATAL_ERROR "MPIEXEC_EXECUTABLE and MPIEXEC_NUMPROC_FLAG must be defined to use test_mpi_launcher")
endif()
if(NOT Nworker)
message(FATAL_ERROR "Nworker must be defined to use test_mpi_launcher")
endif()
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.29)
set_property(TARGET ${target} PROPERTY TEST_LAUNCHER ${MPIEXEC_EXECUTABLE} ${MPIEXEC_NUMPROC_FLAG} ${Nworker})
else()
set_property(TARGET ${target} PROPERTY CROSSCOMPILING_EMULATOR ${MPIEXEC_EXECUTABLE} ${MPIEXEC_NUMPROC_FLAG} ${Nworker})
endif()
set_property(TEST ${test} PROPERTY PROCESSORS ${Nworker})
endfunction()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment