Skip to content

Commit f43b313

Browse files
committed
[SYSTEMDS-3919] Use SIMD Vector API in unary dense operations
This patch introduces SIMD vector operations in selected unary builtin operations. However, the code-path is still disabled because experiments on different hardware platforms showed inconclusive results.
1 parent 4e05c19 commit f43b313

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

src/main/java/org/apache/sysds/runtime/functionobjects/Builtin.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
import org.apache.sysds.runtime.DMLRuntimeException;
2727
import org.apache.sysds.runtime.DMLScriptException;
2828

29+
import jdk.incubator.vector.DoubleVector;
30+
import jdk.incubator.vector.VectorSpecies;
31+
2932

3033
/**
3134
* Class with pre-defined set of objects. This class can not be instantiated elsewhere.
@@ -46,14 +49,17 @@
4649
public class Builtin extends ValueFunction
4750
{
4851
private static final long serialVersionUID = 3836744687789840574L;
49-
52+
5053
public enum BuiltinCode { AUTODIFF, SIN, COS, TAN, SINH, COSH, TANH, ASIN, ACOS, ATAN, LOG, LOG_NZ, MIN,
5154
MAX, ABS, SIGN, SQRT, EXP, PLOGP, PRINT, PRINTF, NROW, NCOL, LENGTH, LINEAGE, ROUND, MAXINDEX, MININDEX,
5255
STOP, CEIL, FLOOR, CUMSUM, ROWCUMSUM, CUMPROD, CUMMIN, CUMMAX, CUMSUMPROD, INVERSE, SPROP, SIGMOID, EVAL, LIST,
5356
TYPEOF, APPLY_SCHEMA, DETECTSCHEMA, ISNA, ISNAN, ISINF, DROP_INVALID_TYPE,
5457
DROP_INVALID_LENGTH, VALUE_SWAP, FRAME_ROW_REPLICATE,
5558
MAP, COUNT_DISTINCT, COUNT_DISTINCT_APPROX, UNIQUE}
5659

60+
private static final VectorSpecies<Double> SPECIES = DoubleVector.SPECIES_PREFERRED;
61+
private static final int vLen = SPECIES.length();
62+
5763

5864
public BuiltinCode bFunc;
5965

@@ -197,6 +203,38 @@ else if (in < 0)
197203
throw new DMLRuntimeException("Builtin.execute(): Unknown operation: " + bFunc);
198204
}
199205
}
206+
207+
public long execute (double[] a, double[] c, int start, int end) {
208+
long nnz = 0;
209+
210+
//process rest or unsupported builtin codes
211+
final int end2 = (bFunc==BuiltinCode.ABS || bFunc==BuiltinCode.SQRT)?
212+
start+((end-start)%vLen) : end;
213+
for( int i = start; i < end2; i++) {
214+
c[i] = execute(a[i]);
215+
nnz += (c[i] != 0) ? 1 : 0;
216+
}
217+
218+
nnz += (end-end2);
219+
if( bFunc == BuiltinCode.ABS) {
220+
for( int i = end2; i < end; i+=vLen ){
221+
DoubleVector aVec = DoubleVector.fromArray(SPECIES, a, i);
222+
DoubleVector cVec = aVec.abs();
223+
nnz -= cVec.eq(0).trueCount();
224+
cVec.intoArray(c, i);
225+
}
226+
}
227+
else if(bFunc == BuiltinCode.SQRT ) {
228+
for( int i = end2; i < end; i+=vLen ){
229+
DoubleVector aVec = DoubleVector.fromArray(SPECIES, a, i);
230+
DoubleVector cVec = aVec.sqrt();
231+
nnz -= cVec.eq(0).trueCount();
232+
cVec.intoArray(c, i);
233+
}
234+
}
235+
return nnz;
236+
}
237+
200238

201239
@Override
202240
public double execute (long in) {

src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixBincell.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3278,10 +3278,17 @@ public Long call() throws Exception {
32783278
double[] avals = _a.values(_rl);
32793279
double[] cvals = _c.values(_rl);
32803280
int start = _a.pos(_rl), end = _a.pos(_ru);
3281-
for( int i=start; i<end; i++ ) {
3282-
cvals[i] = _op.fn.execute(avals[i]);
3283-
nnz += (cvals[i] != 0) ? 1 : 0;
3284-
}
3281+
3282+
//TODO use of vector API inconclusive on different hardware
3283+
//if( _op.fn instanceof Builtin ) {
3284+
// nnz += ((Builtin)_op.fn).execute(avals, cvals, start, end);
3285+
//}
3286+
//else {
3287+
for( int i=start; i<end; i++ ) {
3288+
cvals[i] = _op.fn.execute(avals[i]);
3289+
nnz += (cvals[i] != 0) ? 1 : 0;
3290+
}
3291+
//}
32853292
}
32863293
//generic dense-dense, including large blocks
32873294
else {

0 commit comments

Comments
 (0)