cuda_cffi/cusparse.py
def csrmm2(m, n, k, descrA, csrValA, csrRowPtrA, csrColIndA, B, handle=None,
           C=None, nnz=None, transA=CUSPARSE_OPERATION_NON_TRANSPOSE,
           transB=CUSPARSE_OPERATION_NON_TRANSPOSE, alpha=1.0, beta=0.0,
           ldb=None, ldc=None, check_inputs=True):
    """ multiply two sparse matrices:  C = transA(A) * transB(B)

    higher level wrapper to cusparse<t>csrmm2 routines.
    """
    if check_inputs:
        for item in [csrValA, csrRowPtrA, csrColIndA, B]:
            if not isinstance(item, pycuda.gpuarray.GPUArray):
                raise ValueError("csr*, B, must be pyCUDA gpuarrays")
        if C is not None:
            if not isinstance(C, pycuda.gpuarray.GPUArray):
                raise ValueError("C must be a pyCUDA gpuarray or None")
        # dense matrices must be in column-major order
        if not B.flags.f_contiguous:
            raise ValueError("Dense matrix B must be column-major order")

        if transB == CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE:
            raise ValueError("Conjugate transpose operation not supported "
                             "for dense matrix B")

        if (transB == CUSPARSE_OPERATION_TRANSPOSE) and \
           (transA != CUSPARSE_OPERATION_NON_TRANSPOSE):
            raise ValueError("if B is transposed, only A non-transpose is "
                             "supported")
    if handle is None:
        handle = misc._global_cusparse_handle

    dtype = csrValA.dtype
    if C is None:
        if transA == CUSPARSE_OPERATION_NON_TRANSPOSE:
            ldc = m
        else:
            ldc = k
        alloc = misc._global_cusparse_allocator
        C = gpuarray.zeros((ldc, n), dtype=dtype, order='F',
                           allocator=alloc)
    elif not C.flags.f_contiguous:
        raise ValueError("Dense matrix C must be in column-major order")
    if nnz is None:
        nnz = csrValA.size
    if ldb is None:
        ldb = B.shape[0]
    if ldc is None:
        ldc = C.shape[0]

    # perform some basic sanity checks
    if check_inputs:
        if csrValA.size != nnz:
            raise ValueError("length of csrValA array must match nnz")
        if (B.dtype != dtype) or (C.dtype != dtype):
            raise ValueError("A, B, C must share a common dtype")
        if ldb < B.shape[0]:
            raise ValueError("ldb invalid for matrix B")
        if transA == CUSPARSE_OPERATION_NON_TRANSPOSE:
            ldOpA = m  # leading dimension for op(A)
            tdOpA = k  # trailing dimension for op(A)
        else:
            ldOpA = k
            tdOpA = m
        if transB == CUSPARSE_OPERATION_NON_TRANSPOSE:
            if B.shape[1] != n:
                raise ValueError("B, n incompatible")
            if (ldb < tdOpA):
                raise ValueError("size of A incompatible with B")
        else:
            if ldb < n:
                raise ValueError("B, n incompatible")
            if (B.shape[1] != tdOpA):
                raise ValueError("size of A incompatible with B")
        if (C.shape[1] != n):
            raise ValueError("bad shape for C")
        if (ldc != ldOpA):
            raise ValueError("size of A incompatible with C")
        if csrRowPtrA.size != m+1:
            raise ValueError("length of csrRowPtrA invalid")
    if dtype == np.float32:
        fn = cusparseScsrmm2
    elif dtype == np.float64:
        fn = cusparseDcsrmm2
    elif dtype == np.complex64:
        fn = cusparseCcsrmm2
    elif dtype == np.complex128:
        fn = cusparseZcsrmm2
    else:
        raise ValueError("unsupported sparse matrix dtype: %s" % dtype)
    transa = transA
    transb = transB
    try:
        fn(handle, transa, transb, m, n, k, nnz, alpha, descrA, csrValA,
           csrRowPtrA, csrColIndA, B, ldb, beta, C, ldc)
    except CUSPARSE_STATUS_INVALID_VALUE as e:
        print("m={}, n={}, k={}, nnz={}, ldb={}, ldc={}".format(
            m, n, k, nnz, ldb, ldc))
        raise(e)
    return C
 multiply two sparse matrices:  C = transA(A) * transB(B)

    higher level wrapper to cusparse<t>csrmm2 routines.
    
