Example #1
0
    def test_export_mkl_bad_type(self):
        mkl_handle, dbl = _create_mkl_sparse(self.mat1)

        with self.assertRaises(ValueError):
            _export_mkl(mkl_handle, dbl, output_type="coo")

        _destroy_mkl_handle(mkl_handle)
Example #2
0
    def test_create_export(self):
        mat1 = _spsparse.csc_matrix(self.mat1).copy()
        mat2 = self.mat2.copy()
        mat3 = mat1.astype(np.float32).copy()
        mat4 = self.mat2.astype(np.float32).copy()

        ref_1, precision_1 = _create_mkl_sparse(mat1)
        ref_2, precision_2 = _create_mkl_sparse(mat2)
        ref_3, precision_3 = _create_mkl_sparse(mat3)
        ref_4, precision_4 = _create_mkl_sparse(mat4)

        self.assertTrue(precision_1)
        self.assertTrue(precision_2)
        self.assertFalse(precision_3)
        self.assertFalse(precision_4)

        cycle_1 = _export_mkl(ref_1, precision_1, output_type="csc")
        cycle_2 = _export_mkl(ref_2, precision_2)
        cycle_3 = _export_mkl(ref_3, precision_3, output_type="csc")
        cycle_4 = _export_mkl(ref_4, precision_4)

        self.is_sparse_identical_A(self.mat1, cycle_1)
        self.is_sparse_identical_internal(self.mat2, cycle_2)
        self.is_sparse_identical_A(self.mat1.astype(np.float32), cycle_3)
        self.is_sparse_identical_internal(self.mat2.astype(np.float32),
                                          cycle_4)
Example #3
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)
Example #4
0
    def test_spmm_success(self):
        ref_1, precision_1 = _create_mkl_sparse(self.mat1)
        ref_2, precision_2 = _create_mkl_sparse(self.mat2)

        self.assertTrue(precision_1)
        self.assertTrue(precision_2)

        ref_3 = _matmul_mkl(ref_1, ref_2)
        mat3 = _export_mkl(ref_3, precision_1 or precision_2)

        mat3_sp = self.mat1.dot(self.mat2)
        mat3_np = np.dot(self.mat1.A, self.mat2.A)

        npt.assert_array_almost_equal(mat3.A, mat3_sp.A)
        npt.assert_array_almost_equal(mat3_np, mat3.A)
Example #5
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
Example #6
0
    def test_spmm_success_float32(self):
        self.mat1.data = self.mat1.data.astype(np.float32)
        self.mat2.data = self.mat2.data.astype(np.float32)

        ref_1, precision_1 = _create_mkl_sparse(self.mat1)
        ref_2, precision_2 = _create_mkl_sparse(self.mat2)

        self.assertFalse(precision_1)
        self.assertFalse(precision_2)

        ref_3 = _matmul_mkl(ref_1, ref_2)
        mat3 = _export_mkl(ref_3,
                           precision_1 or precision_2,
                           output_type="csr")

        mat3_sp = self.mat1.dot(self.mat2)
        mat3_np = np.dot(self.mat1.A, self.mat2.A)

        npt.assert_array_almost_equal(mat3.A, mat3_sp.A)
        npt.assert_array_almost_equal(mat3_np, mat3.A)
