diff --git a/mkl2017-xeon-blas/jni/src/main/java/com/intel/analytics/bigdl/mkl/MKL.java b/mkl2017-xeon-blas/jni/src/main/java/com/intel/analytics/bigdl/mkl/MKL.java index 26680930e6d7..51efdfe03ace 100644 --- a/mkl2017-xeon-blas/jni/src/main/java/com/intel/analytics/bigdl/mkl/MKL.java +++ b/mkl2017-xeon-blas/jni/src/main/java/com/intel/analytics/bigdl/mkl/MKL.java @@ -4,6 +4,7 @@ import java.net.URL; import java.nio.channels.FileChannel; import java.nio.channels.ReadableByteChannel; +import java.util.Locale; import static java.io.File.createTempFile; import static java.nio.channels.Channels.newChannel; @@ -29,8 +30,10 @@ public class MKL { tmpFile = extract(jmklFileName); System.load(tmpFile.getAbsolutePath()); tmpFile.delete(); // delete so temp file after loaded - isLoaded = true; + setMklEnv(); + + isLoaded = true; } catch (Exception e) { isLoaded = false; e.printStackTrace(); @@ -39,6 +42,49 @@ public class MKL { } } + /** + * This method will call 4 native methods to set mkl environments. They are + * + * 1. mkl num threads: + * + function: If mkl is linked with parallel mode, one can modify the number of threads omp will use. + * + default value: 1 + * 2. mkl block time: + * + function: Turns off the Intel MKL Memory Allocator for Intel MKL functions + * to directly use the system malloc/free functions. + * + default value: 0 + * 3. wait policy: + * + function: Sets omp wait policy to passive. + * + default value: passive + * 4. disable fast mm: + * + function: Sets the time (milliseconds) that a thread should wait before sleeping, + * after completing the execution of a parallel region. + * + default value: true + */ + private static void setMklEnv() { + String mklBlockTimeStr = System.getProperty("bigdl.mklBlockTime", "0"); + int mklBlockTime = Integer.parseInt(mklBlockTimeStr); + + String mklNumThreadsStr = System.getProperty("bigdl.mklNumThreads", "1"); + int mklNumThreads = Integer.parseInt(mklNumThreadsStr); + + String mklDisableFastMMStr = System.getProperty("bigdl.mklDisableFastMM","true").toLowerCase(); + boolean mklDisableFastMM = true; + if (mklDisableFastMMStr.equals("false")) { + mklDisableFastMM = false; + } + + String mklWaitPolicy = System.getProperty("bigdl.mklWaitPolicy", "passive").toLowerCase(); + + // set mkl environment variables + setNumThreads(mklNumThreads); + setBlockTime(mklBlockTime); + waitPolicyPassive(); + + if (mklDisableFastMM) { + disableFastMM(); + } + } + /** * Check if MKL is loaded * @return @@ -58,8 +104,38 @@ public static String getTmpSoFilePath() { return tmpFile.getAbsolutePath(); } + // {{ mkl environments set up + + /** + * If mkl is linked with parallel mode, one can modify the number of threads omp will use. + * It's an subsitute of environment variable: OMP_NUM_THREADS + * + * @param numThreads how many threads omp will use. + */ + public native static void setNumThreads(int numThreads); + + /** + * Turns off the Intel MKL Memory Allocator for Intel MKL functions + * to directly use the system malloc/free functions. + * It's an substitute of environment variable: MKL_DISABLE_FAST_MM + */ public native static void disableFastMM(); + /** + * Sets the time (milliseconds) that a thread should wait before sleeping, + * after completing the execution of a parallel region. + * It's an substitute of environment variable: KMP_BLOCKTIME + * + * @param msec the time should wait + */ + public native static void setBlockTime(int msec); + + /** + * Sets omp wait policy to passive. + */ + public native static void waitPolicyPassive(); + // }} mkl environments set up + public native static void vsAdd(int n, float[] a, int aOffset, float[] b, int bOffset, float[] y, int yOffset); diff --git a/mkl2017-xeon-blas/mkl/pom.xml b/mkl2017-xeon-blas/mkl/pom.xml index 662740bfdc73..1cadc218aaea 100644 --- a/mkl2017-xeon-blas/mkl/pom.xml +++ b/mkl2017-xeon-blas/mkl/pom.xml @@ -48,8 +48,10 @@ -I ${JAVA_HOME}/include/ ${linkerStartOptionDirectory} -ldl + -liomp5 -shared - -mkl=sequential + -mkl=parallel + -qopenmp -static-intel -no-intel-extensions diff --git a/mkl2017-xeon-blas/mkl/src/main/c/jni/mkl.c b/mkl2017-xeon-blas/mkl/src/main/c/jni/mkl.c index b321121fea00..d62535108933 100644 --- a/mkl2017-xeon-blas/mkl/src/main/c/jni/mkl.c +++ b/mkl2017-xeon-blas/mkl/src/main/c/jni/mkl.c @@ -12,11 +12,12 @@ extern "C" { */ JNIEXPORT void JNICALL Java_com_intel_analytics_bigdl_mkl_MKL_setNumThreads (JNIEnv * env, jclass cls, jint num_threads) { - return ; + mkl_set_dynamic(0); + mkl_set_num_threads(num_threads); } /* - * Class: com_intel_analytics_bigdl_mkl_MKL + * Class: com_intel_analytics_bigdl_mkl_disableFastMM * Method: setNumThreads * Signature: ()V */ @@ -25,6 +26,26 @@ JNIEXPORT void JNICALL Java_com_intel_analytics_bigdl_mkl_MKL_disableFastMM mkl_disable_fast_mm(); } +/* + * Class: com_intel_analytics_bigdl_mkl_setBlockTime + * Method: setNumThreads + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_com_intel_analytics_bigdl_mkl_MKL_setBlockTime + (JNIEnv * env, jclass cls, jint time) { + kmp_set_blocktime(0); +} + +/* + * Class: com_intel_analytics_bigdl_mkl_watiPolicyPassive + * Method: setNumThreads + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_com_intel_analytics_bigdl_mkl_MKL_waitPolicyPassive + (JNIEnv * env, jclass cls) { + kmp_set_library_throughput(); +} + /* * Class: com_intel_analytics_bigdl_mkl_MKL * Method: getNumThreads diff --git a/mkl2017-xeon-dnn/mkl/pom.xml b/mkl2017-xeon-dnn/mkl/pom.xml index bf09c6aa9c17..5100856ab4b9 100644 --- a/mkl2017-xeon-dnn/mkl/pom.xml +++ b/mkl2017-xeon-dnn/mkl/pom.xml @@ -73,6 +73,7 @@ -mkl=parallel -static-intel -qopenmp + -no-intel-extensions -lm