multiply two sparse matrices:  C = transA(A) * transB(B)
higher level wrapper to cusparsecsrmm2 routines.
multiply two sparse matrices : C = transA ( A ) * transB ( B ) higher level wrapper to cusparsecsrmm2 routines .
def csrmm2(m, n, k, descrA, csrValA, csrRowPtrA, csrColIndA, B, handle=None,
           C=None, nnz=None, transA=CUSPARSE_OPERATION_NON_TRANSPOSE,
           transB=CUSPARSE_OPERATION_NON_TRANSPOSE, alpha=1.0, beta=0.0,
           ldb=None, ldc=None, check_inputs=True):
    if check_inputs:
        for item in [csrValA, csrRowPtrA, csrColIndA, B]:
            if not isinstance(item, pycuda.gpuarray.GPUArray):
                raise ValueError("csr*, B, must be pyCUDA gpuarrays")
        if C is not None:
            if not isinstance(C, pycuda.gpuarray.GPUArray):
                raise ValueError("C must be a pyCUDA gpuarray or None")
        if not B.flags.f_contiguous:
            raise ValueError("Dense matrix B must be column-major order")
        if transB == CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE:
            raise ValueError("Conjugate transpose operation not supported "
                             "for dense matrix B")
        if (transB == CUSPARSE_OPERATION_TRANSPOSE) and \
           (transA != CUSPARSE_OPERATION_NON_TRANSPOSE):
            raise ValueError("if B is transposed, only A non-transpose is "
                             "supported")
    if handle is None:
        handle = misc._global_cusparse_handle
    dtype = csrValA.dtype
    if C is None:
        if transA == CUSPARSE_OPERATION_NON_TRANSPOSE:
            ldc = m
        else:
            ldc = k
        alloc = misc._global_cusparse_allocator
        C = gpuarray.zeros((ldc, n), dtype=dtype, order='F',
                           allocator=alloc)
    elif not C.flags.f_contiguous:
        raise ValueError("Dense matrix C must be in column-major order")
    if nnz is None:
        nnz = csrValA.size
    if ldb is None:
        ldb = B.shape[0]
    if ldc is None:
        ldc = C.shape[0]
    if check_inputs:
        if csrValA.size != nnz:
            raise ValueError("length of csrValA array must match nnz")
        if (B.dtype != dtype) or (C.dtype != dtype):
            raise ValueError("A, B, C must share a common dtype")
        if ldb < B.shape[0]:
            raise ValueError("ldb invalid for matrix B")
        if transA == CUSPARSE_OPERATION_NON_TRANSPOSE:
            ldOpA = m  
            tdOpA = k  
        else:
            ldOpA = k
            tdOpA = m
        if transB == CUSPARSE_OPERATION_NON_TRANSPOSE:
            if B.shape[1] != n:
                raise ValueError("B, n incompatible")
            if (ldb < tdOpA):
                raise ValueError("size of A incompatible with B")
        else:
            if ldb < n:
                raise ValueError("B, n incompatible")
            if (B.shape[1] != tdOpA):
                raise ValueError("size of A incompatible with B")
        if (C.shape[1] != n):
            raise ValueError("bad shape for C")
        if (ldc != ldOpA):
            raise ValueError("size of A incompatible with C")
        if csrRowPtrA.size != m+1:
            raise ValueError("length of csrRowPtrA invalid")
    if dtype == np.float32:
        fn = cusparseScsrmm2
    elif dtype == np.float64:
        fn = cusparseDcsrmm2
    elif dtype == np.complex64:
        fn = cusparseCcsrmm2
    elif dtype == np.complex128:
        fn = cusparseZcsrmm2
    else:
        raise ValueError("unsupported sparse matrix dtype: %s" % dtype)
    transa = transA
    transb = transB
    try:
        fn(handle, transa, transb, m, n, k, nnz, alpha, descrA, csrValA,
           csrRowPtrA, csrColIndA, B, ldb, beta, C, ldc)
    except CUSPARSE_STATUS_INVALID_VALUE as e:
        print("m={}, n={}, k={}, nnz={}, ldb={}, ldc={}".format(
            m, n, k, nnz, ldb, ldc))
        raise(e)
    return C
