j143 commented on code in PR #2319: URL: https://github.com/apache/systemds/pull/2319#discussion_r2316413825
########## src/test/java/org/apache/sysds/test/functions/ooc/MatrixMatrixBinaryMultiplicationTest.java: ########## @@ -0,0 +1,130 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.sysds.test.functions.ooc; + +import org.apache.sysds.common.Types; +import org.apache.sysds.runtime.io.MatrixWriter; +import org.apache.sysds.runtime.io.MatrixWriterFactory; +import org.apache.sysds.runtime.matrix.data.MatrixBlock; +import org.apache.sysds.runtime.meta.MatrixCharacteristics; +import org.apache.sysds.runtime.util.DataConverter; +import org.apache.sysds.runtime.util.HDFSTool; +import org.apache.sysds.test.AutomatedTestBase; +import org.apache.sysds.test.TestConfiguration; +import org.apache.sysds.test.TestUtils; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; + +public class MatrixMatrixBinaryMultiplicationTest extends AutomatedTestBase { + private final static String TEST_NAME1 = "MatrixMatrixMultiplication"; + private final static String TEST_DIR = "functions/ooc/"; + private final static String TEST_CLASS_DIR = TEST_DIR + MatrixMatrixBinaryMultiplicationTest.class.getSimpleName() + "/"; + private final static double eps = 1e-10; + private static final String INPUT_NAME = "X"; + private static final String INPUT_NAME2 = "Y"; + private static final String OUTPUT_NAME = "res"; + + private final static int rows = 1000; + private final static int cols_wide = 1000; + private final static int rows2 = 1000; + private final static int cols2 = 1000; + + private final static double sparsity1 = 0.7; + private final static double sparsity2 = 0.1; + + @Override + public void setUp() { + TestUtils.clearAssertionInformation(); + TestConfiguration config = new TestConfiguration(TEST_CLASS_DIR, TEST_NAME1); + addTestConfiguration(TEST_NAME1, config); + } + + @Test + public void testMMBinaryMultiplication1() { + runMatrixMatrixMultiplicationTest(cols_wide, false); + } + + private void runMatrixMatrixMultiplicationTest(int cols, boolean sparse ) + { + Types.ExecMode platformOld = setExecMode(Types.ExecMode.SINGLE_NODE); + + try + { + getAndLoadTestConfiguration(TEST_NAME1); + String HOME = SCRIPT_DIR + TEST_DIR; + fullDMLScriptName = HOME + TEST_NAME1 + ".dml"; + programArgs = new String[]{"-explain", "-stats", "-ooc", + "-args", input(INPUT_NAME), input(INPUT_NAME2), output(OUTPUT_NAME)}; + + // 1. Generate the data in-memory as MatrixBlock objects + double[][] A_data = getRandomMatrix(rows, cols, 0, 1, sparse?sparsity2:sparsity1, 10); + double[][] B_data = getRandomMatrix(rows2, cols2, 0, 1, 1.0, 10); + + // 2. Convert the double arrays to MatrixBlock objects + MatrixBlock A_mb = DataConverter.convertToMatrixBlock(A_data); + MatrixBlock B_mb = DataConverter.convertToMatrixBlock(B_data); + + // 3. Create a binary matrix writer + MatrixWriter writer = MatrixWriterFactory.createMatrixWriter(Types.FileFormat.BINARY); + + // 4. Write matrix A to a binary SequenceFile + writer.writeMatrixToHDFS(A_mb, input(INPUT_NAME), rows, cols, 1000, A_mb.getNonZeros()); + HDFSTool.writeMetaDataFile(input(INPUT_NAME + ".mtd"), Types.ValueType.FP64, + new MatrixCharacteristics(rows, cols, 1000, A_mb.getNonZeros()), Types.FileFormat.BINARY); + + // 5. Write vector B to a binary SequenceFile + writer.writeMatrixToHDFS(B_mb, input(INPUT_NAME2), rows2, cols2, 1000, B_mb.getNonZeros()); + HDFSTool.writeMetaDataFile(input(INPUT_NAME2 + ".mtd"), Types.ValueType.FP64, + new MatrixCharacteristics(rows2, cols2, 1000, B_mb.getNonZeros()), Types.FileFormat.BINARY); + + boolean exceptionExpected = false; + runTest(true, exceptionExpected, null, -1); + + double[][] C1 = readMatrix(output(OUTPUT_NAME), Types.FileFormat.BINARY, rows, cols, 1000, 1000); + double result = 0.0; + for(int i = 0; i < rows; i++) { // verify the results with Java Review Comment: done. ########## src/main/java/org/apache/sysds/runtime/instructions/ooc/MatrixMatrixBinaryOOCInstruction.java: ########## @@ -0,0 +1,257 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.sysds.runtime.instructions.ooc; + +import org.apache.sysds.common.Opcodes; +import org.apache.sysds.runtime.DMLRuntimeException; +import org.apache.sysds.runtime.controlprogram.caching.MatrixObject; +import org.apache.sysds.runtime.controlprogram.context.ExecutionContext; +import org.apache.sysds.runtime.controlprogram.parfor.LocalTaskQueue; +import org.apache.sysds.runtime.functionobjects.Multiply; +import org.apache.sysds.runtime.functionobjects.Plus; +import org.apache.sysds.runtime.instructions.InstructionUtils; +import org.apache.sysds.runtime.instructions.cp.CPOperand; +import org.apache.sysds.runtime.instructions.spark.data.IndexedMatrixValue; +import org.apache.sysds.runtime.matrix.data.MatrixBlock; +import org.apache.sysds.runtime.matrix.data.MatrixIndexes; +import org.apache.sysds.runtime.matrix.operators.AggregateBinaryOperator; +import org.apache.sysds.runtime.matrix.operators.AggregateOperator; +import org.apache.sysds.runtime.matrix.operators.BinaryOperator; +import org.apache.sysds.runtime.matrix.operators.Operator; +import org.apache.sysds.runtime.util.CommonThreadPool; + + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ExecutorService; + +public class MatrixMatrixBinaryOOCInstruction extends ComputationOOCInstruction { + + + protected MatrixMatrixBinaryOOCInstruction(OOCType type, Operator op, CPOperand in1, CPOperand in2, CPOperand out, String opcode, String istr) { + super(type, op, in1, in2, out, opcode, istr); + } + + public static MatrixMatrixBinaryOOCInstruction parseInstruction(String str) { + String[] parts = InstructionUtils.getInstructionPartsWithValueType(str); + InstructionUtils.checkNumFields(parts, 4); + String opcode = parts[0]; + CPOperand in1 = new CPOperand(parts[1]); // the 1st large matrix (streamed) + CPOperand in2 = new CPOperand(parts[2]); // the 2nd large matrix (streamed) + CPOperand out = new CPOperand(parts[3]); + + AggregateOperator agg = new AggregateOperator(0, Plus.getPlusFnObject()); + AggregateBinaryOperator ba = new AggregateBinaryOperator(Multiply.getMultiplyFnObject(), agg); + + return new MatrixMatrixBinaryOOCInstruction(OOCType.MAPMM, ba, in1, in2, out, opcode, str); + } + + /** + * + * @param ec execution context + */ + @Override + public void processInstruction( ExecutionContext ec ) { + // 1. Identify the inputs + MatrixObject min1 = ec.getMatrixObject(input1); + MatrixObject min2 = ec.getMatrixObject(input2); + + + LocalTaskQueue<IndexedMatrixValue> qIn1 = min1.getStreamHandle(); + LocalTaskQueue<IndexedMatrixValue> qIn2 = min2.getStreamHandle(); + LocalTaskQueue<IndexedMatrixValue> qOut = new LocalTaskQueue<>(); + BinaryOperator plus = InstructionUtils.parseBinaryOperator(Opcodes.PLUS.toString()); + ec.getMatrixObject(output).setStreamHandle(qOut); + + // Result matrix rows, cols = rows of A, cols of B + long resultRowBlocks = min1.getDataCharacteristics().getNumRowBlocks(); + System.out.println("resultRowBlocks: " + resultRowBlocks); + long resultColBlocks = min2.getDataCharacteristics().getNumColBlocks(); + System.out.println("resultColBlocks: " + resultColBlocks); + + ExecutorService pool = CommonThreadPool.get(); + try { + // Core logic: background thread + pool.submit(() -> { + IndexedMatrixValue tmpA = null; + IndexedMatrixValue tmpB = null; + try { + // Phase 1: grouping the output blocks by block Index (The Shuffle) + Map<MatrixIndexes, List<TaggedMatrixValue>> groupedBlocks = new HashMap<>(); + HashMap<Long, MatrixBlock> partialResults = new HashMap<>(); + + // Process matrix A: each block A(i,k) contributes to C(i,j) for all j + while((tmpA = qIn1.dequeueTask()) != LocalTaskQueue.NO_MORE_TASKS) { + long i = tmpA.getIndexes().getRowIndex() - 1; + long k = tmpA.getIndexes().getColumnIndex() - 1; + System.out.println("i: " + i + ", k: " + k); + + for (int j=0; j<resultColBlocks; j++) { + MatrixIndexes index = new MatrixIndexes(i, j); // 1,1= A11,A12,A13,B11,B21,B31 + + // Create a copy + MatrixBlock sourceBlock = (MatrixBlock) tmpA.getValue(); + MatrixBlock blockCopy = new MatrixBlock(sourceBlock); Review Comment: done -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
