use of org.apache.sysml.runtime.functionobjects.SortIndex in project incubator-systemml by apache.
the class ReorgCPInstruction method processInstruction.
@Override
public void processInstruction(ExecutionContext ec) throws DMLRuntimeException {
//acquire inputs
MatrixBlock matBlock = ec.getMatrixInput(input1.getName());
ReorgOperator r_op = (ReorgOperator) _optr;
if (r_op.fn instanceof SortIndex) {
//additional attributes for sort
int col = (int) ec.getScalarInput(_col.getName(), _col.getValueType(), _col.isLiteral()).getLongValue();
boolean desc = ec.getScalarInput(_desc.getName(), _desc.getValueType(), _desc.isLiteral()).getBooleanValue();
boolean ixret = ec.getScalarInput(_ixret.getName(), _ixret.getValueType(), _ixret.isLiteral()).getBooleanValue();
r_op.fn = SortIndex.getSortIndexFnObject(col, desc, ixret);
}
//execute operation
MatrixBlock soresBlock = (MatrixBlock) (matBlock.reorgOperations(r_op, new MatrixBlock(), 0, 0, 0));
//release inputs/outputs
ec.releaseMatrixInput(input1.getName());
ec.setMatrixOutput(output.getName(), soresBlock);
}
use of org.apache.sysml.runtime.functionobjects.SortIndex in project incubator-systemml by apache.
the class MatrixBlock method reorgOperations.
@Override
public MatrixValue reorgOperations(ReorgOperator op, MatrixValue ret, int startRow, int startColumn, int length) throws DMLRuntimeException {
if (!(op.fn instanceof SwapIndex || op.fn instanceof DiagIndex || op.fn instanceof SortIndex || op.fn instanceof RevIndex))
throw new DMLRuntimeException("the current reorgOperations cannot support: " + op.fn.getClass() + ".");
MatrixBlock result = checkType(ret);
//compute output dimensions and sparsity flag
CellIndex tempCellIndex = new CellIndex(-1, -1);
op.fn.computeDimension(rlen, clen, tempCellIndex);
boolean sps = evalSparseFormatInMemory(tempCellIndex.row, tempCellIndex.column, nonZeros);
//prepare output matrix block w/ right meta data
if (result == null)
result = new MatrixBlock(tempCellIndex.row, tempCellIndex.column, sps, nonZeros);
else
result.reset(tempCellIndex.row, tempCellIndex.column, sps, nonZeros);
if (LibMatrixReorg.isSupportedReorgOperator(op)) {
//SPECIAL case (operators with special performance requirements,
//or size-dependent special behavior)
//currently supported opcodes: r', rdiag, rsort
LibMatrixReorg.reorg(this, result, op);
} else {
//GENERIC case (any reorg operator)
CellIndex temp = new CellIndex(0, 0);
if (sparse) {
if (sparseBlock != null) {
for (int r = 0; r < Math.min(rlen, sparseBlock.numRows()); r++) {
if (sparseBlock.isEmpty(r))
continue;
int apos = sparseBlock.pos(r);
int alen = sparseBlock.size(r);
int[] aix = sparseBlock.indexes(r);
double[] avals = sparseBlock.values(r);
for (int i = apos; i < apos + alen; i++) {
tempCellIndex.set(r, aix[i]);
op.fn.execute(tempCellIndex, temp);
result.appendValue(temp.row, temp.column, avals[i]);
}
}
}
} else {
if (denseBlock != null) {
if (//SPARSE<-DENSE
result.isInSparseFormat()) {
double[] a = denseBlock;
for (int i = 0, aix = 0; i < rlen; i++) for (int j = 0; j < clen; j++, aix++) {
temp.set(i, j);
op.fn.execute(temp, temp);
result.appendValue(temp.row, temp.column, a[aix]);
}
} else //DENSE<-DENSE
{
result.allocateDenseBlock();
Arrays.fill(result.denseBlock, 0);
double[] a = denseBlock;
double[] c = result.denseBlock;
int n = result.clen;
for (int i = 0, aix = 0; i < rlen; i++) for (int j = 0; j < clen; j++, aix++) {
temp.set(i, j);
op.fn.execute(temp, temp);
c[temp.row * n + temp.column] = a[aix];
}
result.nonZeros = nonZeros;
}
}
}
}
return result;
}
use of org.apache.sysml.runtime.functionobjects.SortIndex in project incubator-systemml by apache.
the class MatrixBlock method sortOperations.
public MatrixValue sortOperations(MatrixValue weights, MatrixValue result) throws DMLRuntimeException {
boolean wtflag = (weights != null);
MatrixBlock wts = (weights == null ? null : checkType(weights));
checkType(result);
if (getNumColumns() != 1) {
throw new DMLRuntimeException("Invalid input dimensions (" + getNumRows() + "x" + getNumColumns() + ") to sort operation.");
}
if (wts != null && wts.getNumColumns() != 1) {
throw new DMLRuntimeException("Invalid weight dimensions (" + wts.getNumRows() + "x" + wts.getNumColumns() + ") to sort operation.");
}
// prepare result, currently always dense
// #rows in temp matrix = 1 + #nnz in the input ( 1 is for the "zero" value)
int dim1 = (int) (1 + this.getNonZeros());
if (result == null)
result = new MatrixBlock(dim1, 2, false);
else
result.reset(dim1, 2, false);
// Copy the input elements into a temporary array for sorting
// First column is data and second column is weights
// (since the inputs are vectors, they are likely dense - hence quickget is sufficient)
MatrixBlock tdw = new MatrixBlock(dim1, 2, false);
double d, w, zero_wt = 0;
int ind = 1;
if (// w/ weights
wtflag) {
for (int i = 0; i < rlen; i++) {
d = quickGetValue(i, 0);
w = wts.quickGetValue(i, 0);
if (d != 0) {
tdw.quickSetValue(ind, 0, d);
tdw.quickSetValue(ind, 1, w);
ind++;
} else
zero_wt += w;
}
} else //w/o weights
{
zero_wt = getNumRows() - getNonZeros();
for (int i = 0; i < rlen; i++) {
d = quickGetValue(i, 0);
if (d != 0) {
tdw.quickSetValue(ind, 0, d);
tdw.quickSetValue(ind, 1, 1);
ind++;
}
}
}
tdw.quickSetValue(0, 0, 0.0);
//num zeros in input
tdw.quickSetValue(0, 1, zero_wt);
// Sort td and tw based on values inside td (ascending sort), incl copy into result
SortIndex sfn = SortIndex.getSortIndexFnObject(1, false, false);
ReorgOperator rop = new ReorgOperator(sfn);
LibMatrixReorg.reorg(tdw, (MatrixBlock) result, rop);
return result;
}
Aggregations