Skip to content

Commit

Permalink
Add CMake test for GPU-aware MPI
Browse files Browse the repository at this point in the history
Try to compile and run send_recv_usm.cpp to determine
whether the MPI implementation is GPU-aware. Enable
building the MPI_with_SYCL examples only if at least
one backend is found to work with MPI over GPU.
  • Loading branch information
rafbiels committed May 13, 2024
1 parent 79142ca commit 07b1228
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# This work is licensed under the Apache License, Version 2.0.
# For a copy, see http://www.apache.org/licenses/LICENSE-2.0

cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.12)
project(SYCL-samples)

# Set build type to Release if unset
Expand Down
70 changes: 64 additions & 6 deletions src/MPI_with_SYCL/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,69 @@
function(run_mpi_test)
cmake_parse_arguments(RUN_MPI_TEST "" "BINARY;BACKEND_SELECTOR" "" ${ARGN})
execute_process(
COMMAND bash -c "! ONEAPI_DEVICE_SELECTOR=${RUN_MPI_TEST_BACKEND_SELECTOR}:gpu mpirun -n 2 ${RUN_MPI_TEST_BINARY}"
RESULT_VARIABLE MPI_TEST_SUCCEEDED
OUTPUT_QUIET
ERROR_QUIET)
set(MPI_TEST_SUCCEEDED ${MPI_TEST_SUCCEEDED} PARENT_SCOPE)
endfunction()

function(test_mpi_gpu_support)
set(MPI_TEST_COMPILE_ARGS ${SYCL_FLAGS})
list(APPEND MPI_TEST_COMPILE_ARGS ${MPI_CXX_COMPILE_OPTIONS} -I${MPI_CXX_INCLUDE_DIRS} ${MPI_CXX_LINK_FLAGS} ${MPI_LIBRARIES})
list(JOIN MPI_TEST_COMPILE_ARGS " " MPI_TEST_COMPILE_ARGS)
set(MPI_TEST_BINARY ${CMAKE_CURRENT_BINARY_DIR}/test-mpi-gpu)
set(MPI_TEST_COMPILE_CMD "${CMAKE_CXX_COMPILER} ${MPI_TEST_COMPILE_ARGS} -o ${MPI_TEST_BINARY} ${CMAKE_CURRENT_SOURCE_DIR}/send_recv_usm.cpp")
execute_process(
COMMAND bash -c "! ${MPI_TEST_COMPILE_CMD}"
RESULT_VARIABLE MPI_TEST_COMPILE_SUCCEEDED
OUTPUT_QUIET
ERROR_QUIET)
set(MPI_GPU_AWARE 0)
if (${MPI_TEST_COMPILE_SUCCEEDED})
if (${ENABLE_CUDA})
run_mpi_test(BINARY ${MPI_TEST_BINARY} BACKEND_SELECTOR "cuda")
if (MPI_TEST_SUCCEEDED)
set(MPI_GPU_AWARE 1)
list(APPEND MPI_AVAILABLE_BACKENDS "CUDA")
endif()
endif()

if (${ENABLE_HIP})
run_mpi_test(BINARY ${MPI_TEST_BINARY} BACKEND_SELECTOR "hip")
if (MPI_TEST_SUCCEEDED)
set(MPI_GPU_AWARE 1)
list(APPEND MPI_AVAILABLE_BACKENDS "HIP")
endif()
endif()

if (${ENABLE_SPIR})
run_mpi_test(BINARY ${MPI_TEST_BINARY} BACKEND_SELECTOR "level_zero")
if (MPI_TEST_SUCCEEDED)
set(MPI_GPU_AWARE 1)
list(APPEND MPI_AVAILABLE_BACKENDS "Level Zero")
endif()
endif()
endif()

set(MPI_GPU_AWARE ${MPI_GPU_AWARE} PARENT_SCOPE)
set(MPI_AVAILABLE_BACKENDS ${MPI_AVAILABLE_BACKENDS} PARENT_SCOPE)
endfunction()

find_package(MPI)
if(NOT MPI_FOUND)
message(STATUS "MPI not found, skipping the MPI_with_SYCL demo")
else()
message(STATUS "Found MPI, configuring the MPI_with_SYCL demo")
foreach(TARGET send_recv_usm send_recv_buff scatter_reduce_gather)
add_executable(${TARGET} ${TARGET}.cpp)
target_compile_options(${TARGET} PUBLIC ${SYCL_FLAGS} ${MPI_INCLUDE_DIRS})
target_link_options(${TARGET} PUBLIC ${SYCL_FLAGS} ${MPI_LIBRARIES})
endforeach()
test_mpi_gpu_support()
if (MPI_GPU_AWARE)
list(JOIN MPI_AVAILABLE_BACKENDS ", " MPI_AVAILABLE_BACKENDS)
message(STATUS "Found GPU-aware MPI, configuring the MPI_with_SYCL demo. Available backends: ${MPI_AVAILABLE_BACKENDS}")
foreach(TARGET send_recv_usm send_recv_buff scatter_reduce_gather)
add_executable(${TARGET} ${TARGET}.cpp)
target_compile_options(${TARGET} PUBLIC ${SYCL_FLAGS} -I${MPI_CXX_INCLUDE_DIRS})
target_link_options(${TARGET} PUBLIC ${SYCL_FLAGS} ${MPI_LIBRARIES})
endforeach()
else()
message(STATUS "Found MPI which is not GPU-aware - skipping the MPI_with_SYCL demo")
endif()
endif()

0 comments on commit 07b1228

Please sign in to comment.