luhenry commented on a change in pull request #30810: URL: https://github.com/apache/spark/pull/30810#discussion_r546452297
########## File path: mllib-local/src/vectorized/java/org/apache/spark/ml/linalg/VectorizedBLAS.java ########## @@ -0,0 +1,162 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.spark.ml.linalg; + +import com.github.fommil.netlib.F2jBLAS; +import jdk.incubator.vector.DoubleVector; +import jdk.incubator.vector.FloatVector; +import jdk.incubator.vector.VectorOperators; +import jdk.incubator.vector.VectorSpecies; + +public class VectorizedBLAS extends F2jBLAS { + + private final static VectorSpecies<Float> FMAX = FloatVector.SPECIES_MAX; + private final static VectorSpecies<Double> DMAX = DoubleVector.SPECIES_MAX; + + // y += alpha * x + @Override + public void daxpy(int n, double alpha, double[] x, int incx, double[] y, int incy) { + if (incx == 1 && incy == 1 && n <= x.length && n <= y.length) { + if (alpha != 0.) { + int i = 0; + for (; i < DMAX.loopBound(n); i += DMAX.length()) { + DoubleVector vx = DoubleVector.fromArray(DMAX, x, i); + DoubleVector vy = DoubleVector.fromArray(DMAX, y, i); + vx.lanewise(VectorOperators.MUL, alpha).add(vy) + .intoArray(y, i); + } + for (; i < n; i += 1) { + y[i] += alpha * x[i]; + } + } + } else { + super.daxpy(n, alpha, x, incx, y, incy); + } + } + + // sum(x * y) + @Override + public float sdot(int n, float[] x, int incx, float[] y, int incy) { + if (incx == 1 && incy == 1) { + float sum = 0.0f; + int i = 0; + for (; i < FMAX.loopBound(n); i += FMAX.length()) { + FloatVector vx = FloatVector.fromArray(FMAX, x, i); + FloatVector vy = FloatVector.fromArray(FMAX, y, i); + sum += vx.mul(vy).reduceLanes(VectorOperators.ADD); + } + for (; i < n; i += 1) { + sum += x[i] * y[i]; + } + return sum; + } else { + return super.sdot(n, x, incx, y, incy); + } + } + + // sum(x * y) + @Override + public double ddot(int n, double[] x, int incx, double[] y, int incy) { + if (incx == 1 && incy == 1) { + double sum = 0.; + int i = 0; + for (; i < DMAX.loopBound(n); i += DMAX.length()) { + DoubleVector vx = DoubleVector.fromArray(DMAX, x, i); + DoubleVector vy = DoubleVector.fromArray(DMAX, y, i); + sum += vx.mul(vy).reduceLanes(VectorOperators.ADD); + } + for (; i < n; i += 1) { + sum += x[i] * y[i]; + } + return sum; + } else { + return super.ddot(n, x, incx, y, incy); + } + } + + // x = alpha * x + @Override + public void dscal(int n, double alpha, double[] x, int incx) { + if (incx == 1) { + if (alpha != 1.) { Review comment: I am concerned that doing that could change the implicit API around errors if a value passed to the method is invalid. On that note, I should likely add checks for the values of `x` (like `x != null && n <= x.length`). ########## File path: mllib-local/src/vectorized/java/org/apache/spark/ml/linalg/VectorizedBLAS.java ########## @@ -0,0 +1,162 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.spark.ml.linalg; + +import com.github.fommil.netlib.F2jBLAS; +import jdk.incubator.vector.DoubleVector; +import jdk.incubator.vector.FloatVector; +import jdk.incubator.vector.VectorOperators; +import jdk.incubator.vector.VectorSpecies; + +public class VectorizedBLAS extends F2jBLAS { + + private final static VectorSpecies<Float> FMAX = FloatVector.SPECIES_MAX; + private final static VectorSpecies<Double> DMAX = DoubleVector.SPECIES_MAX; + + // y += alpha * x + @Override + public void daxpy(int n, double alpha, double[] x, int incx, double[] y, int incy) { + if (incx == 1 && incy == 1 && n <= x.length && n <= y.length) { + if (alpha != 0.) { Review comment: Same as https://github.com/apache/spark/pull/30810#discussion_r546452297 ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
