use of org.apache.sysml.runtime.matrix.data.SparseBlockCSR in project incubator-systemml by apache.
the class GPUObject method copyFromDeviceToHost.
protected void copyFromDeviceToHost() throws DMLRuntimeException {
LOG.trace("GPU : copyFromDeviceToHost, on " + this + ", GPUContext=" + getGPUContext());
if (getJcudaDenseMatrixPtr() != null && getJcudaSparseMatrixPtr() != null) {
throw new DMLRuntimeException("Invalid state : JCuda dense/sparse pointer are both allocated");
}
if (getJcudaDenseMatrixPtr() != null) {
long start = 0;
if (DMLScript.STATISTICS)
start = System.nanoTime();
MatrixBlock tmp = new MatrixBlock(toIntExact(mat.getNumRows()), toIntExact(mat.getNumColumns()), false);
tmp.allocateDenseBlock();
double[] data = tmp.getDenseBlock();
cudaMemcpy(Pointer.to(data), getJcudaDenseMatrixPtr(), getDoubleSizeOf(data.length), cudaMemcpyDeviceToHost);
tmp.recomputeNonZeros();
mat.acquireModify(tmp);
mat.release();
if (DMLScript.STATISTICS)
GPUStatistics.cudaFromDevTime.addAndGet(System.nanoTime() - start);
if (DMLScript.STATISTICS)
GPUStatistics.cudaFromDevCount.addAndGet(1);
} else if (getJcudaSparseMatrixPtr() != null) {
if (!LibMatrixCUDA.isInSparseFormat(getGPUContext(), mat))
throw new DMLRuntimeException("Block not in sparse format on host yet the device sparse matrix pointer is not null");
if (this.isSparseAndEmpty()) {
// Empty Block
MatrixBlock tmp = new MatrixBlock();
mat.acquireModify(tmp);
mat.release();
} else {
long start = 0;
if (DMLScript.STATISTICS)
start = System.nanoTime();
int rows = toIntExact(mat.getNumRows());
int cols = toIntExact(mat.getNumColumns());
int nnz = toIntExact(getJcudaSparseMatrixPtr().nnz);
int[] rowPtr = new int[rows + 1];
int[] colInd = new int[nnz];
double[] values = new double[nnz];
CSRPointer.copyToHost(getJcudaSparseMatrixPtr(), rows, nnz, rowPtr, colInd, values);
SparseBlockCSR sparseBlock = new SparseBlockCSR(rowPtr, colInd, values, nnz);
MatrixBlock tmp = new MatrixBlock(rows, cols, nnz, sparseBlock);
mat.acquireModify(tmp);
mat.release();
if (DMLScript.STATISTICS)
GPUStatistics.cudaFromDevTime.addAndGet(System.nanoTime() - start);
if (DMLScript.STATISTICS)
GPUStatistics.cudaFromDevCount.addAndGet(1);
}
} else {
throw new DMLRuntimeException("Cannot copy from device to host as JCuda dense/sparse pointer is not allocated");
}
dirty = false;
}
use of org.apache.sysml.runtime.matrix.data.SparseBlockCSR 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);
}
}
use of org.apache.sysml.runtime.matrix.data.SparseBlockCSR in project incubator-systemml by apache.
the class SparseBlockGetFirstIndex method runSparseBlockGetFirstIndexTest.
/**
*
* @param sparseM1
* @param sparseM2
* @param instType
*/
private void runSparseBlockGetFirstIndexTest(SparseBlock.Type btype, double sparsity, IndexType itype) {
try {
//data generation
double[][] A = getRandomMatrix(rows, cols, -10, 10, sparsity, 3456);
//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;
}
//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 index values
for (int i = 0; i < rows; i++) {
int ix = getFirstIx(A, i, i, itype);
int sixpos = -1;
switch(itype) {
case GT:
sixpos = sblock.posFIndexGT(i, i);
break;
case GTE:
sixpos = sblock.posFIndexGTE(i, i);
break;
case LTE:
sixpos = sblock.posFIndexLTE(i, i);
break;
}
int six = (sixpos >= 0) ? sblock.indexes(i)[sixpos] : -1;
if (six != ix) {
Assert.fail("Wrong index returned by index probe (" + itype.toString() + "," + i + "): " + six + ", expected: " + ix);
}
}
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
use of org.apache.sysml.runtime.matrix.data.SparseBlockCSR in project incubator-systemml by apache.
the class SparseBlockGetSet method runSparseBlockGetSetTest.
/**
*
* @param sparseM1
* @param sparseM2
* @param instType
*/
private void runSparseBlockGetSetTest(SparseBlock.Type btype, double sparsity, InitType itype) {
try {
//data generation
double[][] A = getRandomMatrix(rows, cols, -10, 10, sparsity, 7654321);
//init sparse block
SparseBlock sblock = null;
if (itype == InitType.BULK) {
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;
}
} else if (itype == InitType.SEQ_SET || itype == InitType.RAND_SET) {
switch(btype) {
case MCSR:
sblock = new SparseBlockMCSR(rows, cols);
break;
case CSR:
sblock = new SparseBlockCSR(rows, cols);
break;
case COO:
sblock = new SparseBlockCOO(rows, cols);
break;
}
if (itype == InitType.SEQ_SET) {
for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) sblock.append(i, j, A[i][j]);
} else if (itype == InitType.RAND_SET) {
LongLongDoubleHashMap map = new LongLongDoubleHashMap();
for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) map.addValue(i, j, A[i][j]);
for (//random hash order
LLDoubleEntry e : //random hash order
map.extractValues()) sblock.set((int) e.key1, (int) e.key2, e.value);
}
}
//check basic meta data
if (sblock.numRows() != rows)
Assert.fail("Wrong number of rows: " + sblock.numRows() + ", expected: " + rows);
//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
for (int i = 0; i < rows; i++) if (!sblock.isEmpty(i))
for (int j = 0; j < cols; j++) {
double tmp = sblock.get(i, j);
if (tmp != A[i][j])
Assert.fail("Wrong get value for cell (" + i + "," + j + "): " + tmp + ", expected: " + A[i][j]);
}
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
use of org.apache.sysml.runtime.matrix.data.SparseBlockCSR in project incubator-systemml by apache.
the class SparseBlockIndexRange method runSparseBlockIndexRangeTest.
/**
*
* @param btype
* @param sparsity
*/
private void runSparseBlockIndexRangeTest(SparseBlock.Type btype, double sparsity, UpdateType utype) {
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
if (utype == UpdateType.DELETE) {
for (int i = 0; i < rows; i++) {
sblock.deleteIndexRange(i, cl, cu);
Arrays.fill(A[i], cl, cu, 0);
}
} else if (utype == UpdateType.INSERT) {
double[] vals = new double[cu - cl];
for (int j = cl; j < cu; j++) vals[j - cl] = j;
for (int i = 0; i < rows; i++) {
sblock.setIndexRange(i, cl, cu, vals, 0, cu - cl);
System.arraycopy(vals, 0, A[i], cl, cu - cl);
}
}
//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