Skip to content

Commit 561ee59

Browse files
authored
Add CMake test for GPU-aware MPI (#17)
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.
1 parent 80cfe84 commit 561ee59

File tree

3 files changed

+73
-9
lines changed

3 files changed

+73
-9
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# This work is licensed under the Apache License, Version 2.0.
33
# For a copy, see http://www.apache.org/licenses/LICENSE-2.0
44

5-
cmake_minimum_required(VERSION 3.5)
5+
cmake_minimum_required(VERSION 3.12)
66
project(SYCL-samples)
77

88
# Set build type to Release if unset

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,12 @@ MPI installation. E.g.
8585
```
8686

8787
Additionally, in order to run the examples, the MPI implementation needs
88-
to be device-aware. This is only detectable at runtime, so the examples may build
89-
fine but crash on execution if the linked MPI library isn't device-aware.
88+
to be device-aware. The CMake configuration attempts to build and execute the
89+
simplest example to evaluate whether the found MPI library supports any of the
90+
enabled backends. This demo will be automatically skipped if this check does not
91+
pass and a corresponding message will appear in the CMake configuration output.
92+
The result of this check can be overwritten with the `-DMPI_DEVICE_AWARE=ON/OFF`
93+
option.
9094

9195
### Parallel Inclusive Scan
9296
Implementation of a parallel inclusive scan with a given associative binary

src/MPI_with_SYCL/CMakeLists.txt

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,71 @@
1+
function(run_mpi_test)
2+
cmake_parse_arguments(RUN_MPI_TEST "" "BINARY;BACKEND_SELECTOR" "" ${ARGN})
3+
execute_process(
4+
COMMAND bash -c "ONEAPI_DEVICE_SELECTOR=${RUN_MPI_TEST_BACKEND_SELECTOR}:gpu mpirun -n 2 ${RUN_MPI_TEST_BINARY} > mpi-test-output.txt && grep 'received status==0' mpi-test-output.txt"
5+
RESULT_VARIABLE MPI_TEST_FAILED
6+
OUTPUT_QUIET
7+
ERROR_QUIET)
8+
if (${MPI_TEST_FAILED})
9+
set(MPI_TEST_SUCCEEDED FALSE PARENT_SCOPE)
10+
else()
11+
set(MPI_TEST_SUCCEEDED TRUE PARENT_SCOPE)
12+
endif()
13+
endfunction()
14+
15+
function(test_mpi_gpu_support)
16+
set(MPI_TEST_BINARY ${CMAKE_CURRENT_BINARY_DIR}/test-mpi-gpu)
17+
try_compile(MPI_TEST_COMPILE_SUCCEEDED
18+
${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/send_recv_usm.cpp
19+
COMPILE_DEFINITIONS "${SYCL_FLAGS} -I${MPI_CXX_INCLUDE_DIRS}"
20+
LINK_OPTIONS ${SYCL_FLAGS}
21+
LINK_LIBRARIES ${MPI_LIBRARIES}
22+
COPY_FILE ${MPI_TEST_BINARY}
23+
OUTPUT_VARIABLE MPI_TEST_COMPILE_OUTPUT)
24+
set(MPI_DEVICE_AWARE 0)
25+
if (${MPI_TEST_COMPILE_SUCCEEDED})
26+
if (${ENABLE_CUDA})
27+
run_mpi_test(BINARY ${MPI_TEST_BINARY} BACKEND_SELECTOR "cuda")
28+
if (MPI_TEST_SUCCEEDED)
29+
set(MPI_DEVICE_AWARE 1)
30+
list(APPEND MPI_AVAILABLE_BACKENDS "CUDA")
31+
endif()
32+
endif()
33+
34+
if (${ENABLE_HIP})
35+
run_mpi_test(BINARY ${MPI_TEST_BINARY} BACKEND_SELECTOR "hip")
36+
if (MPI_TEST_SUCCEEDED)
37+
set(MPI_DEVICE_AWARE 1)
38+
list(APPEND MPI_AVAILABLE_BACKENDS "HIP")
39+
endif()
40+
endif()
41+
42+
if (${ENABLE_SPIR})
43+
run_mpi_test(BINARY ${MPI_TEST_BINARY} BACKEND_SELECTOR "level_zero")
44+
if (MPI_TEST_SUCCEEDED)
45+
set(MPI_DEVICE_AWARE 1)
46+
list(APPEND MPI_AVAILABLE_BACKENDS "Level Zero")
47+
endif()
48+
endif()
49+
endif()
50+
51+
set(MPI_DEVICE_AWARE ${MPI_DEVICE_AWARE} CACHE BOOL "Offload device aware MPI implementation is available")
52+
set(MPI_AVAILABLE_BACKENDS ${MPI_AVAILABLE_BACKENDS} PARENT_SCOPE)
53+
endfunction()
54+
155
find_package(MPI)
256
if(NOT MPI_FOUND)
357
message(STATUS "MPI not found, skipping the MPI_with_SYCL demo")
458
else()
5-
message(STATUS "Found MPI, configuring the MPI_with_SYCL demo")
6-
foreach(TARGET send_recv_usm send_recv_buff scatter_reduce_gather)
7-
add_executable(${TARGET} ${TARGET}.cpp)
8-
target_compile_options(${TARGET} PUBLIC ${SYCL_FLAGS} ${MPI_INCLUDE_DIRS})
9-
target_link_options(${TARGET} PUBLIC ${SYCL_FLAGS} ${MPI_LIBRARIES})
10-
endforeach()
59+
test_mpi_gpu_support()
60+
if (MPI_DEVICE_AWARE)
61+
list(JOIN MPI_AVAILABLE_BACKENDS ", " MPI_AVAILABLE_BACKENDS)
62+
message(STATUS "Found offload device aware MPI, configuring the MPI_with_SYCL demo. Available backends: ${MPI_AVAILABLE_BACKENDS}")
63+
foreach(TARGET send_recv_usm send_recv_buff scatter_reduce_gather)
64+
add_executable(${TARGET} ${TARGET}.cpp)
65+
target_compile_options(${TARGET} PUBLIC ${SYCL_FLAGS} -I${MPI_CXX_INCLUDE_DIRS})
66+
target_link_options(${TARGET} PUBLIC ${SYCL_FLAGS} ${MPI_LIBRARIES})
67+
endforeach()
68+
else()
69+
message(STATUS "Found MPI which is not offload device aware - skipping the MPI_with_SYCL demo")
70+
endif()
1171
endif()

0 commit comments

Comments
 (0)