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

[MXNET-1224]: improve scala maven jni build. #13493

Merged
merged 1 commit into from
Dec 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>

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>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use the C_FLAGS and LD_FLAGS defined and passed from the Makefile

<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>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we make them =0, if we build with GPU what will happened?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a README.md file to describe compiler and linker flags.

<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 @@ -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