def _matmul_mkl(sp_ref_a, sp_ref_b):
    """
    Dot product two MKL objects together and return a handle to the result

    :param sp_ref_a: Sparse matrix A handle
    :type sp_ref_a: sparse_matrix_t
    :param sp_ref_b: Sparse matrix B handle
    :param sp_ref_b: sparse_matrix_t
    :return: Sparse matrix handle that is the dot product A * B
    :rtype: sparse_matrix_t
    """

    ref_handle = sparse_matrix_t()

    ret_val = MKL._mkl_sparse_spmm(_ctypes.c_int(10), sp_ref_a, sp_ref_b,
                                   _ctypes.byref(ref_handle))

    # Check return
    if ret_val != 0:
        _err_msg = "mkl_sparse_spmm returned {v} ({e})".format(
            v=ret_val, e=RETURN_CODES[ret_val])
        if ret_val == 2:
            _err_msg += "; Try changing MKL to int64 with the environment variable MKL_INTERFACE_LAYER=ILP64"
        raise ValueError(_err_msg)

    return ref_handle
Exemple #2
0
    def test_empty_handle(self):
        mkl_handle_empty = sparse_matrix_t()

        with self.assertRaises(ValueError):
            _export_mkl(mkl_handle_empty, True, output_type="csr")

        with self.assertRaises(ValueError):
            _convert_to_csr(mkl_handle_empty)

        with self.assertRaises(ValueError):
            _order_mkl_handle(mkl_handle_empty)

        with self.assertRaises(ValueError):
            _destroy_mkl_handle(mkl_handle_empty)
Exemple #3
0
def _gram_matrix_sparse(matrix_a, aat=False, reorder_output=False):
    """
    Calculate the gram matrix aTa for sparse matrix and return a sparse matrix

    :param matrix_a: Sparse matrix
    :type matrix_a: scipy.sparse.csr_matrix, scipy.sparse.csc_matrix
    :param aat: Return A (dot) AT instead of AT (dot) A
    :type aat: bool
    :param reorder_output:
    :type reorder_output: bool
    :return: Sparse matrix
    :rtype: scipy.sparse.csr_matrix
    """

    sp_ref_a, double_prec = _create_mkl_sparse(matrix_a)

    if _sps.isspmatrix_csc(matrix_a):
        sp_ref_a = _convert_to_csr(sp_ref_a)

    _order_mkl_handle(sp_ref_a)

    ref_handle = sparse_matrix_t()

    ret_val = MKL._mkl_sparse_syrk(10 if aat else 11, sp_ref_a,
                                   _ctypes.byref(ref_handle))

    # Check return
    if ret_val != 0:
        _err_msg = "mkl_sparse_syrk returned {v} ({e})".format(
            v=ret_val, e=RETURN_CODES[ret_val])
        if ret_val == 2:
            _err_msg += "; Try changing MKL to int64 with the environment variable MKL_INTERFACE_LAYER=ILP64"
        raise ValueError(_err_msg)

    if reorder_output:
        _order_mkl_handle(ref_handle)

    output_arr = _export_mkl(ref_handle, double_prec, output_type="csr")

    _destroy_mkl_handle(sp_ref_a)
    _destroy_mkl_handle(ref_handle)

    return output_arr
def _syrk_mkl(sp_ref_a):
    """
    Dot product an MKL object with its transpose and return a handle to the result

    :param sp_ref_a: Sparse matrix A handle
    :type sp_ref_a: sparse_matrix_t
    :return: Sparse matrix handle that is the dot product A * A.T
    :rtype: sparse_matrix_t
    """

    ref_handle = sparse_matrix_t()

    ret_val = MKL._mkl_sparse_syrk(_ctypes.c_int(10), sp_ref_a,
                                   _ctypes.byref(ref_handle))

    # Check return
    if ret_val != 0:
        raise ValueError("mkl_sparse_spmm returned {v} ({e})".format(
            v=ret_val, e=RETURN_CODES[ret_val]))

    return ref_handle
def _matmul_mkl(sp_ref_a, sp_ref_b):
    """
    Dot product two MKL objects together and return a handle to the result

    :param sp_ref_a: Sparse matrix A handle
    :type sp_ref_a: sparse_matrix_t
    :param sp_ref_b: Sparse matrix B handle
    :param sp_ref_b: sparse_matrix_t
    :return: Sparse matrix handle that is the dot product A * B
    :rtype: sparse_matrix_t
    """

    ref_handle = sparse_matrix_t()

    ret_val = MKL._mkl_sparse_spmm(_ctypes.c_int(10), sp_ref_a, sp_ref_b,
                                   _ctypes.byref(ref_handle))

    # Check return
    if ret_val != 0:
        raise ValueError("mkl_sparse_spmm returned {v} ({e})".format(
            v=ret_val, e=RETURN_CODES[ret_val]))

    return ref_handle
Exemple #6
0
 def test_spmm_error_bad_handle(self):
     with self.assertRaises(ValueError):
         _matmul_mkl(sparse_matrix_t(), sparse_matrix_t())