Standalone example of CMake TEST_LAUNCHER target property, often used for testing, especially with MPI.
Last active
May 5, 2024 17:37
-
-
Save scivision/27a35a3ae69ea59959ac0693447d0764 to your computer and use it in GitHub Desktop.
CMake MPI TEST_LAUNCHER
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "*") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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