def _sparse_dot_sparse(matrix_a,
                       matrix_b,
                       cast=False,
                       reorder_output=False,
                       dense=False,
                       dprint=print):
    """
    Multiply together two scipy sparse matrixes using the intel Math Kernel Library.
    This currently only supports float32 and float64 data

    :param matrix_a: Sparse matrix A in CSC/CSR format
    :type matrix_a: scipy.sparse.spmatrix
    :param matrix_b: Sparse matrix B in CSC/CSR format
    :type matrix_b: scipy.sparse.spmatrix
    :param cast: Should the data be coerced into float64 if it isn't float32 or float64
    If set to True and any other dtype is passed, the matrix data will be modified in-place
    If set to False and any dtype that isn't float32 or float64 is passed, a ValueError will be raised
    Defaults to False
    :param reorder_output: Should the array indices be reordered using MKL
    If set to True, the object in C will be ordered and then exported into python
    If set to False, the array column indices will not be ordered.
    The scipy sparse dot product does not yield ordered column indices so this defaults to False
    :type reorder_output: bool
    :param dense: Should the matrix multiplication yield a dense numpy array
    This does not require any copy and is memory efficient if the output array density is > 50%
    :type dense: bool
    :param dprint: Should debug and timing messages be printed. Defaults to false.
    :type dprint: function
    :return: Sparse matrix that is the result of A * B in CSR format
    :rtype: scipy.sparse.csr_matrix
    """

    # Check for allowed sparse matrix types

    if is_csr(matrix_a) and (is_csc(matrix_b) or is_csr(matrix_b)):
        default_output = _spsparse.csr_matrix
        output_type = "csr"
    elif is_csc(matrix_a) and (is_csc(matrix_b) or is_csr(matrix_b)):
        default_output = _spsparse.csc_matrix
        output_type = "csc"
    else:
        raise ValueError(
            "Both input matrices to dot_product_mkl must be CSR or CSC; COO and BSR are not supported"
        )

    # Override output if dense flag is set
    default_output = default_output if not dense else np.zeros

    # Check to make sure that this multiplication can work and check dtypes
    _sanity_check(matrix_a, matrix_b)

    # Check for edge condition inputs which result in empty outputs
    if _empty_output_check(matrix_a, matrix_b):
        dprint(
            "Skipping multiplication because A (dot) B must yield an empty matrix"
        )
        final_dtype = np.float64 if matrix_a.dtype != matrix_b.dtype or matrix_a.dtype != np.float32 else np.float32
        return default_output((matrix_a.shape[0], matrix_b.shape[1]),
                              dtype=final_dtype)

    # Check dtypes
    matrix_a, matrix_b = _type_check(matrix_a,
                                     matrix_b,
                                     cast=cast,
                                     dprint=dprint)

    t0 = time.time()

    # Create intel MKL objects
    mkl_a, a_dbl = _create_mkl_sparse(matrix_a)
    mkl_b, b_dbl = _create_mkl_sparse(matrix_b)

    t1 = time.time()
    dprint("Created MKL sparse handles: {0:.6f} seconds".format(t1 - t0))

    # Call spmmd for dense output directly if the dense flag is set
    if dense:
        dense_arr = _matmul_mkl_dense(mkl_a, mkl_b,
                                      (matrix_a.shape[0], matrix_b.shape[1]),
                                      a_dbl or b_dbl)

        t2 = time.time()
        dprint("Multiplied matrices: {0:.6f} seconds".format(t2 - t1))

        _destroy_mkl_handle(mkl_a)
        _destroy_mkl_handle(mkl_b)

        return dense_arr

    # Call spmm for sparse output if the dense flag is not set and then export the sparse matrix to python
    else:
        # Dot product
        mkl_c = _matmul_mkl(mkl_a, mkl_b)

        _destroy_mkl_handle(mkl_a)
        _destroy_mkl_handle(mkl_b)

        t2 = time.time()
        dprint("Multiplied matrices: {0:.6f} seconds".format(t2 - t1))

        # Reorder
        if reorder_output:
            _order_mkl_handle(mkl_c)

            dprint("Reordered indicies: {0:.6f} seconds".format(time.time() -
                                                                t2))
            t2 = time.time()

        # Extract
        python_c = _export_mkl(mkl_c, a_dbl or b_dbl, output_type=output_type)
        _destroy_mkl_handle(mkl_c)

        dprint("Created python handle: {0:.6f} seconds".format(time.time() -
                                                               t2))

        return python_c
Example #8
0
def _sparse_dot_transpose(matrix_a,
                          cast=False,
                          reorder_output=False,
                          dprint=print):
    """
    Multiply together two scipy sparse matrixes using the intel Math Kernel Library.
    This currently only supports float32 and float64 data

    :param matrix_a: Sparse matrix A in CSC/CSR format
    :type matrix_a: scipy.sparse.spmatrix
    :param cast: Should the data be coerced into float64 if it isn't float32 or float64
    If set to True and any other dtype is passed, the matrix data will be modified in-place
    If set to False and any dtype that isn't float32 or float64 is passed, a ValueError will be raised
    Defaults to False
    :param reorder_output: Should the array indices be reordered using MKL
    If set to True, the object in C will be ordered and then exported into python
    If set to False, the array column indices will not be ordered.
    The scipy sparse dot product does not yield ordered column indices so this defaults to False
    :type reorder_output: bool
    :param dprint: Should debug and timing messages be printed. Defaults to false.
    :type dprint: function
    :return: Sparse matrix that is the result of A * B in CSR format
    :rtype: scipy.sparse.csr_matrix
    """

    # Check for allowed sparse matrix types

    if is_csr(matrix_a):
        default_output = _spsparse.csr_matrix
        output_type = "csr"
    elif is_csc(matrix_a):
        default_output = _spsparse.csc_matrix
        output_type = "csc"
    else:
        raise ValueError(
            "Input matrix to dot_product_transpose_mkl must be CSR or CSC; COO and BSR are not supported"
        )

    # Check for edge condition inputs which result in empty outputs
    if _empty_output_check(matrix_a, matrix_a.T):
        dprint(
            "Skipping multiplication because A (dot) A.T must yield an empty matrix"
        )
        return default_output((matrix_a.shape[0], matrix_a.shape[1]),
                              dtype=matrix_a.dtype)

    t0 = time.time()

    # Create intel MKL objects
    mkl_a, a_dbl = _create_mkl_sparse(matrix_a)

    t1 = time.time()
    dprint("Created MKL sparse handle: {0:.6f} seconds".format(t1 - t0))

    # Dot product
    mkl_c = _syrk_mkl(mkl_a)

    _destroy_mkl_handle(mkl_a)

    t2 = time.time()
    dprint("Multiplied matrices: {0:.6f} seconds".format(t2 - t1))

    # Reorder
    if reorder_output:
        _order_mkl_handle(mkl_c)

        dprint("Reordered indicies: {0:.6f} seconds".format(time.time() - t2))
        t2 = time.time()

    # Extract
    python_c = _export_mkl(mkl_c, a_dbl, output_type=output_type)
    _destroy_mkl_handle(mkl_c)

    dprint("Created python handle: {0:.6f} seconds".format(time.time() - t2))

    return python_c