use of org.apache.sysml.runtime.matrix.data.IJV in project systemml by apache.
the class DataConverter method convertToDenseBlock.
public static DenseBlock convertToDenseBlock(MatrixBlock mb, boolean deep) {
int rows = mb.getNumRows();
int cols = mb.getNumColumns();
DenseBlock ret = (!mb.isInSparseFormat() && mb.isAllocated() && !deep) ? mb.getDenseBlock() : // 0-initialized
DenseBlockFactory.createDenseBlock(rows, cols);
if (!mb.isEmptyBlock(false)) {
if (mb.isInSparseFormat()) {
Iterator<IJV> iter = mb.getSparseBlockIterator();
while (iter.hasNext()) {
IJV cell = iter.next();
ret.set(cell.getI(), cell.getJ(), cell.getV());
}
} else if (deep) {
ret.set(mb.getDenseBlock());
}
}
return ret;
}
use of org.apache.sysml.runtime.matrix.data.IJV in project systemml by apache.
the class DataConverter method convertToDoubleList.
public static List<Double> convertToDoubleList(MatrixBlock mb) {
int rows = mb.getNumRows();
int cols = mb.getNumColumns();
long nnz = mb.getNonZeros();
ArrayList<Double> ret = new ArrayList<>();
if (mb.isInSparseFormat()) {
Iterator<IJV> iter = mb.getSparseBlockIterator();
while (iter.hasNext()) {
IJV cell = iter.next();
ret.add(cell.getV());
}
for (long i = nnz; i < (long) rows * cols; i++) // add remaining values
ret.add(0d);
} else {
for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) ret.add(mb.getValueDenseUnsafe(i, j));
}
return ret;
}
use of org.apache.sysml.runtime.matrix.data.IJV in project systemml by apache.
the class DataConverter method convertToIntVector.
public static int[] convertToIntVector(MatrixBlock mb) {
int rows = mb.getNumRows();
int cols = mb.getNumColumns();
// 0-initialized
int[] ret = new int[rows * cols];
if (mb.isEmptyBlock(false))
return ret;
if (mb.isInSparseFormat()) {
Iterator<IJV> iter = mb.getSparseBlockIterator();
while (iter.hasNext()) {
IJV cell = iter.next();
ret[cell.getI() * cols + cell.getJ()] = (int) cell.getV();
}
} else {
// memcopy row major representation if at least 1 non-zero
for (int i = 0, cix = 0; i < rows; i++) for (int j = 0; j < cols; j++, cix++) ret[cix] = (int) (mb.getValueDenseUnsafe(i, j));
}
return ret;
}
use of org.apache.sysml.runtime.matrix.data.IJV in project systemml by apache.
the class MultiInputCbind method execute.
@Override
public void execute() {
int numInputs = Integer.parseInt(((Scalar) getFunctionInput(0)).getValue());
spagetize = Boolean.parseBoolean(((Scalar) getFunctionInput(1)).getValue());
// Compute output dimensions
numRetCols = 0;
if (spagetize) {
// Assumption the inputs are of same shape
MatrixBlock in = ((Matrix) getFunctionInput(2)).getMatrixObject().acquireRead();
numRetRows = in.getNumRows() * in.getNumColumns();
numRetCols = numInputs;
((Matrix) getFunctionInput(2)).getMatrixObject().release();
} else {
for (int inputID = 2; inputID < numInputs + 2; inputID++) {
MatrixBlock in = ((Matrix) getFunctionInput(inputID)).getMatrixObject().acquireRead();
numRetRows = in.getNumRows();
numRetCols += in.getNumColumns();
((Matrix) getFunctionInput(inputID)).getMatrixObject().release();
}
}
allocateOutput();
// Performs cbind (cbind (cbind ( X1, X2 ), X3 ), X4)
double[] retData = retMB.getDenseBlockValues();
int startColumn = 0;
for (int inputID = 2; inputID < numInputs + 2; inputID++) {
MatrixBlock in = ((Matrix) getFunctionInput(inputID)).getMatrixObject().acquireRead();
if (spagetize && in.getNumRows() * in.getNumColumns() != numRetRows) {
throw new RuntimeException("Expected the inputs to be of same size when spagetization is turned on.");
}
int inputNumCols = in.getNumColumns();
if (in.isInSparseFormat()) {
Iterator<IJV> iter = in.getSparseBlockIterator();
while (iter.hasNext()) {
IJV ijv = iter.next();
if (spagetize) {
// Perform matrix(X1, rows=length(X1), cols=1) operation before cbind
// Output Column ID = inputID-2 for all elements of inputs
int outputRowIndex = ijv.getI() * inputNumCols + ijv.getJ();
int outputColIndex = inputID - 2;
retData[(int) (outputRowIndex * retMB.getNumColumns() + outputColIndex)] = ijv.getV();
} else {
// Traditional cbind
// Row ID remains the same as that of input
int outputRowIndex = ijv.getI();
int outputColIndex = ijv.getJ() + startColumn;
retData[(int) (outputRowIndex * retMB.getNumColumns() + outputColIndex)] = ijv.getV();
}
}
} else {
double[] denseBlock = in.getDenseBlockValues();
if (denseBlock != null) {
if (spagetize) {
// Perform matrix(X1, rows=length(X1), cols=1) operation before cbind
// Output Column ID = inputID-2 for all elements of inputs
int j = inputID - 2;
for (int i = 0; i < numRetRows; i++) {
retData[(int) (i * numRetCols + j)] = denseBlock[i];
}
} else {
// Row ID remains the same as that of input
for (int i = 0; i < retMB.getNumRows(); i++) {
for (int j = 0; j < inputNumCols; j++) {
int outputColIndex = j + startColumn;
retData[(int) (i * numRetCols + outputColIndex)] = denseBlock[i * inputNumCols + j];
}
}
}
}
}
((Matrix) getFunctionInput(inputID)).getMatrixObject().release();
startColumn += inputNumCols;
}
retMB.recomputeNonZeros();
try {
retMB.examSparsity();
ret.setMatrixDoubleArray(retMB, OutputInfo.BinaryBlockOutputInfo, InputInfo.BinaryBlockInputInfo);
} catch (DMLRuntimeException e) {
throw new RuntimeException("Error while executing MultiInputCbind", e);
} catch (IOException e) {
throw new RuntimeException("Error while executing MultiInputCbind", e);
}
}
use of org.apache.sysml.runtime.matrix.data.IJV in project systemml by apache.
the class SGDNesterovUpdate method multiplyByConstant.
private static void multiplyByConstant(MatrixBlock in, double constant, double[] out) {
if (in.isInSparseFormat()) {
Iterator<IJV> iter = in.getSparseBlockIterator();
while (iter.hasNext()) {
IJV ijv = iter.next();
out[ijv.getI() * ijv.getJ()] += ijv.getV() * constant;
}
} else {
double[] denseBlock = in.getDenseBlockValues();
if (denseBlock != null) {
// If not empty block
for (int i = 0; i < out.length; i++) {
out[i] += denseBlock[i] * constant;
}
}
}
}
Aggregations