Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
[WIP]: improve scala maven jni build and packing.
Browse files Browse the repository at this point in the history
  • Loading branch information
frankfliu committed Dec 4, 2018
1 parent 8949989 commit 8b99ff3
Show file tree
Hide file tree
Showing 17 changed files with 228 additions and 96 deletions.
4 changes: 4 additions & 0 deletions scala-package/assembly/linux-x86_64-cpu/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<name>MXNet Scala Package - Full Linux-x86_64 CPU-only</name>
<packaging>jar</packaging>

<properties>
<MXNET_DIR>${project.parent.parent.basedir}/..</MXNET_DIR>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.mxnet</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@
</includes>
</dependencySet>
</dependencySets>
<files>
<file>
<source>${MXNET_DIR}/lib/libmxnet.so</source>
<outputDirectory>lib/native</outputDirectory>
</file>
</files>
</assembly>
4 changes: 4 additions & 0 deletions scala-package/assembly/linux-x86_64-gpu/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<name>MXNet Scala Package - Full Linux-x86_64 GPU</name>
<packaging>jar</packaging>

<properties>
<MXNET_DIR>${project.parent.parent.basedir}/..</MXNET_DIR>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.mxnet</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@
</includes>
</dependencySet>
</dependencySets>
<files>
<file>
<source>${MXNET_DIR}/lib/libmxnet.so</source>
<outputDirectory>lib/native</outputDirectory>
</file>
</files>
</assembly>
30 changes: 0 additions & 30 deletions scala-package/assembly/osx-x86_64-cpu/main/assembly/assembly.xml

This file was deleted.

4 changes: 4 additions & 0 deletions scala-package/assembly/osx-x86_64-cpu/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<name>MXNet Scala Package - Full OSX-x86_64 CPU-only</name>
<packaging>jar</packaging>

<properties>
<MXNET_DIR>${project.parent.parent.basedir}/..</MXNET_DIR>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.mxnet</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@
</includes>
</dependencySet>
</dependencySets>
<files>
<file>
<source>${MXNET_DIR}/lib/libmxnet.so</source>
<outputDirectory>lib/native</outputDirectory>
</file>
</files>
</assembly>
8 changes: 8 additions & 0 deletions scala-package/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

<properties>
<skipTests>true</skipTests>
<MXNET_DIR>${project.parent.basedir}/..</MXNET_DIR>
</properties>

<artifactId>mxnet-core_2.11</artifactId>
Expand Down Expand Up @@ -77,6 +78,9 @@
-Djava.library.path=${project.parent.basedir}/native/${platform}/target \
-Dlog4j.configuration=file://${project.basedir}/src/test/resources/log4j.properties
</argLine>
<environmentVariables>
<LD_LIBRARY_PATH>${MXNET_DIR}/lib</LD_LIBRARY_PATH>
</environmentVariables>
</configuration>
</plugin>
<plugin>
Expand All @@ -88,6 +92,10 @@
-Djava.library.path=${project.parent.basedir}/native/${platform}/target
</argLine>
<skipTests>${skipTests}</skipTests>
<forkMode>always</forkMode>
<environmentVariables>
<LD_LIBRARY_PATH>${MXNET_DIR}/lib</LD_LIBRARY_PATH>
</environmentVariables>
</configuration>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,10 @@ private[mxnet] object NativeLibraryLoader {
}
logger.debug(s"Attempting to load $loadLibname")
val libFileInJar = libPathInJar + loadLibname
val is: InputStream = getClass.getResourceAsStream(libFileInJar)
if (is == null) {
throw new UnsatisfiedLinkError(s"Couldn't find the resource $loadLibname")
}
logger.info(s"Loading $loadLibname from $libPathInJar copying to $libname")
loadLibraryFromStream(libname, is)
saveLibraryToTemp("libmxnet.so", "/lib/native/libmxnet.so")
val tempfile: File = saveLibraryToTemp(libname, libFileInJar)

loadLibraryFromFile(libname, tempfile)
}

/**
Expand All @@ -109,19 +107,42 @@ private[mxnet] object NativeLibraryLoader {

@throws(classOf[IOException])
private def createTempFile(name: String): File = {
new File(_tempDir + File.separator + name)
new File(_tempDir, name)
}

/**
* Load a system library from a stream. Copies the library to a temp file
* and loads from there.
*
* @param libname name of the library (just used in constructing the library name)
* @param is InputStream pointing to the library
* @param tempfile File pointing to the library
*/
private def loadLibraryFromStream(libname: String, is: InputStream) {
private def loadLibraryFromFile(libname: String, tempfile: File) {
try {
logger.debug("Loading library from {}", tempfile.getPath)
System.load(tempfile.getPath)
} catch {
case ule: UnsatisfiedLinkError =>
logger.error("Couldn't load copied link file: {}", ule.toString)
throw ule
}
}

/**
* Load a system library from a stream. Copies the library to a temp file
* and loads from there.
*
* @param libname name of the library (just used in constructing the library name)
* @param resource String resource path in the jar file
*/
private def saveLibraryToTemp(libname: String, resource: String): File = {
try {
val tempfile: File = createTempFile(libname)
val is: InputStream = getClass.getResourceAsStream(resource)
if (is == null) {
throw new UnsatisfiedLinkError(s"Couldn't find the resource $resource")
}

val tempfile: File = new File(_tempDir, libname)
val os: OutputStream = new FileOutputStream(tempfile)
logger.debug("tempfile.getPath() = {}", tempfile.getPath)
val savedTime: Long = System.currentTimeMillis
Expand All @@ -131,20 +152,14 @@ private[mxnet] object NativeLibraryLoader {
os.write(buf, 0, len)
len = is.read(buf)
}
os.flush()
val lock: InputStream = new FileInputStream(tempfile)
os.close()
is.close()
val seconds: Double = (System.currentTimeMillis - savedTime).toDouble / 1e3
logger.debug(s"Copying took $seconds seconds.")
logger.debug("Loading library from {}", tempfile.getPath)
System.load(tempfile.getPath)
lock.close()
logger.debug(s"Copying $libname took $seconds seconds.")
tempfile
} catch {
case io: IOException =>
logger.error("Could not create the temp file: {}", io.toString)
case ule: UnsatisfiedLinkError =>
logger.error("Couldn't load copied link file: {}", ule.toString)
throw ule
throw new UnsatisfiedLinkError(s"Could not create temp file for $libname")
}
}
}
4 changes: 4 additions & 0 deletions scala-package/examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<properties>
<skipTests>true</skipTests>
<MXNET_DIR>${project.parent.basedir}/..</MXNET_DIR>
</properties>

<profiles>
Expand Down Expand Up @@ -137,6 +138,9 @@
-Djava.library.path=${project.parent.basedir}/native/${platform}/target \
-Dlog4j.configuration=file://${project.basedir}/src/test/resources/log4j.properties
</argLine>
<environmentVariables>
<LD_LIBRARY_PATH>${MXNET_DIR}/lib</LD_LIBRARY_PATH>
</environmentVariables>
</configuration>
</plugin>
<plugin>
Expand Down
4 changes: 4 additions & 0 deletions scala-package/infer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<properties>
<skipTests>true</skipTests>
<MXNET_DIR>${project.parent.basedir}/..</MXNET_DIR>
</properties>

<profiles>
Expand Down Expand Up @@ -77,6 +78,9 @@
-Djava.library.path=${project.parent.basedir}/native/${platform}/target \
-Dlog4j.configuration=file://${project.basedir}/src/test/resources/log4j.properties
</argLine>
<environmentVariables>
<LD_LIBRARY_PATH>${MXNET_DIR}/lib</LD_LIBRARY_PATH>
</environmentVariables>
</configuration>
</plugin>
<plugin>
Expand Down
42 changes: 33 additions & 9 deletions scala-package/init-native/linux-x86_64/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

<packaging>so</packaging>

<properties>
<MXNET_DIR>${project.parent.parent.basedir}/..</MXNET_DIR>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.mxnet</groupId>
Expand Down Expand Up @@ -62,22 +66,24 @@
<compilerStartOption>-std=c++0x</compilerStartOption>
</compilerStartOptions>
<compilerEndOptions>
<compilerEndOption>-I${project.basedir}/../../../include</compilerEndOption>
<compilerEndOption>${all_includes}</compilerEndOption>
<compilerEndOption>${cflags}</compilerEndOption>
<compilerEndOption>-I${MXNET_DIR}/include</compilerEndOption>
<compilerEndOption>-I${MXNET_DIR}/3rdparty/dmlc-core/include</compilerEndOption>
<compilerEndOption>-I${MXNET_DIR}/3rdparty/mshadow</compilerEndOption>
<compilerEndOption>-I${MXNET_DIR}/3rdparty/dlpack/include</compilerEndOption>
<compilerEndOption>-I${MXNET_DIR}/3rdparty/tvm/nnvm/include</compilerEndOption>
<compilerEndOption>-DMSHADOW_USE_MKL=0 -DMSHADOW_USE_CUDA=0</compilerEndOption>
<compilerEndOption>-O3 -DNDEBUG=1 -fPIC -msse3 -mf16c</compilerEndOption>
<compilerEndOption>-Wall -Wsign-compare -Wno-unused-parameter -Wno-unknown-pragmas -Wno-unused-local-typedefs</compilerEndOption>
</compilerEndOptions>
<linkerStartOptions>
<linkerStartOption>-shared</linkerStartOption>
</linkerStartOptions>
<linkerMiddleOptions>
<linkerMiddleOption>${all_ldpaths}</linkerMiddleOption>
<linkerMiddleOption>-Wl,--whole-archive</linkerMiddleOption>
<linkerMiddleOption>${lddeps}</linkerMiddleOption>
<linkerMiddleOption>-Wl,--no-whole-archive</linkerMiddleOption>
<linkerMiddleOption>-Wl,--no-whole-archive -pthread -lm -fopenmp -lrt</linkerMiddleOption>
</linkerMiddleOptions>
<linkerEndOptions>
<linkerEndOption>${ldflags}</linkerEndOption>
<linkerEndOption>-fopenmp</linkerEndOption>
<linkerEndOption>-Wl,-rpath=${dollor}ORIGIN -lmxnet -L${MXNET_DIR}/lib</linkerEndOption>
</linkerEndOptions>
</configuration>

Expand All @@ -86,7 +92,6 @@
<id>javah</id>
<phase>generate-sources</phase>
<configuration>
<javahOS>linux</javahOS>
<javahProvider>default</javahProvider>
<javahOutputDirectory>${project.build.directory}/custom-javah</javahOutputDirectory>
<workingDirectory>${basedir}</workingDirectory>
Expand All @@ -101,6 +106,25 @@
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>link-native-lib</id>
<phase>generate-resources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>ln</executable>
<commandlineArgs>-sf ${MXNET_DIR}/lib/libmxnet.so ${project.build.directory}/libmxnet.so</commandlineArgs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Loading

0 comments on commit 8b99ff3

Please sign in to comment.