|
21 | 21 |
|
22 | 22 | import org.apache.spark.api.java.JavaPairRDD; |
23 | 23 | import org.apache.spark.api.java.function.Function; |
| 24 | +import org.apache.spark.api.java.function.PairFunction; |
24 | 25 | import org.apache.sysds.common.Types.DataType; |
25 | 26 | import org.apache.sysds.common.Types.ValueType; |
26 | 27 | import org.apache.sysds.runtime.controlprogram.context.ExecutionContext; |
27 | 28 | import org.apache.sysds.runtime.controlprogram.context.SparkExecutionContext; |
| 29 | +import org.apache.sysds.runtime.functionobjects.KahanPlus; |
28 | 30 | import org.apache.sysds.runtime.instructions.InstructionUtils; |
29 | 31 | import org.apache.sysds.runtime.instructions.cp.CPOperand; |
| 32 | +import org.apache.sysds.runtime.instructions.cp.KahanObject; |
30 | 33 | import org.apache.sysds.runtime.matrix.data.MatrixBlock; |
31 | 34 | import org.apache.sysds.runtime.matrix.data.MatrixIndexes; |
32 | 35 | import org.apache.sysds.runtime.matrix.operators.Operator; |
33 | 36 | import org.apache.sysds.runtime.matrix.operators.UnaryOperator; |
| 37 | +import scala.Serializable; |
| 38 | +import scala.Tuple2; |
| 39 | +import java.util.*; |
34 | 40 |
|
35 | 41 | public class UnaryMatrixSPInstruction extends UnarySPInstruction { |
36 | 42 |
|
@@ -61,6 +67,210 @@ public void processInstruction(ExecutionContext ec) { |
61 | 67 | updateUnaryOutputDataCharacteristics(sec); |
62 | 68 | sec.setRDDHandleForVariable(output.getName(), out); |
63 | 69 | sec.addLineageRDD(output.getName(), input1.getName()); |
| 70 | + |
| 71 | + if ( "urowcumk+".equals(getOpcode()) ) { |
| 72 | + |
| 73 | + JavaPairRDD< MatrixIndexes, Tuple2<MatrixBlock, MatrixBlock> > localRowcumsum = in.mapToPair( new LocalRowCumsumFunction() ); |
| 74 | + |
| 75 | + |
| 76 | + // Collect end-values of every block of every row for offset calc by grouping by global row index |
| 77 | + JavaPairRDD< Long, Iterable<Tuple3<Long, Long, double[]>> > rowEndValues = localRowcumsum |
| 78 | + .mapToPair( tuple2 -> { |
| 79 | + |
| 80 | + // get index of block |
| 81 | + MatrixIndexes indexes = tuple2._1; |
| 82 | + // get cum matrix block |
| 83 | + MatrixBlock localRowcumsumBlock = tuple2._2._2; |
| 84 | + |
| 85 | + // get row and column block index |
| 86 | + long rowBlockIndex = indexes.getRowIndex(); |
| 87 | + long colBlockIndex = indexes.getColumnIndex(); |
| 88 | + |
| 89 | + // Save end value of every row of every block (if block is empty save 0) |
| 90 | + double[] endValues = new double[ localRowcumsumBlock.getNumRows() ]; |
| 91 | + |
| 92 | + for ( int i = 0; i < localRowcumsumBlock.getNumRows(); i ++ ) { |
| 93 | + if (localRowcumsumBlock.getNumColumns() > 0) { |
| 94 | + endValues[i] = localRowcumsumBlock.get(i, localRowcumsumBlock.getNumColumns() - 1); |
| 95 | + } else { |
| 96 | + endValues[i] = 0.0 ; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return new Tuple2<>(rowBlockIndex, new Tuple3<>(rowBlockIndex, colBlockIndex, endValues)); |
| 101 | + } |
| 102 | + |
| 103 | + ).groupByKey(); |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + // compute offset for every block |
| 109 | + List< Tuple2 <Tuple2<Long, Long>, double[]> > offsetList = rowEndValues |
| 110 | + .flatMapToPair(tuple2 -> { |
| 111 | + |
| 112 | + Long rowBlockIndex = tuple2._1; |
| 113 | + |
| 114 | + List< Tuple3<Long, Long, double[]> > colValues = new ArrayList<>(); |
| 115 | + for ( Tuple3<Long, Long, double[]> cv : tuple2._2 ) { |
| 116 | + colValues.add(cv); |
| 117 | + } |
| 118 | + |
| 119 | + // sort blocks from one row by column index |
| 120 | + colValues.sort(Comparator.comparing(Tuple3::_2)); |
| 121 | + |
| 122 | + // get number of rows of a block by counting amount of end (row) values of said block |
| 123 | + int numberOfRows = 0; |
| 124 | + if ( !colValues.isEmpty() ) { |
| 125 | + Tuple3<Long, Long, double[]> firstTuple = colValues.get(0); |
| 126 | + double[] lastValuesArray = firstTuple._3(); |
| 127 | + numberOfRows = lastValuesArray.length; |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | + List<Tuple2<Tuple2<Long, Long>, double[]>> blockOffsets = new ArrayList<>(); |
| 132 | + |
| 133 | + double[] cumulativeOffsets = new double[numberOfRows]; |
| 134 | + |
| 135 | + for (Tuple3<Long, Long, double[]> colValue : colValues) { |
| 136 | + |
| 137 | + Long colBlockIndex = colValue._2(); |
| 138 | + double[] endValues = colValue._3(); |
| 139 | + |
| 140 | + // copy current offsets |
| 141 | + double[] currentOffsets = cumulativeOffsets.clone(); |
| 142 | + |
| 143 | + // and save block indexes with its offsets |
| 144 | + blockOffsets.add( new Tuple2<>(new Tuple2<>(rowBlockIndex, colBlockIndex), currentOffsets) ); |
| 145 | + |
| 146 | + for ( int i = 0; i < numberOfRows && i < endValues.length; i++ ) { |
| 147 | + cumulativeOffsets[i] += endValues[i]; |
| 148 | + } |
| 149 | + |
| 150 | + } |
| 151 | + return blockOffsets.iterator(); |
| 152 | + } |
| 153 | + ).collect(); |
| 154 | + |
| 155 | + |
| 156 | + // convert list to map for easier access to offsets |
| 157 | + Map< Tuple2<Long, Long>, double[] > offsetMap = new HashMap<>(); |
| 158 | + for (Tuple2<Tuple2<Long, Long>, double[]> offset : offsetList) { |
| 159 | + offsetMap.put(offset._1, offset._2); |
| 160 | + } |
| 161 | + |
| 162 | + |
| 163 | + out = localRowcumsum.mapToPair( new FinalRowCumsumFunction(offsetMap)) ; |
| 164 | + |
| 165 | + updateUnaryOutputDataCharacteristics(sec); |
| 166 | + sec.setRDDHandleForVariable(output.getName(), out); |
| 167 | + sec.addLineageRDD(output.getName(), input1.getName()); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + |
| 172 | + |
| 173 | + private static class LocalRowCumsumFunction implements PairFunction< Tuple2<MatrixIndexes, MatrixBlock>, MatrixIndexes, Tuple2<MatrixBlock, MatrixBlock> > { |
| 174 | + |
| 175 | + @Override |
| 176 | + public Tuple2< MatrixIndexes, Tuple2<MatrixBlock, MatrixBlock> > call(Tuple2<MatrixIndexes, MatrixBlock> tuple2) { |
| 177 | + |
| 178 | + |
| 179 | + MatrixBlock inputBlock = tuple2._2; |
| 180 | + MatrixBlock cumsumBlock = new MatrixBlock( inputBlock.getNumRows(), inputBlock.getNumColumns(), false ); |
| 181 | + |
| 182 | + |
| 183 | + for ( int i = 0; i < inputBlock.getNumRows(); i++ ) { |
| 184 | + |
| 185 | + KahanObject kbuff = new KahanObject(0, 0); |
| 186 | + KahanPlus kplus = KahanPlus.getKahanPlusFnObject(); |
| 187 | + |
| 188 | + for ( int j = 0; j < inputBlock.getNumColumns(); j++ ) { |
| 189 | + |
| 190 | + double val = inputBlock.get(i, j); |
| 191 | + kplus.execute2(kbuff, val); |
| 192 | + cumsumBlock.set(i, j, kbuff._sum); |
| 193 | + } |
| 194 | + } |
| 195 | + // original index, original matrix and local cumsum block |
| 196 | + return new Tuple2<>( tuple2._1, new Tuple2<>(inputBlock, cumsumBlock) ); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + |
| 201 | + |
| 202 | + |
| 203 | + private static class FinalRowCumsumFunction implements PairFunction<Tuple2< MatrixIndexes, Tuple2<MatrixBlock, MatrixBlock> >, MatrixIndexes, MatrixBlock> { |
| 204 | + |
| 205 | + |
| 206 | + // map block indexes to the row offsets |
| 207 | + private final Map< Tuple2<Long, Long>, double[] > offsetMap; |
| 208 | + |
| 209 | + public FinalRowCumsumFunction(Map<Tuple2<Long, Long>, double[]> offsetMap) { |
| 210 | + this.offsetMap = offsetMap; |
| 211 | + } |
| 212 | + |
| 213 | + |
| 214 | + @Override |
| 215 | + public Tuple2<MatrixIndexes, MatrixBlock> call( Tuple2< MatrixIndexes, Tuple2<MatrixBlock, MatrixBlock> > tuple ) { |
| 216 | + |
| 217 | + MatrixIndexes indexes = tuple._1; |
| 218 | + MatrixBlock inputBlock = tuple._2._1; |
| 219 | + MatrixBlock localRowCumsumBlock = tuple._2._2; |
| 220 | + |
| 221 | + // key to get the row offset for this block |
| 222 | + Tuple2<Long, Long> blockKey = new Tuple2<>( indexes.getRowIndex(), indexes.getColumnIndex()) ; |
| 223 | + double[] offsets = offsetMap.get(blockKey); |
| 224 | + |
| 225 | + MatrixBlock cumsumBlock = new MatrixBlock( inputBlock.getNumRows(), inputBlock.getNumColumns(), false ); |
| 226 | + |
| 227 | + |
| 228 | + for ( int i = 0; i < inputBlock.getNumRows(); i++ ) { |
| 229 | + |
| 230 | + double rowOffset = 0.0; |
| 231 | + if ( offsets != null && i < offsets.length ) { |
| 232 | + rowOffset = offsets[i]; |
| 233 | + } |
| 234 | + |
| 235 | + for ( int j = 0; j < inputBlock.getNumColumns(); j++ ) { |
| 236 | + double cumsumValue = localRowCumsumBlock.get(i, j); |
| 237 | + cumsumBlock.set(i, j, cumsumValue + rowOffset); |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + // block index and final cumsum block |
| 242 | + return new Tuple2<>(indexes, cumsumBlock); |
| 243 | + } |
| 244 | + } |
| 245 | + |
| 246 | + |
| 247 | + |
| 248 | + // helper class |
| 249 | + private static class Tuple3<Type1, Type2, Type3> implements Serializable { |
| 250 | + |
| 251 | + private static final long serialVersionUID = 123; |
| 252 | + private final Type1 _1; |
| 253 | + private final Type2 _2; |
| 254 | + private final Type3 _3; |
| 255 | + |
| 256 | + |
| 257 | + public Tuple3( Type1 _1, Type2 _2, Type3 _3 ) { |
| 258 | + this._1 = _1; |
| 259 | + this._2 = _2; |
| 260 | + this._3 = _3; |
| 261 | + } |
| 262 | + |
| 263 | + public Type1 _1() { |
| 264 | + return _1; |
| 265 | + } |
| 266 | + |
| 267 | + public Type2 _2() { |
| 268 | + return _2; |
| 269 | + } |
| 270 | + |
| 271 | + public Type3 _3() { |
| 272 | + return _3; |
| 273 | + } |
64 | 274 | } |
65 | 275 |
|
66 | 276 | private static class RDDMatrixBuiltinUnaryOp implements Function<MatrixBlock,MatrixBlock> |
|
0 commit comments