Skip to content

Commit

Permalink
clean library and header dependencies (deepmodeling#930)
Browse files Browse the repository at this point in the history
* clean library and header dependencies

Fix deepmodeling#926.

* fix typo in rocm

* set INSTALL_RPATH for libraries
  • Loading branch information
njzjz authored Aug 10, 2021
1 parent 70fd2b6 commit e01e7d7
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 83 deletions.
2 changes: 1 addition & 1 deletion doc/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ where `e`, `f` and `v` are predicted energy, force and virial of the system, res
You can compile `infer_water.cpp` using `gcc`:
```sh
gcc infer_water.cpp -D HIGH_PREC -L $deepmd_root/lib -L $tensorflow_root/lib -I $deepmd_root/include -I $tensorflow_root/include -Wl,--no-as-needed -ldeepmd_op -ldeepmd -ldeepmd_cc -ltensorflow_cc -ltensorflow_framework -lstdc++ -Wl,-rpath=$deepmd_root/lib -Wl,-rpath=$tensorflow_root/lib -o infer_water
gcc infer_water.cpp -D HIGH_PREC -L $deepmd_root/lib -L $tensorflow_root/lib -I $deepmd_root/include -I $tensorflow_root/include -Wl,--no-as-needed -ldeepmd_cc -lstdc++ -Wl,-rpath=$deepmd_root/lib -Wl,-rpath=$tensorflow_root/lib -o infer_water
```
and then run the program:
```sh
Expand Down
12 changes: 0 additions & 12 deletions source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,6 @@ if (OPENMP_FOUND)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif()

# headers of lib
list (APPEND DeePMD_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/lib/include/)
list (APPEND DeePMD_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/3rdparty/)
if (BUILD_CPP_IF)
list (APPEND DeePMD_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/api_cc/include/)
include_directories(${CMAKE_BINARY_DIR}/api_cc/)
endif (BUILD_CPP_IF)

# include
include_directories(${DeePMD_INCLUDE_DIRS})
include_directories(${TensorFlow_INCLUDE_DIRS})

# define names of libs
set (LIB_DEEPMD "deepmd")
if (BUILD_CPP_IF)
Expand Down
20 changes: 5 additions & 15 deletions source/api_cc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ configure_file(
@ONLY
)

if (USE_CUDA_TOOLKIT)
include_directories("${CUDA_INCLUDE_DIRS}")
endif()

if (USE_ROCM_TOOLKIT)
include_directories("${ROCM_INCLUDE_DIRS}")
endif()

file(GLOB LIB_SRC src/*.cc src/*.cpp)
file(GLOB INC_SRC include/*.h ${CMAKE_CURRENT_BINARY_DIR}/version.h)

Expand All @@ -23,18 +15,16 @@ set (libname "${LIB_DEEPMD_CC}${variant_name}")

add_library(${libname} SHARED ${LIB_SRC})

if (USE_CUDA_TOOLKIT)
target_link_libraries (${libname} ${CUDA_LIBRARIES})
endif()

if (USE_ROCM_TOOLKIT)
target_link_libraries (${libname} ${ROCM_LIBRARIES})
endif()
# link: libdeepmd libdeepmd_op libtensorflow_cc libtensorflow_framework
target_link_libraries (${libname} PUBLIC ${LIB_DEEPMD} ${TensorFlow_LIBRARY} ${TensorFlowFramework_LIBRARY})
target_link_libraries (${libname} PRIVATE ${LIB_DEEPMD_OP})
target_include_directories(${libname} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_BINARY_DIR} ${TensorFlow_INCLUDE_DIRS})

set_target_properties(
${libname}
PROPERTIES
COMPILE_DEFINITIONS ${prec_def}
INSTALL_RPATH "$ORIGIN;${TensorFlow_LIBRARY_PATH}"
)

install(TARGETS ${libname} DESTINATION lib/)
Expand Down
2 changes: 1 addition & 1 deletion source/install/build_lammps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ cp -r ${BUILD_TMP_DIR2}/USER-DEEPMD/* ${BUILD_TMP_DIR}/lammps-${LAMMPS_VERSION}/

mkdir -p ${BUILD_TMP_DIR}/lammps-${LAMMPS_VERSION}/build
cd ${BUILD_TMP_DIR}/lammps-${LAMMPS_VERSION}/build
cmake -C ../cmake/presets/all_off.cmake -D PKG_USER-DEEPMD=ON -D PKG_KSPACE=ON -D CMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} -D CMAKE_CXX_FLAGS="${PREC_DEF} -I${INSTALL_PREFIX}/include -L${INSTALL_PREFIX}/lib -Wl,--no-as-needed -lrt -ldeepmd_op -ldeepmd -ldeepmd_cc${PREC_SUFFIX} -ltensorflow_cc -ltensorflow_framework -Wl,-rpath=${INSTALL_PREFIX}/lib" ../cmake
cmake -C ../cmake/presets/all_off.cmake -D PKG_USER-DEEPMD=ON -D PKG_KSPACE=ON -D CMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} -D CMAKE_CXX_FLAGS="${PREC_DEF} -I${INSTALL_PREFIX}/include -L${INSTALL_PREFIX}/lib -Wl,--no-as-needed -lrt -ldeepmd_cc${PREC_SUFFIX} -ltensorflow_cc -ltensorflow_framework" ../cmake

make -j${NPROC}
make install
Expand Down
6 changes: 4 additions & 2 deletions source/ipi/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
# md

list (APPEND MD_INCLUDE_PATH "include")
include_directories (${MD_INCLUDE_PATH})

file(GLOB IN_SRC src/*.cc src/*.c)

function(_add_ipi_variant variant_name prec_def)
set (ipiname "dp_ipi${variant_name}")
set (libipiname "${LIB_DEEPMD_IPI}${variant_name}")
add_library(${libipiname} SHARED ${IN_SRC})
target_include_directories(${libipiname} PUBLIC ${MD_INCLUDE_PATH})

set(DRIVER_SOURCE_FILES driver.cc)
add_executable(${ipiname} ${DRIVER_SOURCE_FILES})
target_link_libraries(${ipiname} ${libipiname} ${LIB_DEEPMD_OP} ${LIB_DEEPMD_CC}${variant_name} ${LIB_DEEPMD} ${TensorFlow_LIBRARY})
# link: libdeepmd_cc
target_link_libraries(${ipiname} PRIVATE ${libipiname} ${LIB_DEEPMD_CC}${variant_name})
target_include_directories(${ipiname} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../3rdparty/)

set_target_properties(
${ipiname}
Expand Down
19 changes: 9 additions & 10 deletions source/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
# libmd
set (libname ${LIB_DEEPMD})

if (USE_CUDA_TOOLKIT)
include_directories("${CUDA_INCLUDE_DIRS}")
endif()

if (USE_ROCM_TOOLKIT)
include_directories("${ROCM_INCLUDE_DIRS}")
endif()

file(GLOB LIB_SRC src/*.cc src/*.cpp)
file(GLOB INC_SRC include/*.h ${CMAKE_CURRENT_BINARY_DIR}/version.h)

add_library(${libname} SHARED ${LIB_SRC})
target_include_directories(${libname} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)

if (USE_CUDA_TOOLKIT)
add_definitions("-DGOOGLE_CUDA")
add_subdirectory(src/cuda)
set (EXTRA_LIBS ${EXTRA_LIBS} deepmd_op_cuda)
target_link_libraries (${libname} ${CUDA_LIBRARIES} ${EXTRA_LIBS})
target_link_libraries (${libname} PRIVATE ${CUDA_LIBRARIES} ${EXTRA_LIBS})
# gpu_cuda.h
target_include_directories(${libname} PUBLIC ${CUDA_INCLUDE_DIRS})
endif()

if (USE_ROCM_TOOLKIT)
add_definitions("-DTENSORFLOW_USE_ROCM")
add_subdirectory(src/rocm)
set (EXTRA_LIBS ${EXTRA_LIBS} deepmd_op_rocm)
target_link_libraries (${libname} ${ROCM_LIBRARIES} ${EXTRA_LIBS})
target_link_libraries (${libname} PRIVATE ${ROCM_LIBRARIES} ${EXTRA_LIBS})
# gpu_rocm.h
target_include_directories(${libname} PUBLIC ${ROCM_INCLUDE_DIRS})
endif()

set_target_properties(${libname} PROPERTIES INSTALL_RPATH $ORIGIN)

if(BUILD_PY_IF)
install(TARGETS ${libname} DESTINATION deepmd/op/)
endif(BUILD_PY_IF)
Expand Down
1 change: 1 addition & 0 deletions source/lib/src/cuda/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ endif()
file (GLOB SOURCE_FILES "*.cu" )

cuda_add_library(deepmd_op_cuda SHARED ${SOURCE_FILES})
target_include_directories(deepmd_op_cuda PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../include/)

if (BUILD_CPP_IF)
install(TARGETS deepmd_op_cuda DESTINATION lib/)
Expand Down
1 change: 1 addition & 0 deletions source/lib/src/rocm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ set (HIP_HIPCC_FLAGS -hc; -fno-gpu-rdc; --amdgpu-target=gfx906; -fPIC; -O3; --st
file (GLOB SOURCE_FILES "*.hip.cu" )

hip_add_library(deepmd_op_rocm SHARED ${SOURCE_FILES})
target_include_directories(deepmd_op_rocm PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../include/)

install(TARGETS deepmd_op_rocm DESTINATION lib/)
if (BUILD_CPP_IF)
Expand Down
2 changes: 1 addition & 1 deletion source/lmp/env.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ TF_RPATH=`echo $TENSORFLOW_LIBRARY_PATH | sed "s/;/ -Wl,-rpath=/g"`

NNP_INC=" -std=c++11 -D@prec_def@ @TTM_DEF@ @OLD_LMP_PPPM_DEF@ -I$TF_INCLUDE_DIRS -I$DEEPMD_ROOT/include/ "
NNP_PATH=" -L$TF_LIBRARY_PATH -L$DEEPMD_ROOT/lib"
NNP_LIB=" -Wl,--no-as-needed -l@LIB_DEEPMD_OP_DEVICE@ -l@LIB_DEEPMD_OP@ -l@LIB_DEEPMD_CC@@variant_name@ -l@LIB_DEEPMD@ -ltensorflow_cc -ltensorflow_framework -Wl,-rpath=$TF_RPATH -Wl,-rpath=$DEEPMD_ROOT/lib"
NNP_LIB=" -Wl,--no-as-needed -l@LIB_DEEPMD_CC@@variant_name@ -ltensorflow_cc -ltensorflow_framework -Wl,-rpath=$TF_RPATH -Wl,-rpath=$DEEPMD_ROOT/lib"
9 changes: 5 additions & 4 deletions source/md/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ find_package(xdrfile REQUIRED)

list (APPEND MD_INCLUDE_PATH "include")
list (APPEND MD_INCLUDE_PATH ${XDRFILE_INCLUDE_DIRS})
include_directories (${MD_INCLUDE_PATH})

file(GLOB MD_SRC src/*.cc src/*.cpp)
add_library(${LIB_DEEPMD_NATIVE} SHARED ${MD_SRC})
target_include_directories(${LIB_DEEPMD_NATIVE} PUBLIC ${MD_INCLUDE_PATH})

set(MDNN_SOURCE_FILES mdnn.cc)
if (MAKE_FF_AD)
Expand All @@ -28,10 +28,11 @@ if (MAKE_FF_AD)
add_executable(${dp_mdad_name} ${MDAD_SOURCE_FILES})
endif()

target_link_libraries(${dp_mdnn_name} ${LIB_DEEPMD_NATIVE} ${LIB_DEEPMD_OP} ${LIB_DEEPMD} ${XDRFILE_LIBRARIES} ${TensorFlow_LIBRARY})
# link: libdeepmd_native libdeepmd_cc libxdr
target_link_libraries(${dp_mdnn_name} PRIVATE ${LIB_DEEPMD_NATIVE} ${LIB_DEEPMD_CC} ${XDRFILE_LIBRARIES})
if (MAKE_FF_AD)
target_link_libraries(${dp_mdad_name} ${LIB_DEEPMD_NATIVE} ${LIB_DEEPMD_OP} ${LIB_DEEPMD} ${XDRFILE_LIBRARIES} ${TensorFlow_LIBRARY})
target_link_libraries(${dp_mdff_name} ${LIB_DEEPMD_NATIVE} ${LIB_DEEPMD} ${XDRFILE_LIBRARIES} ${TensorFlow_LIBRARY})
target_link_libraries(${dp_mdad_name} PRIVATE ${LIB_DEEPMD_NATIVE} ${LIB_DEEPMD_CC} ${XDRFILE_LIBRARIES})
target_link_libraries(${dp_mdff_name} PRIVATE ${LIB_DEEPMD_NATIVE} ${LIB_DEEPMD_CC} ${XDRFILE_LIBRARIES})
endif()

set_target_properties(
Expand Down
54 changes: 17 additions & 37 deletions source/op/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,61 +10,41 @@ file(GLOB OP_GRADS_SRC custom_op.cc prod_force_grad.cc prod_force_grad_multi_dev
file(GLOB OP_PY *.py)

if (BUILD_CPP_IF)
if (USE_CUDA_TOOLKIT)
add_library(${LIB_DEEPMD_OP} SHARED ${OP_CUDA_SRC})
find_package(CUDA REQUIRED)
include_directories(${CUDA_INCLUDE_DIRS})
target_link_libraries (${LIB_DEEPMD_OP} ${CUDA_LIBRARIES})
elseif (USE_ROCM_TOOLKIT)
add_library(${LIB_DEEPMD_OP} SHARED ${OP_ROCM_SRC})
find_package(ROCM REQUIRED)
include_directories(${ROCM_INCLUDE_DIRS})
target_link_libraries (${LIB_DEEPMD_OP} ${ROCM_LIBRARIES})
else ()
add_library(${LIB_DEEPMD_OP} SHARED ${OP_SRC})
endif()
add_library(${LIB_DEEPMD_OP} SHARED ${OP_SRC})
# link: libdeepmd libtensorflow_cc libtensorflow_framework
target_link_libraries (${LIB_DEEPMD_OP} PUBLIC ${TensorFlow_LIBRARY} ${TensorFlowFramework_LIBRARY})
target_link_libraries (${LIB_DEEPMD_OP} PRIVATE ${LIB_DEEPMD})
target_include_directories(${LIB_DEEPMD_OP} PUBLIC ${TensorFlow_INCLUDE_DIRS})
set_target_properties(${LIB_DEEPMD_OP} PROPERTIES INSTALL_RPATH "$ORIGIN;${TensorFlow_LIBRARY_PATH}")
endif (BUILD_CPP_IF)

if (BUILD_PY_IF)
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(CMAKE_INSTALL_RPATH $ORIGIN)
if (USE_CUDA_TOOLKIT)
add_library(op_abi SHARED ${OP_SRC} ${OP_LIB})
add_library(op_grads SHARED ${OP_GRADS_SRC})
find_package(CUDA REQUIRED)
include_directories(${CUDA_INCLUDE_DIRS})
target_link_libraries (op_abi ${LIB_DEEPMD_OP_DEVICE})
target_link_libraries (op_grads ${LIB_DEEPMD_OP_DEVICE})
elseif(USE_ROCM_TOOLKIT)
add_library(op_abi SHARED ${OP_SRC} ${OP_LIB})
add_library(op_grads SHARED ${OP_GRADS_SRC})
find_package(ROCM REQUIRED)
include_directories(${ROCM_INCLUDE_DIRS})
target_link_libraries (op_abi ${LIB_DEEPMD_OP_DEVICE})
target_link_libraries (op_grads ${LIB_DEEPMD_OP_DEVICE})
else()
add_library(op_abi SHARED ${OP_SRC} ${OP_LIB})
add_library(op_grads SHARED ${OP_GRADS_SRC})
endif()
add_library(op_abi SHARED ${OP_SRC} ${OP_LIB})
add_library(op_grads SHARED ${OP_GRADS_SRC})

message(STATUS ${TensorFlowFramework_LIBRARY})
target_link_libraries(op_abi ${LIB_DEEPMD})
target_link_libraries(op_grads ${LIB_DEEPMD})
# link: libdeepmd libtensorflow_framework
target_link_libraries(op_abi PRIVATE ${LIB_DEEPMD})
target_link_libraries(op_grads PRIVATE ${LIB_DEEPMD})
target_link_libraries(
op_abi ${TensorFlowFramework_LIBRARY}
op_abi PRIVATE ${TensorFlowFramework_LIBRARY}
)
target_link_libraries(
op_grads ${TensorFlowFramework_LIBRARY}
op_grads PRIVATE ${TensorFlowFramework_LIBRARY}
)
target_include_directories(op_abi PUBLIC ${TensorFlow_INCLUDE_DIRS})
target_include_directories(op_grads PUBLIC ${TensorFlow_INCLUDE_DIRS})
set_target_properties(
op_abi
PROPERTIES
COMPILE_FLAGS ${OP_CXX_FLAG}
INSTALL_RPATH $ORIGIN
)
set_target_properties(
op_grads
PROPERTIES
COMPILE_FLAGS ${OP_CXX_FLAG}
INSTALL_RPATH $ORIGIN
)
endif (BUILD_PY_IF)

Expand Down

0 comments on commit e01e7d7

Please sign in to comment.