ucx.cmake 3.6 KB
Newer Older
M
Megvii Engine Team 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
include(ExternalProject)

option(UCX_WITH_GDRCOPY "Build ucx with gdrcopy" OFF)

if(${CMAKE_GENERATOR} STREQUAL "Ninja")
    set(MAKE_COMMAND make)
else()
    set(MAKE_COMMAND "$(MAKE)")
endif()

get_filename_component(CUDA_HOME ${CMAKE_CUDA_COMPILER} DIRECTORY)
get_filename_component(CUDA_HOME ${CUDA_HOME} DIRECTORY)

if(UCX_WITH_GDRCOPY)
    set(GDRCOPY_DIR ${PROJECT_SOURCE_DIR}/third_party/gdrcopy)
    set(GDRCOPY_BUILD_DIR ${PROJECT_BINARY_DIR}/third_party/gdrcopy)
    set(GDRCOPY_LIB ${GDRCOPY_BUILD_DIR}/lib64/libgdrapi.so)
    ExternalProject_add(
        gdrcopy
        SOURCE_DIR ${GDRCOPY_DIR}
        PREFIX ${GDRCOPY_BUILD_DIR}
        CONFIGURE_COMMAND ""
        BUILD_COMMAND ${MAKE_COMMAND} -C ${GDRCOPY_DIR} lib CUDA_HOME=${CUDA_HOME}
        INSTALL_COMMAND ${MAKE_COMMAND} -C ${GDRCOPY_DIR} lib_install PREFIX="" DESTDIR=${GDRCOPY_BUILD_DIR}
        BUILD_BYPRODUCTS ${GDRCOPY_LIB}
    )

    ExternalProject_Add_Step(
        gdrcopy
        clean
        COMMAND make clean
        WORKING_DIRECTORY <SOURCE_DIR>
        DEPENDEES install
    )

    set(GDRCOPY_INC ${GDRCOPY_BUILD_DIR}/include)
    file(MAKE_DIRECTORY ${GDRCOPY_INC})

    add_library(libgdrcopy SHARED IMPORTED GLOBAL)
    add_dependencies(libgdrcopy gdrcopy)
    set_target_properties(
        libgdrcopy PROPERTIES
        IMPORTED_LOCATION ${GDRCOPY_LIB}
        INTERFACE_INCLUDE_DIRECTORIES ${GDRCOPY_INC}
    )

    list(APPEND UCX_CONFIGURE_ARGS --with-gdrcopy=${GDRCOPY_BUILD_DIR})
endif()

set(UCX_DIR ${PROJECT_SOURCE_DIR}/third_party/ucx)
set(UCX_BUILD_DIR ${PROJECT_BINARY_DIR}/third_party/ucx)

file(STRINGS ${UCX_DIR}/configure.ac UCX_SO_LINE REGEX "\\[libucx_so_version\\]")
string(REGEX MATCH "([0-9]+):([0-9]+):([0-9]+)" _ ${UCX_SO_LINE})
set(UCX_SO_VER_MAJOR ${CMAKE_MATCH_1})
set(UCX_SO_VER_MINOR ${CMAKE_MATCH_2})
set(UCX_SO_VER_PATCH ${CMAKE_MATCH_3})

set(UCP_LIB ${UCX_BUILD_DIR}/lib/libucp.so.${UCX_SO_VER_MAJOR})
set(UCT_LIB ${UCX_BUILD_DIR}/lib/libuct.so.${UCX_SO_VER_MAJOR})
set(UCS_LIB ${UCX_BUILD_DIR}/lib/libucs.so.${UCX_SO_VER_MAJOR})
set(UCM_LIB ${UCX_BUILD_DIR}/lib/libucm.so.${UCX_SO_VER_MAJOR})

ExternalProject_add(
    ucx
    SOURCE_DIR ${UCX_DIR}
    PREFIX ${UCX_BUILD_DIR}
    CONFIGURE_COMMAND ${UCX_DIR}/configure --enable-mt --with-pic --disable-static
         --with-cuda --disable-numa --prefix=${UCX_BUILD_DIR} ${UCX_CONFIGURE_ARGS}
    BUILD_COMMAND ${MAKE_COMMAND} all
    INSTALL_COMMAND ${MAKE_COMMAND} install
    BUILD_BYPRODUCTS ${UCP_LIB} ${UCT_LIB} ${UCS_LIB} ${UCM_LIB}
)

ExternalProject_Add_Step(
    ucx
    autogen
    COMMAND ./autogen.sh
    WORKING_DIRECTORY <SOURCE_DIR>
    DEPENDERS configure
)

if(UCX_WITH_GDRCOPY)
    add_dependencies(ucx gdrcopy)
endif()

set(UCX_INC ${UCX_BUILD_DIR}/include)
file(MAKE_DIRECTORY ${UCX_INC})

add_library(libucp SHARED IMPORTED GLOBAL)
add_dependencies(libucp ucx)
set_target_properties(
    libucp PROPERTIES
    IMPORTED_LOCATION ${UCP_LIB}
)

add_library(libuct SHARED IMPORTED GLOBAL)
add_dependencies(libuct ucx)
set_target_properties(
    libuct PROPERTIES
    IMPORTED_LOCATION ${UCT_LIB}
)

add_library(libucs SHARED IMPORTED GLOBAL)
add_dependencies(libucs ucx)
set_target_properties(
    libucs PROPERTIES
    IMPORTED_LOCATION ${UCS_LIB}
)

add_library(libucm SHARED IMPORTED GLOBAL)
add_dependencies(libucm ucx)
set_target_properties(
    libucm PROPERTIES
    IMPORTED_LOCATION ${UCM_LIB}
)

add_library(libucx INTERFACE)
target_link_libraries(libucx INTERFACE libucp libuct libucs libucm)
if(UCX_WITH_GDRCOPY)
    target_link_libraries(libucx INTERFACE libgdrcopy)
endif()
target_include_directories(libucx INTERFACE ${UCX_INC})