use of org.apache.sysml.runtime.matrix.data.IJV in project incubator-systemml by apache.
the class DataConverter method copyToDoubleVector.
public static void copyToDoubleVector(MatrixBlock mb, double[] dest, int destPos) {
if (mb.isEmptyBlock(false))
// quick path
return;
int rows = mb.getNumRows();
int cols = mb.getNumColumns();
if (mb.isInSparseFormat()) {
Iterator<IJV> iter = mb.getSparseBlockIterator();
while (iter.hasNext()) {
IJV cell = iter.next();
dest[destPos + cell.getI() * cols + cell.getJ()] = cell.getV();
}
} else {
// memcopy row major representation if at least 1 non-zero
System.arraycopy(mb.getDenseBlockValues(), 0, dest, destPos, rows * cols);
}
}
use of org.apache.sysml.runtime.matrix.data.IJV in project incubator-systemml by apache.
the class DataConverter method convertToMatrixBlockPartitions.
public static MatrixBlock[] convertToMatrixBlockPartitions(MatrixBlock mb, boolean colwise) {
MatrixBlock[] ret = null;
int rows = mb.getNumRows();
int cols = mb.getNumColumns();
long nnz = mb.getNonZeros();
boolean sparse = mb.isInSparseFormat();
double sparsity = ((double) nnz) / (rows * cols);
if (// COL PARTITIONS
colwise) {
// allocate output partitions
ret = new MatrixBlock[cols];
for (int j = 0; j < cols; j++) ret[j] = new MatrixBlock(rows, 1, false);
// cache-friendly sequential read/append
if (!mb.isEmptyBlock(false)) {
if (sparse) {
// SPARSE
Iterator<IJV> iter = mb.getSparseBlockIterator();
while (iter.hasNext()) {
IJV cell = iter.next();
ret[cell.getJ()].appendValue(cell.getI(), 0, cell.getV());
}
} else {
// DENSE
for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) ret[j].appendValue(i, 0, mb.getValueDenseUnsafe(i, j));
}
}
} else // ROW PARTITIONS
{
// allocate output partitions
ret = new MatrixBlock[rows];
for (int i = 0; i < rows; i++) ret[i] = new MatrixBlock(1, cols, sparse, (long) (cols * sparsity));
// cache-friendly sparse/dense row slicing
if (!mb.isEmptyBlock(false)) {
for (int i = 0; i < rows; i++) mb.slice(i, i, 0, cols - 1, ret[i]);
}
}
return ret;
}
use of org.apache.sysml.runtime.matrix.data.IJV in project incubator-systemml by apache.
the class DataConverter method convertToDoubleMatrix.
// ////////////
// Utils for CREATING and COPYING matrix blocks
// /////
/**
* Creates a two-dimensional double matrix of the input matrix block.
*
* @param mb matrix block
* @return 2d double array
*/
public static double[][] convertToDoubleMatrix(MatrixBlock mb) {
int rows = mb.getNumRows();
int cols = mb.getNumColumns();
// 0-initialized
double[][] ret = new double[rows][cols];
if (mb.getNonZeros() > 0) {
if (mb.isInSparseFormat()) {
Iterator<IJV> iter = mb.getSparseBlockIterator();
while (iter.hasNext()) {
IJV cell = iter.next();
ret[cell.getI()][cell.getJ()] = cell.getV();
}
} else {
double[] a = mb.getDenseBlockValues();
for (int i = 0, ix = 0; i < rows; i++) for (int j = 0; j < cols; j++, ix++) ret[i][j] = a[ix];
}
}
return ret;
}
use of org.apache.sysml.runtime.matrix.data.IJV in project incubator-systemml by apache.
the class WriterMatrixMarket method writeMatrixMarketMatrixToFile.
protected static void writeMatrixMarketMatrixToFile(Path path, JobConf job, FileSystem fs, MatrixBlock src, int rl, int ru) throws IOException {
boolean sparse = src.isInSparseFormat();
int rlen = src.getNumRows();
int clen = src.getNumColumns();
long nnz = src.getNonZeros();
BufferedWriter br = new BufferedWriter(new OutputStreamWriter(fs.create(path, true)));
try {
// for obj reuse and preventing repeated buffer re-allocations
StringBuilder sb = new StringBuilder();
if (rl == 0) {
// First output MM header
sb.append("%%MatrixMarket matrix coordinate real general\n");
// output number of rows, number of columns and number of nnz
sb.append(rlen + " " + clen + " " + nnz + "\n");
br.write(sb.toString());
sb.setLength(0);
}
// output matrix cell
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);
} finally {
IOUtilFunctions.closeSilently(br);
}
}
use of org.apache.sysml.runtime.matrix.data.IJV in project incubator-systemml by apache.
the class SparseBlockDelete method runSparseBlockDeleteTest.
/**
* @param btype
* @param sparsity
*/
private void runSparseBlockDeleteTest(SparseBlock.Type btype, double sparsity) {
try {
// data generation
double[][] A = getRandomMatrix(rows, cols, -10, 10, sparsity, 456);
// init sparse block
SparseBlock sblock = null;
MatrixBlock mbtmp = DataConverter.convertToMatrixBlock(A);
SparseBlock srtmp = mbtmp.getSparseBlock();
switch(btype) {
case MCSR:
sblock = new SparseBlockMCSR(srtmp);
break;
case CSR:
sblock = new SparseBlockCSR(srtmp);
break;
case COO:
sblock = new SparseBlockCOO(srtmp);
break;
}
// delete range per row via set
for (int i = 0; i < rows; i++) for (int j = cl; j < cu; j++) {
A[i][j] = 0;
sblock.set(i, j, 0);
}
// check for correct number of non-zeros
int[] rnnz = new int[rows];
int nnz = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) rnnz[i] += (A[i][j] != 0) ? 1 : 0;
nnz += rnnz[i];
}
if (nnz != sblock.size())
Assert.fail("Wrong number of non-zeros: " + sblock.size() + ", expected: " + nnz);
// check correct isEmpty return
for (int i = 0; i < rows; i++) if (sblock.isEmpty(i) != (rnnz[i] == 0))
Assert.fail("Wrong isEmpty(row) result for row nnz: " + rnnz[i]);
// check correct values
Iterator<IJV> iter = sblock.getIterator();
int count = 0;
while (iter.hasNext()) {
IJV cell = iter.next();
if (cell.getV() != A[cell.getI()][cell.getJ()])
Assert.fail("Wrong value returned by iterator: " + cell.getV() + ", expected: " + A[cell.getI()][cell.getJ()]);
count++;
}
if (count != nnz)
Assert.fail("Wrong number of values returned by iterator: " + count + ", expected: " + nnz);
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
Aggregations