Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ if(WITH_OMPI OR WITH_MPICH)
find_package(MPI REQUIRED)
endif()

set(INFINICCL_MPI_DEVICE_AWARE_QUERY_SUPPORTED OFF)
if(WITH_OMPI OR WITH_MPICH)
infiniccl_configure_mpi_device_aware(
INFINICCL_MPI_DEVICE_AWARE_QUERY_SUPPORTED)
endif()

if(WITH_NCCL)
infiniccl_configure_nccl(NCCL_INC NCCL_LIB NCCL_COMPILE_DEFINITIONS)
include_directories(${NCCL_INC})
Expand Down
147 changes: 147 additions & 0 deletions cmake/backends/MPI.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,150 @@ function(infiniccl_detect_mpi output)

set(${output} "${_implementation}" PARENT_SCOPE)
endfunction()

function(infiniccl_configure_mpi_device_aware output)
set(_supported 0)
set(_probe_includes "")
set(_probe_libraries "")
set(_probe_compile_flags "")
set(_probe_link_flags "")
set(_probe_library_files "")

if(WITH_OMPI)
set(_probe_includes "${MPI_CXX_INCLUDE_DIRS}")
if(TARGET MPI::MPI_CXX)
set(_probe_libraries MPI::MPI_CXX)
get_target_property(_mpi_imported_locations MPI::MPI_CXX
IMPORTED_LOCATION)
if(_mpi_imported_locations)
list(APPEND _probe_library_files ${_mpi_imported_locations})
endif()
else()
set(_probe_libraries "${MPI_CXX_LIBRARIES}")
set(_probe_library_files "${_probe_libraries}")
set(_probe_compile_flags "${MPI_CXX_COMPILE_FLAGS}")
set(_probe_link_flags "${MPI_CXX_LINK_FLAGS}")
endif()
endif()

set(_fingerprint
"gen=1|implementation=${WITH_OMPI}|includes=${_probe_includes}|libraries=${_probe_libraries}|compile_flags=${_probe_compile_flags}|link_flags=${_probe_link_flags}")
foreach(_library IN LISTS _probe_library_files)
if(EXISTS "${_library}")
set(_library_mtime "")
file(TIMESTAMP "${_library}" _library_mtime "%s")
string(APPEND _fingerprint "|mtime:${_library}=${_library_mtime}")
endif()
endforeach()
string(MD5 _fingerprint_md5 "${_fingerprint}")

set(_reuse FALSE)
set(_cached_supported "")
set(_stamp
"${CMAKE_BINARY_DIR}/src/backends/mpi/ompi/device_support_config.stamp")
if(EXISTS "${_stamp}")
file(READ "${_stamp}" _stamp_text)
string(REPLACE "\n" ";" _stamp_lines "${_stamp_text}")
list(GET _stamp_lines 0 _stamp_md5)
list(LENGTH _stamp_lines _stamp_line_count)
if(_stamp_line_count GREATER 1
AND "${_stamp_md5}" STREQUAL "${_fingerprint_md5}")
list(GET _stamp_lines 1 _cached_supported)
set(_reuse TRUE)
endif()
endif()

if(NOT _reuse)
if(WITH_OMPI)
include(CheckCXXSourceCompiles)
include(CMakePushCheckState)
cmake_push_check_state(RESET)

set(CMAKE_REQUIRED_INCLUDES ${_probe_includes})

set(CMAKE_REQUIRED_LIBRARIES ${_probe_libraries})
if(_probe_compile_flags)
set(CMAKE_REQUIRED_FLAGS ${_probe_compile_flags})
endif()
if(_probe_link_flags)
separate_arguments(_MPI_DEVICE_AWARE_LINK_OPTIONS NATIVE_COMMAND
"${_probe_link_flags}")
set(CMAKE_REQUIRED_LINK_OPTIONS
${_MPI_DEVICE_AWARE_LINK_OPTIONS})
endif()