def csrmm2 ( m , n , k , descrA , csrValA , csrRowPtrA , csrColIndA , B , handle = None , C = None , nnz = None , transA = CUSPARSE_OPERATION_NON_TRANSPOSE , transB = CUSPARSE_OPERATION_NON_TRANSPOSE , alpha = 1.0 , beta = 0.0 , ldb = None , ldc = None , check_inputs = True ) : if check_inputs : for item in [ csrValA , csrRowPtrA , csrColIndA , B ] : if not isinstance ( item , pycuda . gpuarray . GPUArray ) : raise ValueError ( "csr*, B, must be pyCUDA gpuarrays" ) if C is not None : if not isinstance ( C , pycuda . gpuarray . GPUArray ) : raise ValueError ( "C must be a pyCUDA gpuarray or None" ) if not B . flags . f_contiguous : raise ValueError ( "Dense matrix B must be column-major order" ) if transB == CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE : raise ValueError ( "Conjugate transpose operation not supported " "for dense matrix B" ) if ( transB == CUSPARSE_OPERATION_TRANSPOSE ) and ( transA != CUSPARSE_OPERATION_NON_TRANSPOSE ) : raise ValueError ( "if B is transposed, only A non-transpose is " "supported" ) if handle is None : handle = misc . _global_cusparse_handle dtype = csrValA . dtype if C is None : if transA == CUSPARSE_OPERATION_NON_TRANSPOSE : ldc = m else : ldc = k alloc = misc . _global_cusparse_allocator C = gpuarray . zeros ( ( ldc , n ) , dtype = dtype , order = 'F' , allocator = alloc ) elif not C . flags . f_contiguous : raise ValueError ( "Dense matrix C must be in column-major order" ) if nnz is None : nnz = csrValA . size if ldb is None : ldb = B . shape [ 0 ] if ldc is None : ldc = C . shape [ 0 ] if check_inputs : if csrValA . size != nnz : raise ValueError ( "length of csrValA array must match nnz" ) if ( B . dtype != dtype ) or ( C . dtype != dtype ) : raise ValueError ( "A, B, C must share a common dtype" ) if ldb < B . shape [ 0 ] : raise ValueError ( "ldb invalid for matrix B" ) if transA == CUSPARSE_OPERATION_NON_TRANSPOSE : ldOpA = m tdOpA = k else : ldOpA = k tdOpA = m if transB == CUSPARSE_OPERATION_NON_TRANSPOSE : if B . shape [ 1 ] != n : raise ValueError ( "B, n incompatible" ) if ( ldb < tdOpA ) : raise ValueError ( "size of A incompatible with B" ) else : if ldb < n : raise ValueError ( "B, n incompatible" ) if ( B . shape [ 1 ] != tdOpA ) : raise ValueError ( "size of A incompatible with B" ) if ( C . shape [ 1 ] != n ) : raise ValueError ( "bad shape for C" ) if ( ldc != ldOpA ) : raise ValueError ( "size of A incompatible with C" ) if csrRowPtrA . size != m + 1 : raise ValueError ( "length of csrRowPtrA invalid" ) if dtype == np . float32 : fn = cusparseScsrmm2 elif dtype == np . float64 : fn = cusparseDcsrmm2 elif dtype == np . complex64 : fn = cusparseCcsrmm2 elif dtype == np . complex128 : fn = cusparseZcsrmm2 else : raise ValueError ( "unsupported sparse matrix dtype: %s" % dtype ) transa = transA transb = transB try : fn ( handle , transa , transb , m , n , k , nnz , alpha , descrA , csrValA , csrRowPtrA , csrColIndA , B , ldb , beta , C , ldc ) except CUSPARSE_STATUS_INVALID_VALUE as e : print ( "m={}, n={}, k={}, nnz={}, ldb={}, ldc={}" . format ( m , n , k , nnz , ldb , ldc ) ) raise ( e ) return C
multiply two sparse matrices:  C = transA(A) * transB(B)
higher level wrapper to cusparse<t>csrmm2 routines.
multiply two sparse matrices : C = transA ( A ) * transB ( B ) higher level wrapper to cusparse<t > csrmm2 routines .
""" multiply two sparse matrices:  C = transA(A) * transB(B)

    higher level wrapper to cusparse<t>csrmm2 routines.
    """ # dense matrices must be in column-major order # perform some basic sanity checks # leading dimension for op(A) # trailing dimension for op(A)
m
n
k
descrA
csrValA
csrRowPtrA
csrColIndA
B
handle
C
nnz
transA
transB
alpha
beta
ldb
ldc
check_inputs