|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.sysds.runtime.instructions.ooc; |
| 21 | + |
| 22 | +import org.apache.sysds.runtime.DMLRuntimeException; |
| 23 | +import org.apache.sysds.runtime.controlprogram.caching.MatrixObject; |
| 24 | +import org.apache.sysds.runtime.controlprogram.context.ExecutionContext; |
| 25 | +import org.apache.sysds.runtime.controlprogram.parfor.LocalTaskQueue; |
| 26 | +import org.apache.sysds.runtime.instructions.cp.CM_COV_Object; |
| 27 | +import org.apache.sysds.runtime.instructions.cp.CPOperand; |
| 28 | +import org.apache.sysds.runtime.instructions.cp.CentralMomentCPInstruction; |
| 29 | +import org.apache.sysds.runtime.instructions.cp.DoubleObject; |
| 30 | +import org.apache.sysds.runtime.instructions.cp.ScalarObject; |
| 31 | +import org.apache.sysds.runtime.instructions.spark.data.IndexedMatrixValue; |
| 32 | +import org.apache.sysds.runtime.matrix.data.MatrixBlock; |
| 33 | +import org.apache.sysds.runtime.matrix.data.MatrixIndexes; |
| 34 | +import org.apache.sysds.runtime.matrix.data.MatrixValue; |
| 35 | +import org.apache.sysds.runtime.matrix.operators.CMOperator; |
| 36 | +import org.apache.sysds.runtime.meta.DataCharacteristics; |
| 37 | + |
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.HashMap; |
| 40 | +import java.util.List; |
| 41 | +import java.util.Map; |
| 42 | +import java.util.Optional; |
| 43 | + |
| 44 | +public class CentralMomentOOCInstruction extends AggregateUnaryOOCInstruction { |
| 45 | + |
| 46 | + private CentralMomentOOCInstruction(CMOperator cm, CPOperand in1, CPOperand in2, CPOperand in3, CPOperand out, |
| 47 | + String opcode, String str) { |
| 48 | + super(OOCType.CM, cm, in1, in2, in3, out, opcode, str); |
| 49 | + } |
| 50 | + |
| 51 | + public static CentralMomentOOCInstruction parseInstruction(String str) { |
| 52 | + CentralMomentCPInstruction cpInst = CentralMomentCPInstruction.parseInstruction(str); |
| 53 | + return parseInstruction(cpInst); |
| 54 | + } |
| 55 | + |
| 56 | + public static CentralMomentOOCInstruction parseInstruction(CentralMomentCPInstruction inst) { |
| 57 | + return new CentralMomentOOCInstruction((CMOperator) inst.getOperator(), inst.input1, inst.input2, inst.input3, |
| 58 | + inst.output, inst.getOpcode(), inst.getInstructionString()); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void processInstruction(ExecutionContext ec) { |
| 63 | + String output_name = output.getName(); |
| 64 | + |
| 65 | + /* |
| 66 | + * The "order" of the central moment in the instruction can |
| 67 | + * be set to INVALID when the exact value is unknown at |
| 68 | + * compilation time. We first need to determine the exact |
| 69 | + * order and update the CMOperator, if needed. |
| 70 | + */ |
| 71 | + |
| 72 | + MatrixObject matObj = ec.getMatrixObject(input1.getName()); |
| 73 | + LocalTaskQueue<IndexedMatrixValue> qIn = matObj.getStreamHandle(); |
| 74 | + |
| 75 | + CPOperand scalarInput = (input3 == null ? input2 : input3); |
| 76 | + ScalarObject order = ec.getScalarInput(scalarInput); |
| 77 | + |
| 78 | + CMOperator cm_op = ((CMOperator) _optr); |
| 79 | + if(cm_op.getAggOpType() == CMOperator.AggregateOperationTypes.INVALID) |
| 80 | + cm_op = cm_op.setCMAggOp((int) order.getLongValue()); |
| 81 | + |
| 82 | + CMOperator finalCm_op = cm_op; |
| 83 | + |
| 84 | + List<CM_COV_Object> cmObjs = new ArrayList<>(); |
| 85 | + |
| 86 | + if(input3 == null) { |
| 87 | + try { |
| 88 | + IndexedMatrixValue tmp; |
| 89 | + |
| 90 | + while((tmp = qIn.dequeueTask()) != LocalTaskQueue.NO_MORE_TASKS) { |
| 91 | + // We only handle MatrixBlock, other types of MatrixValue will fail here |
| 92 | + cmObjs.add(((MatrixBlock) tmp.getValue()).cmOperations(cm_op)); |
| 93 | + } |
| 94 | + } |
| 95 | + catch(Exception ex) { |
| 96 | + throw new DMLRuntimeException(ex); |
| 97 | + } |
| 98 | + } |
| 99 | + else { |
| 100 | + // Here we use a hash join approach |
| 101 | + // Note that this may keep blocks in the cache for a while, depending on when a matching block arrives in the stream |
| 102 | + MatrixObject wtObj = ec.getMatrixObject(input2.getName()); |
| 103 | + |
| 104 | + DataCharacteristics dc = ec.getDataCharacteristics(input1.getName()); |
| 105 | + DataCharacteristics dcW = ec.getDataCharacteristics(input2.getName()); |
| 106 | + |
| 107 | + if (dc.getBlocksize() != dcW.getBlocksize()) |
| 108 | + throw new DMLRuntimeException("Different block sizes are not yet supported"); |
| 109 | + |
| 110 | + LocalTaskQueue<IndexedMatrixValue> wIn = wtObj.getStreamHandle(); |
| 111 | + |
| 112 | + try { |
| 113 | + IndexedMatrixValue tmp = qIn.dequeueTask(); |
| 114 | + IndexedMatrixValue tmpW = wIn.dequeueTask(); |
| 115 | + Map<MatrixIndexes, MatrixValue> left = new HashMap<>(); |
| 116 | + Map<MatrixIndexes, MatrixValue> right = new HashMap<>(); |
| 117 | + |
| 118 | + boolean cont = tmp != LocalTaskQueue.NO_MORE_TASKS || tmpW != LocalTaskQueue.NO_MORE_TASKS; |
| 119 | + |
| 120 | + while(cont) { |
| 121 | + cont = false; |
| 122 | + |
| 123 | + if(tmp != LocalTaskQueue.NO_MORE_TASKS) { |
| 124 | + MatrixValue weights = right.remove(tmp.getIndexes()); |
| 125 | + |
| 126 | + if(weights != null) |
| 127 | + cmObjs.add(((MatrixBlock) tmp.getValue()).cmOperations(cm_op, (MatrixBlock) weights)); |
| 128 | + else |
| 129 | + left.put(tmp.getIndexes(), tmp.getValue()); |
| 130 | + |
| 131 | + tmp = qIn.dequeueTask(); |
| 132 | + cont = tmp != LocalTaskQueue.NO_MORE_TASKS; |
| 133 | + } |
| 134 | + |
| 135 | + if(tmpW != LocalTaskQueue.NO_MORE_TASKS) { |
| 136 | + MatrixValue q = left.remove(tmpW.getIndexes()); |
| 137 | + |
| 138 | + if(q != null) |
| 139 | + cmObjs.add(((MatrixBlock) q).cmOperations(cm_op, (MatrixBlock) tmpW.getValue())); |
| 140 | + else |
| 141 | + right.put(tmpW.getIndexes(), tmpW.getValue()); |
| 142 | + |
| 143 | + tmpW = wIn.dequeueTask(); |
| 144 | + cont |= tmpW != LocalTaskQueue.NO_MORE_TASKS; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + if (!left.isEmpty() || !right.isEmpty()) |
| 149 | + throw new DMLRuntimeException("Unmatched blocks: values=" + left.size() + ", weights=" + right.size()); |
| 150 | + } |
| 151 | + catch(Exception ex) { |
| 152 | + throw new DMLRuntimeException(ex); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + Optional<CM_COV_Object> res = cmObjs.stream() |
| 157 | + .reduce((arg0, arg1) -> (CM_COV_Object) finalCm_op.fn.execute(arg0, arg1)); |
| 158 | + |
| 159 | + try { |
| 160 | + ec.setScalarOutput(output_name, new DoubleObject(res.get().getRequiredResult(finalCm_op))); |
| 161 | + } |
| 162 | + catch(Exception ex) { |
| 163 | + throw new DMLRuntimeException(ex); |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments