Skip to content

Commit 7d3b337

Browse files
committed
netlib.allowNativeBlas
1 parent 9f146c8 commit 7d3b337

File tree

13 files changed

+37
-54
lines changed

13 files changed

+37
-54
lines changed

common/utils/src/main/scala/org/apache/spark/util/SparkEnvUtils.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ private[spark] trait SparkEnvUtils {
2828
}
2929

3030
/**
31-
* Whether allow using native BLAS library if available.
31+
* Whether allow using native BLAS/LAPACK/ARPACK libraries if available.
3232
*/
33-
val allowNativeBlas = "true".equals(System.getProperty("spark.ml.allowNativeBlas", "true"))
33+
val allowNativeBlas = "true".equals(System.getProperty("netlib.allowNativeBlas", "true"))
3434
}
3535

3636
object SparkEnvUtils extends SparkEnvUtils

core/src/main/scala/org/apache/spark/SparkContext.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3435,7 +3435,7 @@ object SparkContext extends Logging {
34353435
}
34363436

34373437
private def supplementBlasOptions(conf: SparkConf): Unit = {
3438-
val opts = s"-Dspark.ml.allowNativeBlas=${Utils.allowNativeBlas}"
3438+
val opts = s"-Dnetlib.allowNativeBlas=${Utils.allowNativeBlas}"
34393439
supplementJavaOpts(conf, DRIVER_JAVA_OPTIONS, opts)
34403440
supplementJavaOpts(conf, EXECUTOR_JAVA_OPTIONS, opts)
34413441
}

core/src/main/scala/org/apache/spark/internal/config/package.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2893,4 +2893,12 @@ package object config {
28932893
.checkValue(v => v.forall(Set("stdout", "stderr").contains),
28942894
"The value only can be one or more of 'stdout, stderr'.")
28952895
.createWithDefault(Seq("stdout", "stderr"))
2896+
2897+
private[spark] val SPARK_ML_ALLOW_NATIVE_BLAS =
2898+
ConfigBuilder("spark.ml.allowNativeBlas")
2899+
.doc("Whether allow using native BLAS/LAPACK/ARPACK implementations when native " +
2900+
"libraries are available. If disabled, always use Java implementations.")
2901+
.version("4.1.0")
2902+
.booleanConf
2903+
.createWithDefault(true)
28962904
}

docs/ml-linalg-guide.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ The installation should be done on all nodes of the cluster. Generic version of
4646

