From e01e7d78f5849c2e6e78a3bf79245fe748fd8ecd Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Mon, 9 Aug 2021 20:49:28 -0400 Subject: [PATCH] clean library and header dependencies (#930) * clean library and header dependencies Fix #926. * fix typo in rocm * set INSTALL_RPATH for libraries --- doc/getting-started.md | 2 +- source/CMakeLists.txt | 12 ------- source/api_cc/CMakeLists.txt | 20 +++-------- source/install/build_lammps.sh | 2 +- source/ipi/CMakeLists.txt | 6 ++-- source/lib/CMakeLists.txt | 19 +++++------ source/lib/src/cuda/CMakeLists.txt | 1 + source/lib/src/rocm/CMakeLists.txt | 1 + source/lmp/env.sh.in | 2 +- source/md/CMakeLists.txt | 9 ++--- source/op/CMakeLists.txt | 54 ++++++++++-------------------- 11 files changed, 45 insertions(+), 83 deletions(-) diff --git a/doc/getting-started.md b/doc/getting-started.md index 82c8e0937a..0965b72661 100644 --- a/doc/getting-started.md +++ b/doc/getting-started.md @@ -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 diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 81acb5bf8f..414b5b1564 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -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) diff --git a/source/api_cc/CMakeLists.txt b/source/api_cc/CMakeLists.txt index 4389fdfc92..895a06baea 100644 --- a/source/api_cc/CMakeLists.txt +++ b/source/api_cc/CMakeLists.txt @@ -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) @@ -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/) diff --git a/source/install/build_lammps.sh b/source/install/build_lammps.sh index 6bcec33685..28002dde6f 100755 --- a/source/install/build_lammps.sh +++ b/source/install/build_lammps.sh @@ -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 diff --git a/source/ipi/CMakeLists.txt b/source/ipi/CMakeLists.txt index cdb3f80b08..d71a904e36 100644 --- a/source/ipi/CMakeLists.txt +++ b/source/ipi/CMakeLists.txt @@ -1,7 +1,6 @@ # md list (APPEND MD_INCLUDE_PATH "include") -include_directories (${MD_INCLUDE_PATH}) file(GLOB IN_SRC src/*.cc src/*.c) @@ -9,10 +8,13 @@ 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} diff --git a/source/lib/CMakeLists.txt b/source/lib/CMakeLists.txt index c1369c23ee..c040be3be5 100644 --- a/source/lib/CMakeLists.txt +++ b/source/lib/CMakeLists.txt @@ -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) diff --git a/source/lib/src/cuda/CMakeLists.txt b/source/lib/src/cuda/CMakeLists.txt index 2d9aa03cf6..eb1d1893bb 100644 --- a/source/lib/src/cuda/CMakeLists.txt +++ b/source/lib/src/cuda/CMakeLists.txt @@ -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/) diff --git a/source/lib/src/rocm/CMakeLists.txt b/source/lib/src/rocm/CMakeLists.txt index 393844b8bb..602f2f4524 100644 --- a/source/lib/src/rocm/CMakeLists.txt +++ b/source/lib/src/rocm/CMakeLists.txt @@ -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) diff --git a/source/lmp/env.sh.in b/source/lmp/env.sh.in index 25ca8dc90c..c29519913c 100644 --- a/source/lmp/env.sh.in +++ b/source/lmp/env.sh.in @@ -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" diff --git a/source/md/CMakeLists.txt b/source/md/CMakeLists.txt index 4de0c00d6e..647a902d8d 100644 --- a/source/md/CMakeLists.txt +++ b/source/md/CMakeLists.txt @@ -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) @@ -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( diff --git a/source/op/CMakeLists.txt b/source/op/CMakeLists.txt index 1075847953..abe81d6f32 100644 --- a/source/op/CMakeLists.txt +++ b/source/op/CMakeLists.txt @@ -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)