unset(_INFINICCL_MPI_HAS_OMPI_CUDA_QUERY CACHE)
unset(_INFINICCL_MPI_HAS_OMPI_CUDA_QUERY)
check_cxx_source_compiles("
#include <mpi.h>
#include <mpi-ext.h>
int main() {
return MPIX_Query_cuda_support();
}
" _INFINICCL_MPI_HAS_OMPI_CUDA_QUERY)
cmake_pop_check_state()

if(_INFINICCL_MPI_HAS_OMPI_CUDA_QUERY)
set(_supported 1)
endif()
endif()

file(WRITE "${_stamp}" "${_fingerprint_md5}\n${_supported}")
else()
set(_supported "${_cached_supported}")
endif()

set(_output
"${CMAKE_BINARY_DIR}/src/backends/mpi/ompi/device_support_config.h")
set(_extension_includes "")
set(_query_body "return false;")
if(_supported)
set(_extension_includes
"#include <mpi.h>\n#include <mpi-ext.h>\n")
set(_query_body "return MPIX_Query_cuda_support() != 0;")
endif()

string(CONCAT _content
"#ifndef INFINI_CCL_BACKENDS_MPI_OMPI_DEVICE_SUPPORT_CONFIG_H_\n"
"#define INFINI_CCL_BACKENDS_MPI_OMPI_DEVICE_SUPPORT_CONFIG_H_\n"
"\n"
"// Generated by `infiniccl_configure_mpi_device_aware()` at\n"
"// configure time from a compile-link probe against the selected MPI.\n"
"// Do not edit.\n"
"\n"
"${_extension_includes}"
"\n"
"namespace infini::ccl::mpi_config {\n"
"\n"
"inline constexpr bool kHasOmpiCudaQuery = ${_supported};\n"
"\n"
"inline bool QueryOmpiCudaSupport() {\n"
" ${_query_body}\n"
"}\n"
"\n"
"} // namespace infini::ccl::mpi_config\n"
"\n"
"#endif // INFINI_CCL_BACKENDS_MPI_OMPI_DEVICE_SUPPORT_CONFIG_H_\n")

set(_header_changed TRUE)
if(EXISTS "${_output}")
file(READ "${_output}" _existing_content)
if(_existing_content STREQUAL "${_content}")
set(_header_changed FALSE)
endif()
endif()
if(_header_changed)
file(WRITE "${_output}" "${_content}")
message(STATUS
"MPI device-aware query: wrote ${_output}")
else()
message(STATUS
"MPI device-aware query: ${_output} unchanged")
endif()

if(_supported)
set(${output} ON PARENT_SCOPE)
else()
set(${output} OFF PARENT_SCOPE)
endif()
endfunction()
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ if(WITH_OMPI)
if(TARGET MPI::MPI_CXX)
target_link_libraries(infiniccl PRIVATE MPI::MPI_CXX)
endif()

endif()

if(WITH_MPICH)
Expand Down
166 changes: 166 additions & 0 deletions src/backends/mpi/ompi/buffer_plan.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
#ifndef INFINI_CCL_BACKENDS_MPI_OMPI_BUFFER_PLAN_H_
#define INFINI_CCL_BACKENDS_MPI_OMPI_BUFFER_PLAN_H_

#include <cstdlib>
#include <memory>

#include "runtime.h"

namespace infini::ccl {

namespace detail {

using HostBuffer = std::unique_ptr<void, decltype(&std::free)>;

inline HostBuffer AllocateHostBuffer(std::size_t bytes) {
return HostBuffer(std::malloc(bytes), &std::free);
}

template <Device::Type kDev>
bool IsDeviceBuffer(const void *ptr) {
return ptr != nullptr && GetMemorySpace<kDev>(ptr) == MemorySpace::kDevice;
}

} // namespace detail

template <Device::Type kDev>
class MpiBufferPlan {
public:
using Rt = Runtime<kDev>;

enum class Mode {
kPair,
kAlias,
};

ReturnStatus Prepare(const void *send_buff, void *recv_buff,
std::size_t send_bytes, std::size_t recv_bytes,
bool allow_direct, void *stream, Mode mode) {
const bool send_active = send_bytes != 0;
const bool recv_active = recv_bytes != 0;
if (!send_active && !recv_active) {
return ReturnStatus::kSuccess;
}

user_recv_buff_ = recv_buff;
recv_bytes_ = recv_bytes;

const bool send_device =
send_active && detail::IsDeviceBuffer<kDev>(send_buff);
const bool recv_device =
recv_active && detail::IsDeviceBuffer<kDev>(recv_buff);
recv_is_device_ = recv_device;

if (mode == Mode::kPair) {
return PreparePair(send_buff, recv_buff, send_bytes, recv_bytes,
allow_direct, stream, send_active, recv_active,
send_device, recv_device);
}

return PrepareAlias(recv_buff, recv_bytes, allow_direct, stream,
send_active, recv_device);
}

ReturnStatus Commit() {
if (!host_recv_buff_) {
return ReturnStatus::kSuccess;
}

CHECK_STATUS(Rt, Rt::Memcpy(user_recv_buff_, host_recv_buff_.get(),
recv_bytes_, Rt::MemcpyHostToDevice));
return ReturnStatus::kSuccess;
}

const void *send_buff() const { return mpi_send_buff_; }

void *recv_buff() const { return mpi_recv_buff_; }

bool direct() const { return direct_; }

private:
ReturnStatus PreparePair(const void *send_buff, void *recv_buff,
std::size_t send_bytes, std::size_t recv_bytes,
bool allow_direct, void *stream, bool send_active,
bool recv_active, bool send_device,
bool recv_device) {
direct_ = allow_direct && (!send_active || send_device) &&
(!recv_active || recv_device);
if (direct_) {
if (send_device) {
CHECK_STATUS(Rt, Rt::StreamSynchronize(
static_cast<typename Rt::Stream>(stream)));
}

mpi_send_buff_ = send_buff;
mpi_recv_buff_ = recv_buff;
return ReturnStatus::kSuccess;
}

if (send_device) {
CHECK_STATUS(
Rt, Rt::StreamSynchronize(static_cast<typename Rt::Stream>(stream)));
host_send_buff_ = detail::AllocateHostBuffer(send_bytes);
if (!host_send_buff_) {
return ReturnStatus::kSystemError;
}

CHECK_STATUS(Rt, Rt::Memcpy(host_send_buff_.get(), send_buff, send_bytes,
Rt::MemcpyDeviceToHost));
mpi_send_buff_ = host_send_buff_.get();
} else {
mpi_send_buff_ = send_buff;
}

if (recv_device) {
host_recv_buff_ = detail::AllocateHostBuffer(recv_bytes);
if (!host_recv_buff_) {
return ReturnStatus::kSystemError;
}
mpi_recv_buff_ = host_recv_buff_.get();
} else {
mpi_recv_buff_ = recv_buff;
}

return ReturnStatus::kSuccess;
}

ReturnStatus PrepareAlias(void *recv_buff, std::size_t recv_bytes,
bool allow_direct, void *stream, bool send_active,
bool recv_device) {
direct_ = allow_direct && recv_device && !send_active;
if (direct_) {
CHECK_STATUS(
Rt, Rt::StreamSynchronize(static_cast<typename Rt::Stream>(stream)));
mpi_recv_buff_ = recv_buff;
return ReturnStatus::kSuccess;
}

if (recv_device) {
CHECK_STATUS(
Rt, Rt::StreamSynchronize(static_cast<typename Rt::Stream>(stream)));
host_recv_buff_ = detail::AllocateHostBuffer(recv_bytes);
if (!host_recv_buff_) {
return ReturnStatus::kSystemError;
}

mpi_recv_buff_ = host_recv_buff_.get();
} else {
mpi_recv_buff_ = recv_buff;
}

return ReturnStatus::kSuccess;
}

void *user_recv_buff_ = nullptr;
const void *mpi_send_buff_ = nullptr;
void *mpi_recv_buff_ = nullptr;
detail::HostBuffer host_send_buff_{nullptr, &std::free};
detail::HostBuffer host_recv_buff_{nullptr, &std::free};
std::size_t recv_bytes_ = 0;
bool recv_is_device_ = false;
bool direct_ = false;
};

} // namespace infini::ccl

#endif // INFINI_CCL_BACKENDS_MPI_OMPI_BUFFER_PLAN_H_
3 changes: 3 additions & 0 deletions src/backends/mpi/ompi/comm_instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@

#include "checks.h"
#include "communicator.h"
#include "device_support.h"

namespace infini::ccl {

struct OmpiInstance : public BackendCommInstance {
MPI_Comm handle = MPI_COMM_NULL;

MpiDeviceSupport device_support = MpiDeviceSupport::kHostStaged;

OmpiInstance() { type = BackendType::kOmpi; }
~OmpiInstance() override { Destroy(); }

Expand Down
Loading