http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/CacheMatrix.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/CacheMatrix.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/CacheMatrix.java deleted file mode 100644 index 1f832bc..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/CacheMatrix.java +++ /dev/null @@ -1,158 +0,0 @@ -/* - * 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.ignite.ml.math.impls.matrix; - -import org.apache.ignite.IgniteCache; -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.distributed.CacheUtils; -import org.apache.ignite.ml.math.distributed.MatrixKeyMapper; -import org.apache.ignite.ml.math.distributed.ValueMapper; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; -import org.apache.ignite.ml.math.functions.IgniteDoubleFunction; -import org.apache.ignite.ml.math.functions.IgniteFunction; -import org.apache.ignite.ml.math.impls.storage.matrix.CacheMatrixStorage; - -/** - * Matrix based on existing cache and key and value mapping functions. - */ -public class CacheMatrix<K, V> extends AbstractMatrix { - /** - * - */ - public CacheMatrix() { - // No-op. - } - - /** - * Creates new matrix over existing cache. - * - * @param rows Amount of rows in matrix. - * @param cols Amount of columns in matrix. - * @param cache Ignite cache. - * @param keyMapper {@link MatrixKeyMapper} to validate cache key. - * @param valMapper {@link ValueMapper} to obtain value for given cache key. - */ - public CacheMatrix( - int rows, - int cols, - IgniteCache<K, V> cache, - MatrixKeyMapper<K> keyMapper, - ValueMapper<V> valMapper) { - assert rows > 0; - assert cols > 0; - assert cache != null; - assert keyMapper != null; - assert valMapper != null; - - setStorage(new CacheMatrixStorage<>(rows, cols, cache, keyMapper, valMapper)); - } - - /** - * - */ - @SuppressWarnings({"unchecked"}) - private CacheMatrixStorage<K, V> storage() { - return (CacheMatrixStorage<K, V>)getStorage(); - } - - /** {@inheritDoc} */ - @Override public Matrix copy() { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - @Override public Matrix like(int rows, int cols) { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - @Override public Vector likeVector(int crd) { - throw new UnsupportedOperationException(); - } - - /** - * Return the same matrix with updates values (broken contract). - * - * @param d Value to divide to. - */ - @Override public Matrix divide(double d) { - return mapOverValues(v -> v / d); - } - - /** - * Return the same matrix with updates values (broken contract). - * - * @param x Value to add. - */ - @Override public Matrix plus(double x) { - return mapOverValues(v -> v + x); - } - - /** - * Return the same matrix with updates values (broken contract). - * - * @param x Value to multiply to. - */ - @Override public Matrix times(double x) { - return mapOverValues(v -> v * x); - } - - /** {@inheritDoc} */ - @Override public Matrix assign(double val) { - return mapOverValues(v -> val); - } - - /** {@inheritDoc} */ - @Override public Matrix map(IgniteDoubleFunction<Double> fun) { - return mapOverValues(fun::apply); - } - - /** {@inheritDoc} */ - @Override public double sum() { - CacheMatrixStorage<K, V> sto = storage(); - - return CacheUtils.sum(sto.cache().getName(), sto.keyMapper(), sto.valueMapper()); - } - - /** {@inheritDoc} */ - @Override public double maxValue() { - CacheMatrixStorage<K, V> sto = storage(); - - return CacheUtils.max(sto.cache().getName(), sto.keyMapper(), sto.valueMapper()); - } - - /** {@inheritDoc} */ - @Override public double minValue() { - CacheMatrixStorage<K, V> sto = storage(); - - return CacheUtils.min(sto.cache().getName(), sto.keyMapper(), sto.valueMapper()); - } - - /** - * @param mapper Mapping function. - * @return Matrix with mapped values. - */ - private Matrix mapOverValues(IgniteFunction<Double, Double> mapper) { - CacheMatrixStorage<K, V> sto = storage(); - - CacheUtils.map(sto.cache().getName(), sto.keyMapper(), sto.valueMapper(), mapper); - - return this; - } -}
http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/DiagonalMatrix.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/DiagonalMatrix.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/DiagonalMatrix.java deleted file mode 100644 index bd9a4a1..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/DiagonalMatrix.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * 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.ignite.ml.math.impls.matrix; - -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.impls.storage.matrix.DiagonalMatrixStorage; -import org.apache.ignite.ml.math.impls.vector.ConstantVector; -import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector; -import org.apache.ignite.ml.math.impls.vector.SingleElementVectorView; - -/** - * Implementation of diagonal view of the {@link Matrix}. - * - * <p>See also: <a href="https://en.wikipedia.org/wiki/Diagonal_matrix">Wikipedia article</a>.</p> - */ -public class DiagonalMatrix extends AbstractMatrix { - /** - * - */ - public DiagonalMatrix() { - // No-op. - } - - /** - * @param diagonal Backing {@link Vector}. - */ - public DiagonalMatrix(Vector diagonal) { - super(new DiagonalMatrixStorage(diagonal)); - } - - /** - * @param mtx Backing {@link Matrix}. - */ - public DiagonalMatrix(Matrix mtx) { - super(new DiagonalMatrixStorage(mtx == null ? null : mtx.viewDiagonal())); - } - - /** - * @param vals Backing array of values at diagonal. - */ - public DiagonalMatrix(double[] vals) { - super(new DiagonalMatrixStorage(new DenseLocalOnHeapVector(vals))); - } - - /** - * - * - */ - private DiagonalMatrixStorage storage() { - return (DiagonalMatrixStorage)getStorage(); - } - - /** - * @param size Size of diagonal. - * @param val Constant value at diagonal. - */ - public DiagonalMatrix(int size, double val) { - super(new DiagonalMatrixStorage(new ConstantVector(size, val))); - } - - /** {@inheritDoc} */ - @Override public Vector viewRow(int row) { - return new SingleElementVectorView(storage().diagonal(), row); - } - - /** {@inheritDoc} */ - @Override public Vector viewColumn(int col) { - return new SingleElementVectorView(storage().diagonal(), col); - } - - /** {@inheritDoc} */ - @Override public Matrix copy() { - return new DiagonalMatrix(storage().diagonal()); - } - - /** {@inheritDoc} */ - @Override public Matrix like(int rows, int cols) { - return storage().diagonal().likeMatrix(rows, cols); - } - - /** {@inheritDoc} */ - @Override public Vector likeVector(int crd) { - return storage().diagonal().like(crd); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/FunctionMatrix.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/FunctionMatrix.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/FunctionMatrix.java deleted file mode 100644 index 020d50a..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/FunctionMatrix.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * 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.ignite.ml.math.impls.matrix; - -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; -import org.apache.ignite.ml.math.functions.IntIntDoubleToVoidFunction; -import org.apache.ignite.ml.math.functions.IntIntToDoubleFunction; -import org.apache.ignite.ml.math.impls.storage.matrix.FunctionMatrixStorage; - -/** - * Implementation of {@link Matrix} that maps row and column index to {@link java.util.function} interfaces. - */ -public class FunctionMatrix extends AbstractMatrix { - /** - * - */ - public FunctionMatrix() { - // No-op. - } - - /** - * Creates read-write or read-only function matrix. - * - * @param rows Amount of rows in the matrix. - * @param cols Amount of columns in the matrix. - * @param getFunc Function that returns value corresponding to given row and column index. - * @param setFunc Set function. If {@code null} - this will be a read-only matrix. - */ - public FunctionMatrix(int rows, int cols, IntIntToDoubleFunction getFunc, IntIntDoubleToVoidFunction setFunc) { - assert rows > 0; - assert cols > 0; - assert getFunc != null; - - setStorage(new FunctionMatrixStorage(rows, cols, getFunc, setFunc)); - } - - /** - * Creates read-only function matrix. - * - * @param rows Amount of rows in the matrix. - * @param cols Amount of columns in the matrix. - * @param getFunc Function that returns value corresponding to given row and column index. - */ - public FunctionMatrix(int rows, int cols, IntIntToDoubleFunction getFunc) { - assert rows > 0; - assert cols > 0; - assert getFunc != null; - - setStorage(new FunctionMatrixStorage(rows, cols, getFunc)); - } - - /** - * - * - */ - private FunctionMatrixStorage storage() { - return (FunctionMatrixStorage)getStorage(); - } - - /** {@inheritDoc} */ - @Override public Matrix copy() { - FunctionMatrixStorage sto = storage(); - - return new FunctionMatrix(sto.rowSize(), sto.columnSize(), sto.getFunction(), sto.setFunction()); - } - - /** {@inheritDoc} */ - @Override public Matrix like(int rows, int cols) { - FunctionMatrixStorage sto = storage(); - - return new FunctionMatrix(rows, cols, sto.getFunction(), sto.setFunction()); - } - - /** {@inheritDoc} */ - @Override public Vector likeVector(int crd) { - throw new UnsupportedOperationException(); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/MatrixBlockEntry.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/MatrixBlockEntry.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/MatrixBlockEntry.java deleted file mode 100644 index a2d13a1..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/MatrixBlockEntry.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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.ignite.ml.math.impls.matrix; - -import org.apache.ignite.ml.math.Matrix; - -/** - * Block for {@link SparseBlockDistributedMatrix}. - */ -public final class MatrixBlockEntry extends SparseLocalOnHeapMatrix { - /** Max block size. */ - public static final int MAX_BLOCK_SIZE = 32; - - /** */ - public MatrixBlockEntry() { - // No-op. - } - - /** */ - public MatrixBlockEntry(int row, int col) { - super(row, col); - - assert col <= MAX_BLOCK_SIZE; - assert row <= MAX_BLOCK_SIZE; - } - - /** */ - public MatrixBlockEntry(Matrix mtx) { - assert mtx.columnSize() <= MAX_BLOCK_SIZE; - assert mtx.rowSize() <= MAX_BLOCK_SIZE; - - setStorage(mtx.getStorage()); - } - -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/PivotedMatrixView.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/PivotedMatrixView.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/PivotedMatrixView.java deleted file mode 100644 index 361bee5..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/PivotedMatrixView.java +++ /dev/null @@ -1,241 +0,0 @@ -/* - * 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.ignite.ml.math.impls.matrix; - -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.MatrixStorage; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.exceptions.IndexException; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; -import org.apache.ignite.ml.math.impls.storage.matrix.PivotedMatrixStorage; -import org.apache.ignite.ml.math.impls.vector.PivotedVectorView; - -/** - * Pivoted (index mapped) view over another matrix implementation. - */ -public class PivotedMatrixView extends AbstractMatrix { - /** Pivoted matrix. */ - private Matrix mtx; - - /** - * - */ - public PivotedMatrixView() { - // No-op. - } - - /** - * @param mtx Parent matrix. - * @param rowPivot Pivot array for rows. - * @param colPivot Pivot array for columns. - */ - public PivotedMatrixView(Matrix mtx, int[] rowPivot, int[] colPivot) { - super(new PivotedMatrixStorage(mtx == null ? null : mtx.getStorage(), rowPivot, colPivot)); - - this.mtx = mtx; - } - - /** - * @param mtx Parent matrix. - */ - public PivotedMatrixView(Matrix mtx) { - super(new PivotedMatrixStorage(mtx == null ? null : mtx.getStorage())); - - this.mtx = mtx; - } - - /** - * @param mtx Parent matrix. - * @param pivot Pivot array for rows and columns. - */ - public PivotedMatrixView(Matrix mtx, int[] pivot) { - super(new PivotedMatrixStorage(mtx == null ? null : mtx.getStorage(), pivot)); - - this.mtx = mtx; - } - - /** - * Swaps indexes {@code i} and {@code j} for both both row and column. - * - * @param i First index to swap. - * @param j Second index to swap. - */ - public Matrix swap(int i, int j) { - swapRows(i, j); - swapColumns(i, j); - - return this; - } - - /** {@inheritDoc} */ - @Override public Matrix swapRows(int i, int j) { - if (i < 0 || i >= storage().rowPivot().length) - throw new IndexException(i); - if (j < 0 || j >= storage().rowPivot().length) - throw new IndexException(j); - - storage().swapRows(i, j); - - return this; - } - - /** {@inheritDoc} */ - @Override public Matrix swapColumns(int i, int j) { - if (i < 0 || i >= storage().columnPivot().length) - throw new IndexException(i); - if (j < 0 || j >= storage().columnPivot().length) - throw new IndexException(j); - - storage().swapColumns(i, j); - - return this; - } - - /** {@inheritDoc} */ - @Override public Vector viewRow(int row) { - return new PivotedVectorView( - mtx.viewRow(storage().rowPivot()[row]), - storage().columnPivot(), - storage().columnUnpivot() - ); - } - - /** {@inheritDoc} */ - @Override public Vector viewColumn(int col) { - return new PivotedVectorView( - mtx.viewColumn(storage().columnPivot()[col]), - storage().rowPivot(), - storage().rowUnpivot() - ); - } - - /** - * @return Parent matrix. - */ - public Matrix getBaseMatrix() { - return mtx; - } - - /** - * @return Pivot array for rows. - */ - public int[] rowPivot() { - return storage().rowPivot(); - } - - /** - * @return Pivot array for columns. - */ - public int[] columnPivot() { - return storage().columnPivot(); - } - - /** - * @param i Index for row pivot. - * @return Row pivot for given index. - */ - public int rowPivot(int i) { - return storage().rowPivot()[i]; - } - - /** - * @param i Index for column pivot. - * @return Column pivot for given index. - */ - public int columnPivot(int i) { - return storage().columnPivot()[i]; - } - - /** - * @param i Index for row unpivot. - * @return Row unpivot for given index. - */ - public int rowUnpivot(int i) { - return storage().rowUnpivot()[i]; - } - - /** - * @param i Index for column unpivot. - * @return Column unpivot for given index. - */ - public int columnUnpivot(int i) { - return storage().columnUnpivot()[i]; - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - super.writeExternal(out); - - out.writeObject(mtx); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - super.readExternal(in); - - mtx = (Matrix)in.readObject(); - } - - /** */ - private PivotedMatrixStorage storage() { - return (PivotedMatrixStorage)getStorage(); - } - - /** {@inheritDoc} */ - @Override public Matrix copy() { - return new PivotedMatrixView(mtx, storage().rowPivot(), storage().columnPivot()); - } - - /** {@inheritDoc} */ - @Override public Matrix like(int rows, int cols) { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - @Override public Vector likeVector(int crd) { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = 1; - - res = res * 37 + mtx.hashCode(); - res = res * 37 + getStorage().hashCode(); - - return res; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object o) { - if (this == o) - return true; - - if (o == null || getClass() != o.getClass()) - return false; - - PivotedMatrixView that = (PivotedMatrixView)o; - - MatrixStorage sto = storage(); - - return mtx.equals(that.mtx) && sto.equals(that.storage()); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/RandomMatrix.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/RandomMatrix.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/RandomMatrix.java deleted file mode 100644 index ece4ca9..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/RandomMatrix.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * 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.ignite.ml.math.impls.matrix; - -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.MatrixStorage; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.impls.storage.matrix.RandomMatrixStorage; -import org.apache.ignite.ml.math.impls.vector.RandomVector; - -/** - * Implementation of {@link Matrix} with random values in the elements. - */ -public class RandomMatrix extends AbstractMatrix { - /** Whether fast hash is used, see {@link RandomMatrixStorage}. */ - private boolean fastHash; - - /** - * @param rows Amount of rows in the matrix. - * @param cols Amount of columns in the matrix. - * @param fastHash Whether fast hash is used. - */ - private MatrixStorage mkStorage(int rows, int cols, boolean fastHash) { - this.fastHash = fastHash; - - return new RandomMatrixStorage(rows, cols, fastHash); - } - - /** - * @param rows Amount of rows in the matrix. - * @param cols Amount of columns in the matrix. - * @param fastHash Whether fast hash is used. - */ - public RandomMatrix(int rows, int cols, boolean fastHash) { - setStorage(mkStorage(rows, cols, fastHash)); - } - - /** - * @param rows Amount of rows in the matrix. - * @param cols Amount of columns in the matrix. - */ - public RandomMatrix(int rows, int cols) { - this(rows, cols, true); - } - - /** */ - public RandomMatrix() { - // No-op. - } - - /** {@inheritDoc} */ - @Override public Matrix copy() { - return new RandomMatrix(rowSize(), columnSize(), fastHash); - } - - /** {@inheritDoc} */ - @Override public Matrix like(int rows, int cols) { - return new RandomMatrix(rows, cols); - } - - /** {@inheritDoc} */ - @Override public Vector likeVector(int crd) { - return new RandomVector(crd); - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - super.writeExternal(out); - - out.writeBoolean(fastHash); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - super.readExternal(in); - - fastHash = in.readBoolean(); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/SparseBlockDistributedMatrix.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/SparseBlockDistributedMatrix.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/SparseBlockDistributedMatrix.java deleted file mode 100644 index d387d21..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/SparseBlockDistributedMatrix.java +++ /dev/null @@ -1,314 +0,0 @@ -/* - * 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.ignite.ml.math.impls.matrix; - -import java.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.UUID; -import org.apache.ignite.Ignite; -import org.apache.ignite.IgniteCache; -import org.apache.ignite.Ignition; -import org.apache.ignite.cache.affinity.Affinity; -import org.apache.ignite.cluster.ClusterNode; -import org.apache.ignite.internal.util.lang.IgnitePair; -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.StorageConstants; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.distributed.CacheUtils; -import org.apache.ignite.ml.math.distributed.keys.impl.MatrixBlockKey; -import org.apache.ignite.ml.math.distributed.keys.impl.VectorBlockKey; -import org.apache.ignite.ml.math.exceptions.CardinalityException; -import org.apache.ignite.ml.math.functions.IgniteDoubleFunction; -import org.apache.ignite.ml.math.impls.storage.matrix.BlockMatrixStorage; -import org.apache.ignite.ml.math.impls.storage.matrix.BlockVectorStorage; -import org.apache.ignite.ml.math.impls.vector.SparseBlockDistributedVector; -import org.apache.ignite.ml.math.impls.vector.VectorBlockEntry; - -/** - * Sparse block distributed matrix. This matrix represented by blocks 32x32 {@link MatrixBlockEntry}. - * - * Using separate cache with keys {@link MatrixBlockKey} and values {@link MatrixBlockEntry}. - */ -public class SparseBlockDistributedMatrix extends AbstractMatrix implements StorageConstants { - /** - * - */ - public SparseBlockDistributedMatrix() { - // No-op. - } - - /** - * @param rows Amount of rows in the matrix. - * @param cols Amount of columns in the matrix. - */ - public SparseBlockDistributedMatrix(int rows, int cols) { - assert rows > 0; - assert cols > 0; - - setStorage(new BlockMatrixStorage(rows, cols)); - } - - /** - * @param data Data to fill the matrix - */ - public SparseBlockDistributedMatrix(double[][] data) { - assert data.length > 0; - - setStorage(new BlockMatrixStorage(data.length, getMaxAmountOfColumns(data))); - - for (int i = 0; i < data.length; i++) - for (int j = 0; j < data[i].length; j++) - storage().set(i, j, data[i][j]); - } - - /** - * Return the same matrix with updates values (broken contract). - * - * @param d Value to divide to. - */ - @Override public Matrix divide(double d) { - return mapOverValues(v -> v / d); - } - - /** - * Return the same matrix with updates values (broken contract). - * - * @param x Value to add. - */ - @Override public Matrix plus(double x) { - return mapOverValues(v -> v + x); - } - - /** - * Return the same matrix with updates values (broken contract). - * - * @param x Value to multiply. - */ - @Override public Matrix times(double x) { - return mapOverValues(v -> v * x); - } - - /** - * {@inheritDoc} - */ - @SuppressWarnings({"unchecked"}) - @Override public Matrix times(final Matrix mtx) { - if (mtx == null) - throw new IllegalArgumentException("The matrix should be not null."); - - if (columnSize() != mtx.rowSize()) - throw new CardinalityException(columnSize(), mtx.rowSize()); - - SparseBlockDistributedMatrix matrixA = this; - SparseBlockDistributedMatrix matrixB = (SparseBlockDistributedMatrix)mtx; - - String cacheName = this.storage().cacheName(); - SparseBlockDistributedMatrix matrixC = new SparseBlockDistributedMatrix(matrixA.rowSize(), matrixB.columnSize()); - - CacheUtils.bcast(cacheName, () -> { - Ignite ignite = Ignition.localIgnite(); - Affinity<MatrixBlockKey> affinity = ignite.affinity(cacheName); - - IgniteCache<MatrixBlockKey, MatrixBlockEntry> cache = ignite.getOrCreateCache(cacheName); - ClusterNode locNode = ignite.cluster().localNode(); - - BlockMatrixStorage storageC = matrixC.storage(); - - Map<ClusterNode, Collection<MatrixBlockKey>> keysCToNodes = affinity.mapKeysToNodes(storageC.getAllKeys()); - Collection<MatrixBlockKey> locKeys = keysCToNodes.get(locNode); - - if (locKeys == null) - return; - - // compute Cij locally on each node - // TODO: IGNITE:5114, exec in parallel - locKeys.forEach(key -> { - long newBlockIdRow = key.blockRowId(); - long newBlockIdCol = key.blockColId(); - - IgnitePair<Long> newBlockId = new IgnitePair<>(newBlockIdRow, newBlockIdCol); - - MatrixBlockEntry blockC = null; - - List<MatrixBlockEntry> aRow = matrixA.storage().getRowForBlock(newBlockId); - List<MatrixBlockEntry> bCol = matrixB.storage().getColForBlock(newBlockId); - - for (int i = 0; i < aRow.size(); i++) { - MatrixBlockEntry blockA = aRow.get(i); - MatrixBlockEntry blockB = bCol.get(i); - - MatrixBlockEntry tmpBlock = new MatrixBlockEntry(blockA.times(blockB)); - - blockC = blockC == null ? tmpBlock : new MatrixBlockEntry(blockC.plus(tmpBlock)); - } - - cache.put(storageC.getCacheKey(newBlockIdRow, newBlockIdCol), blockC); - }); - }); - - return matrixC; - } - - /** - * {@inheritDoc} - */ - @SuppressWarnings({"unchecked"}) - @Override public Vector times(final Vector vec) { - if (vec == null) - throw new IllegalArgumentException("The vector should be not null."); - - if (columnSize() != vec.size()) - throw new CardinalityException(columnSize(), vec.size()); - - SparseBlockDistributedMatrix matrixA = this; - SparseBlockDistributedVector vectorB = (SparseBlockDistributedVector)vec; - - String cacheName = this.storage().cacheName(); - SparseBlockDistributedVector vectorC = new SparseBlockDistributedVector(matrixA.rowSize()); - - CacheUtils.bcast(cacheName, () -> { - Ignite ignite = Ignition.localIgnite(); - Affinity<VectorBlockKey> affinity = ignite.affinity(cacheName); - - IgniteCache<VectorBlockKey, VectorBlockEntry> cache = ignite.getOrCreateCache(cacheName); - ClusterNode locNode = ignite.cluster().localNode(); - - BlockVectorStorage storageC = vectorC.storage(); - - Map<ClusterNode, Collection<VectorBlockKey>> keysCToNodes = affinity.mapKeysToNodes(storageC.getAllKeys()); - Collection<VectorBlockKey> locKeys = keysCToNodes.get(locNode); - - if (locKeys == null) - return; - - // compute Cij locally on each node - // TODO: IGNITE:5114, exec in parallel - locKeys.forEach(key -> { - long newBlockId = key.blockId(); - - IgnitePair<Long> newBlockIdForMtx = new IgnitePair<>(newBlockId, 0L); - - VectorBlockEntry blockC = null; - - List<MatrixBlockEntry> aRow = matrixA.storage().getRowForBlock(newBlockIdForMtx); - List<VectorBlockEntry> bCol = vectorB.storage().getColForBlock(newBlockId); - - for (int i = 0; i < aRow.size(); i++) { - MatrixBlockEntry blockA = aRow.get(i); - VectorBlockEntry blockB = bCol.get(i); - - VectorBlockEntry tmpBlock = new VectorBlockEntry(blockA.times(blockB)); - - blockC = blockC == null ? tmpBlock : new VectorBlockEntry(blockC.plus(tmpBlock)); - } - - cache.put(storageC.getCacheKey(newBlockId), blockC); - }); - }); - return vectorC; - } - - /** {@inheritDoc} */ - @Override public Vector getCol(int col) { - checkColumnIndex(col); - - Vector res = new SparseBlockDistributedVector(rowSize()); - - for (int i = 0; i < rowSize(); i++) - res.setX(i, getX(i, col)); - return res; - } - - /** {@inheritDoc} */ - @Override public Vector getRow(int row) { - checkRowIndex(row); - - Vector res = new SparseBlockDistributedVector(columnSize()); - - for (int i = 0; i < columnSize(); i++) - res.setX(i, getX(row, i)); - return res; - } - - /** {@inheritDoc} */ - @Override public Matrix assign(double val) { - return mapOverValues(v -> val); - } - - /** {@inheritDoc} */ - @Override public Matrix map(IgniteDoubleFunction<Double> fun) { - return mapOverValues(fun); - } - - /** {@inheritDoc} */ - @Override public double sum() { - return CacheUtils.sparseSum(getUUID(), this.storage().cacheName()); - } - - /** {@inheritDoc} */ - @Override public double maxValue() { - return CacheUtils.sparseMax(getUUID(), this.storage().cacheName()); - } - - /** {@inheritDoc} */ - @Override public double minValue() { - return CacheUtils.sparseMin(getUUID(), this.storage().cacheName()); - } - - /** {@inheritDoc} */ - @Override public Matrix copy() { - Matrix cp = like(rowSize(), columnSize()); - - cp.assign(this); - - return cp; - } - - /** {@inheritDoc} */ - @Override public Matrix like(int rows, int cols) { - return new SparseBlockDistributedMatrix(rows, cols); - } - - /** {@inheritDoc} */ - @Override public Vector likeVector(int crd) { - return new SparseBlockDistributedVector(crd); - } - - /** */ - private UUID getUUID() { - return ((BlockMatrixStorage)getStorage()).getUUID(); - } - - /** - * @param mapper Mapping function. - * @return Matrix with mapped values. - */ - private Matrix mapOverValues(IgniteDoubleFunction<Double> mapper) { - CacheUtils.sparseMap(getUUID(), mapper, this.storage().cacheName()); - - return this; - } - - /** - * - */ - private BlockMatrixStorage storage() { - return (BlockMatrixStorage)getStorage(); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/SparseDistributedMatrix.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/SparseDistributedMatrix.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/SparseDistributedMatrix.java deleted file mode 100644 index 026138b..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/SparseDistributedMatrix.java +++ /dev/null @@ -1,290 +0,0 @@ -/* - * 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.ignite.ml.math.impls.matrix; - -import java.util.Collection; -import java.util.Map; -import java.util.UUID; -import org.apache.ignite.Ignite; -import org.apache.ignite.Ignition; -import org.apache.ignite.cache.affinity.Affinity; -import org.apache.ignite.cluster.ClusterNode; -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.StorageConstants; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.distributed.CacheUtils; -import org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey; -import org.apache.ignite.ml.math.exceptions.CardinalityException; -import org.apache.ignite.ml.math.functions.IgniteDoubleFunction; -import org.apache.ignite.ml.math.impls.storage.matrix.SparseDistributedMatrixStorage; -import org.apache.ignite.ml.math.impls.storage.vector.SparseDistributedVectorStorage; -import org.apache.ignite.ml.math.impls.vector.SparseDistributedVector; - -/** - * Sparse distributed matrix implementation based on data grid. - * <p> - * Unlike {@link CacheMatrix} that is based on existing cache, this implementation creates distributed - * cache internally and doesn't rely on pre-existing cache.</p> - * <p> - * You also need to call {@link #destroy()} to remove the underlying cache when you no longer need this - * matrix.</p> - * <p> - * This class is not intended for fast calculations (for example, matrix multiplication). If better performance - * is needed, {@link SparseBlockDistributedMatrix} should be used instead.</p> - * <p> - * <b>Currently fold supports only commutative operations.<b/></p> - */ -public class SparseDistributedMatrix extends AbstractMatrix implements StorageConstants { - /** - * - */ - public SparseDistributedMatrix() { - // No-op. - } - - /** - * @param rows Amount of rows in the matrix. - * @param cols Amount of columns in the matrix. - * @param stoMode Matrix storage mode. - * @param acsMode Matrix elements access mode. - */ - public SparseDistributedMatrix(int rows, int cols, int stoMode, int acsMode) { - assert rows > 0; - assert cols > 0; - assertAccessMode(acsMode); - assertStorageMode(stoMode); - - setStorage(new SparseDistributedMatrixStorage(rows, cols, stoMode, acsMode)); - - } - - /** - * @param data Data to fill the matrix - */ - public SparseDistributedMatrix(double[][] data) { - assert data.length > 0; - setStorage(new SparseDistributedMatrixStorage(data.length, getMaxAmountOfColumns(data), StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE)); - - for (int i = 0; i < data.length; i++) - for (int j = 0; j < data[i].length; j++) - storage().set(i, j, data[i][j]); - } - - /** - * @param rows Amount of rows in the matrix. - * @param cols Amount of columns in the matrix. - */ - public SparseDistributedMatrix(int rows, int cols) { - this(rows, cols, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE); - } - - /** */ - private SparseDistributedMatrixStorage storage() { - return (SparseDistributedMatrixStorage)getStorage(); - } - - /** - * Return the same matrix with updates values (broken contract). - * - * @param d Value to divide to. - */ - @Override public Matrix divide(double d) { - return mapOverValues(v -> v / d); - } - - /** - * Return the same matrix with updates values (broken contract). - * - * @param x Value to add. - */ - @Override public Matrix plus(double x) { - return mapOverValues(v -> v + x); - } - - /** - * Return the same matrix with updates values (broken contract). - * - * @param x Value to multiply. - */ - @Override public Matrix times(double x) { - return mapOverValues(v -> v * x); - } - - /** {@inheritDoc} */ - @Override public Matrix times(Matrix mtx) { - if (mtx == null) - throw new IllegalArgumentException("The matrix should be not null."); - - if (columnSize() != mtx.rowSize()) - throw new CardinalityException(columnSize(), mtx.rowSize()); - - SparseDistributedMatrix matrixA = this; - SparseDistributedMatrix matrixB = (SparseDistributedMatrix)mtx; - - String cacheName = storage().cacheName(); - SparseDistributedMatrix matrixC = new SparseDistributedMatrix(matrixA.rowSize(), matrixB.columnSize() - , getStorage().storageMode(), getStorage().isRandomAccess() ? RANDOM_ACCESS_MODE : SEQUENTIAL_ACCESS_MODE); - - CacheUtils.bcast(cacheName, () -> { - Ignite ignite = Ignition.localIgnite(); - Affinity<RowColMatrixKey> affinity = ignite.affinity(cacheName); - - ClusterNode locNode = ignite.cluster().localNode(); - - SparseDistributedMatrixStorage storageC = matrixC.storage(); - - Map<ClusterNode, Collection<RowColMatrixKey>> keysCToNodes = affinity.mapKeysToNodes(storageC.getAllKeys()); - Collection<RowColMatrixKey> locKeys = keysCToNodes.get(locNode); - - boolean isRowMode = storageC.storageMode() == ROW_STORAGE_MODE; - - if (locKeys == null) - return; - - // compute Cij locally on each node - // TODO: IGNITE:5114, exec in parallel - locKeys.forEach(key -> { - int idx = key.index(); - - if (isRowMode) { - Vector Aik = matrixA.getRow(idx); - - for (int i = 0; i < matrixB.columnSize(); i++) { - Vector Bkj = matrixB.getCol(i); - matrixC.set(idx, i, Aik.times(Bkj).sum()); - } - } - else { - Vector Bkj = matrixB.getCol(idx); - - for (int i = 0; i < matrixA.rowSize(); i++) { - Vector Aik = matrixA.getRow(i); - matrixC.set(idx, i, Aik.times(Bkj).sum()); - } - } - }); - }); - - return matrixC; - } - - /** {@inheritDoc} */ - @Override public Vector times(Vector vec) { - if (vec == null) - throw new IllegalArgumentException("The vector should be not null."); - - if (columnSize() != vec.size()) - throw new CardinalityException(columnSize(), vec.size()); - - SparseDistributedMatrix matrixA = this; - SparseDistributedVector vectorB = (SparseDistributedVector)vec; - - String cacheName = storage().cacheName(); - int rows = this.rowSize(); - - SparseDistributedVector vectorC = (SparseDistributedVector)likeVector(rows); - - CacheUtils.bcast(cacheName, () -> { - Ignite ignite = Ignition.localIgnite(); - Affinity<RowColMatrixKey> affinity = ignite.affinity(cacheName); - - ClusterNode locNode = ignite.cluster().localNode(); - - SparseDistributedVectorStorage storageC = vectorC.storage(); - - Map<ClusterNode, Collection<RowColMatrixKey>> keysCToNodes = affinity.mapKeysToNodes(storageC.getAllKeys()); - Collection<RowColMatrixKey> locKeys = keysCToNodes.get(locNode); - - if (locKeys == null) - return; - - // compute Cij locally on each node - // TODO: IGNITE:5114, exec in parallel - locKeys.forEach(key -> { - int idx = key.index(); - Vector Aik = matrixA.getRow(idx); - vectorC.set(idx, Aik.times(vectorB).sum()); - }); - }); - - return vectorC; - } - - /** {@inheritDoc} */ - @Override public Matrix assign(double val) { - return mapOverValues(v -> val); - } - - /** {@inheritDoc} */ - @Override public Matrix map(IgniteDoubleFunction<Double> fun) { - return mapOverValues(fun); - } - - /** - * @param mapper Mapping function. - * @return Matrix with mapped values. - */ - private Matrix mapOverValues(IgniteDoubleFunction<Double> mapper) { - CacheUtils.sparseMap(getUUID(), mapper, storage().cacheName()); - - return this; - } - - /** {@inheritDoc} */ - @Override public double sum() { - return CacheUtils.sparseSum(getUUID(), storage().cacheName()); - } - - /** {@inheritDoc} */ - @Override public double maxValue() { - return CacheUtils.sparseMax(getUUID(), storage().cacheName()); - } - - /** {@inheritDoc} */ - @Override public double minValue() { - return CacheUtils.sparseMin(getUUID(), storage().cacheName()); - } - - /** {@inheritDoc} */ - @Override public Matrix copy() { - Matrix cp = like(rowSize(), columnSize()); - - cp.assign(this); - - return cp; - } - - /** {@inheritDoc} */ - @Override public Matrix like(int rows, int cols) { - if (storage() == null) - return new SparseDistributedMatrix(rows, cols); - else - return new SparseDistributedMatrix(rows, cols, storage().storageMode(), storage().accessMode()); - - } - - /** {@inheritDoc} */ - @Override public Vector likeVector(int crd) { - return new SparseDistributedVector(crd, StorageConstants.RANDOM_ACCESS_MODE); - } - - /** */ - public UUID getUUID() { - return ((SparseDistributedMatrixStorage)getStorage()).getUUID(); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/TransposedMatrixView.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/TransposedMatrixView.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/TransposedMatrixView.java deleted file mode 100644 index 309570b..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/TransposedMatrixView.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * 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.ignite.ml.math.impls.matrix; - -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.MatrixStorage; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; -import org.apache.ignite.ml.math.impls.storage.matrix.MatrixDelegateStorage; - -/** - * Implements transposed view of the parent {@link Matrix}. - */ -public class TransposedMatrixView extends AbstractMatrix { - /** */ - public TransposedMatrixView() { - //No-op. - } - - /** - * @param mtx Parent matrix. - */ - public TransposedMatrixView(Matrix mtx) { - this(mtx == null ? null : mtx.getStorage()); - } - - /** */ - private TransposedMatrixView(MatrixStorage sto) { - super(new MatrixDelegateStorage(sto, 0, 0, - sto == null ? 0 : sto.rowSize(), sto == null ? 0 : sto.columnSize())); - } - - /** {@inheritDoc} */ - @Override protected void storageSet(int row, int col, double v) { - super.storageSet(col, row, v); - } - - /** {@inheritDoc} */ - @Override protected double storageGet(int row, int col) { - return super.storageGet(col, row); - } - - /** {@inheritDoc} */ - @Override public int rowSize() { - return getStorage().columnSize(); - } - - /** {@inheritDoc} */ - @Override public int columnSize() { - return getStorage().rowSize(); - } - - /** {@inheritDoc} */ - @Override public Matrix copy() { - MatrixDelegateStorage sto = (MatrixDelegateStorage)getStorage(); - - return new TransposedMatrixView(sto.delegate()); - } - - /** {@inheritDoc} */ - @Override public Matrix like(int rows, int cols) { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - @Override public Vector likeVector(int crd) { - throw new UnsupportedOperationException(); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/BlockMatrixStorage.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/BlockMatrixStorage.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/BlockMatrixStorage.java deleted file mode 100644 index e73ef22..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/BlockMatrixStorage.java +++ /dev/null @@ -1,434 +0,0 @@ -/* - * 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.ignite.ml.math.impls.storage.matrix; - -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; -import java.util.UUID; -import org.apache.ignite.IgniteCache; -import org.apache.ignite.Ignition; -import org.apache.ignite.cache.CacheAtomicityMode; -import org.apache.ignite.cache.CacheMode; -import org.apache.ignite.cache.CachePeekMode; -import org.apache.ignite.cache.CacheWriteSynchronizationMode; -import org.apache.ignite.configuration.CacheConfiguration; -import org.apache.ignite.internal.util.lang.IgnitePair; -import org.apache.ignite.ml.math.MatrixStorage; -import org.apache.ignite.ml.math.StorageConstants; -import org.apache.ignite.ml.math.distributed.CacheUtils; -import org.apache.ignite.ml.math.distributed.DistributedStorage; -import org.apache.ignite.ml.math.distributed.keys.impl.MatrixBlockKey; -import org.apache.ignite.ml.math.impls.matrix.MatrixBlockEntry; -import org.apache.ignite.ml.math.impls.matrix.SparseBlockDistributedMatrix; - -import static org.apache.ignite.ml.math.impls.matrix.MatrixBlockEntry.MAX_BLOCK_SIZE; - -/** - * Storage for {@link SparseBlockDistributedMatrix}. - */ -public class BlockMatrixStorage extends CacheUtils implements MatrixStorage, StorageConstants, DistributedStorage<MatrixBlockKey> { - /** Cache name used for all instances of {@link BlockMatrixStorage}. */ - private static final String CACHE_NAME = "ML_BLOCK_SPARSE_MATRICES_CONTAINER"; - - /** */ - private int blocksInCol; - - /** */ - private int blocksInRow; - - /** Amount of rows in the matrix. */ - private int rows; - - /** Amount of columns in the matrix. */ - private int cols; - - /** Matrix uuid. */ - private UUID uuid; - - /** Block size about 8 KB of data. */ - private int maxBlockEdge = MAX_BLOCK_SIZE; - - /** Actual distributed storage. */ - private IgniteCache< - MatrixBlockKey /* Matrix block number with uuid. */, - MatrixBlockEntry /* Block of matrix, local sparse matrix. */ - > cache = null; - - /** */ - public BlockMatrixStorage() { - // No-op. - } - - /** - * @param rows Amount of rows in the matrix. - * @param cols Amount of columns in the matrix. - */ - public BlockMatrixStorage(int rows, int cols) { - assert rows > 0; - assert cols > 0; - - this.rows = rows; - this.cols = cols; - - this.blocksInRow = rows % maxBlockEdge == 0 ? rows / maxBlockEdge : rows / maxBlockEdge + 1; - this.blocksInCol = cols % maxBlockEdge == 0 ? cols / maxBlockEdge : cols / maxBlockEdge + 1; - - cache = newCache(); - - uuid = UUID.randomUUID(); - } - - /** */ - public IgniteCache<MatrixBlockKey, MatrixBlockEntry> cache() { - return cache; - } - - /** {@inheritDoc} */ - @Override public double get(int x, int y) { - return matrixGet(x, y); - } - - /** {@inheritDoc} */ - @Override public void set(int x, int y, double v) { - matrixSet(x, y, v); - } - - /** {@inheritDoc} */ - @Override public int columnSize() { - return cols; - } - - /** {@inheritDoc} */ - @Override public int rowSize() { - return rows; - } - - /** {@inheritDoc} */ - @Override public int storageMode() { - return UNKNOWN_STORAGE_MODE; - } - - /** {@inheritDoc} */ - @Override public int accessMode() { - return RANDOM_ACCESS_MODE; - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeInt(rows); - out.writeInt(cols); - out.writeInt(blocksInRow); - out.writeInt(blocksInCol); - out.writeObject(uuid); - out.writeUTF(cache.getName()); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - rows = in.readInt(); - cols = in.readInt(); - blocksInRow = in.readInt(); - blocksInCol = in.readInt(); - uuid = (UUID)in.readObject(); - cache = ignite().getOrCreateCache(in.readUTF()); - } - - /** {@inheritDoc} */ - @Override public boolean isSequentialAccess() { - return false; - } - - /** {@inheritDoc} */ - @Override public boolean isDense() { - return false; - } - - /** {@inheritDoc} */ - @Override public boolean isRandomAccess() { - return true; - } - - /** {@inheritDoc} */ - @Override public boolean isDistributed() { - return true; - } - - /** {@inheritDoc} */ - @Override public boolean isArrayBased() { - return false; - } - - /** Delete all data from cache. */ - @Override public void destroy() { - cache.clearAll(getAllKeys()); - } - - /** - * Get storage UUID. - * - * @return storage UUID. - */ - public UUID getUUID() { - return uuid; - } - - /** - * Build the cache key for the given blocks id. - * - * NB: NOT cell indices. - */ - public MatrixBlockKey getCacheKey(long blockIdRow, long blockIdCol) { - return new MatrixBlockKey(blockIdRow, blockIdCol, uuid, getAffinityKey(blockIdRow, blockIdCol)); - } - - /** - * Build the cache key for the given blocks id. - * - * NB: NOT cell indices. - */ - private MatrixBlockKey getCacheKey(IgnitePair<Long> blockId) { - return new MatrixBlockKey(blockId.get1(), blockId.get2(), uuid, getAffinityKey(blockId.get1(), blockId.get2())); - } - - /** {@inheritDoc} */ - @Override public Set<MatrixBlockKey> getAllKeys() { - int maxRowIdx = rows - 1; - int maxColIdx = cols - 1; - IgnitePair<Long> maxBlockId = getBlockId(maxRowIdx, maxColIdx); - - Set<MatrixBlockKey> keyset = new HashSet<>(); - - for (int i = 0; i <= maxBlockId.get1(); i++) - for (int j = 0; j <= maxBlockId.get2(); j++) - keyset.add(getCacheKey(i, j)); - - return keyset; - } - - /** {@inheritDoc} */ - @Override public String cacheName() { - return CACHE_NAME; - } - - /** - * Get rows for current block. - * - * @param blockId block id. - * @return The list of block entries. - */ - public List<MatrixBlockEntry> getRowForBlock(IgnitePair<Long> blockId) { - List<MatrixBlockEntry> res = new LinkedList<>(); - - for (int i = 0; i < blocksInCol; i++) - res.add(getEntryById(new IgnitePair<>(blockId.get1(), (long)i))); - - return res; - } - - /** - * Get cols for current block. - * - * @param blockId block id. - * @return The list of block entries. - */ - public List<MatrixBlockEntry> getColForBlock(IgnitePair<Long> blockId) { - List<MatrixBlockEntry> res = new LinkedList<>(); - - for (int i = 0; i < blocksInRow; i++) - res.add(getEntryById(new IgnitePair<>((long)i, blockId.get2()))); - - return res; - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = blocksInCol; - - res = 31 * res + blocksInRow; - res = 31 * res + rows; - res = 31 * res + cols; - res = 31 * res + uuid.hashCode(); - res = 31 * res + maxBlockEdge; - res = 31 * res + cache.getName().hashCode(); - - return res; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; - - BlockMatrixStorage that = (BlockMatrixStorage)o; - - return blocksInCol == that.blocksInCol && blocksInRow == that.blocksInRow && rows == that.rows - && cols == that.cols && maxBlockEdge == that.maxBlockEdge && uuid.equals(that.uuid) - && cache.getName().equals(that.cache.getName()); - - } - - /** - * Returns cached or new BlockEntry by given blockId. - * - * @param blockId blockId - * @return BlockEntry - */ - private MatrixBlockEntry getEntryById(IgnitePair<Long> blockId) { - MatrixBlockKey key = getCacheKey(blockId.get1(), blockId.get2()); - - MatrixBlockEntry entry = cache.localPeek(key, CachePeekMode.PRIMARY); - entry = entry != null ? entry : cache.get(key); - - if (entry == null) - entry = getEmptyBlockEntry(blockId); - - return entry; - } - - /** - * Builds empty BlockEntry with sizes based on blockId and BlockMatrixStorage fields' values. - * - * @param blockId blockId - * @return Empty BlockEntry - */ - private MatrixBlockEntry getEmptyBlockEntry(IgnitePair<Long> blockId) { - MatrixBlockEntry entry; - int rowMod = rows % maxBlockEdge; - int colMod = cols % maxBlockEdge; - - int rowSize; - - if (rowMod == 0) - rowSize = maxBlockEdge; - else - rowSize = blockId.get1() != (blocksInRow - 1) ? maxBlockEdge : rowMod; - - int colSize; - - if (colMod == 0) - colSize = maxBlockEdge; - else - colSize = blockId.get2() != (blocksInCol - 1) ? maxBlockEdge : colMod; - - entry = new MatrixBlockEntry(rowSize, colSize); - return entry; - } - - /** - * TODO: IGNITE-5646, WIP - * - * Get affinity key for the given id. - */ - private UUID getAffinityKey(long blockIdRow, long blockIdCol) { - return null; - } - - /** - * Distributed matrix set. - * - * @param a Row or column index. - * @param b Row or column index. - * @param v New value to set. - */ - private void matrixSet(int a, int b, double v) { - IgnitePair<Long> blockId = getBlockId(a, b); - // Remote set on the primary node (where given row or column is stored locally). - ignite().compute(getClusterGroupForGivenKey(CACHE_NAME, blockId)).run(() -> { - IgniteCache<MatrixBlockKey, MatrixBlockEntry> cache = Ignition.localIgnite().getOrCreateCache(CACHE_NAME); - - MatrixBlockKey key = getCacheKey(blockId.get1(), blockId.get2()); - - // Local get. - MatrixBlockEntry block = getEntryById(blockId); - - block.set(a % block.rowSize(), b % block.columnSize(), v); - - // Local put. - cache.put(key, block); - }); - } - - /** - * Calculates blockId for given cell's coordinates. - * - * @param x x1 attribute in (x1,x2) coordinates - * @param y x2 attribute in (x1, x2) coordinates - * @return blockId as an IgnitePair - */ - private IgnitePair<Long> getBlockId(int x, int y) { - return new IgnitePair<>((long)x / maxBlockEdge, (long)y / maxBlockEdge); - } - - /** - * Distributed matrix get. - * - * @param a Row or column index. - * @param b Row or column index. - * @return Matrix value at (a, b) index. - */ - private double matrixGet(int a, int b) { - // Remote get from the primary node (where given row or column is stored locally). - return ignite().compute(getClusterGroupForGivenKey(CACHE_NAME, getBlockId(a, b))).call(() -> { - IgniteCache<MatrixBlockKey, MatrixBlockEntry> cache = Ignition.localIgnite().getOrCreateCache(CACHE_NAME); - - MatrixBlockKey key = getCacheKey(getBlockId(a, b)); - - // Local get. - MatrixBlockEntry block = cache.localPeek(key, CachePeekMode.PRIMARY); - - if (block == null) - block = cache.get(key); - - return block == null ? 0.0 : block.get(a % block.rowSize(), b % block.columnSize()); - }); - } - - /** - * Create new ML cache if needed. - */ - private IgniteCache<MatrixBlockKey, MatrixBlockEntry> newCache() { - CacheConfiguration<MatrixBlockKey, MatrixBlockEntry> cfg = new CacheConfiguration<>(); - - // Write to primary. - cfg.setWriteSynchronizationMode(CacheWriteSynchronizationMode.PRIMARY_SYNC); - - // Atomic transactions only. - cfg.setAtomicityMode(CacheAtomicityMode.ATOMIC); - - // No eviction. - cfg.setEvictionPolicy(null); - - // No copying of values. - cfg.setCopyOnRead(false); - - // Cache is partitioned. - cfg.setCacheMode(CacheMode.PARTITIONED); - - // Random cache name. - cfg.setName(CACHE_NAME); - - return Ignition.localIgnite().getOrCreateCache(cfg); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/BlockVectorStorage.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/BlockVectorStorage.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/BlockVectorStorage.java deleted file mode 100644 index 6400b4d..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/BlockVectorStorage.java +++ /dev/null @@ -1,368 +0,0 @@ -/* - * 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.ignite.ml.math.impls.storage.matrix; - -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; -import java.util.UUID; -import org.apache.ignite.IgniteCache; -import org.apache.ignite.Ignition; -import org.apache.ignite.cache.CacheAtomicityMode; -import org.apache.ignite.cache.CacheMode; -import org.apache.ignite.cache.CachePeekMode; -import org.apache.ignite.cache.CacheWriteSynchronizationMode; -import org.apache.ignite.configuration.CacheConfiguration; -import org.apache.ignite.ml.math.StorageConstants; -import org.apache.ignite.ml.math.VectorStorage; -import org.apache.ignite.ml.math.distributed.CacheUtils; -import org.apache.ignite.ml.math.distributed.DistributedStorage; -import org.apache.ignite.ml.math.distributed.keys.impl.VectorBlockKey; -import org.apache.ignite.ml.math.impls.vector.SparseBlockDistributedVector; -import org.apache.ignite.ml.math.impls.vector.VectorBlockEntry; -import org.jetbrains.annotations.NotNull; - -import static org.apache.ignite.ml.math.impls.matrix.MatrixBlockEntry.MAX_BLOCK_SIZE; - -/** - * Storage for {@link SparseBlockDistributedVector}. - */ -public class BlockVectorStorage extends CacheUtils implements VectorStorage, StorageConstants, DistributedStorage<VectorBlockKey> { - /** Cache name used for all instances of {@link BlockVectorStorage}. */ - private static final String CACHE_NAME = "ML_BLOCK_SPARSE_MATRICES_CONTAINER"; - - /** */ - private int blocks; - - /** Amount of columns in the vector. */ - private int size; - - /** Matrix uuid. */ - private UUID uuid; - - /** Block size about 8 KB of data. */ - private int maxBlockEdge = MAX_BLOCK_SIZE; - - /** Actual distributed storage. */ - private IgniteCache< - VectorBlockKey /* Matrix block number with uuid. */, - VectorBlockEntry /* Block of matrix, local sparse matrix. */ - > cache = null; - - /** - * - */ - public BlockVectorStorage() { - // No-op. - } - - /** - * @param size Amount of columns in the vector. - */ - public BlockVectorStorage(int size) { - - assert size > 0; - - this.size = size; - this.blocks = size % maxBlockEdge == 0 ? size / maxBlockEdge : size / maxBlockEdge + 1; - - cache = newCache(); - uuid = UUID.randomUUID(); - } - - /** */ - public IgniteCache<VectorBlockKey, VectorBlockEntry> cache() { - return cache; - } - - /** {@inheritDoc} */ - @Override public double get(int x) { - return matrixGet(x); - } - - /** {@inheritDoc} */ - @Override public void set(int x, double v) { - matrixSet(x, v); - } - - /** {@inheritDoc} */ - @Override public int size() { - return size; - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeInt(size); - out.writeInt(blocks); - out.writeObject(uuid); - out.writeUTF(cache.getName()); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - size = in.readInt(); - blocks = in.readInt(); - uuid = (UUID)in.readObject(); - - cache = ignite().getOrCreateCache(in.readUTF()); - } - - /** {@inheritDoc} */ - @Override public boolean isSequentialAccess() { - return false; - } - - /** {@inheritDoc} */ - @Override public boolean isDense() { - return false; - } - - /** {@inheritDoc} */ - @Override public boolean isRandomAccess() { - return true; - } - - /** {@inheritDoc} */ - @Override public boolean isDistributed() { - return true; - } - - /** {@inheritDoc} */ - @Override public boolean isArrayBased() { - return false; - } - - /** Delete all data from cache. */ - @Override public void destroy() { - cache.clearAll(getAllKeys()); - } - - /** - * Get storage UUID. - * - * @return storage UUID. - */ - public UUID getUUID() { - return uuid; - } - - /** - * Build the cache key for the given blocks id. - * - * NB: NOT cell indices. - */ - public VectorBlockKey getCacheKey(long blockId) { - return new VectorBlockKey(blockId, uuid, getAffinityKey(blockId)); - } - - /** {@inheritDoc} */ - @Override public Set<VectorBlockKey> getAllKeys() { - int maxIdx = size - 1; - long maxBlockId = getBlockId(maxIdx); - - Set<VectorBlockKey> keyset = new HashSet<>(); - - for (int i = 0; i <= maxBlockId; i++) - keyset.add(getCacheKey(i)); - - return keyset; - } - - /** {@inheritDoc} */ - @Override public String cacheName() { - return CACHE_NAME; - } - - /** - * Get column for current block. - * - * @param blockId block id. - * @return The list of block entries. - */ - public List<VectorBlockEntry> getColForBlock(long blockId) { - List<VectorBlockEntry> res = new LinkedList<>(); - - for (int i = 0; i < blocks; i++) - res.add(getEntryById(i)); - - return res; - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = 1; - - res = res * 37 + size; - res = res * 37 + uuid.hashCode(); - res = res * 37 + cache.hashCode(); - - return res; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object obj) { - if (this == obj) - return true; - - if (obj == null || getClass() != obj.getClass()) - return false; - - BlockVectorStorage that = (BlockVectorStorage)obj; - - return size == that.size && uuid.equals(that.uuid) - && (cache != null ? cache.equals(that.cache) : that.cache == null); - } - - /** - * - */ - private VectorBlockEntry getEntryById(long blockId) { - VectorBlockKey key = getCacheKey(blockId); - - VectorBlockEntry entry = cache.localPeek(key, CachePeekMode.PRIMARY); - entry = entry != null ? entry : cache.get(key); - - if (entry == null) - entry = getEmptyBlockEntry(blockId); - - return entry; - } - - /** - * Get empty block entry by the given block id. - */ - @NotNull - private VectorBlockEntry getEmptyBlockEntry(long blockId) { - VectorBlockEntry entry; - int colMod = size % maxBlockEdge; - - int colSize; - - if (colMod == 0) - colSize = maxBlockEdge; - else - colSize = blockId != (blocks - 1) ? maxBlockEdge : colMod; - - entry = new VectorBlockEntry(colSize); - return entry; - } - - /** - * TODO: IGNITE-5646, WIP - * - * Get affinity key for the given id. - */ - private UUID getAffinityKey(long blockId) { - return null; - } - - /** - * Distributed matrix set. - * - * @param idx Row or column index. - * @param v New value to set. - */ - private void matrixSet(int idx, double v) { - long blockId = getBlockId(idx); - // Remote set on the primary node (where given row or column is stored locally). - ignite().compute(getClusterGroupForGivenKey(CACHE_NAME, blockId)).run(() -> { - IgniteCache<VectorBlockKey, VectorBlockEntry> cache = Ignition.localIgnite().getOrCreateCache(CACHE_NAME); - - VectorBlockKey key = getCacheKey(blockId); - - // Local get. - VectorBlockEntry block = getEntryById(blockId); - - block.set(idx % block.size(), v); - - // Local put. - cache.put(key, block); - }); - } - - /** */ - private long getBlockId(int x) { - return (long)x / maxBlockEdge; - } - - /** - * Distributed vector get. - * - * @param idx index. - * @return Vector value at (idx) index. - */ - private double matrixGet(int idx) { - // Remote get from the primary node (where given row or column is stored locally). - return ignite().compute(getClusterGroupForGivenKey(CACHE_NAME, getBlockId(idx))).call(() -> { - IgniteCache<VectorBlockKey, VectorBlockEntry> cache = Ignition.localIgnite().getOrCreateCache(CACHE_NAME); - - VectorBlockKey key = getCacheKey(getBlockId(idx)); - - // Local get. - VectorBlockEntry block = cache.localPeek(key, CachePeekMode.PRIMARY); - - if (block == null) - block = cache.get(key); - - return block == null ? 0.0 : block.get(idx % block.size()); - }); - } - - /** - * Create new ML cache if needed. - */ - private IgniteCache<VectorBlockKey, VectorBlockEntry> newCache() { - CacheConfiguration<VectorBlockKey, VectorBlockEntry> cfg = new CacheConfiguration<>(); - - // Write to primary. - cfg.setWriteSynchronizationMode(CacheWriteSynchronizationMode.PRIMARY_SYNC); - - // Atomic transactions only. - cfg.setAtomicityMode(CacheAtomicityMode.ATOMIC); - - // No eviction. - cfg.setEvictionPolicy(null); - - // No copying of values. - cfg.setCopyOnRead(false); - - // Cache is partitioned. - cfg.setCacheMode(CacheMode.PARTITIONED); - - // Random cache name. - cfg.setName(CACHE_NAME); - - return Ignition.localIgnite().getOrCreateCache(cfg); - } - - /** - * Avoid this method for large vectors - * - * @return data presented as array - */ - @Override public double[] data() { - double[] res = new double[this.size]; - for (int i = 0; i < this.size; i++) - res[i] = this.get(i); - return res; - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/CacheMatrixStorage.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/CacheMatrixStorage.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/CacheMatrixStorage.java deleted file mode 100644 index fbad957..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/CacheMatrixStorage.java +++ /dev/null @@ -1,196 +0,0 @@ -/* - * 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.ignite.ml.math.impls.storage.matrix; - -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; -import org.apache.ignite.IgniteCache; -import org.apache.ignite.Ignition; -import org.apache.ignite.ml.math.MatrixStorage; -import org.apache.ignite.ml.math.StorageConstants; -import org.apache.ignite.ml.math.distributed.MatrixKeyMapper; -import org.apache.ignite.ml.math.distributed.ValueMapper; - -/** - * Matrix storage based on arbitrary cache and key and value mapping functions. - */ -public class CacheMatrixStorage<K, V> implements MatrixStorage { - /** */ - private int rows; - /** */ - private int cols; - /** */ - private IgniteCache<K, V> cache; - /** */ - private MatrixKeyMapper<K> keyMapper; - /** */ - private ValueMapper<V> valMapper; - - /** - * - */ - public CacheMatrixStorage() { - // No-op. - } - - /** - * @param rows Amount of rows in matrix. - * @param cols Amount of columns in matrix. - * @param cache Ignite cache. - * @param keyMapper {@link MatrixKeyMapper} to validate cache key. - * @param valMapper {@link ValueMapper} to obtain value for given cache key. - */ - public CacheMatrixStorage(int rows, int cols, IgniteCache<K, V> cache, MatrixKeyMapper<K> keyMapper, - ValueMapper<V> valMapper) { - assert rows > 0; - assert cols > 0; - assert cache != null; - assert keyMapper != null; - assert valMapper != null; - - this.rows = rows; - this.cols = cols; - this.cache = cache; - this.keyMapper = keyMapper; - this.valMapper = valMapper; - } - - /** - * @return Ignite cache. - */ - public IgniteCache<K, V> cache() { - return cache; - } - - /** - * @return Key mapper. - */ - public MatrixKeyMapper<K> keyMapper() { - return keyMapper; - } - - /** - * @return Value mapper. - */ - public ValueMapper<V> valueMapper() { - return valMapper; - } - - /** {@inheritDoc} */ - @Override public double get(int x, int y) { - return valMapper.toDouble(cache.get(keyMapper.apply(x, y))); - } - - /** {@inheritDoc} */ - @Override public void set(int x, int y, double v) { - cache.put(keyMapper.apply(x, y), valMapper.fromDouble(v)); - } - - /** {@inheritDoc} */ - @Override public int columnSize() { - return cols; - } - - /** {@inheritDoc} */ - @Override public int rowSize() { - return rows; - } - - /** {@inheritDoc} */ - @Override public int storageMode() { - return StorageConstants.ROW_STORAGE_MODE; - } - - /** {@inheritDoc} */ - @Override public int accessMode() { - return StorageConstants.RANDOM_ACCESS_MODE; - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeInt(rows); - out.writeInt(cols); - out.writeUTF(cache.getName()); - out.writeObject(keyMapper); - out.writeObject(valMapper); - } - - /** {@inheritDoc} */ - @SuppressWarnings({"unchecked"}) - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - rows = in.readInt(); - cols = in.readInt(); - cache = Ignition.localIgnite().getOrCreateCache(in.readUTF()); - keyMapper = (MatrixKeyMapper<K>)in.readObject(); - valMapper = (ValueMapper<V>)in.readObject(); - } - - /** {@inheritDoc} */ - @Override public boolean isSequentialAccess() { - return false; - } - - /** {@inheritDoc} */ - @Override public boolean isDense() { - return false; - } - - /** {@inheritDoc} */ - @Override public boolean isRandomAccess() { - return true; - } - - /** {@inheritDoc} */ - @Override public boolean isDistributed() { - return true; - } - - /** {@inheritDoc} */ - @Override public boolean isArrayBased() { - return false; - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = 1; - - res = res * 37 + rows; - res = res * 37 + cols; - res = res * 37 + cache.hashCode(); - res = res * 37 + keyMapper.hashCode(); - res = res * 37 + valMapper.hashCode(); - - return res; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object o) { - if (this == o) - return true; - - if (o == null || getClass() != o.getClass()) - return false; - - CacheMatrixStorage that = (CacheMatrixStorage)o; - - return (cache != null ? cache.equals(that.cache) : that.cache == null) && - (keyMapper != null ? keyMapper.equals(that.keyMapper) : that.keyMapper == null) && - (valMapper != null ? valMapper.equals(that.valMapper) : that.valMapper == null); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/DiagonalMatrixStorage.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/DiagonalMatrixStorage.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/DiagonalMatrixStorage.java deleted file mode 100644 index a0f102a..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/DiagonalMatrixStorage.java +++ /dev/null @@ -1,158 +0,0 @@ -/* - * 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.ignite.ml.math.impls.storage.matrix; - -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; -import org.apache.ignite.ml.math.MatrixStorage; -import org.apache.ignite.ml.math.StorageConstants; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; - -/** - * {@link MatrixStorage} implementation for diagonal Matrix view. - */ -public class DiagonalMatrixStorage implements MatrixStorage { - /** Backing vector for matrix diagonal. */ - private Vector diagonal; - - /** - * - */ - public DiagonalMatrixStorage() { - // No-op. - } - - /** - * @param diagonal Backing {@link Vector} for matrix diagonal. - */ - public DiagonalMatrixStorage(Vector diagonal) { - assert diagonal != null; - - this.diagonal = diagonal; - } - - /** - * - */ - public Vector diagonal() { - return diagonal; - } - - /** {@inheritDoc} */ - @Override public double get(int x, int y) { - return x == y ? diagonal.get(x) : 0.0; - } - - /** {@inheritDoc} */ - @Override public void set(int x, int y, double v) { - if (x == y) - diagonal.set(x, v); - else - throw new UnsupportedOperationException("Can't set off-diagonal element."); - } - - /** {@inheritDoc} */ - @Override public int columnSize() { - return diagonal.size(); - } - - /** {@inheritDoc} */ - @Override public int rowSize() { - return diagonal.size(); - } - - /** {@inheritDoc} */ - @Override public int storageMode() { - return StorageConstants.UNKNOWN_STORAGE_MODE; - } - - /** {@inheritDoc} */ - @Override public int accessMode() { - return StorageConstants.RANDOM_ACCESS_MODE; - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeObject(diagonal); - } - - /** {@inheritDoc} */ - @Override public double[] data() { - int size = diagonal.size(); - double[] res = new double[size * size]; - - for (int i = 0; i < size; i++) - res[i * size + i % size] = diagonal.getX(i); - - return res; - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - diagonal = (Vector)in.readObject(); - } - - /** {@inheritDoc} */ - @Override public boolean isSequentialAccess() { - return diagonal.isSequentialAccess(); - } - - /** {@inheritDoc} */ - @Override public boolean isDense() { - return diagonal.isDense(); - } - - /** {@inheritDoc} */ - @Override public boolean isRandomAccess() { - return diagonal.isRandomAccess(); - } - - /** {@inheritDoc} */ - @Override public boolean isDistributed() { - return diagonal.isDistributed(); - } - - /** {@inheritDoc} */ - @Override public boolean isArrayBased() { - return false; - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = 1; - - res = res * 37 + diagonal.hashCode(); - - return res; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object o) { - if (this == o) - return true; - - if (o == null || getClass() != o.getClass()) - return false; - - DiagonalMatrixStorage that = (DiagonalMatrixStorage)o; - - return diagonal.equals(that.diagonal); - } -}
