From 2d9052bc7dfc4d40e915820e0bede05c626b588d Mon Sep 17 00:00:00 2001 From: Sergei Lebedev Date: Sun, 4 Jun 2017 02:14:51 +0200 Subject: [PATCH] libxgboost4j is now part of the CMake build (#2373) * [jvm-packages] Added libxgboost4j to CMake build * [jvm-packages] Wired CMake build into create_jni.sh * User newer CMake version on Travis * Lowered CMake version constraints * Fixed various quirks in the new CMake build --- .travis.yml | 3 ++ CMakeLists.txt | 47 ++++++++++++------- jvm-packages/create_jni.sh | 13 +++-- .../dmlc/xgboost4j/java/NativeLibLoader.java | 2 +- .../xgboost4j/src/native/xgboost4j.cpp | 8 ++-- 5 files changed, 48 insertions(+), 25 deletions(-) diff --git a/.travis.yml b/.travis.yml index 03480cdff..dc65a1180 100644 --- a/.travis.yml +++ b/.travis.yml @@ -45,7 +45,10 @@ addons: apt: sources: - ubuntu-toolchain-r-test + - george-edison55-precise-backports packages: + - cmake + - cmake-data - doxygen - wget - libcurl4-openssl-dev diff --git a/CMakeLists.txt b/CMakeLists.txt index f71cc769e..36c70206b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required (VERSION 3.5) +cmake_minimum_required (VERSION 3.2) project (xgboost) find_package(OpenMP) @@ -50,12 +50,12 @@ if(MSVC) endif() include_directories ( - ${PROJECT_SOURCE_DIR}/include + ${PROJECT_SOURCE_DIR}/include ${PROJECT_SOURCE_DIR}/dmlc-core/include - ${PROJECT_SOURCE_DIR}/rabit/include - ) + ${PROJECT_SOURCE_DIR}/rabit/include +) -file(GLOB SOURCES +file(GLOB SOURCES src/c_api/*.cc src/common/*.cc src/data/*.cc @@ -73,7 +73,7 @@ set(RABIT_SOURCES rabit/src/c_api.cc ) -add_subdirectory(dmlc-core) +add_subdirectory(dmlc-core) add_library(rabit STATIC ${RABIT_SOURCES}) @@ -91,6 +91,8 @@ else() set(CMAKE_SHARED_LIBRARY_PREFIX "") endif() +set(LINK_LIBRARIES dmlccore rabit) + option(PLUGIN_UPDATER_GPU "Build GPU accelerated tree construction plugin") if(PLUGIN_UPDATER_GPU) @@ -101,24 +103,37 @@ if(PLUGIN_UPDATER_GPU) if(NOT MSVC) set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS};-Xcompiler -fPIC") endif() - set(SOURCES ${SOURCES} + set(SOURCES ${SOURCES} plugin/updater_gpu/src/updater_gpu.cc ) find_package(CUDA QUIET REQUIRED) file(GLOB_RECURSE CUDA_SOURCES "plugin/updater_gpu/src/*") - cuda_add_library(updater_gpu STATIC + cuda_add_library(updater_gpu STATIC ${CUDA_SOURCES} ) + + set(LINK_LIBRARIES ${LINK_LIBRARIES} updater_gpu) endif() -add_executable(xgboost ${SOURCES}) -add_library(libxgboost SHARED ${SOURCES}) -target_link_libraries(xgboost dmlccore rabit) -target_link_libraries(libxgboost dmlccore rabit) +add_library(objxgboost OBJECT ${SOURCES}) +set_target_properties(${objxgboost} PROPERTIES POSITION_INDEPENDENT_CODE 1) +add_library(libxgboost SHARED $) +add_executable(xgboost $) +target_link_libraries(xgboost ${LINK_LIBRARIES}) +target_link_libraries(libxgboost ${LINK_LIBRARIES}) -if(PLUGIN_UPDATER_GPU) - target_link_libraries(xgboost updater_gpu) - target_link_libraries(libxgboost updater_gpu) +option(JVM_BINDINGS "Build JVM bindings" OFF) + +if(JVM_BINDINGS) + find_package(JNI QUIET REQUIRED) + + include_directories(${JNI_INCLUDE_DIRS} jvm-packages/xgboost4j/src/native) + + add_library(libxgboost4j SHARED + $ + jvm-packages/xgboost4j/src/native/xgboost4j.cpp) + target_link_libraries(libxgboost4j + ${LINK_LIBRARIES} + ${JNI_LIBRARIES}) endif() - diff --git a/jvm-packages/create_jni.sh b/jvm-packages/create_jni.sh index cf14d7f2d..902be52e2 100755 --- a/jvm-packages/create_jni.sh +++ b/jvm-packages/create_jni.sh @@ -9,18 +9,21 @@ pushd `dirname $0` > /dev/null #settings according to os dl="so" -dis_omp=0 +USE_OMP=ON if [ $(uname) == "Darwin" ]; then export JAVA_HOME=$(/usr/libexec/java_home) dl="dylib" #change this to 0 if your compiler support openmp - dis_omp=1 + USE_OMP=OFF fi cd .. -make jvm no_omp=${dis_omp} -cd jvm-packages +mkdir -p build +cd build +cmake .. -DJVM_BINDINGS:BOOL=ON -DUSE_OPENMP:BOOL=${USE_OMP} +make +cd ../jvm-packages echo "move native lib" libPath="xgboost4j/src/main/resources/lib" @@ -29,7 +32,7 @@ if [ ! -d "$libPath" ]; then fi rm -f xgboost4j/src/main/resources/lib/libxgboost4j.${dl} -mv lib/libxgboost4j.so xgboost4j/src/main/resources/lib/libxgboost4j.${dl} +cp ../lib/libxgboost4j.${dl} xgboost4j/src/main/resources/lib/libxgboost4j.${dl} # copy python to native resources cp ../dmlc-core/tracker/dmlc_tracker/tracker.py xgboost4j/src/main/resources/tracker.py # copy test data files diff --git a/jvm-packages/xgboost4j/src/main/java/ml/dmlc/xgboost4j/java/NativeLibLoader.java b/jvm-packages/xgboost4j/src/main/java/ml/dmlc/xgboost4j/java/NativeLibLoader.java index 0ab2ca077..3fa05e0ed 100644 --- a/jvm-packages/xgboost4j/src/main/java/ml/dmlc/xgboost4j/java/NativeLibLoader.java +++ b/jvm-packages/xgboost4j/src/main/java/ml/dmlc/xgboost4j/java/NativeLibLoader.java @@ -31,7 +31,7 @@ class NativeLibLoader { private static final Log logger = LogFactory.getLog(NativeLibLoader.class); private static boolean initialized = false; - private static final String nativePath = "../lib/"; + private static final String nativePath = "../../lib/"; private static final String nativeResourcePath = "/lib/"; private static final String[] libNames = new String[]{"xgboost4j"}; diff --git a/jvm-packages/xgboost4j/src/native/xgboost4j.cpp b/jvm-packages/xgboost4j/src/native/xgboost4j.cpp index 6b027e2f5..bfd302ef1 100644 --- a/jvm-packages/xgboost4j/src/native/xgboost4j.cpp +++ b/jvm-packages/xgboost4j/src/native/xgboost4j.cpp @@ -25,7 +25,7 @@ // set handle void setHandle(JNIEnv *jenv, jlongArray jhandle, void* handle) { #ifdef __APPLE__ - long out = (long) handle; + jlong out = (long) handle; #else int64_t out = (int64_t) handle; #endif @@ -76,7 +76,8 @@ XGB_EXTERN_C int XGBoost4jCallbackDataIterNext( batch, jenv->GetFieldID(batchClass, "featureValue", "[F")); XGBoostBatchCSR cbatch; cbatch.size = jenv->GetArrayLength(joffset) - 1; - cbatch.offset = jenv->GetLongArrayElements(joffset, 0); + cbatch.offset = reinterpret_cast( + jenv->GetLongArrayElements(joffset, 0)); if (jlabel != nullptr) { cbatch.label = jenv->GetFloatArrayElements(jlabel, 0); CHECK_EQ(jenv->GetArrayLength(jlabel), static_cast(cbatch.size)) @@ -103,7 +104,8 @@ XGB_EXTERN_C int XGBoost4jCallbackDataIterNext( CHECK_EQ((*set_function)(set_function_handle, cbatch), 0) << XGBGetLastError(); // release the elements. - jenv->ReleaseLongArrayElements(joffset, cbatch.offset, 0); + jenv->ReleaseLongArrayElements( + joffset, reinterpret_cast(cbatch.offset), 0); jenv->DeleteLocalRef(joffset); if (jlabel != nullptr) { jenv->ReleaseFloatArrayElements(jlabel, cbatch.label, 0);