SINGA-244 Separating swig interface and python binding files - add java binding cmake files - todo: add test code
Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/32ba40d2 Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/32ba40d2 Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/32ba40d2 Branch: refs/heads/master Commit: 32ba40d2c9ed659eb0cc89960af49156c191bd61 Parents: d76caea Author: aaronwwf <[email protected]> Authored: Wed Sep 7 15:09:07 2016 +0800 Committer: aaronwwf <[email protected]> Committed: Wed Sep 7 15:09:07 2016 +0800 ---------------------------------------------------------------------- CMakeLists.txt | 5 +- cmake/Dependencies.cmake | 6 ++ java/CMakeLists.txt | 138 ++++++++++++++++++++++++++++++++++++++++++ src/api/.gitignore | 1 + src/api/config.i | 4 -- src/api/config.i.in | 2 + src/api/core_tensor.i | 3 + src/proto/core.proto | 2 + src/proto/io.proto | 1 + src/proto/model.proto | 1 + test/java/Test.java | 12 ++++ 11 files changed, 169 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/32ba40d2/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/CMakeLists.txt b/CMakeLists.txt index 762839b..e799348 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,7 +38,7 @@ SET(SINGA_INCLUDE_DIR INCLUDE_DIRECTORIES(${SINGA_INCLUDE_DIR}) OPTION(USE_CBLAS "Use CBlas libs" ON) -OPTION(USE_CUDA "Use Cuda libs" ON) +OPTION(USE_CUDA "Use Cuda libs" OFF) OPTION(USE_CUDNN "Use Cudnn libs" ON) OPTION(USE_OPENCV "Use opencv" OFF) OPTION(USE_LMDB "Use LMDB libs" OFF) @@ -54,7 +54,7 @@ ADD_DEFINITIONS(-DUSE_CMAKE) CONFIGURE_FILE ( "${PROJECT_SOURCE_DIR}/cmake/Templates/singa_config.h.in" - "${PROJECT_SOURCE_DIR}/include/singa/singa_config.h") + "${PROJECT_BINARY_DIR}/include/singa/singa_config.h") #set(SINGA_CONFIGURE_SRC "${PROJECT_BINARY_DIR}/singa_config.h") #LIST(APPEND SRCS ${SINGA_CONFIGURE_SRCS} ${PROJECT_BINARY_DIR}/singa_config.h) @@ -83,6 +83,7 @@ IF (USE_PYTHON) ENDIF() IF (USE_JAVA) + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing ") ADD_SUBDIRECTORY(java) ENDIF() http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/32ba40d2/cmake/Dependencies.cmake ---------------------------------------------------------------------- diff --git a/cmake/Dependencies.cmake b/cmake/Dependencies.cmake index d4e68ac..5f3d6a7 100644 --- a/cmake/Dependencies.cmake +++ b/cmake/Dependencies.cmake @@ -92,3 +92,9 @@ IF(USE_PYTHON) FIND_PACKAGE(PythonInterp 2.7 REQUIRED) FIND_PACKAGE(SWIG 3.0 REQUIRED) ENDIF() + +IF(USE_JAVA) + FIND_PACKAGE(Java) + FIND_PACKAGE(JNI ) + FIND_PACKAGE(SWIG 3.0 REQUIRED) +ENDIF() http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/32ba40d2/java/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/java/CMakeLists.txt b/java/CMakeLists.txt new file mode 100644 index 0000000..ef38461 --- /dev/null +++ b/java/CMakeLists.txt @@ -0,0 +1,138 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# This following function is taken from +# https://github.com/Kitware/CMake/blob/master/Modules/FindProtobuf.cmake +# and modified to our compilation. +function(PROTOBUF_GENERATE_JAVA OUTPUT) + if(NOT ARGN) + message(SEND_ERROR "Error: PROTOBUF_GENERATE_JAVA() called + without any proto files") + return() + endif(NOT ARGN) + + set(${OUTPUT}) + foreach(FIL ${ARGN}) + get_filename_component(ABS_FIL ${FIL} ABSOLUTE) + get_filename_component(FIL_WE ${FIL} NAME_WE) + get_filename_component(PATH ${FIL} PATH) + + list(APPEND ${OUTPUT} "${CMAKE_BINARY_DIR}/java/singa/proto/${FIL_WE}.java") + + add_custom_command( + OUTPUT "${CMAKE_BINARY_DIR}/java/singa/proto/${FIL_WE}.java" + COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} + ARGS --java_out ${CMAKE_BINARY_DIR}/java + --proto_path ${PATH} ${ABS_FIL} + DEPENDS ${ABS_FIL} + COMMENT "Running java protocol buffer compiler on ${FIL}" VERBATIM) + endforeach() + + set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE) + set(${OUTPUT} ${${OUTPUT}} PARENT_SCOPE) +endfunction() + +function (create_symlinks) + # Do nothing if building in-source + if (${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) + return() + endif() + + foreach (path_file ${ARGN}) + get_filename_component(folder ${path_file} PATH) + + # Delete symlink if it exists + file(REMOVE "${CMAKE_CURRENT_BINARY_DIR}/${path_file}") + + # Get OS dependent path to use in `execute_process` + file(TO_NATIVE_PATH "${CMAKE_CURRENT_BINARY_DIR}/${path_file}" link) + file(TO_NATIVE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/${path_file}" target) + + if (UNIX) + set(command ln -s ${target} ${link}) + else() + set(command cmd.exe /c mklink ${link} ${target}) + endif() + + execute_process(COMMAND ${command} + RESULT_VARIABLE result + ERROR_VARIABLE output) + + if (NOT ${result} EQUAL 0) + message(FATAL_ERROR "Could not create symbolic link for: ${target} --> ${output}") + endif() + + endforeach(path_file) +endfunction(create_symlinks) + + +# generate protobuf sources +FILE(GLOB proto_files ${CMAKE_SOURCE_DIR}/src/proto/*.proto) +PROTOBUF_GENERATE_JAVA(proto_javas ${proto_files}) +MESSAGE(STATUS "proto javas: ${proto_javas}") + +# generate cxx and wrap.py +if(NOT EXISTS "${CMKAE_BINARY_DIR}/java") + execute_process( + COMMAND mkdir ${CMAKE_BINARY_DIR}/java + COMMAND mkdir ${CMAKE_BINARY_DIR}/java/singa + COMMAND mkdir ${CMAKE_BINARY_DIR}/java/singa/proto + ERROR_QUIET) +endif() +execute_process( + COMMAND mkdir ${CMAKE_BINARY_DIR}/src/api + COMMAND swig -c++ -java -I${CMAKE_SOURCE_DIR}/include + -I${JAVA_INCLUDE_PATH} -I${JAVA_INCLUDE_PATH2} + -outdir ${CMAKE_BINARY_DIR}/java/singa + -package singa + -o ${CMAKE_BINARY_DIR}/src/api/singa_java_wrap.cxx + ${CMAKE_SOURCE_DIR}/src/api/singa.i ) + +#MESSAGE(STATUS "java include: ${JAVA_INCLUDE_DIRS} ${JNI_INCLUDE_DIRS} ${JAVA_INCLUDE_PATH} ${JAVA_INCLUDE_PATH2}") +set(java_srcs "${CMAKE_BINARY_DIR}/src/api/singa_java_wrap.cxx") + +#Create symlinks for all java source files Do not omit !!!RELATIVE!!! +file(GLOB_RECURSE java_source_files RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.java) +create_symlinks(${java_source_files}) + +IF(USE_CUDA) +# remain this custom command to avoid cuda objs can't find +ADD_CUSTOM_COMMAND( + OUTPUT ${global_cuda_objs} + COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/" + ) +ENDIF(USE_CUDA) + + +ADD_LIBRARY(singa_wrap SHARED $<TARGET_OBJECTS:singa_objects> ${java_srcs} ${proto_javas} ${global_cuda_objs}) +TARGET_LINK_LIBRARIES(singa_wrap ${SINGA_LINKER_LIBS} ${JNI_LIBRARIES}) +TARGET_INCLUDE_DIRECTORIES(singa_wrap PRIVATE ${JAVA_INCLUDE_PATH} ${JAVA_INCLUDE_PATH2}) + +SET_TARGET_PROPERTIES(singa_wrap + PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/java/singa +) + +IF(APPLE) +ADD_CUSTOM_TARGET( + change_suffix ALL + COMMAND ${CMAKE_COMMAND} -E rename "${CMAKE_BINARY_DIR}/java/singa/libsinga_wrap.dylib" "${CMAKE_BINARY_DIR}/java/singa/libsinga_wrap.so" + COMMENT "change .dylib to .so in mac system" +) +ADD_DEPENDENCIES(change_suffix _singa_wrap) +ENDIF(APPLE) + http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/32ba40d2/src/api/.gitignore ---------------------------------------------------------------------- diff --git a/src/api/.gitignore b/src/api/.gitignore new file mode 100644 index 0000000..adb5d03 --- /dev/null +++ b/src/api/.gitignore @@ -0,0 +1 @@ +config.i http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/32ba40d2/src/api/config.i ---------------------------------------------------------------------- diff --git a/src/api/config.i b/src/api/config.i deleted file mode 100644 index cfbcd46..0000000 --- a/src/api/config.i +++ /dev/null @@ -1,4 +0,0 @@ -// Pass in cmake configurations to swig -#define USE_CUDA 1 -#define USE_CUDNN 1 -#define CUDNN_VERSION_SWIG 5005 http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/32ba40d2/src/api/config.i.in ---------------------------------------------------------------------- diff --git a/src/api/config.i.in b/src/api/config.i.in index 5743ba3..7da916c 100644 --- a/src/api/config.i.in +++ b/src/api/config.i.in @@ -1,4 +1,6 @@ // Pass in cmake configurations to swig #cmakedefine01 USE_CUDA #cmakedefine01 USE_CUDNN +#cmakedefine01 USE_PYTHON +#cmakedefine01 USE_JAVA #cmakedefine CUDNN_VERSION_SWIG ${CUDNN_VERSION_SWIG} http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/32ba40d2/src/api/core_tensor.i ---------------------------------------------------------------------- diff --git a/src/api/core_tensor.i b/src/api/core_tensor.i index 60f8b45..d85e7f0 100644 --- a/src/api/core_tensor.i +++ b/src/api/core_tensor.i @@ -44,10 +44,13 @@ using singa::DataType; %} %shared_ptr(singa::Device) +#if USE_PYTHON %include "numpy.i" %init %{ import_array(); %} +#endif //USE_PYTHON + %apply (float *IN_ARRAY1, int DIM1) { (const float *src, const size_t num) } http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/32ba40d2/src/proto/core.proto ---------------------------------------------------------------------- diff --git a/src/proto/core.proto b/src/proto/core.proto index c88bee9..dc5ed34 100644 --- a/src/proto/core.proto +++ b/src/proto/core.proto @@ -18,6 +18,8 @@ package singa; +option java_package = "singa.proto"; + // TODO(wangwei) check protobuf version to include the syntax //syntax = "proto2"; http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/32ba40d2/src/proto/io.proto ---------------------------------------------------------------------- diff --git a/src/proto/io.proto b/src/proto/io.proto index f349f74..ab56983 100644 --- a/src/proto/io.proto +++ b/src/proto/io.proto @@ -18,6 +18,7 @@ package singa; +option java_package = "singa.proto"; message EncoderConf { optional string type = 1 [default = "jpg2proto"]; http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/32ba40d2/src/proto/model.proto ---------------------------------------------------------------------- diff --git a/src/proto/model.proto b/src/proto/model.proto index 3df68e2..6be5e0a 100644 --- a/src/proto/model.proto +++ b/src/proto/model.proto @@ -18,6 +18,7 @@ package singa; +option java_package = "singa.proto"; /// \file layer.proto is adapted from [Caffe](https://github.com/BVLC/caffe/)'s /// proto file with commit id c419f8517b1e1b3d7a07fe212fc6c90a70b519ea. We /// use caffe's protocol for configuring layer hyper-parameters for easy http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/32ba40d2/test/java/Test.java ---------------------------------------------------------------------- diff --git a/test/java/Test.java b/test/java/Test.java new file mode 100644 index 0000000..f9e5411 --- /dev/null +++ b/test/java/Test.java @@ -0,0 +1,12 @@ +import singa.*; + +public class Test { + static { + System.loadLibrary("singa_wrap"); + } + + public static void main(String argv[]) { + Tensor t = new Tensor(); + System.out.println(t); + } +}
