use of org.apache.sysml.runtime.matrix.data.IJV in project incubator-systemml by apache.
the class WriterBinaryCell method writeBinaryCellMatrixToHDFS.
@SuppressWarnings("deprecation")
protected void writeBinaryCellMatrixToHDFS(Path path, JobConf job, MatrixBlock src, long rlen, long clen, int brlen, int bclen) throws IOException {
boolean sparse = src.isInSparseFormat();
boolean entriesWritten = false;
FileSystem fs = IOUtilFunctions.getFileSystem(path, job);
SequenceFile.Writer writer = new SequenceFile.Writer(fs, job, path, MatrixIndexes.class, MatrixCell.class);
MatrixIndexes indexes = new MatrixIndexes();
MatrixCell cell = new MatrixCell();
int rows = src.getNumRows();
int cols = src.getNumColumns();
try {
// bound check per block
if (rows > rlen || cols > clen) {
throw new IOException("Matrix block [1:" + rows + ",1:" + cols + "] " + "out of overall matrix range [1:" + rlen + ",1:" + clen + "].");
}
if (// SPARSE
sparse) {
Iterator<IJV> iter = src.getSparseBlockIterator();
while (iter.hasNext()) {
IJV lcell = iter.next();
indexes.setIndexes(lcell.getI() + 1, lcell.getJ() + 1);
cell.setValue(lcell.getV());
writer.append(indexes, cell);
entriesWritten = true;
}
} else // DENSE
{
for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) {
double lvalue = src.getValueDenseUnsafe(i, j);
if (// for nnz
lvalue != 0) {
indexes.setIndexes(i + 1, j + 1);
cell.setValue(lvalue);
writer.append(indexes, cell);
entriesWritten = true;
}
}
}
// handle empty result
if (!entriesWritten) {
writer.append(new MatrixIndexes(1, 1), new MatrixCell(0));
}
} finally {
IOUtilFunctions.closeSilently(writer);
}
}
use of org.apache.sysml.runtime.matrix.data.IJV in project incubator-systemml by apache.
the class WriterTextCell method writeTextCellMatrixToFile.
protected static void writeTextCellMatrixToFile(Path path, JobConf job, FileSystem fs, MatrixBlock src, int rl, int ru) throws IOException {
boolean sparse = src.isInSparseFormat();
int clen = src.getNumColumns();
try (BufferedWriter br = new BufferedWriter(new OutputStreamWriter(fs.create(path, true)))) {
// for obj reuse and preventing repeated buffer re-allocations
StringBuilder sb = new StringBuilder();
if (// SPARSE
sparse) {
Iterator<IJV> iter = src.getSparseBlockIterator(rl, ru);
while (iter.hasNext()) {
IJV cell = iter.next();
sb.append(cell.getI() + 1);
sb.append(' ');
sb.append(cell.getJ() + 1);
sb.append(' ');
sb.append(cell.getV());
sb.append('\n');
// same as append
br.write(sb.toString());
sb.setLength(0);
}
} else // DENSE
{
for (int i = rl; i < ru; i++) {
String rowIndex = Integer.toString(i + 1);
for (int j = 0; j < clen; j++) {
double lvalue = src.getValueDenseUnsafe(i, j);
if (// for nnz
lvalue != 0) {
sb.append(rowIndex);
sb.append(' ');
sb.append(j + 1);
sb.append(' ');
sb.append(lvalue);
sb.append('\n');
// same as append
br.write(sb.toString());
sb.setLength(0);
}
}
}
}
// handle empty result
if (src.isEmptyBlock(false) && rl == 0)
br.write(IOUtilFunctions.EMPTY_TEXT_LINE);
}
}
use of org.apache.sysml.runtime.matrix.data.IJV in project incubator-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;
}
}
}
}
use of org.apache.sysml.runtime.matrix.data.IJV in project incubator-systemml by apache.
the class SGDNesterovUpdate method copy.
// Assumption dest is zero-ed out.
private static void copy(MatrixBlock src, double[] dest) {
if (src.isInSparseFormat()) {
Iterator<IJV> iter = src.getSparseBlockIterator();
while (iter.hasNext()) {
IJV ijv = iter.next();
dest[ijv.getI() * ijv.getJ()] = ijv.getV();
}
} else {
double[] denseBlock = src.getDenseBlockValues();
if (denseBlock != null) {
// If not empty block
System.arraycopy(denseBlock, 0, dest, 0, dest.length);
}
}
}
use of org.apache.sysml.runtime.matrix.data.IJV in project incubator-systemml by apache.
the class DataConverter method convertToLongVector.
public static long[] convertToLongVector(MatrixBlock mb) {
int rows = mb.getNumRows();
int cols = mb.getNumColumns();
// 0-initialized
long[] ret = new long[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;
}
Aggregations