Class LibMatrixReorg
java.lang.Object
org.apache.sysds.runtime.matrix.data.LibMatrixReorg
MB:
Library for selected matrix reorg operations including special cases
and all combinations of dense and sparse representations.
Current list of supported operations:
- reshape,
- r' (transpose),
- rdiag (diagV2M/diagM2V),
- rsort (sorting data/indexes)
- rmempty (remove empty)
- rexpand (outer/table-seq expansion)
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic longstatic final intstatic final booleanstatic final booleanstatic final boolean -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcheckRexpand(MatrixBlock in, boolean ignore) Quick check if the input is valid for rexpand, this check does not guarantee that the input is valid for rexpandstatic voidcopyDenseMtx(MatrixBlock in, MatrixBlock out, int inIdx, int outIdx, int copyLen, boolean isAllocated, boolean copyTotalNonZeros) static voidcopyMtx(MatrixBlock in, MatrixBlock out, int inStart, int outStart, int copyLen, boolean isAllocated, boolean copyTotalNonZeros) static Future<int[]>countNNZColumns(MatrixBlock in, int k, ExecutorService pool) countNNZColumnsFuture(MatrixBlock in, int k, ExecutorService pool) static int[]static int[]countNnzPerColumn(MatrixBlock in, int rl, int ru) static MatrixBlockdiag(MatrixBlock in, MatrixBlock out) static intfindSplitIndex(int[] perm) Finds a split index for a tensor permutation that allows reduction of the permutation to a 2D matrix transpose.static MatrixBlockfusedSeqRexpand(int seqHeight, MatrixBlock A, double w) The DML code to activate this function:static MatrixBlockfusedSeqRexpand(int seqHeight, MatrixBlock A, double w, MatrixBlock ret, boolean updateClen) The DML code to activate this function:static MatrixBlockfusedSeqRexpand(int seqHeight, MatrixBlock A, double w, MatrixBlock ret, boolean updateClen, int k) The DML code to activate this function:static booleanstatic int[]mergeNnzCounts(int[] cnt, int[] cnt2) static MatrixBlockpermute(MatrixBlock in, int[] inDims, int[] perm) static MatrixBlockpermute(MatrixBlock in, int[] inDims, int[] perm, int k) static MatrixBlockremoveEmptyRows(MatrixBlock in, MatrixBlock ret, boolean emptyReturn, boolean selectNull, boolean[] flags, int rlen2) Remove selected rows, based on the boolean array given.static MatrixBlockreorg(MatrixBlock in, MatrixBlock out, ReorgOperator op) static MatrixBlockreorgInPlace(MatrixBlock in, ReorgOperator op) static List<IndexedMatrixValue>reshape(IndexedMatrixValue in, DataCharacteristics mcIn, DataCharacteristics mcOut, boolean rowwise, boolean outputEmptyBlocks) MR/SPARK reshape interface - for reshape we cannot view blocks independently, and hence, there are different CP and MR interfaces.static MatrixBlockreshape(MatrixBlock in, int rows, int cols, boolean rowwise) CP reshape operation (single input, single output matrix) NOTE: In contrast to R, the rowwise parameter specifies both the read and write order, with row-wise being the default, while R uses always a column-wise read, rowwise specifying the write order and column-wise being the default.static MatrixBlockreshape(MatrixBlock in, MatrixBlock out, int rows, int cols, boolean rowwise) CP reshape operation (single input, single output matrix) NOTE: In contrast to R, the rowwise parameter specifies both the read and write order, with row-wise being the default, while R uses always a column-wise read, rowwise specifying the write order and column-wise being the default.static MatrixBlockreshape(MatrixBlock in, MatrixBlock out, int rows, int cols, boolean rowwise, int k) CP reshape operation (single input, single output matrix) NOTE: In contrast to R, the rowwise parameter specifies both the read and write order, with row-wise being the default, while R uses always a column-wise read, rowwise specifying the write order and column-wise being the default.static voidrev(IndexedMatrixValue in, long rlen, int blen, ArrayList<IndexedMatrixValue> out) static MatrixBlockrev(MatrixBlock in, MatrixBlock out) static MatrixBlockrev(MatrixBlock in, MatrixBlock out, int k) static voidrexpand(IndexedMatrixValue data, double max, boolean rows, boolean cast, boolean ignore, long blen, ArrayList<IndexedMatrixValue> outList) MR/Spark rexpand operation (single input, multiple outputs incl empty blocks)static MatrixBlockrexpand(MatrixBlock in, MatrixBlock ret, double max, boolean rows, boolean cast, boolean ignore, int k) CP rexpand operation (single input, single output), the classic example of this operation is one hot encoding of a column to multiple columns.static MatrixBlockrexpand(MatrixBlock in, MatrixBlock ret, int max, boolean rows, boolean cast, boolean ignore, int k) CP rexpand operation (single input, single output), the classic example of this operation is one hot encoding of a column to multiple columns.static intrexpandSingleRow(int row, double v2, double w, int[] retIx, double[] retVals, boolean updateClen, int maxOutCol) static voidrmempty(IndexedMatrixValue data, IndexedMatrixValue offset, boolean rmRows, long len, long blen, ArrayList<IndexedMatrixValue> outList) MR rmempty interface - for rmempty we cannot view blocks independently, and hence, there are different CP and MR interfaces.static MatrixBlockrmempty(MatrixBlock in, MatrixBlock ret, boolean rows, boolean emptyReturn, MatrixBlock select) CP rmempty operation (single input, single output matrix)static MatrixBlockrmemptyEarlyAbort(MatrixBlock in, MatrixBlock ret, boolean rows, boolean emptyReturn, MatrixBlock select) Handle the early-termination cases of removeEmpty that do not require scanning for empty rows/columns.static MatrixBlockrmemptyUnsafe(MatrixBlock in, MatrixBlock ret, boolean rows, boolean emptyReturn, MatrixBlock select) static voidroll(IndexedMatrixValue in, long rlen, int blen, int shift, ArrayList<IndexedMatrixValue> out) static MatrixBlockroll(MatrixBlock in, MatrixBlock out, int shift) static MatrixBlockroll(MatrixBlock input, MatrixBlock output, int shift, int numThreads) static MatrixBlocksort(MatrixBlock in, MatrixBlock out, int[] by, boolean desc, boolean ixret) static MatrixBlocksort(MatrixBlock in, MatrixBlock out, int[] by, boolean desc, boolean ixret, int k) static MatrixBlocktranspose(MatrixBlock in) static MatrixBlocktranspose(MatrixBlock in, int k) static MatrixBlocktranspose(MatrixBlock in, int k, boolean allowCSR) static MatrixBlocktranspose(MatrixBlock in, MatrixBlock out) static MatrixBlocktranspose(MatrixBlock in, MatrixBlock out, int k) static MatrixBlocktranspose(MatrixBlock in, MatrixBlock out, int k, boolean allowCSR) static MatrixBlocktransposeInPlace(MatrixBlock in, int k) static voidtransposeInPlaceDenseBrenner(MatrixBlock in, int k) Transposes a dense matrix in-place using following cycles based on Brenner's method.static booleantransposeInPlaceTensor(MatrixBlock in, int[] shape, int[] perm) Performs prime in-place tensor transposition for arbitrary permutations.
-
Field Details
-
PAR_NUMCELL_THRESHOLD
public static long PAR_NUMCELL_THRESHOLD -
PAR_NUMCELL_THRESHOLD_SORT
public static final int PAR_NUMCELL_THRESHOLD_SORT- See Also:
-
SHALLOW_COPY_REORG
public static final boolean SHALLOW_COPY_REORG- See Also:
-
SPARSE_OUTPUTS_IN_CSR
public static final boolean SPARSE_OUTPUTS_IN_CSR- See Also:
-
TRANSPOSE_IN_PLACE_DENSE_LEGACY
public static final boolean TRANSPOSE_IN_PLACE_DENSE_LEGACY- See Also:
-
-
Method Details
-
isSupportedReorgOperator
-
reorg
-
reorgInPlace
-
transpose
-
transpose
-
transpose
-
transpose
-
transpose
-
transpose
-
countNNZColumns
public static Future<int[]> countNNZColumns(MatrixBlock in, int k, ExecutorService pool) throws InterruptedException, ExecutionException -
countNNZColumnsFuture
public static List<Future<int[]>> countNNZColumnsFuture(MatrixBlock in, int k, ExecutorService pool) throws InterruptedException - Throws:
InterruptedException
-
transposeInPlace
-
rev
-
rev
-
rev
public static void rev(IndexedMatrixValue in, long rlen, int blen, ArrayList<IndexedMatrixValue> out) -
roll
-
roll
-
roll
public static void roll(IndexedMatrixValue in, long rlen, int blen, int shift, ArrayList<IndexedMatrixValue> out) -
diag
-
sort
public static MatrixBlock sort(MatrixBlock in, MatrixBlock out, int[] by, boolean desc, boolean ixret) -
sort
public static MatrixBlock sort(MatrixBlock in, MatrixBlock out, int[] by, boolean desc, boolean ixret, int k) - Parameters:
in- Input matrix to sortout- Output matrix where the sorted input is inserted toby- The Ordering parameterdesc- A boolean, specifying if it should be descending order.ixret- A boolean, specifying if the return should be the sorted indexes.k- Number of parallel threads- Returns:
- The sorted out matrix.
-
reshape
CP reshape operation (single input, single output matrix) NOTE: In contrast to R, the rowwise parameter specifies both the read and write order, with row-wise being the default, while R uses always a column-wise read, rowwise specifying the write order and column-wise being the default.- Parameters:
in- input matrixrows- number of rowscols- number of columnsrowwise- if true, reshape by row- Returns:
- output matrix
-
reshape
public static MatrixBlock reshape(MatrixBlock in, MatrixBlock out, int rows, int cols, boolean rowwise) CP reshape operation (single input, single output matrix) NOTE: In contrast to R, the rowwise parameter specifies both the read and write order, with row-wise being the default, while R uses always a column-wise read, rowwise specifying the write order and column-wise being the default.- Parameters:
in- input matrixout- output matrixrows- number of rowscols- number of columnsrowwise- if true, reshape by row- Returns:
- output matrix
-
reshape
public static MatrixBlock reshape(MatrixBlock in, MatrixBlock out, int rows, int cols, boolean rowwise, int k) CP reshape operation (single input, single output matrix) NOTE: In contrast to R, the rowwise parameter specifies both the read and write order, with row-wise being the default, while R uses always a column-wise read, rowwise specifying the write order and column-wise being the default.- Parameters:
in- input matrixout- output matrixrows- number of rowscols- number of columnsrowwise- if true, reshape by rowk- The parallelization degree- Returns:
- output matrix
-
reshape
public static List<IndexedMatrixValue> reshape(IndexedMatrixValue in, DataCharacteristics mcIn, DataCharacteristics mcOut, boolean rowwise, boolean outputEmptyBlocks) MR/SPARK reshape interface - for reshape we cannot view blocks independently, and hence, there are different CP and MR interfaces.- Parameters:
in- indexed matrix valuemcIn- input matrix characteristicsmcOut- output matrix characteristicsrowwise- if true, reshape by rowoutputEmptyBlocks- output blocks with nnz=0- Returns:
- list of indexed matrix values
-
rmempty
public static MatrixBlock rmempty(MatrixBlock in, MatrixBlock ret, boolean rows, boolean emptyReturn, MatrixBlock select) CP rmempty operation (single input, single output matrix)- Parameters:
in- The input matrixret- The output matrixrows- If we are removing based on rows, or columns.emptyReturn- Return row/column of zeros for empty inputselect- An optional selection vector, to remove based on rather than empty rows or columns- Returns:
- The result MatrixBlock
-
rmemptyUnsafe
public static MatrixBlock rmemptyUnsafe(MatrixBlock in, MatrixBlock ret, boolean rows, boolean emptyReturn, MatrixBlock select) -
rmemptyEarlyAbort
public static MatrixBlock rmemptyEarlyAbort(MatrixBlock in, MatrixBlock ret, boolean rows, boolean emptyReturn, MatrixBlock select) Handle the early-termination cases of removeEmpty that do not require scanning for empty rows/columns.- Parameters:
in- The input matrixret- The output matrix, reused for the empty-input caserows- If removing based on rows, or columnsemptyReturn- Return a row/column of zeros for empty inputselect- An optional selection vector- Returns:
- The early-abort result, or
nullif no early termination applies and the caller must continue. For the select-all case the returned block is the inputinitself (a shallow alias, not a copy).
-
rmempty
public static void rmempty(IndexedMatrixValue data, IndexedMatrixValue offset, boolean rmRows, long len, long blen, ArrayList<IndexedMatrixValue> outList) MR rmempty interface - for rmempty we cannot view blocks independently, and hence, there are different CP and MR interfaces.- Parameters:
data- ?offset- ?rmRows- ?len- ?blen- block lengthoutList- list of indexed matrix values
-
rexpand
public static MatrixBlock rexpand(MatrixBlock in, MatrixBlock ret, double max, boolean rows, boolean cast, boolean ignore, int k) CP rexpand operation (single input, single output), the classic example of this operation is one hot encoding of a column to multiple columns.- Parameters:
in- Input matrixret- Output matrixmax- Number of rows/cols of the outputrows- If the expansion is in rows directioncast- If the values contained should be cast to double (rounded up and down)ignore- Ignore if the input contain values below zero that technically is incorrect input.k- Degree of parallelism- Returns:
- Output matrix rexpanded
-
rexpand
public static MatrixBlock rexpand(MatrixBlock in, MatrixBlock ret, int max, boolean rows, boolean cast, boolean ignore, int k) CP rexpand operation (single input, single output), the classic example of this operation is one hot encoding of a column to multiple columns.- Parameters:
in- Input matrixret- Output matrixmax- Number of rows/cols of the outputrows- If the expansion is in rows directioncast- If the values contained should be cast to double (rounded up and down)ignore- Ignore if the input contain values below zero that technically is incorrect input.k- Degree of parallelism- Returns:
- Output matrix rexpanded
-
fusedSeqRexpand
The DML code to activate this function:ret = table(seq(1, nrow(A)), A, w)
- Parameters:
seqHeight- A sequence vector height.A- The MatrixBlock vector to encode.w- The weight matrix to multiply on output cells.- Returns:
- A new MatrixBlock with the table result.
-
fusedSeqRexpand
public static MatrixBlock fusedSeqRexpand(int seqHeight, MatrixBlock A, double w, MatrixBlock ret, boolean updateClen) The DML code to activate this function:ret = table(seq(1, nrow(A)), A, w)
- Parameters:
seqHeight- A sequence vector height.A- The MatrixBlock vector to encode.w- The weight scalar to multiply on output cells.ret- The output MatrixBlock, does not have to be used, but depending on updateClen determine the output size.updateClen- Update clen, if set to true, ignore dimensions of ret, otherwise use the column dimension of ret.- Returns:
- A new MatrixBlock or ret.
-
fusedSeqRexpand
public static MatrixBlock fusedSeqRexpand(int seqHeight, MatrixBlock A, double w, MatrixBlock ret, boolean updateClen, int k) The DML code to activate this function:ret = table(seq(1, nrow(A)), A, w)
- Parameters:
seqHeight- A sequence vector height.A- The MatrixBlock vector to encode.w- The weight matrix to multiply on output cells.ret- The output MatrixBlock, does not have to be used, but depending on updateClen determine the output size.updateClen- Update clen, if set to true, ignore dimensions of ret, otherwise use the column dimension of ret.k- Parallelization degree- Returns:
- A new MatrixBlock or ret.
-
rexpandSingleRow
public static int rexpandSingleRow(int row, double v2, double w, int[] retIx, double[] retVals, boolean updateClen, int maxOutCol) -
checkRexpand
Quick check if the input is valid for rexpand, this check does not guarantee that the input is valid for rexpand- Parameters:
in- Input matrix blockignore- If zero valued cells should be ignored
-
rexpand
public static void rexpand(IndexedMatrixValue data, double max, boolean rows, boolean cast, boolean ignore, long blen, ArrayList<IndexedMatrixValue> outList) MR/Spark rexpand operation (single input, multiple outputs incl empty blocks)- Parameters:
data- Input indexed matrix blockmax- Total nrows/cols of the outputrows- If the expansion is in rows directioncast- If the values contained should be cast to double (rounded up and down)ignore- Ignore if the input contain values below zero that technically is incorrect input.blen- The block size to slice the output up intooutList- The output indexedMatrixValues (a list to add all the output blocks to / modify)
-
countNnzPerColumn
-
countNnzPerColumn
-
mergeNnzCounts
public static int[] mergeNnzCounts(int[] cnt, int[] cnt2) -
copyMtx
public static void copyMtx(MatrixBlock in, MatrixBlock out, int inStart, int outStart, int copyLen, boolean isAllocated, boolean copyTotalNonZeros) -
copyDenseMtx
public static void copyDenseMtx(MatrixBlock in, MatrixBlock out, int inIdx, int outIdx, int copyLen, boolean isAllocated, boolean copyTotalNonZeros) -
removeEmptyRows
public static MatrixBlock removeEmptyRows(MatrixBlock in, MatrixBlock ret, boolean emptyReturn, boolean selectNull, boolean[] flags, int rlen2) Remove selected rows, based on the boolean array given. Note this function is internal use only, and require a boolean vector to be constructed first.- Parameters:
in- Input to remove rows fromret- Output to assign the result intoemptyReturn- If the output is allowed to be empty.selectNull- If the original caller did not have a selection matrix.flags- The boolean selection vector to specify which rows to keep.rlen2- The number of true values in the flags argument.- Returns:
- Another reference to the ret matrix input argument.
-
transposeInPlaceDenseBrenner
Transposes a dense matrix in-place using following cycles based on Brenner's method. This method shifts cycles with a focus on less storage by using cycle leaders based on prime factorization. The used storage is in O(n+m). Quadratic matrices should be handled outside this method (using the trivial method) for a speedup. This method is based on: Algorithm 467, Brenner, https://dl.acm.org/doi/pdf/10.1145/355611.362542.- Parameters:
in- The input matrix to be transposed.k- The number of threads.
-
transposeInPlaceTensor
Performs prime in-place tensor transposition for arbitrary permutations.- Parameters:
in- Tensor stored as MatrixBlockshape- Original shape informtion of tensorperm- Permutation of tensor
-
findSplitIndex
public static int findSplitIndex(int[] perm) Finds a split index for a tensor permutation that allows reduction of the permutation to a 2D matrix transpose.- Parameters:
perm- permutation of tensor axes- Returns:
- split index {i} if reducible, otherwise {-1}
-
permute
-
permute
-