Skip to content

Commit

Permalink
use mpi_launcher.cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Mar 29, 2024
1 parent 968a6a1 commit b9ecc8e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 29 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.19...3.28)
cmake_minimum_required(VERSION 3.19...3.29)

project(FortranMPIexamples
LANGUAGES C Fortran
Expand All @@ -12,6 +12,7 @@ message(STATUS "CMake ${CMAKE_VERSION} Generator ${CMAKE_GENERATOR} Build type

# this has checks that can be reused in other projects as well
include(cmake/mpi.cmake)
include(cmake/mpi_launcher.cmake)

include(cmake/print_target_props.cmake)
print_target_props(MPI::MPI_Fortran)
Expand Down
1 change: 0 additions & 1 deletion cmake/mpi.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ message(STATUS "MPI_Fortran_LINK_FLAGS: ${MPI_Fortran_LINK_FLAGS}")

include(${CMAKE_CURRENT_LIST_DIR}/openmpi.cmake)


if(MPI_Fortran_HAVE_F08_MODULE)
return()
endif()
Expand Down
24 changes: 24 additions & 0 deletions cmake/mpi_launcher.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function(test_mpi_launcher name Nworker)
# assumes target and test have same "name" (could be made different)

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(TEST ${name} PROPERTY TEST_LAUNCHER ${MPIEXEC_EXECUTABLE} ${MPIEXEC_NUMPROC_FLAG} ${Nworker})
else()
set_property(TARGET ${name} PROPERTY CROSSCOMPILING_EMULATOR ${MPIEXEC_EXECUTABLE} ${MPIEXEC_NUMPROC_FLAG} ${Nworker})
endif()

set_property(TEST ${name} PROPERTY PROCESSORS ${Nworker})

if(DEFINED mpi_tmpdir)
set_property(TEST ${name} PROPERTY ENVIRONMENT "TMPDIR=${mpi_tmpdir}")
endif()

endfunction()
6 changes: 4 additions & 2 deletions cmake/openmpi.cmake
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# https://github.com/open-mpi/ompi/issues/7393
# https://github.com/gerlero/openfoam-app/pull/112
# https://apple.stackexchange.com/a/287710
# *** OpenMPI workaround

message(VERBOSE "MPI_C_LIBRARY_VERSION_STRING: ${MPI_C_LIBRARY_VERSION_STRING}")

if(UNIX AND MPI_C_LIBRARY_VERSION_STRING MATCHES "Open[ ]?MPI")
if(NOT DEFINED mpi_tmpdir)
execute_process(COMMAND mktemp -d /tmp/mpi-XXXXXXXX
RESULT_VARIABLE ret
OUTPUT_VARIABLE mpi_tmpdir
OUTPUT_STRIP_TRAILING_WHITESPACE
TIMEOUT 10
)
if(NOT ret EQUAL 0)
message(FATAL_ERROR "could not create MPI working dir via mktemp -d: ${ret}")
Expand All @@ -16,4 +19,3 @@ if(UNIX AND MPI_C_LIBRARY_VERSION_STRING MATCHES "Open[ ]?MPI")
message(STATUS "${ret}: Created MPI working dir ${mpi_tmpdir}")
endif()
endif()
# *** END OpenMPI workaround
35 changes: 10 additions & 25 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
add_executable(mpi_basic_C basic.c)
target_link_libraries(mpi_basic_C PRIVATE MPI::MPI_C)
add_test(NAME mpi_basic_C COMMAND mpi_basic_C)
test_mpi_launcher(mpi_basic_C 1)

add_executable(mpi_version_C mpivers.c)
target_link_libraries(mpi_version_C PRIVATE MPI::MPI_C)
add_test(NAME mpi_version_C COMMAND mpi_version_C)
test_mpi_launcher(mpi_version_C 1)

# --- Fortran MPI-3

add_executable(mpi_basic_Fortran basic.f90)
target_link_libraries(mpi_basic_Fortran PRIVATE MPI::MPI_Fortran)
add_test(NAME mpi_basic_Fortran COMMAND mpi_basic_Fortran)
test_mpi_launcher(mpi_basic_Fortran 1)

add_executable(mpi_version_Fortran mpivers.f90)
target_link_libraries(mpi_version_Fortran PRIVATE MPI::MPI_Fortran)
add_test(NAME mpi_version_Fortran COMMAND mpi_version_Fortran)
test_mpi_launcher(mpi_version_Fortran 1)

# --- more than one MPI image

add_executable(mpi_hello helloworld.f90)
target_link_libraries(mpi_hello PRIVATE MPI::MPI_Fortran)
add_test(NAME mpi_hello COMMAND mpi_hello)
test_mpi_launcher(mpi_hello ${MPIEXEC_MAX_NUMPROCS})
set_property(TEST mpi_hello PROPERTY FIXTURES_SETUP mpi_fxt)

# --- actual message passing
Expand All @@ -31,6 +36,7 @@ target_link_libraries(mpi_pass PRIVATE MPI::MPI_Fortran)
if(MPIEXEC_MAX_NUMPROCS GREATER_EQUAL 2)

add_test(NAME mpi_pass COMMAND mpi_pass)
test_mpi_launcher(mpi_pass 2)
set_property(TEST mpi_pass PROPERTY FIXTURES_REQUIRED mpi_fxt)

endif()
Expand All @@ -39,28 +45,7 @@ endif()

get_property(tests DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY TESTS)

set_property(TEST ${tests} PROPERTY cpu_mpi)

set_property(TEST ${tests} PROPERTY SKIP_REGULAR_EXPRESSION "No host list provided")

# convenience vars
set(single mpi_basic_C mpi_version_C mpi_basic_Fortran mpi_version_Fortran)
set(_m ${MPIEXEC_EXECUTABLE} ${MPIEXEC_NUMPROC_FLAG})

if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.29)

set_property(TEST ${single} PROPERTY TEST_LAUNCHER ${_m} 1)
set_property(TEST mpi_hello PROPERTY TEST_LAUNCHER ${_m} ${MPIEXEC_MAX_NUMPROCS})
set_property(TEST mpi_pass PROPERTY TEST_LAUNCHER ${_m} 2)

else()

set_property(TARGET ${single} PROPERTY CROSSCOMPILING_EMULATOR ${_m} 1)
set_property(TARGET mpi_hello PROPERTY CROSSCOMPILING_EMULATOR ${_m} ${MPIEXEC_MAX_NUMPROCS})
set_property(TARGET mpi_pass PROPERTY CROSSCOMPILING_EMULATOR ${_m} 2)

endif()

if(DEFINED mpi_tmpdir)
set_property(TEST ${tests} PROPERTY ENVIRONMENT TMPDIR=${mpi_tmpdir})
endif()
set_tests_properties(${tests} PROPERTIES
RESOURCE_LOCK mpi
SKIP_REGULAR_EXPRESSION "No host list provided"
)

0 comments on commit b9ecc8e

Please sign in to comment.