Repository: incubator-systemml Updated Branches: refs/heads/master 0f2085498 -> de1e119de
[SYSTEMML-1150] Fix aggregate loop vectorization rewrite, incl tests Project: http://git-wip-us.apache.org/repos/asf/incubator-systemml/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-systemml/commit/827cdba9 Tree: http://git-wip-us.apache.org/repos/asf/incubator-systemml/tree/827cdba9 Diff: http://git-wip-us.apache.org/repos/asf/incubator-systemml/diff/827cdba9 Branch: refs/heads/master Commit: 827cdba935adebef02eed710f0e66d5b22d0cba8 Parents: 0f20854 Author: Matthias Boehm <[email protected]> Authored: Thu Feb 9 20:32:47 2017 +0100 Committer: Matthias Boehm <[email protected]> Committed: Fri Feb 10 07:55:51 2017 +0100 ---------------------------------------------------------------------- .../rewrite/RewriteForLoopVectorization.java | 13 ++- .../misc/RewriteLoopVectorization.java | 106 +++++++++++++++++++ .../misc/RewriteLoopVectorizationSum.R | 31 ++++++ .../misc/RewriteLoopVectorizationSum.dml | 30 ++++++ .../misc/RewriteLoopVectorizationSum2.R | 31 ++++++ .../misc/RewriteLoopVectorizationSum2.dml | 31 ++++++ 6 files changed, 240 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/827cdba9/src/main/java/org/apache/sysml/hops/rewrite/RewriteForLoopVectorization.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/hops/rewrite/RewriteForLoopVectorization.java b/src/main/java/org/apache/sysml/hops/rewrite/RewriteForLoopVectorization.java index 99c4995..08a9599 100644 --- a/src/main/java/org/apache/sysml/hops/rewrite/RewriteForLoopVectorization.java +++ b/src/main/java/org/apache/sysml/hops/rewrite/RewriteForLoopVectorization.java @@ -179,11 +179,14 @@ public class RewriteForLoopVectorization extends StatementBlockRewriteRule Hop ix = cast.getInput().get(0); int aggOpPos = HopRewriteUtils.getValidOpPos(bop.getOp(), MAP_SCALAR_AGGREGATE_SOURCE_OPS); AggOp aggOp = MAP_SCALAR_AGGREGATE_TARGET_OPS[aggOpPos]; + //replace cast with sum - AggUnaryOp newSum = new AggUnaryOp(cast.getName(), DataType.SCALAR, ValueType.DOUBLE, aggOp, Direction.RowCol, ix); + AggUnaryOp newSum = new AggUnaryOp(cast.getName(), DataType.SCALAR, ValueType.DOUBLE, + aggOp, Direction.RowCol, ix); HopRewriteUtils.removeChildReference(cast, ix); HopRewriteUtils.removeChildReference(bop, cast); HopRewriteUtils.addChildReference(bop, newSum, leftScalar?1:0 ); + //modify indexing expression according to loop predicate from-to //NOTE: any redundant index operations are removed via dynamic algebraic simplification rewrites int index1 = rowIx ? 1 : 3; @@ -193,8 +196,14 @@ public class RewriteForLoopVectorization extends StatementBlockRewriteRule HopRewriteUtils.removeChildReferenceByPos(ix, ix.getInput().get(index2), index2); HopRewriteUtils.addChildReference(ix, to, index2); + //update indexing size information + if( rowIx ) + ((IndexingOp)ix).setRowLowerEqualsUpper(false); + else + ((IndexingOp)ix).setColLowerEqualsUpper(false); + ix.refreshSizeInformation(); + ret = csb; - //ret.liveIn().removeVariable(itervar); LOG.debug("Applied vectorizeScalarSumForLoop."); } http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/827cdba9/src/test/java/org/apache/sysml/test/integration/functions/misc/RewriteLoopVectorization.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/sysml/test/integration/functions/misc/RewriteLoopVectorization.java b/src/test/java/org/apache/sysml/test/integration/functions/misc/RewriteLoopVectorization.java new file mode 100644 index 0000000..97632b1 --- /dev/null +++ b/src/test/java/org/apache/sysml/test/integration/functions/misc/RewriteLoopVectorization.java @@ -0,0 +1,106 @@ +/* + * 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.sysml.test.integration.functions.misc; + +import java.util.HashMap; + +import org.junit.Test; +import org.apache.sysml.hops.OptimizerUtils; +import org.apache.sysml.runtime.matrix.data.MatrixValue.CellIndex; +import org.apache.sysml.test.integration.AutomatedTestBase; +import org.apache.sysml.test.integration.TestConfiguration; +import org.apache.sysml.test.utils.TestUtils; + +/** + * Regression test for loop vectorization rewrite + * for(i in 1:n) s = s + as.scalar(A[i,1]) -> s = s + sum(A[1:n,1]) + * + */ +public class RewriteLoopVectorization extends AutomatedTestBase +{ + private static final String TEST_NAME1 = "RewriteLoopVectorizationSum"; //amendable + private static final String TEST_NAME2 = "RewriteLoopVectorizationSum2"; //not amendable + + private static final String TEST_DIR = "functions/misc/"; + private static final String TEST_CLASS_DIR = TEST_DIR + RewriteLoopVectorization.class.getSimpleName() + "/"; + + @Override + public void setUp() { + TestUtils.clearAssertionInformation(); + addTestConfiguration( TEST_NAME1, new TestConfiguration(TEST_CLASS_DIR, TEST_NAME1, new String[] { "R" }) ); + addTestConfiguration( TEST_NAME2, new TestConfiguration(TEST_CLASS_DIR, TEST_NAME2, new String[] { "R" }) ); + } + + @Test + public void testLoopVectorizationSumNoRewrite() { + testRewriteLoopVectorizationSum( TEST_NAME1, false ); + } + + @Test + public void testLoopVectorizationSumRewrite() { + testRewriteLoopVectorizationSum( TEST_NAME1, true ); + } + + @Test + public void testLoopVectorizationSum2NoRewrite() { + testRewriteLoopVectorizationSum( TEST_NAME2, false ); + } + + @Test + public void testLoopVectorizationSum2Rewrite() { + testRewriteLoopVectorizationSum( TEST_NAME2, true ); + } + + /** + * + * @param testname + * @param rewrites + */ + private void testRewriteLoopVectorizationSum( String testname, boolean rewrites ) + { + boolean oldFlag = OptimizerUtils.ALLOW_ALGEBRAIC_SIMPLIFICATION; + + try + { + TestConfiguration config = getTestConfiguration(testname); + loadTestConfiguration(config); + + String HOME = SCRIPT_DIR + TEST_DIR; + fullDMLScriptName = HOME + testname + ".dml"; + programArgs = new String[]{ "-stats","-args", output("Scalar") }; + + fullRScriptName = HOME + testname + ".R"; + rCmd = getRCmd(inputDir(), expectedDir()); + + OptimizerUtils.ALLOW_ALGEBRAIC_SIMPLIFICATION = rewrites; + + runTest(true, false, null, -1); + runRScript(true); + + //compare scalars + HashMap<CellIndex, Double> dmlfile = readDMLScalarFromHDFS("Scalar"); + HashMap<CellIndex, Double> rfile = readRScalarFromFS("Scalar"); + TestUtils.compareScalars(dmlfile.toString(), rfile.toString()); + } + finally { + OptimizerUtils.ALLOW_ALGEBRAIC_SIMPLIFICATION = oldFlag; + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/827cdba9/src/test/scripts/functions/misc/RewriteLoopVectorizationSum.R ---------------------------------------------------------------------- diff --git a/src/test/scripts/functions/misc/RewriteLoopVectorizationSum.R b/src/test/scripts/functions/misc/RewriteLoopVectorizationSum.R new file mode 100644 index 0000000..b4a0cd2 --- /dev/null +++ b/src/test/scripts/functions/misc/RewriteLoopVectorizationSum.R @@ -0,0 +1,31 @@ +# 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. +# +#------------------------------------------------------------- + +args<-commandArgs(TRUE) + +A = matrix(7.0, 10, 10) +n = nrow(A) +s = 0.0 + +for( i in 1:n ) { + s = s + A[i,1] +} + +write(s, paste(args[2], "Scalar",sep="")) + http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/827cdba9/src/test/scripts/functions/misc/RewriteLoopVectorizationSum.dml ---------------------------------------------------------------------- diff --git a/src/test/scripts/functions/misc/RewriteLoopVectorizationSum.dml b/src/test/scripts/functions/misc/RewriteLoopVectorizationSum.dml new file mode 100644 index 0000000..f199af3 --- /dev/null +++ b/src/test/scripts/functions/misc/RewriteLoopVectorizationSum.dml @@ -0,0 +1,30 @@ +#------------------------------------------------------------- +# +# 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. +# +#------------------------------------------------------------- + +A = matrix(7.0, 10, 10) +n = nrow(A) +s = 0.0 + +for( i in 1:n ) { + s = s + as.scalar(A[i,1]) +} + +write(s, $1) http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/827cdba9/src/test/scripts/functions/misc/RewriteLoopVectorizationSum2.R ---------------------------------------------------------------------- diff --git a/src/test/scripts/functions/misc/RewriteLoopVectorizationSum2.R b/src/test/scripts/functions/misc/RewriteLoopVectorizationSum2.R new file mode 100644 index 0000000..b4a0cd2 --- /dev/null +++ b/src/test/scripts/functions/misc/RewriteLoopVectorizationSum2.R @@ -0,0 +1,31 @@ +# 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. +# +#------------------------------------------------------------- + +args<-commandArgs(TRUE) + +A = matrix(7.0, 10, 10) +n = nrow(A) +s = 0.0 + +for( i in 1:n ) { + s = s + A[i,1] +} + +write(s, paste(args[2], "Scalar",sep="")) + http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/827cdba9/src/test/scripts/functions/misc/RewriteLoopVectorizationSum2.dml ---------------------------------------------------------------------- diff --git a/src/test/scripts/functions/misc/RewriteLoopVectorizationSum2.dml b/src/test/scripts/functions/misc/RewriteLoopVectorizationSum2.dml new file mode 100644 index 0000000..382733a --- /dev/null +++ b/src/test/scripts/functions/misc/RewriteLoopVectorizationSum2.dml @@ -0,0 +1,31 @@ +#------------------------------------------------------------- +# +# 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. +# +#------------------------------------------------------------- + +A = matrix(7.0, 10, 10) +n = nrow(A) +s = 0.0 + +for( i in 1:n ) { + print("[i,1]: " + i) + s = s + as.scalar(A[i,1]) +} + +write(s, $1)
