|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.sysds.runtime.compress.lib; |
| 21 | + |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.List; |
| 24 | +import java.util.concurrent.ExecutorService; |
| 25 | +import java.util.concurrent.Future; |
| 26 | + |
| 27 | +import org.apache.commons.logging.Log; |
| 28 | +import org.apache.commons.logging.LogFactory; |
| 29 | +import org.apache.sysds.runtime.compress.CompressedMatrixBlock; |
| 30 | +import org.apache.sysds.runtime.compress.DMLCompressionException; |
| 31 | +import org.apache.sysds.runtime.compress.colgroup.AColGroup; |
| 32 | +import org.apache.sysds.runtime.data.DenseBlock; |
| 33 | +import org.apache.sysds.runtime.data.SparseBlock; |
| 34 | +import org.apache.sysds.runtime.data.SparseBlockMCSR; |
| 35 | +import org.apache.sysds.runtime.functionobjects.SwapIndex; |
| 36 | +import org.apache.sysds.runtime.matrix.data.LibMatrixReorg; |
| 37 | +import org.apache.sysds.runtime.matrix.data.MatrixBlock; |
| 38 | +import org.apache.sysds.runtime.matrix.operators.ReorgOperator; |
| 39 | +import org.apache.sysds.runtime.util.CommonThreadPool; |
| 40 | + |
| 41 | +public class CLALibReorg { |
| 42 | + |
| 43 | + protected static final Log LOG = LogFactory.getLog(CLALibReorg.class.getName()); |
| 44 | + |
| 45 | + public static boolean warned = false; |
| 46 | + |
| 47 | + public static MatrixBlock reorg(CompressedMatrixBlock cmb, ReorgOperator op, MatrixBlock ret, int startRow, |
| 48 | + int startColumn, int length) { |
| 49 | + // SwapIndex is transpose |
| 50 | + if(op.fn instanceof SwapIndex && cmb.getNumColumns() == 1) { |
| 51 | + MatrixBlock tmp = cmb.decompress(op.getNumThreads()); |
| 52 | + long nz = tmp.setNonZeros(tmp.getNonZeros()); |
| 53 | + if(tmp.isInSparseFormat()) |
| 54 | + return LibMatrixReorg.transpose(tmp); // edge case... |
| 55 | + else |
| 56 | + tmp = new MatrixBlock(tmp.getNumColumns(), tmp.getNumRows(), tmp.getDenseBlockValues()); |
| 57 | + tmp.setNonZeros(nz); |
| 58 | + return tmp; |
| 59 | + } |
| 60 | + else if(op.fn instanceof SwapIndex) { |
| 61 | + if(cmb.getCachedDecompressed() != null) |
| 62 | + return cmb.getCachedDecompressed().reorgOperations(op, ret, startRow, startColumn, length); |
| 63 | + |
| 64 | + return transpose(cmb, ret, op.getNumThreads()); |
| 65 | + } |
| 66 | + else { |
| 67 | + // Allow transpose to be compressed output. In general we need to have a transposed flag on |
| 68 | + // the compressed matrix. https://issues.apache.org/jira/browse/SYSTEMDS-3025 |
| 69 | + String message = !warned ? op.getClass().getSimpleName() + " -- " + op.fn.getClass().getSimpleName() : null; |
| 70 | + MatrixBlock tmp = cmb.getUncompressed(message, op.getNumThreads()); |
| 71 | + warned = true; |
| 72 | + return tmp.reorgOperations(op, ret, startRow, startColumn, length); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private static MatrixBlock transpose(CompressedMatrixBlock cmb, MatrixBlock ret, int k) { |
| 77 | + |
| 78 | + final long nnz = cmb.getNonZeros(); |
| 79 | + final int nRow = cmb.getNumRows(); |
| 80 | + final int nCol = cmb.getNumColumns(); |
| 81 | + final boolean sparseOut = MatrixBlock.evalSparseFormatInMemory(nRow, nCol, nnz); |
| 82 | + if(sparseOut) |
| 83 | + return transposeSparse(cmb, ret, k, nRow, nCol, nnz); |
| 84 | + else |
| 85 | + return transposeDense(cmb, ret, k, nRow, nCol, nnz); |
| 86 | + } |
| 87 | + |
| 88 | + private static MatrixBlock transposeSparse(CompressedMatrixBlock cmb, MatrixBlock ret, int k, int nRow, int nCol, |
| 89 | + long nnz) { |
| 90 | + if(ret == null) |
| 91 | + ret = new MatrixBlock(nCol, nRow, true, nnz); |
| 92 | + else |
| 93 | + ret.reset(nCol, nRow, true, nnz); |
| 94 | + |
| 95 | + ret.allocateAndResetSparseBlock(true, SparseBlock.Type.MCSR); |
| 96 | + |
| 97 | + final int nColOut = ret.getNumColumns(); |
| 98 | + |
| 99 | + if(k > 1) |
| 100 | + decompressToTransposedSparseParallel((SparseBlockMCSR) ret.getSparseBlock(), cmb.getColGroups(), nColOut, k); |
| 101 | + else |
| 102 | + decompressToTransposedSparseSingleThread((SparseBlockMCSR) ret.getSparseBlock(), cmb.getColGroups(), nColOut); |
| 103 | + |
| 104 | + return ret; |
| 105 | + } |
| 106 | + |
| 107 | + private static MatrixBlock transposeDense(CompressedMatrixBlock cmb, MatrixBlock ret, int k, int nRow, int nCol, |
| 108 | + long nnz) { |
| 109 | + if(ret == null) |
| 110 | + ret = new MatrixBlock(nCol, nRow, false, nnz); |
| 111 | + else |
| 112 | + ret.reset(nCol, nRow, false, nnz); |
| 113 | + |
| 114 | + // TODO: parallelize |
| 115 | + ret.allocateDenseBlock(); |
| 116 | + |
| 117 | + decompressToTransposedDense(ret.getDenseBlock(), cmb.getColGroups(), nRow, 0, nRow); |
| 118 | + return ret; |
| 119 | + } |
| 120 | + |
| 121 | + private static void decompressToTransposedDense(DenseBlock ret, List<AColGroup> groups, int rlen, int rl, int ru) { |
| 122 | + for(int i = 0; i < groups.size(); i++) { |
| 123 | + AColGroup g = groups.get(i); |
| 124 | + g.decompressToDenseBlockTransposed(ret, rl, ru); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private static void decompressToTransposedSparseSingleThread(SparseBlockMCSR ret, List<AColGroup> groups, |
| 129 | + int nColOut) { |
| 130 | + for(int i = 0; i < groups.size(); i++) { |
| 131 | + AColGroup g = groups.get(i); |
| 132 | + g.decompressToSparseBlockTransposed(ret, nColOut); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + private static void decompressToTransposedSparseParallel(SparseBlockMCSR ret, List<AColGroup> groups, int nColOut, |
| 137 | + int k) { |
| 138 | + final ExecutorService pool = CommonThreadPool.get(k); |
| 139 | + try { |
| 140 | + final List<Future<?>> tasks = new ArrayList<>(groups.size()); |
| 141 | + |
| 142 | + for(int i = 0; i < groups.size(); i++) { |
| 143 | + final AColGroup g = groups.get(i); |
| 144 | + tasks.add(pool.submit(() -> g.decompressToSparseBlockTransposed(ret, nColOut))); |
| 145 | + } |
| 146 | + |
| 147 | + for(Future<?> f : tasks) |
| 148 | + f.get(); |
| 149 | + |
| 150 | + } |
| 151 | + catch(Exception e) { |
| 152 | + throw new DMLCompressionException("Failed to parallel decompress transpose sparse", e); |
| 153 | + } |
| 154 | + finally { |
| 155 | + pool.shutdown(); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments