|
| 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.concurrent.ExecutorService; |
| 23 | + |
| 24 | +import org.apache.sysds.common.Types.DataType; |
| 25 | +import org.apache.sysds.runtime.DMLRuntimeException; |
| 26 | +import org.apache.sysds.runtime.controlprogram.caching.MatrixObject; |
| 27 | +import org.apache.sysds.runtime.controlprogram.context.ExecutionContext; |
| 28 | +import org.apache.sysds.runtime.controlprogram.parfor.LocalTaskQueue; |
| 29 | +import org.apache.sysds.runtime.instructions.InstructionUtils; |
| 30 | +import org.apache.sysds.runtime.instructions.cp.CPOperand; |
| 31 | +import org.apache.sysds.runtime.instructions.cp.ScalarObject; |
| 32 | +import org.apache.sysds.runtime.instructions.spark.data.IndexedMatrixValue; |
| 33 | +import org.apache.sysds.runtime.matrix.data.MatrixBlock; |
| 34 | +import org.apache.sysds.runtime.matrix.operators.Operator; |
| 35 | +import org.apache.sysds.runtime.matrix.operators.ScalarOperator; |
| 36 | +import org.apache.sysds.runtime.util.CommonThreadPool; |
| 37 | + |
| 38 | +public class BinaryOOCInstruction extends ComputationOOCInstruction { |
| 39 | + |
| 40 | + protected BinaryOOCInstruction(OOCType type, Operator bop, |
| 41 | + CPOperand in1, CPOperand in2, CPOperand out, String opcode, String istr) { |
| 42 | + super(type, bop, in1, in2, out, opcode, istr); |
| 43 | + } |
| 44 | + |
| 45 | + public static BinaryOOCInstruction parseInstruction(String str) { |
| 46 | + String[] parts = InstructionUtils.getInstructionPartsWithValueType(str); |
| 47 | + InstructionUtils.checkNumFields(parts, 3); |
| 48 | + String opcode = parts[0]; |
| 49 | + CPOperand in1 = new CPOperand(parts[1]); |
| 50 | + CPOperand in2 = new CPOperand(parts[2]); |
| 51 | + CPOperand out = new CPOperand(parts[3]); |
| 52 | + Operator bop = InstructionUtils.parseExtendedBinaryOrBuiltinOperator(opcode, in1, in2); |
| 53 | + |
| 54 | + return new BinaryOOCInstruction( |
| 55 | + OOCType.Binary, bop, in1, in2, out, opcode, str); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void processInstruction( ExecutionContext ec ) { |
| 60 | + //TODO support all types, currently only binary matrix-scalar |
| 61 | + |
| 62 | + //get operator and scalar |
| 63 | + CPOperand scalar = ( input1.getDataType() == DataType.MATRIX ) ? input2 : input1; |
| 64 | + ScalarObject constant = ec.getScalarInput(scalar); |
| 65 | + ScalarOperator sc_op = ((ScalarOperator)_optr).setConstant(constant.getDoubleValue()); |
| 66 | + |
| 67 | + //create thread and process binary operation |
| 68 | + MatrixObject min = ec.getMatrixObject(input1); |
| 69 | + LocalTaskQueue<IndexedMatrixValue> qIn = min.getStreamHandle(); |
| 70 | + LocalTaskQueue<IndexedMatrixValue> qOut = new LocalTaskQueue<>(); |
| 71 | + ec.getMatrixObject(output).setStreamHandle(qOut); |
| 72 | + |
| 73 | + ExecutorService pool = CommonThreadPool.get(); |
| 74 | + try { |
| 75 | + pool.submit(() -> { |
| 76 | + IndexedMatrixValue tmp = null; |
| 77 | + try { |
| 78 | + while((tmp = qIn.dequeueTask()) != LocalTaskQueue.NO_MORE_TASKS) { |
| 79 | + IndexedMatrixValue tmpOut = new IndexedMatrixValue(); |
| 80 | + tmpOut.set(tmp.getIndexes(), |
| 81 | + tmp.getValue().scalarOperations(sc_op, new MatrixBlock())); |
| 82 | + qOut.enqueueTask(tmpOut); |
| 83 | + } |
| 84 | + qOut.closeInput(); |
| 85 | + } |
| 86 | + catch(Exception ex) { |
| 87 | + throw new DMLRuntimeException(ex); |
| 88 | + } |
| 89 | + }); |
| 90 | + } |
| 91 | + finally { |
| 92 | + pool.shutdown(); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments