|
| 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 java.util.HashMap; |
| 23 | + |
| 24 | +import org.apache.sysds.common.Types; |
| 25 | +import org.apache.sysds.lops.Ctable; |
| 26 | +import org.apache.sysds.runtime.DMLRuntimeException; |
| 27 | +import org.apache.sysds.runtime.controlprogram.caching.MatrixObject; |
| 28 | +import org.apache.sysds.runtime.controlprogram.context.ExecutionContext; |
| 29 | +import org.apache.sysds.runtime.controlprogram.parfor.LocalTaskQueue; |
| 30 | +import org.apache.sysds.runtime.instructions.Instruction; |
| 31 | +import org.apache.sysds.runtime.instructions.InstructionUtils; |
| 32 | +import org.apache.sysds.runtime.instructions.cp.CPOperand; |
| 33 | +import org.apache.sysds.runtime.instructions.spark.data.IndexedMatrixValue; |
| 34 | +import org.apache.sysds.runtime.matrix.data.CTableMap; |
| 35 | +import org.apache.sysds.runtime.matrix.data.MatrixBlock; |
| 36 | +import org.apache.sysds.runtime.matrix.operators.Operator; |
| 37 | +import org.apache.sysds.runtime.util.DataConverter; |
| 38 | +import org.apache.sysds.runtime.util.LongLongDoubleHashMap; |
| 39 | + |
| 40 | +public class CtableOOCInstruction extends ComputationOOCInstruction { |
| 41 | + private final CPOperand _outDim1; |
| 42 | + private final CPOperand _outDim2; |
| 43 | + private final boolean _ignoreZeros; |
| 44 | + |
| 45 | + protected CtableOOCInstruction(OOCType type, Operator op, CPOperand in1, |
| 46 | + CPOperand in2, CPOperand in3, CPOperand out, CPOperand outDim1, CPOperand outDim2, |
| 47 | + boolean ignoreZeros, String opcode, String istr) |
| 48 | + { |
| 49 | + super(type, op, in1, in2, in3, out, opcode, istr); |
| 50 | + _ignoreZeros = ignoreZeros; |
| 51 | + _outDim1 = outDim1; |
| 52 | + _outDim2 = outDim2; |
| 53 | + } |
| 54 | + |
| 55 | + public static CtableOOCInstruction parseInstruction(String str) { |
| 56 | + String[] parts = InstructionUtils.getInstructionPartsWithValueType(str); |
| 57 | + InstructionUtils.checkNumFields(parts, 8); |
| 58 | + |
| 59 | + String opcode = parts[0]; |
| 60 | + CPOperand in1 = new CPOperand(parts[1]); |
| 61 | + CPOperand in2 = new CPOperand(parts[2]); |
| 62 | + CPOperand in3 = new CPOperand(parts[3]); |
| 63 | + CPOperand out = new CPOperand(parts[6]); |
| 64 | + |
| 65 | + String[] dim1Fields = parts[4].split(Instruction.LITERAL_PREFIX); |
| 66 | + String[] dim2Fields = parts[5].split(Instruction.LITERAL_PREFIX); |
| 67 | + CPOperand outDim1 = new CPOperand(dim1Fields[0], Types.ValueType.FP64, |
| 68 | + Types.DataType.SCALAR, Boolean.parseBoolean(dim1Fields[1])); |
| 69 | + CPOperand outDim2 = new CPOperand(dim2Fields[0], Types.ValueType.FP64, |
| 70 | + Types.DataType.SCALAR, Boolean.parseBoolean(dim2Fields[1])); |
| 71 | + |
| 72 | + boolean ignoreZeros = Boolean.parseBoolean(parts[7]); |
| 73 | + |
| 74 | + // does not require any op |
| 75 | + return new CtableOOCInstruction(OOCType.Ctable, null, in1, in2, in3, out, outDim1, outDim2, ignoreZeros, opcode, str); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void processInstruction( ExecutionContext ec ) { |
| 80 | + |
| 81 | + MatrixObject in1 = ec.getMatrixObject(input1); // stream |
| 82 | + LocalTaskQueue<IndexedMatrixValue> qIn1 = in1.getStreamHandle(); |
| 83 | + IndexedMatrixValue tmp1 = null; |
| 84 | + |
| 85 | + long outputDim1 = ec.getScalarInput(_outDim1).getLongValue(); |
| 86 | + long outputDim2 = ec.getScalarInput(_outDim2).getLongValue(); |
| 87 | + |
| 88 | + long cols = in1.getDataCharacteristics().getNumColBlocks(); |
| 89 | + CTableMap map = new CTableMap(LongLongDoubleHashMap.EntryType.INT); |
| 90 | + |
| 91 | + Ctable.OperationTypes ctableOp = findCtableOperation(); |
| 92 | + MatrixObject in2 = null, in3 = null; |
| 93 | + LocalTaskQueue<IndexedMatrixValue> qIn2 = null, qIn3 = null; |
| 94 | + double cst2 = 0, cst3 = 0; |
| 95 | + |
| 96 | + // init vars based on ctableOp |
| 97 | + if (ctableOp.hasSecondInput()){ |
| 98 | + in2 = ec.getMatrixObject(input2); // stream |
| 99 | + qIn2 = in2.getStreamHandle(); |
| 100 | + } else |
| 101 | + cst2 = ec.getScalarInput(input2).getDoubleValue(); |
| 102 | + |
| 103 | + if (ctableOp.hasThirdInput()){ |
| 104 | + in3 = ec.getMatrixObject(input3); // stream |
| 105 | + qIn3 = in3.getStreamHandle(); |
| 106 | + } else |
| 107 | + cst3 = ec.getScalarInput(input3).getDoubleValue(); |
| 108 | + |
| 109 | + HashMap<Long, MatrixBlock> blocksIn2 = new HashMap<>(), blocksIn3 = new HashMap<>(); |
| 110 | + MatrixBlock block2, block3; |
| 111 | + |
| 112 | + // only init result block if output dims known and dense |
| 113 | + MatrixBlock result = null; |
| 114 | + boolean outputDimsKnown = (outputDim1 != -1 && outputDim2 != -1); |
| 115 | + if (outputDimsKnown){ |
| 116 | + long totalRows = in1.getDataCharacteristics().getRows(); |
| 117 | + long totalCols = in1.getDataCharacteristics().getCols(); |
| 118 | + boolean sparse = MatrixBlock.evalSparseFormatInMemory(outputDim1, outputDim2, totalRows*totalCols); |
| 119 | + if(!sparse) |
| 120 | + result = new MatrixBlock((int)outputDim1, (int)outputDim2, false); |
| 121 | + } |
| 122 | + |
| 123 | + try { |
| 124 | + while((tmp1 = qIn1.dequeueTask()) != LocalTaskQueue.NO_MORE_TASKS) { |
| 125 | + |
| 126 | + MatrixBlock block1 = (MatrixBlock) tmp1.getValue(); |
| 127 | + long r = tmp1.getIndexes().getRowIndex(); |
| 128 | + long c = tmp1.getIndexes().getColumnIndex(); |
| 129 | + long key = (r-1) * cols + (c-1); |
| 130 | + |
| 131 | + switch(ctableOp) { |
| 132 | + case CTABLE_TRANSFORM: |
| 133 | + // ctable(A,B,W) |
| 134 | + block2 = getOrDequeueBlock(key, cols, blocksIn2, qIn2); |
| 135 | + block3 = getOrDequeueBlock(key, cols, blocksIn3, qIn3); |
| 136 | + block1.ctableOperations(_optr, block2, block3, map, result); |
| 137 | + break; |
| 138 | + case CTABLE_TRANSFORM_SCALAR_WEIGHT: |
| 139 | + // ctable(A,B) or ctable(A,B,1) |
| 140 | + block2 = getOrDequeueBlock(key, cols, blocksIn2, qIn2); |
| 141 | + block1.ctableOperations(_optr, block2, cst3, _ignoreZeros, map, result); |
| 142 | + break; |
| 143 | + case CTABLE_TRANSFORM_HISTOGRAM: |
| 144 | + // ctable(A,1) or ctable(A,1,1) |
| 145 | + block1.ctableOperations(_optr, cst2, cst3, map, result); |
| 146 | + break; |
| 147 | + case CTABLE_TRANSFORM_WEIGHTED_HISTOGRAM: |
| 148 | + // ctable(A,1,W) |
| 149 | + block3 = getOrDequeueBlock(key, cols, blocksIn3, qIn3); |
| 150 | + block1.ctableOperations(_optr, cst2, block3, map, result); |
| 151 | + break; |
| 152 | + |
| 153 | + default: |
| 154 | + throw new DMLRuntimeException("Encountered an invalid OOC ctable operation " |
| 155 | + + "("+ctableOp+") while executing instruction: " + this); |
| 156 | + } |
| 157 | + } |
| 158 | + if (result == null){ |
| 159 | + if(outputDimsKnown) |
| 160 | + result = DataConverter.convertToMatrixBlock(map, (int)outputDim1, (int)outputDim2); |
| 161 | + else |
| 162 | + result = DataConverter.convertToMatrixBlock(map); |
| 163 | + } |
| 164 | + else |
| 165 | + result.examSparsity(); |
| 166 | + |
| 167 | + ec.setMatrixOutput(output.getName(), result); |
| 168 | + } |
| 169 | + catch(Exception ex) { |
| 170 | + throw new DMLRuntimeException(ex); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + private MatrixBlock getOrDequeueBlock(long key, long cols, HashMap<Long, MatrixBlock> blocks, |
| 175 | + LocalTaskQueue<IndexedMatrixValue> queue) throws InterruptedException |
| 176 | + { |
| 177 | + MatrixBlock block = blocks.get(key); |
| 178 | + if (block == null) { |
| 179 | + IndexedMatrixValue tmp; |
| 180 | + // corresponding block still in queue, dequeue until found |
| 181 | + while ((tmp = queue.dequeueTask()) != LocalTaskQueue.NO_MORE_TASKS) { |
| 182 | + block = (MatrixBlock) tmp.getValue(); |
| 183 | + long r = tmp.getIndexes().getRowIndex(); |
| 184 | + long c = tmp.getIndexes().getColumnIndex(); |
| 185 | + long tmpKey = (r-1) * cols + (c-1); |
| 186 | + // found corresponding block |
| 187 | + if (tmpKey == key) break; |
| 188 | + // store all dequeued blocks in cache that we don't need yet |
| 189 | + blocks.put(tmpKey, block); |
| 190 | + } |
| 191 | + } |
| 192 | + else |
| 193 | + blocks.remove(key); // needed only once |
| 194 | + |
| 195 | + return block; |
| 196 | + } |
| 197 | + |
| 198 | + private Ctable.OperationTypes findCtableOperation() { |
| 199 | + Types.DataType dt1 = input1.getDataType(); |
| 200 | + Types.DataType dt2 = input2.getDataType(); |
| 201 | + Types.DataType dt3 = input3.getDataType(); |
| 202 | + return Ctable.findCtableOperationByInputDataTypes(dt1, dt2, dt3); |
| 203 | + } |
| 204 | +} |
0 commit comments