|
| 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.common.Opcodes; |
| 23 | +import org.apache.sysds.runtime.DMLRuntimeException; |
| 24 | +import org.apache.sysds.runtime.controlprogram.caching.MatrixObject; |
| 25 | +import org.apache.sysds.runtime.controlprogram.context.ExecutionContext; |
| 26 | +import org.apache.sysds.runtime.instructions.InstructionUtils; |
| 27 | +import org.apache.sysds.runtime.instructions.cp.CPOperand; |
| 28 | +import org.apache.sysds.runtime.instructions.spark.data.IndexedMatrixValue; |
| 29 | +import org.apache.sysds.runtime.io.FileFormatProperties; |
| 30 | +import org.apache.sysds.runtime.io.FileFormatPropertiesCSV; |
| 31 | +import org.apache.sysds.runtime.io.ReaderTextCSVParallel; |
| 32 | +import org.apache.sysds.runtime.matrix.operators.Operator; |
| 33 | +import org.apache.sysds.runtime.meta.DataCharacteristics; |
| 34 | + |
| 35 | +public class CSVReblockOOCInstruction extends ComputationOOCInstruction { |
| 36 | + private final int blen; |
| 37 | + |
| 38 | + private CSVReblockOOCInstruction(Operator op, CPOperand in, CPOperand out, int blocklength, String opcode, |
| 39 | + String instr) { |
| 40 | + super(OOCType.Reblock, op, in, out, opcode, instr); |
| 41 | + blen = blocklength; |
| 42 | + } |
| 43 | + |
| 44 | + public static CSVReblockOOCInstruction parseInstruction(String str) { |
| 45 | + String[] parts = InstructionUtils.getInstructionPartsWithValueType(str); |
| 46 | + String opcode = parts[0]; |
| 47 | + if(!opcode.equals(Opcodes.CSVRBLK.toString())) |
| 48 | + throw new DMLRuntimeException("Incorrect opcode for CSVReblockOOCInstruction:" + opcode); |
| 49 | + |
| 50 | + CPOperand in = new CPOperand(parts[1]); |
| 51 | + CPOperand out = new CPOperand(parts[2]); |
| 52 | + int blen = Integer.parseInt(parts[3]); |
| 53 | + return new CSVReblockOOCInstruction(null, in, out, blen, opcode, str); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void processInstruction(ExecutionContext ec) { |
| 58 | + MatrixObject min = ec.getMatrixObject(input1); |
| 59 | + DataCharacteristics mc = ec.getDataCharacteristics(input1.getName()); |
| 60 | + DataCharacteristics mcOut = ec.getDataCharacteristics(output.getName()); |
| 61 | + mcOut.set(mc.getRows(), mc.getCols(), blen, mc.getNonZeros()); |
| 62 | + |
| 63 | + OOCStream<IndexedMatrixValue> qOut = createWritableStream(); |
| 64 | + addOutStream(qOut); |
| 65 | + |
| 66 | + FileFormatProperties props = min.getFileFormatProperties(); |
| 67 | + final FileFormatPropertiesCSV csvProps = props instanceof FileFormatPropertiesCSV ? (FileFormatPropertiesCSV) props |
| 68 | + : new FileFormatPropertiesCSV(); |
| 69 | + |
| 70 | + final ReaderTextCSVParallel reader = new ReaderTextCSVParallel(csvProps); |
| 71 | + final String fileName = min.getFileName(); |
| 72 | + final long rows = mc.getRows(); |
| 73 | + final long cols = mc.getCols(); |
| 74 | + final long nnz = mc.getNonZeros(); |
| 75 | + |
| 76 | + submitOOCTask(() -> { |
| 77 | + try { |
| 78 | + reader.readMatrixAsStream(qOut, fileName, rows, cols, blen, nnz); |
| 79 | + } |
| 80 | + catch(Exception ex) { |
| 81 | + throw (ex instanceof DMLRuntimeException) ? (DMLRuntimeException) ex : new DMLRuntimeException(ex); |
| 82 | + } |
| 83 | + }, qOut); |
| 84 | + |
| 85 | + MatrixObject mout = ec.getMatrixObject(output); |
| 86 | + mout.setStreamHandle(qOut); |
| 87 | + } |
| 88 | +} |
0 commit comments