Skip to content

Commit

Permalink
Add jni method about omp and others (intel-analytics#10)
Browse files Browse the repository at this point in the history
* fix: add jni method about omp environments and convert back to parallel mode

* fix: set threads to 1 through mkl_set_num_threads, opm_set_num_threads can't work as OMP_NUM_THREADS

* fix: icc warning fix

* fix: typo of waitPolicyPasssive

* fix: set mkl environment variables after load the library.

* fix: typo
  • Loading branch information
i8run authored Jun 16, 2017
1 parent 456d08f commit f61d1fb
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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
Expand All @@ -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);

Expand Down
4 changes: 3 additions & 1 deletion mkl2017-xeon-blas/mkl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@
<linkerStartOption>-I ${JAVA_HOME}/include/</linkerStartOption>
<linkerStartOption>${linkerStartOptionDirectory}</linkerStartOption>
<linkerStartOption>-ldl</linkerStartOption>
<linkerStartOption>-liomp5</linkerStartOption>
<linkerStartOption>-shared</linkerStartOption>
<linkerStartOption>-mkl=sequential</linkerStartOption>
<linkerStartOption>-mkl=parallel</linkerStartOption>
<linkerStartOption>-qopenmp</linkerStartOption>
<linkerStartOption>-static-intel</linkerStartOption>
<linkerStartOption>-no-intel-extensions</linkerStartOption>
</linkerStartOptions>
Expand Down
25 changes: 23 additions & 2 deletions mkl2017-xeon-blas/mkl/src/main/c/jni/mkl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions mkl2017-xeon-dnn/mkl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<linkerStartOption>-mkl=parallel</linkerStartOption>
<linkerStartOption>-static-intel</linkerStartOption>
<linkerStartOption>-qopenmp</linkerStartOption>
<linkerStartOption>-no-intel-extensions</linkerStartOption>
</linkerStartOptions>
<linkerMiddleOptions>
<linkerMiddleOption>-lm</linkerMiddleOption>
Expand Down

0 comments on commit f61d1fb

Please sign in to comment.