use of org.apache.sysml.runtime.matrix.data.SparseBlockCOO in project incubator-systemml by apache.
the class SparseBlockScan method runSparseBlockScanTest.
/**
* @param sparseM1
* @param sparseM2
* @param instType
*/
private void runSparseBlockScanTest(SparseBlock.Type btype, double sparsity) {
try {
// data generation
double[][] A = getRandomMatrix(rows, cols, -10, 10, sparsity, 1234);
// 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 values
int count = 0;
for (int i = 0; i < rows; i++) {
int alen = sblock.size(i);
int apos = sblock.pos(i);
int[] aix = sblock.indexes(i);
double[] avals = sblock.values(i);
for (int j = 0; j < alen; j++) {
if (avals[apos + j] != A[i][aix[apos + j]])
Assert.fail("Wrong value returned by scan: " + avals[apos + j] + ", expected: " + A[i][apos + aix[j]]);
count++;
}
}
if (count != nnz)
Assert.fail("Wrong number of values returned by scan: " + count + ", expected: " + nnz);
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
use of org.apache.sysml.runtime.matrix.data.SparseBlockCOO in project incubator-systemml by apache.
the class SparseBlockSize method runSparseBlockSizeTest.
/**
* @param btype
* @param sparsity
*/
private void runSparseBlockSizeTest(SparseBlock.Type btype, double sparsity) {
try {
// data generation
double[][] A = getRandomMatrix(rows, cols, -10, 10, sparsity, 123);
// 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;
}
// prepare summary statistics nnz
int[] rnnz = new int[rows];
int nnz = 0;
int nnz2 = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
rnnz[i] += (A[i][j] != 0) ? 1 : 0;
nnz2 += (i >= rl && j >= cl && i < ru && j < cu && A[i][j] != 0) ? 1 : 0;
}
nnz += rnnz[i];
}
// check full block nnz
if (nnz != sblock.size())
Assert.fail("Wrong number of non-zeros: " + sblock.size() + ", expected: " + nnz);
// check row nnz
for (int i = 0; i < rows; i++) if (sblock.size(i) != rnnz[i]) {
Assert.fail("Wrong number of row non-zeros (" + i + "): " + sblock.size(i) + ", expected: " + rnnz[i]);
}
// check two row nnz
for (int i = 1; i < rows; i++) if (sblock.size(i - 1, i + 1) != rnnz[i - 1] + rnnz[i]) {
Assert.fail("Wrong number of row block non-zeros (" + (i - 1) + "," + (i + 1) + "): " + sblock.size(i - 1, i + 1) + ", expected: " + rnnz[i - 1] + rnnz[i]);
}
// check index range nnz
if (sblock.size(rl, ru, cl, cu) != nnz2)
Assert.fail("Wrong number of range non-zeros: " + sblock.size(rl, ru, cl, cu) + ", expected: " + nnz2);
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
use of org.apache.sysml.runtime.matrix.data.SparseBlockCOO in project incubator-systemml by apache.
the class SparseBlockAppendSort method runSparseBlockAppendSortTest.
/**
* @param sparseM1
* @param sparseM2
* @param instType
*/
private void runSparseBlockAppendSortTest(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;
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]);
Iterator<ADoubleEntry> iter = map.getIterator();
while (iter.hasNext()) {
// random hash order
ADoubleEntry e = iter.next();
sblock.append((int) e.getKey1(), (int) e.getKey2(), e.value);
}
}
// sort appended values
sblock.sort();
// 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.SparseBlockCOO in project incubator-systemml by apache.
the class SparseBlockIterator method runSparseBlockIteratorTest.
/**
* @param sparseM1
* @param sparseM2
* @param instType
*/
private void runSparseBlockIteratorTest(SparseBlock.Type btype, double sparsity, boolean partial) {
try {
// data generation
double[][] A = getRandomMatrix(rows, cols, -10, 10, sparsity, 8765432);
// 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;
int rl = partial ? rlPartial : 0;
for (int i = rl; i < rows; i++) {
for (int j = 0; j < cols; j++) rnnz[i] += (A[i][j] != 0) ? 1 : 0;
nnz += rnnz[i];
}
if (!partial && nnz != sblock.size())
Assert.fail("Wrong number of non-zeros: " + sblock.size() + ", expected: " + nnz);
// check correct isEmpty return
for (int i = rl; 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 = !partial ? sblock.getIterator() : sblock.getIterator(rl, rows);
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.SparseBlockCOO in project incubator-systemml by apache.
the class GPUObject method copyFromHostToDevice.
void copyFromHostToDevice(String opcode) {
if (LOG.isTraceEnabled()) {
LOG.trace("GPU : copyFromHostToDevice, on " + this + ", GPUContext=" + getGPUContext());
}
long start = 0;
if (DMLScript.STATISTICS)
start = System.nanoTime();
long acqrTime = DMLScript.FINEGRAINED_STATISTICS ? System.nanoTime() : 0;
MatrixBlock tmp = mat.acquireRead();
if (DMLScript.FINEGRAINED_STATISTICS) {
if (tmp.isInSparseFormat())
GPUStatistics.maintainCPMiscTimes(opcode, CPInstruction.MISC_TIMER_GET_SPARSE_MB, System.nanoTime() - acqrTime);
else
GPUStatistics.maintainCPMiscTimes(opcode, CPInstruction.MISC_TIMER_GET_DENSE_MB, System.nanoTime() - acqrTime);
}
if (tmp.isInSparseFormat()) {
int[] rowPtr = null;
int[] colInd = null;
double[] values = null;
// Only recompute non-zero if unknown, else this will incur huge penalty !!
if (tmp.getNonZeros() < 0) {
tmp.recomputeNonZeros();
}
long nnz = tmp.getNonZeros();
mat.getMatrixCharacteristics().setNonZeros(nnz);
SparseBlock block = tmp.getSparseBlock();
boolean copyToDevice = true;
if (block == null && tmp.getNonZeros() == 0) {
// // Allocate empty block --> not necessary
// // To reproduce this, see org.apache.sysml.test.integration.applications.dml.ID3DMLTest
// rowPtr = new int[0];
// colInd = new int[0];
// values = new double[0];
copyToDevice = false;
} else if (block == null && tmp.getNonZeros() != 0) {
throw new DMLRuntimeException("Expected CP sparse block to be not null.");
} else {
// CSR is the preferred format for cuSparse GEMM
// Converts MCSR and COO to CSR
SparseBlockCSR csrBlock = null;
long t0 = 0;
if (block instanceof SparseBlockCSR) {
csrBlock = (SparseBlockCSR) block;
} else if (block instanceof SparseBlockCOO) {
// TODO - should we do this on the GPU using cusparse<t>coo2csr() ?
if (DMLScript.STATISTICS)
t0 = System.nanoTime();
SparseBlockCOO cooBlock = (SparseBlockCOO) block;
csrBlock = new SparseBlockCSR(toIntExact(mat.getNumRows()), cooBlock.rowIndexes(), cooBlock.indexes(), cooBlock.values());
if (DMLScript.STATISTICS)
GPUStatistics.cudaSparseConversionTime.add(System.nanoTime() - t0);
if (DMLScript.STATISTICS)
GPUStatistics.cudaSparseConversionCount.increment();
} else if (block instanceof SparseBlockMCSR) {
if (DMLScript.STATISTICS)
t0 = System.nanoTime();
SparseBlockMCSR mcsrBlock = (SparseBlockMCSR) block;
csrBlock = new SparseBlockCSR(mcsrBlock.getRows(), toIntExact(mcsrBlock.size()));
if (DMLScript.STATISTICS)
GPUStatistics.cudaSparseConversionTime.add(System.nanoTime() - t0);
if (DMLScript.STATISTICS)
GPUStatistics.cudaSparseConversionCount.increment();
} else {
throw new DMLRuntimeException("Unsupported sparse matrix format for CUDA operations");
}
rowPtr = csrBlock.rowPointers();
colInd = csrBlock.indexes();
values = csrBlock.values();
}
allocateSparseMatrixOnDevice();
if (copyToDevice) {
long t1 = DMLScript.FINEGRAINED_STATISTICS ? System.nanoTime() : 0;
CSRPointer.copyToDevice(getGPUContext(), getJcudaSparseMatrixPtr(), tmp.getNumRows(), tmp.getNonZeros(), rowPtr, colInd, values);
if (DMLScript.FINEGRAINED_STATISTICS)
GPUStatistics.maintainCPMiscTimes(opcode, GPUInstruction.MISC_TIMER_HOST_TO_DEVICE, System.nanoTime() - t1);
}
} else {
double[] data = tmp.getDenseBlockValues();
if (data == null && tmp.getSparseBlock() != null)
throw new DMLRuntimeException("Incorrect sparsity calculation");
else if (data == null && tmp.getNonZeros() != 0)
throw new DMLRuntimeException("MatrixBlock is not allocated");
allocateDenseMatrixOnDevice();
if (tmp.getNonZeros() == 0) {
// Minor optimization: No need to allocate empty error for CPU
// data = new double[tmp.getNumRows() * tmp.getNumColumns()];
long t1 = DMLScript.FINEGRAINED_STATISTICS ? System.nanoTime() : 0;
cudaMemset(getJcudaDenseMatrixPtr(), 0, getDatatypeSizeOf(mat.getNumRows() * mat.getNumColumns()));
if (DMLScript.FINEGRAINED_STATISTICS)
GPUStatistics.maintainCPMiscTimes(opcode, GPUInstruction.MISC_TIMER_SET_ZERO, System.nanoTime() - t1);
} else {
// Copy dense block
// H2D now only measures the time taken to do
LibMatrixCUDA.cudaSupportFunctions.hostToDevice(getGPUContext(), data, getJcudaDenseMatrixPtr(), opcode);
}
}
mat.release();
if (DMLScript.STATISTICS)
GPUStatistics.cudaToDevTime.add(System.nanoTime() - start);
if (DMLScript.STATISTICS)
GPUStatistics.cudaToDevCount.add(1);
}
Aggregations