|
| 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.commons.lang3.NotImplementedException; |
| 23 | +import org.apache.commons.lang3.mutable.MutableObject; |
| 24 | +import org.apache.sysds.common.Opcodes; |
| 25 | +import org.apache.sysds.common.Types; |
| 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.functionobjects.ParameterizedBuiltin; |
| 30 | +import org.apache.sysds.runtime.functionobjects.ValueFunction; |
| 31 | +import org.apache.sysds.runtime.instructions.InstructionUtils; |
| 32 | +import org.apache.sysds.runtime.instructions.cp.BooleanObject; |
| 33 | +import org.apache.sysds.runtime.instructions.cp.CPOperand; |
| 34 | +import org.apache.sysds.runtime.instructions.cp.Data; |
| 35 | +import org.apache.sysds.runtime.instructions.cp.ParameterizedBuiltinCPInstruction; |
| 36 | +import org.apache.sysds.runtime.instructions.cp.ScalarObject; |
| 37 | +import org.apache.sysds.runtime.instructions.cp.ScalarObjectFactory; |
| 38 | +import org.apache.sysds.runtime.instructions.spark.data.IndexedMatrixValue; |
| 39 | +import org.apache.sysds.runtime.matrix.data.MatrixBlock; |
| 40 | +import org.apache.sysds.runtime.matrix.operators.Operator; |
| 41 | +import org.apache.sysds.runtime.matrix.operators.SimpleOperator; |
| 42 | + |
| 43 | +import java.util.LinkedHashMap; |
| 44 | +import java.util.concurrent.CompletableFuture; |
| 45 | +import java.util.concurrent.ExecutionException; |
| 46 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 47 | + |
| 48 | +public class ParameterizedBuiltinOOCInstruction extends ComputationOOCInstruction { |
| 49 | + |
| 50 | + protected final LinkedHashMap<String, String> params; |
| 51 | + |
| 52 | + protected ParameterizedBuiltinOOCInstruction(Operator op, LinkedHashMap<String, String> paramsMap, CPOperand out, |
| 53 | + String opcode, String istr) { |
| 54 | + super(OOCInstruction.OOCType.ParameterizedBuiltin, op, null, null, out, opcode, istr); |
| 55 | + params = paramsMap; |
| 56 | + } |
| 57 | + |
| 58 | + public static ParameterizedBuiltinOOCInstruction parseInstruction(String str) { |
| 59 | + String[] parts = InstructionUtils.getInstructionPartsWithValueType(str); |
| 60 | + // first part is always the opcode |
| 61 | + String opcode = parts[0]; |
| 62 | + // last part is always the output |
| 63 | + CPOperand out = new CPOperand(parts[parts.length - 1]); |
| 64 | + |
| 65 | + // process remaining parts and build a hash map |
| 66 | + LinkedHashMap<String, String> paramsMap = ParameterizedBuiltinCPInstruction.constructParameterMap(parts); |
| 67 | + |
| 68 | + // determine the appropriate value function |
| 69 | + ValueFunction func = null; |
| 70 | + |
| 71 | + if(opcode.equalsIgnoreCase(Opcodes.REPLACE.toString())) { |
| 72 | + func = ParameterizedBuiltin.getParameterizedBuiltinFnObject(opcode); |
| 73 | + return new ParameterizedBuiltinOOCInstruction(new SimpleOperator(func), paramsMap, out, opcode, str); |
| 74 | + } |
| 75 | + else if(opcode.equalsIgnoreCase(Opcodes.CONTAINS.toString())) { |
| 76 | + return new ParameterizedBuiltinOOCInstruction(null, paramsMap, out, opcode, str); |
| 77 | + } |
| 78 | + else |
| 79 | + throw new NotImplementedException(); // TODO |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void processInstruction(ExecutionContext ec) { |
| 84 | + if(instOpcode.equalsIgnoreCase(Opcodes.REPLACE.toString())) { |
| 85 | + if(ec.isFrameObject(params.get("target"))){ |
| 86 | + throw new NotImplementedException(); |
| 87 | + } else{ |
| 88 | + MatrixObject targetObj = ec.getMatrixObject(params.get("target")); |
| 89 | + OOCStream<IndexedMatrixValue> qIn = targetObj.getStreamHandle(); |
| 90 | + OOCStream<IndexedMatrixValue> qOut = createWritableStream(); |
| 91 | + |
| 92 | + double pattern = Double.parseDouble(params.get("pattern")); |
| 93 | + double replacement = Double.parseDouble(params.get("replacement")); |
| 94 | + |
| 95 | + mapOOC(qIn, qOut, tmp -> new IndexedMatrixValue(tmp.getIndexes(), tmp.getValue().replaceOperations(new MatrixBlock(), pattern, replacement))); |
| 96 | + |
| 97 | + ec.getMatrixObject(output).setStreamHandle(qOut); |
| 98 | + } |
| 99 | + } |
| 100 | + else if(instOpcode.equalsIgnoreCase(Opcodes.CONTAINS.toString())) { |
| 101 | + MatrixObject targetObj = ec.getMatrixObject(params.get("target")); |
| 102 | + OOCStream<IndexedMatrixValue> qIn = targetObj.getStreamHandle(); |
| 103 | + Data pattern = ec.getVariable(params.get("pattern")); |
| 104 | + |
| 105 | + if( pattern == null ) //literal |
| 106 | + pattern = ScalarObjectFactory.createScalarObject(Types.ValueType.FP64, params.get("pattern")); |
| 107 | + |
| 108 | + if (!pattern.getDataType().isScalar()) |
| 109 | + throw new NotImplementedException(); |
| 110 | + |
| 111 | + Data finalPattern = pattern; |
| 112 | + |
| 113 | + AtomicBoolean found = new AtomicBoolean(false); |
| 114 | + |
| 115 | + MutableObject<CompletableFuture<Void>> futureRef = new MutableObject<>(); |
| 116 | + CompletableFuture<Void> future = submitOOCTasks(qIn, tmp -> { |
| 117 | + boolean contains = ((MatrixBlock)tmp.getValue()).containsValue(((ScalarObject)finalPattern).getDoubleValue()); |
| 118 | + |
| 119 | + if (contains) { |
| 120 | + found.set(true); |
| 121 | + |
| 122 | + // Now we may complete the future |
| 123 | + if (futureRef.getValue() != null) |
| 124 | + futureRef.getValue().complete(null); |
| 125 | + } |
| 126 | + }, () -> {}); |
| 127 | + futureRef.setValue(future); |
| 128 | + |
| 129 | + try { |
| 130 | + futureRef.getValue().get(); |
| 131 | + } catch (InterruptedException | ExecutionException e) { |
| 132 | + throw new DMLRuntimeException(e); |
| 133 | + } |
| 134 | + |
| 135 | + boolean ret = found.get(); |
| 136 | + ec.setScalarOutput(output.getName(), new BooleanObject(ret)); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments