Skip to content

Commit

Permalink
[MXNET-1224]: improve scala maven jni build and packing. (apache#13493)
Browse files Browse the repository at this point in the history
Major JNI feature changes. Please find more info here: https://cwiki.apache.org/confluence/display/MXNET/Scala+maven+build+improvement
  • Loading branch information
frankfliu authored and lanking520 committed Apr 26, 2019
1 parent b2b49a5 commit 2e226ac
Show file tree
Hide file tree
Showing 18 changed files with 291 additions and 112 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 @@ -30,6 +30,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 @@ -41,4 +41,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 @@ -30,6 +30,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 @@ -41,4 +41,10 @@
</includes>
</dependencySet>
</dependencySets>
<files>
<file>
<source>${MXNET_DIR}/lib/libmxnet.so</source>
<outputDirectory>lib/native</outputDirectory>
</file>
</files>
</assembly>
46 changes: 0 additions & 46 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 @@ -30,6 +30,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 @@ -41,4 +41,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 @@ -28,6 +28,7 @@

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

<artifactId>mxnet-core_2.11</artifactId>
Expand Down Expand Up @@ -93,6 +94,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 @@ -104,6 +108,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 @@ -31,6 +31,7 @@

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

<profiles>
Expand Down Expand Up @@ -167,6 +168,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 @@ -31,6 +31,7 @@

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

<profiles>
Expand Down Expand Up @@ -93,6 +94,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 @@ -32,6 +32,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 @@ -78,22 +82,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=${dollar}ORIGIN -lmxnet -L${MXNET_DIR}/lib</linkerEndOption>
</linkerEndOptions>
</configuration>

Expand All @@ -102,7 +108,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 @@ -117,6 +122,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 2e226ac

Please sign in to comment.