4747
For Debian / Ubuntu:
4848
```
49-
sudo apt-get install libopenblas-base
50-
sudo update-alternatives --config libblas.so.3
49+
sudo apt-get install libopenblas-dev
5150
```
5251
For CentOS / RHEL:
5352
```
@@ -76,7 +75,7 @@ You can also point `dev.ludovic.netlib` to specific libraries names and paths. F
7675

7776
If native libraries are not properly configured in the system, the Java implementation (javaBLAS) will be used as fallback option.
7877

79-
You can also set `spark.ml.allowNativeBlas` to `false` to disable native BLAS and always use the Java implementation.
78+
You can also set spark conf `spark.ml.allowNativeBlas` or Java system property `netlib.allowNativeBlas` to `false` to disable native BLAS and always use the Java implementation.
8079

8180
## Spark Configuration
8281

launcher/src/main/java/org/apache/spark/launcher/SparkSubmitCommandBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ private List<String> buildSparkSubmitCommand(Map<String, String> env)
340340

341341
if (config.containsKey("spark.ml.allowNativeBlas")) {
342342
String allowNativeBlas = config.get("spark.ml.allowNativeBlas");
343-
addOptionString(cmd, "-Dspark.ml.allowNativeBlas=" + allowNativeBlas);
343+
addOptionString(cmd, "-Dnetlib.allowNativeBlas=" + allowNativeBlas);
344344
}
345345

346346
// SPARK-36796: Always add some JVM runtime default options to submit command

mllib-local/src/main/scala/org/apache/spark/ml/linalg/BLAS.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private[spark] object BLAS extends Serializable with Logging {
4545
_nativeBLAS = if (SparkEnvUtils.allowNativeBlas) {
4646
try { NetlibNativeBLAS.getInstance } catch { case _: Throwable => javaBLAS }
4747
} else {
48-
logInfo("Disable native BLAS because spark.ml.allowNativeBlas is false.")
48+
logInfo("Disable native BLAS because netlib.allowNativeBlas is false.")
4949
javaBLAS
5050
}
5151
}

mllib/src/main/scala/org/apache/spark/mllib/linalg/ARPACK.scala

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ package org.apache.spark.mllib.linalg
1919

2020
import dev.ludovic.netlib.arpack.{ARPACK => NetlibARPACK, JavaARPACK => NetlibJavaARPACK, NativeARPACK => NetlibNativeARPACK}
2121

22+
import org.apache.spark.internal.Logging
23+
import org.apache.spark.util.SparkEnvUtils
24+
2225
/**
2326
* ARPACK routines for MLlib's vectors and matrices.
2427
*/
25-
private[spark] object ARPACK extends Serializable {
28+
private[spark] object ARPACK extends Serializable with Logging {
2629

2730
@transient private var _javaARPACK: NetlibARPACK = _
2831
@transient private var _nativeARPACK: NetlibARPACK = _
@@ -36,8 +39,12 @@ private[spark] object ARPACK extends Serializable {
3639

3740
private[spark] def nativeARPACK: NetlibARPACK = {
3841
if (_nativeARPACK == null) {
39-
_nativeARPACK =
42+
_nativeARPACK = if (SparkEnvUtils.allowNativeBlas) {
4043
try { NetlibNativeARPACK.getInstance } catch { case _: Throwable => javaARPACK }
44+
} else {
45+
logInfo("Disable native ARPACK because netlib.allowNativeBlas is false.")
46+
javaARPACK
47+
}
4148
}
4249
_nativeARPACK
4350
}

mllib/src/main/scala/org/apache/spark/mllib/linalg/BLAS.scala

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,44 +17,14 @@
1717

1818
package org.apache.spark.mllib.linalg
1919

20-
import dev.ludovic.netlib.blas.{BLAS => NetlibBLAS, JavaBLAS => NetlibJavaBLAS, NativeBLAS => NetlibNativeBLAS}
21-
2220
import org.apache.spark.internal.Logging
21+
import org.apache.spark.ml.linalg.BLAS.{getBLAS, nativeBLAS}
2322

2423
/**
2524
* BLAS routines for MLlib's vectors and matrices.
2625
*/
2726
private[spark] object BLAS extends Serializable with Logging {
2827

29-
@transient private var _javaBLAS: NetlibBLAS = _
30-
@transient private var _nativeBLAS: NetlibBLAS = _
31-
private val nativeL1Threshold: Int = 256
32-
33-
// For level-1 function dspmv, use javaBLAS for better performance.
34-
private[spark] def javaBLAS: NetlibBLAS = {
35-
if (_javaBLAS == null) {
36-
_javaBLAS = NetlibJavaBLAS.getInstance
37-
}
38-
_javaBLAS
39-
}
40-
41-
// For level-3 routines, we use the native BLAS.
42-
private[spark] def nativeBLAS: NetlibBLAS = {
43-
if (_nativeBLAS == null) {
44-
_nativeBLAS =
45-
try { NetlibNativeBLAS.getInstance } catch { case _: Throwable => javaBLAS }
46-
}
47-
_nativeBLAS
48-
}
49-
50-
private[spark] def getBLAS(vectorSize: Int): NetlibBLAS = {
51-
if (vectorSize < nativeL1Threshold) {
52-
javaBLAS
53-
} else {
54-
nativeBLAS
55-
}
56-
}
57-
5828
/**
5929
* y += a * x
6030
*/

mllib/src/main/scala/org/apache/spark/mllib/linalg/LAPACK.scala

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ package org.apache.spark.mllib.linalg
1919

2020
import dev.ludovic.netlib.lapack.{JavaLAPACK => NetlibJavaLAPACK, LAPACK => NetlibLAPACK, NativeLAPACK => NetlibNativeLAPACK}
2121

22+
import org.apache.spark.internal.Logging
23+
import org.apache.spark.util.SparkEnvUtils
24+
2225
/**
2326
* LAPACK routines for MLlib's vectors and matrices.
2427
*/
25-
private[spark] object LAPACK extends Serializable {
28+
private[spark] object LAPACK extends Serializable with Logging {
2629

2730
@transient private var _javaLAPACK: NetlibLAPACK = _
2831
@transient private var _nativeLAPACK: NetlibLAPACK = _
@@ -36,8 +39,12 @@ private[spark] object LAPACK extends Serializable {
3639

3740
private[spark] def nativeLAPACK: NetlibLAPACK = {
3841
if (_nativeLAPACK == null) {
39-
_nativeLAPACK =
42+
_nativeLAPACK = if (SparkEnvUtils.allowNativeBlas) {
4043
try { NetlibNativeLAPACK.getInstance } catch { case _: Throwable => javaLAPACK }
44+
} else {
45+
logInfo("Disable native LAPACK because netlib.allowNativeBlas is false.")
46+
javaLAPACK
47+
}
4148
}
4249
_nativeLAPACK
4350
}

mllib/src/main/scala/org/apache/spark/mllib/linalg/Matrices.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ class DenseMatrix @Since("1.3.0") (
427427
if (isTransposed) {
428428
Iterator.tabulate(numCols) { j =>
429429
val col = new Array[Double](numRows)
430-
BLAS.nativeBLAS.dcopy(numRows, values, j, numCols, col, 0, 1)
430+
newlinalg.BLAS.nativeBLAS.dcopy(numRows, values, j, numCols, col, 0, 1)
431431
new DenseVector(col)
432432
}
433433
} else {

0 commit comments

Comments
 (0)