[SYSTEMML-2044,2045] New dense row block implementations (DRB, LDRB) This patch adds small and large dense row block implementations as instantiations of the new dense block abstraction. Furthermore, this also includes a basic factory for constructing these blocks according to the given row and column dimensions.
Project: http://git-wip-us.apache.org/repos/asf/systemml/repo Commit: http://git-wip-us.apache.org/repos/asf/systemml/commit/9526f7d9 Tree: http://git-wip-us.apache.org/repos/asf/systemml/tree/9526f7d9 Diff: http://git-wip-us.apache.org/repos/asf/systemml/diff/9526f7d9 Branch: refs/heads/master Commit: 9526f7d943b78cc5c15b032a0f2e73a4f7e501d4 Parents: ead46f7 Author: Matthias Boehm <[email protected]> Authored: Fri Dec 8 21:17:57 2017 -0800 Committer: Matthias Boehm <[email protected]> Committed: Tue Dec 12 18:56:02 2017 -0800 ---------------------------------------------------------------------- .../sysml/runtime/matrix/data/DenseBlock.java | 2 +- .../runtime/matrix/data/DenseBlockDRB.java | 116 +++++++++++++++ .../runtime/matrix/data/DenseBlockFactory.java | 48 ++++++ .../runtime/matrix/data/DenseBlockLDRB.java | 146 +++++++++++++++++++ 4 files changed, 311 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/systemml/blob/9526f7d9/src/main/java/org/apache/sysml/runtime/matrix/data/DenseBlock.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/runtime/matrix/data/DenseBlock.java b/src/main/java/org/apache/sysml/runtime/matrix/data/DenseBlock.java index 54d34fb..d498b79 100644 --- a/src/main/java/org/apache/sysml/runtime/matrix/data/DenseBlock.java +++ b/src/main/java/org/apache/sysml/runtime/matrix/data/DenseBlock.java @@ -35,7 +35,7 @@ public abstract class DenseBlock implements Serializable public enum Type { DRB, //dense row block - LDBR, //large dense row block + LDRB, //large dense row block } /** http://git-wip-us.apache.org/repos/asf/systemml/blob/9526f7d9/src/main/java/org/apache/sysml/runtime/matrix/data/DenseBlockDRB.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/runtime/matrix/data/DenseBlockDRB.java b/src/main/java/org/apache/sysml/runtime/matrix/data/DenseBlockDRB.java new file mode 100644 index 0000000..6f2b11d --- /dev/null +++ b/src/main/java/org/apache/sysml/runtime/matrix/data/DenseBlockDRB.java @@ -0,0 +1,116 @@ +/* + * 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.runtime.matrix.data; + +import java.util.Arrays; + +public class DenseBlockDRB extends DenseBlock +{ + private static final long serialVersionUID = 8546723684649816489L; + + private double[] data; + private int rlen; + private int clen; + + public DenseBlockDRB(int rlen, int clen) { + reset(rlen, clen); + } + + @Override + public void reset() { + reset(rlen, clen); + } + + @Override + public void reset(int rlen, int clen) { + int len = rlen * clen; + if( len < capacity() ) + Arrays.fill(data, 0, len, 0); + else + data = new double[len]; + this.rlen = rlen; + this.clen = clen; + } + + @Override + public int numRows() { + return rlen; + } + + @Override + public int numBlocks() { + return 1; + } + + @Override + public long size() { + return rlen * clen; + } + + @Override + public long capacity() { + return (data!=null) ? data.length : -1; + } + + @Override + public long countNonZeros() { + final int len = rlen * clen; + int nnz = 0; + for(int i=0; i<len; i++) + nnz += (data[i]!=0) ? 1 : 0; + return nnz; + } + + @Override + public double[][] values() { + return new double[][]{data}; + } + + @Override + public double[] values(int bix) { + return data; + } + + @Override + public int index(int r) { + return 0; + } + + @Override + public int pos(int r) { + return r * clen; + } + + @Override + public int pos(int r, int c) { + return r * clen + c; + } + + @Override + public void set(int r, int c, double v) { + data[pos(r, c)] = v; + } + + @Override + public double get(int r, int c) { + return data[pos(r, c)]; + } +} http://git-wip-us.apache.org/repos/asf/systemml/blob/9526f7d9/src/main/java/org/apache/sysml/runtime/matrix/data/DenseBlockFactory.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/runtime/matrix/data/DenseBlockFactory.java b/src/main/java/org/apache/sysml/runtime/matrix/data/DenseBlockFactory.java new file mode 100644 index 0000000..695b0af --- /dev/null +++ b/src/main/java/org/apache/sysml/runtime/matrix/data/DenseBlockFactory.java @@ -0,0 +1,48 @@ +/* + * 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.runtime.matrix.data; + +public abstract class DenseBlockFactory +{ + public static DenseBlock createDenseBlock(int rlen, int clen) { + DenseBlock.Type type = ((long)rlen*clen < Integer.MAX_VALUE) ? + DenseBlock.Type.DRB : DenseBlock.Type.LDRB; + return createDenseBlock(type, rlen, clen); + } + + public static DenseBlock createDenseBlock(DenseBlock.Type type, int rlen, int clen) { + switch( type ) { + case DRB: return new DenseBlockDRB(rlen, clen); + case LDRB: return new DenseBlockLDRB(rlen, clen); + default: + throw new RuntimeException("Unexpected dense block type: "+type.name()); + } + } + + public static boolean isDenseBlockType(DenseBlock sblock, DenseBlock.Type type) { + return (getDenseBlockType(sblock) == type); + } + + public static DenseBlock.Type getDenseBlockType(DenseBlock dblock) { + return (dblock instanceof DenseBlockDRB) ? DenseBlock.Type.DRB : + (dblock instanceof DenseBlockLDRB) ? DenseBlock.Type.LDRB : null; + } +} http://git-wip-us.apache.org/repos/asf/systemml/blob/9526f7d9/src/main/java/org/apache/sysml/runtime/matrix/data/DenseBlockLDRB.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/runtime/matrix/data/DenseBlockLDRB.java b/src/main/java/org/apache/sysml/runtime/matrix/data/DenseBlockLDRB.java new file mode 100644 index 0000000..aeaad9a --- /dev/null +++ b/src/main/java/org/apache/sysml/runtime/matrix/data/DenseBlockLDRB.java @@ -0,0 +1,146 @@ +/* + * 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.runtime.matrix.data; + +import java.util.Arrays; + +public class DenseBlockLDRB extends DenseBlock +{ + private static final long serialVersionUID = -7285459683402612969L; + + private double[][] data; + private int rlen; + private int clen; + private int blen; + + public DenseBlockLDRB(int rlen, int clen) { + this(rlen, clen, blocksize(rlen, clen)); + } + + public DenseBlockLDRB(int rlen, int clen, int blen) { + reset(rlen, clen, blen); + } + + @Override + public void reset() { + reset(rlen, clen, blen); + } + + @Override + public void reset(int rlen, int clen) { + reset(rlen, clen, blen); + } + + private void reset(int rlen, int clen, int blen) { + long llen = (long) rlen * clen; + int numPart = (int)Math.ceil((double)rlen / blen); + if( this.blen == blen && llen < capacity() ) { + for(int i=0; i<numPart; i++) { + int len = Math.min((i+1)*blen,rlen)-i*blen; + Arrays.fill(data[i], 0, len, 0); + } + } + else { + data = new double[numPart][]; + for(int i=0; i<numPart; i++) { + int len = Math.min((i+1)*blen,rlen)-i*blen; + data[i] = new double[len]; + } + } + + this.rlen = rlen; + this.clen = clen; + this.blen = blen; + } + + @Override + public int numRows() { + return rlen; + } + + @Override + public int numBlocks() { + return data.length; + } + + @Override + public long size() { + return (long)rlen * clen; + } + + @Override + public long capacity() { + int len = 0; + for(int i=0; i<numBlocks(); i++) + len += data[i].length; + return len; + } + + @Override + public long countNonZeros() { + long nnz = 0; + for(int i=0; i<numBlocks(); i++ ) { + double[] a = values(i); + for(int j=0; j<a.length; j++) + nnz += (a[j]!=0) ? 1 : 0; + } + return nnz; + } + + @Override + public double[][] values() { + return data; + } + + @Override + public double[] values(int bix) { + return data[bix]; + } + + @Override + public int index(int r) { + return r / blen; + } + + @Override + public int pos(int r) { + return (r % blen) * clen; + } + + @Override + public int pos(int r, int c) { + return (r % blen) * clen + c; + } + + @Override + public void set(int r, int c, double v) { + data[index(r)][pos(r, c)] = v; + } + + @Override + public double get(int r, int c) { + return data[index(r)][pos(r, c)]; + } + + private static int blocksize(int rlen, int clen) { + return Math.min(rlen, Integer.MAX_VALUE / clen); + } +}
