Search in sources :

Example 6 with SparseBlockCSR

use of org.apache.sysml.runtime.matrix.data.SparseBlockCSR 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);
    }
}
Also used : MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) SparseBlockMCSR(org.apache.sysml.runtime.matrix.data.SparseBlockMCSR) SparseBlockCSR(org.apache.sysml.runtime.matrix.data.SparseBlockCSR) SparseBlock(org.apache.sysml.runtime.matrix.data.SparseBlock) SparseBlockCOO(org.apache.sysml.runtime.matrix.data.SparseBlockCOO)

Example 7 with SparseBlockCSR

use of org.apache.sysml.runtime.matrix.data.SparseBlockCSR 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);
    }
}
Also used : MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) SparseBlockMCSR(org.apache.sysml.runtime.matrix.data.SparseBlockMCSR) SparseBlockCSR(org.apache.sysml.runtime.matrix.data.SparseBlockCSR) SparseBlock(org.apache.sysml.runtime.matrix.data.SparseBlock) SparseBlockCOO(org.apache.sysml.runtime.matrix.data.SparseBlockCOO)

Example 8 with SparseBlockCSR

use of org.apache.sysml.runtime.matrix.data.SparseBlockCSR in project incubator-systemml by apache.

the class SparseBlockAlignment method runSparseBlockScanTest.

/**
	 * 
	 * @param btype
	 * @param sparsity
	 * @param positive
	 */
private void runSparseBlockScanTest(SparseBlock.Type btype, double sparsity, boolean positive) {
    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;
        }
        //init second sparse block and deep copy
        SparseBlock sblock2 = null;
        switch(btype) {
            case MCSR:
                sblock2 = new SparseBlockMCSR(sblock);
                break;
            case CSR:
                sblock2 = new SparseBlockCSR(sblock);
                break;
            case COO:
                sblock2 = new SparseBlockCOO(sblock);
                break;
        }
        //modify second block if necessary
        if (!positive) {
            sblock2.deleteIndexRange(37, 0, cols - 1);
            sblock2.deleteIndexRange(38, 0, cols - 1);
        }
        //check for block comparison
        boolean blockAligned = sblock.isAligned(sblock2);
        if (blockAligned != positive)
            Assert.fail("Wrong block alignment indicated: " + blockAligned + ", expected: " + positive);
        //check for row comparison
        boolean rowsAligned37 = true;
        boolean rowsAlignedRest = true;
        for (int i = 0; i < rows; i++) {
            if (i == 37 || i == 38)
                rowsAligned37 &= sblock.isAligned(i, sblock2);
            else if (//CSR/COO different after update pos
            i < 37)
                rowsAlignedRest &= sblock.isAligned(i, sblock2);
        }
        if (rowsAligned37 != positive)
            Assert.fail("Wrong row alignment indicated: " + rowsAligned37 + ", expected: " + positive);
        if (!rowsAlignedRest)
            Assert.fail("Wrong row alignment rest indicated: false.");
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new RuntimeException(ex);
    }
}
Also used : MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) SparseBlockMCSR(org.apache.sysml.runtime.matrix.data.SparseBlockMCSR) SparseBlockCSR(org.apache.sysml.runtime.matrix.data.SparseBlockCSR) SparseBlock(org.apache.sysml.runtime.matrix.data.SparseBlock) SparseBlockCOO(org.apache.sysml.runtime.matrix.data.SparseBlockCOO)

Example 9 with SparseBlockCSR

use of org.apache.sysml.runtime.matrix.data.SparseBlockCSR in project incubator-systemml by apache.

the class GPUObject method copyFromHostToDevice.

void copyFromHostToDevice() throws DMLRuntimeException {
    LOG.trace("GPU : copyFromHostToDevice, on " + this + ", GPUContext=" + getGPUContext());
    long start = 0;
    if (DMLScript.STATISTICS)
        start = System.nanoTime();
    MatrixBlock tmp = mat.acquireRead();
    if (tmp.isInSparseFormat()) {
        int[] rowPtr = null;
        int[] colInd = null;
        double[] values = null;
        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.addAndGet(System.nanoTime() - t0);
                if (DMLScript.STATISTICS)
                    GPUStatistics.cudaSparseConversionCount.incrementAndGet();
            } 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.addAndGet(System.nanoTime() - t0);
                if (DMLScript.STATISTICS)
                    GPUStatistics.cudaSparseConversionCount.incrementAndGet();
            } else {
                throw new DMLRuntimeException("Unsupported sparse matrix format for CUDA operations");
            }
            rowPtr = csrBlock.rowPointers();
            colInd = csrBlock.indexes();
            values = csrBlock.values();
        }
        allocateSparseMatrixOnDevice();
        if (copyToDevice) {
            CSRPointer.copyToDevice(getJcudaSparseMatrixPtr(), tmp.getNumRows(), tmp.getNonZeros(), rowPtr, colInd, values);
        }
    } else {
        double[] data = tmp.getDenseBlock();
        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");
        else if (tmp.getNonZeros() == 0)
            data = new double[tmp.getNumRows() * tmp.getNumColumns()];
        // Copy dense block
        allocateDenseMatrixOnDevice();
        cudaMemcpy(getJcudaDenseMatrixPtr(), Pointer.to(data), getDoubleSizeOf(mat.getNumRows() * mat.getNumColumns()), cudaMemcpyHostToDevice);
    }
    mat.release();
    if (DMLScript.STATISTICS)
        GPUStatistics.cudaToDevTime.addAndGet(System.nanoTime() - start);
    if (DMLScript.STATISTICS)
        GPUStatistics.cudaToDevCount.addAndGet(1);
}
Also used : MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) SparseBlockMCSR(org.apache.sysml.runtime.matrix.data.SparseBlockMCSR) SparseBlockCSR(org.apache.sysml.runtime.matrix.data.SparseBlockCSR) SparseBlock(org.apache.sysml.runtime.matrix.data.SparseBlock) SparseBlockCOO(org.apache.sysml.runtime.matrix.data.SparseBlockCOO) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 10 with SparseBlockCSR

use of org.apache.sysml.runtime.matrix.data.SparseBlockCSR 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]);
            for (//random hash order
            LLDoubleEntry e : //random hash order
            map.extractValues()) sblock.append((int) e.key1, (int) e.key2, 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);
    }
}
Also used : SparseBlockMCSR(org.apache.sysml.runtime.matrix.data.SparseBlockMCSR) LongLongDoubleHashMap(org.apache.sysml.runtime.util.LongLongDoubleHashMap) SparseBlockCSR(org.apache.sysml.runtime.matrix.data.SparseBlockCSR) SparseBlock(org.apache.sysml.runtime.matrix.data.SparseBlock) SparseBlockCOO(org.apache.sysml.runtime.matrix.data.SparseBlockCOO) LLDoubleEntry(org.apache.sysml.runtime.util.LongLongDoubleHashMap.LLDoubleEntry)

Aggregations

SparseBlockCSR (org.apache.sysml.runtime.matrix.data.SparseBlockCSR)11 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)10 SparseBlock (org.apache.sysml.runtime.matrix.data.SparseBlock)10 SparseBlockCOO (org.apache.sysml.runtime.matrix.data.SparseBlockCOO)10 SparseBlockMCSR (org.apache.sysml.runtime.matrix.data.SparseBlockMCSR)10 IJV (org.apache.sysml.runtime.matrix.data.IJV)3 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)2 LongLongDoubleHashMap (org.apache.sysml.runtime.util.LongLongDoubleHashMap)2 LLDoubleEntry (org.apache.sysml.runtime.util.LongLongDoubleHashMap.LLDoubleEntry)2