Skip to content

Commit

Permalink
Remove redundant namespace in MKL backends
Browse files Browse the repository at this point in the history
  • Loading branch information
Rbiessy committed Sep 20, 2024
1 parent 84565a9 commit 44dc73d
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 190 deletions.
56 changes: 23 additions & 33 deletions src/sparse_blas/backends/mkl_common/mkl_handles.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -17,69 +17,61 @@
*
**************************************************************************/

// In this file functions and types using the namespace oneapi::mkl::sparse:: refer to the backend's namespace for better readability.

// Dense vector
template <typename fpType>
void init_dense_vector(sycl::queue & /*queue*/,
oneapi::mkl::sparse::dense_vector_handle_t *p_dvhandle, std::int64_t size,
sycl::buffer<fpType, 1> val) {
*p_dvhandle = new oneapi::mkl::sparse::dense_vector_handle(val, size);
void init_dense_vector(sycl::queue & /*queue*/, dense_vector_handle_t *p_dvhandle,
std::int64_t size, sycl::buffer<fpType, 1> val) {
*p_dvhandle = new dense_vector_handle(val, size);
}

template <typename fpType>
void init_dense_vector(sycl::queue & /*queue*/,
oneapi::mkl::sparse::dense_vector_handle_t *p_dvhandle, std::int64_t size,
fpType *val) {
*p_dvhandle = new oneapi::mkl::sparse::dense_vector_handle(val, size);
void init_dense_vector(sycl::queue & /*queue*/, dense_vector_handle_t *p_dvhandle,
std::int64_t size, fpType *val) {
*p_dvhandle = new dense_vector_handle(val, size);
}

template <typename fpType>
void set_dense_vector_data(sycl::queue & /*queue*/,
oneapi::mkl::sparse::dense_vector_handle_t dvhandle, std::int64_t size,
sycl::buffer<fpType, 1> val) {
void set_dense_vector_data(sycl::queue & /*queue*/, dense_vector_handle_t dvhandle,
std::int64_t size, sycl::buffer<fpType, 1> val) {
detail::check_can_reset_value_handle<fpType>(__func__, dvhandle, true);
dvhandle->size = size;
dvhandle->set_buffer(val);
}

template <typename fpType>
void set_dense_vector_data(sycl::queue & /*queue*/,
oneapi::mkl::sparse::dense_vector_handle_t dvhandle, std::int64_t size,
fpType *val) {
void set_dense_vector_data(sycl::queue & /*queue*/, dense_vector_handle_t dvhandle,
std::int64_t size, fpType *val) {
detail::check_can_reset_value_handle<fpType>(__func__, dvhandle, false);
dvhandle->size = size;
dvhandle->set_usm_ptr(val);
}

FOR_EACH_FP_TYPE(INSTANTIATE_DENSE_VECTOR_FUNCS);

sycl::event release_dense_vector(sycl::queue &queue,
oneapi::mkl::sparse::dense_vector_handle_t dvhandle,
sycl::event release_dense_vector(sycl::queue &queue, dense_vector_handle_t dvhandle,
const std::vector<sycl::event> &dependencies) {
return detail::submit_release(queue, dvhandle, dependencies);
}

// Dense matrix
template <typename fpType>
void init_dense_matrix(sycl::queue & /*queue*/,
oneapi::mkl::sparse::dense_matrix_handle_t *p_dmhandle,
void init_dense_matrix(sycl::queue & /*queue*/, dense_matrix_handle_t *p_dmhandle,
std::int64_t num_rows, std::int64_t num_cols, std::int64_t ld,
oneapi::mkl::layout dense_layout, sycl::buffer<fpType, 1> val) {
*p_dmhandle =
new oneapi::mkl::sparse::dense_matrix_handle(val, num_rows, num_cols, ld, dense_layout);
*p_dmhandle = new dense_matrix_handle(val, num_rows, num_cols, ld, dense_layout);
}

template <typename fpType>
void init_dense_matrix(sycl::queue & /*queue*/,
oneapi::mkl::sparse::dense_matrix_handle_t *p_dmhandle,
void init_dense_matrix(sycl::queue & /*queue*/, dense_matrix_handle_t *p_dmhandle,
std::int64_t num_rows, std::int64_t num_cols, std::int64_t ld,
oneapi::mkl::layout dense_layout, fpType *val) {
*p_dmhandle =
new oneapi::mkl::sparse::dense_matrix_handle(val, num_rows, num_cols, ld, dense_layout);
*p_dmhandle = new dense_matrix_handle(val, num_rows, num_cols, ld, dense_layout);
}

template <typename fpType>
void set_dense_matrix_data(sycl::queue & /*queue*/,
oneapi::mkl::sparse::dense_matrix_handle_t dmhandle,
void set_dense_matrix_data(sycl::queue & /*queue*/, dense_matrix_handle_t dmhandle,
std::int64_t num_rows, std::int64_t num_cols, std::int64_t ld,
oneapi::mkl::layout dense_layout, sycl::buffer<fpType, 1> val) {
detail::check_can_reset_value_handle<fpType>(__func__, dmhandle, true);
Expand All @@ -91,8 +83,7 @@ void set_dense_matrix_data(sycl::queue & /*queue*/,
}

template <typename fpType>
void set_dense_matrix_data(sycl::queue & /*queue*/,
oneapi::mkl::sparse::dense_matrix_handle_t dmhandle,
void set_dense_matrix_data(sycl::queue & /*queue*/, dense_matrix_handle_t dmhandle,
std::int64_t num_rows, std::int64_t num_cols, std::int64_t ld,
oneapi::mkl::layout dense_layout, fpType *val) {
detail::check_can_reset_value_handle<fpType>(__func__, dmhandle, false);
Expand All @@ -105,8 +96,7 @@ void set_dense_matrix_data(sycl::queue & /*queue*/,

FOR_EACH_FP_TYPE(INSTANTIATE_DENSE_MATRIX_FUNCS);

sycl::event release_dense_matrix(sycl::queue &queue,
oneapi::mkl::sparse::dense_matrix_handle_t dmhandle,
sycl::event release_dense_matrix(sycl::queue &queue, dense_matrix_handle_t dmhandle,
const std::vector<sycl::event> &dependencies) {
return detail::submit_release(queue, dmhandle, dependencies);
}
Expand Down Expand Up @@ -286,18 +276,18 @@ sycl::event release_sparse_matrix(sycl::queue &queue, oneapi::mkl::sparse::matri
}

bool set_matrix_property(sycl::queue & /*queue*/, oneapi::mkl::sparse::matrix_handle_t smhandle,
oneapi::mkl::sparse::matrix_property property) {
matrix_property property) {
auto internal_smhandle = detail::get_internal_handle(smhandle);
// Store the matrix property internally for better error checking
internal_smhandle->set_matrix_property(property);
// Set the matrix property on the backend handle
// Backend and oneMKL interface types for the property don't match
switch (property) {
case oneapi::mkl::sparse::matrix_property::symmetric:
case matrix_property::symmetric:
oneapi::mkl::sparse::set_matrix_property(internal_smhandle->backend_handle,
oneapi::mkl::sparse::property::symmetric);
return true;
case oneapi::mkl::sparse::matrix_property::sorted:
case matrix_property::sorted:
oneapi::mkl::sparse::set_matrix_property(internal_smhandle->backend_handle,
oneapi::mkl::sparse::property::sorted);
return true;
Expand Down
92 changes: 40 additions & 52 deletions src/sparse_blas/backends/mkl_common/mkl_spmm.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,39 @@
*
**************************************************************************/

// In this file functions and types using the namespace oneapi::mkl::sparse:: refer to the backend's namespace for better readability.

namespace oneapi::mkl::sparse {

struct spmm_descr {
bool buffer_size_called = false;
bool optimized_called = false;
oneapi::mkl::transpose last_optimized_opA;
oneapi::mkl::transpose last_optimized_opB;
oneapi::mkl::sparse::matrix_view last_optimized_A_view;
oneapi::mkl::sparse::matrix_handle_t last_optimized_A_handle;
oneapi::mkl::sparse::dense_matrix_handle_t last_optimized_B_handle;
oneapi::mkl::sparse::dense_matrix_handle_t last_optimized_C_handle;
oneapi::mkl::sparse::spmm_alg last_optimized_alg;
matrix_view last_optimized_A_view;
matrix_handle_t last_optimized_A_handle;
dense_matrix_handle_t last_optimized_B_handle;
dense_matrix_handle_t last_optimized_C_handle;
spmm_alg last_optimized_alg;
};

} // namespace oneapi::mkl::sparse

namespace oneapi::mkl::sparse::BACKEND {

void init_spmm_descr(sycl::queue & /*queue*/, oneapi::mkl::sparse::spmm_descr_t *p_spmm_descr) {
void init_spmm_descr(sycl::queue & /*queue*/, spmm_descr_t *p_spmm_descr) {
*p_spmm_descr = new spmm_descr();
}

sycl::event release_spmm_descr(sycl::queue &queue, oneapi::mkl::sparse::spmm_descr_t spmm_descr,
sycl::event release_spmm_descr(sycl::queue &queue, spmm_descr_t spmm_descr,
const std::vector<sycl::event> &dependencies) {
return detail::submit_release(queue, spmm_descr, dependencies);
}

void check_valid_spmm(const std::string &function_name, oneapi::mkl::transpose opA,
oneapi::mkl::sparse::matrix_view A_view,
oneapi::mkl::sparse::matrix_handle_t A_handle,
oneapi::mkl::sparse::dense_matrix_handle_t B_handle,
oneapi::mkl::sparse::dense_matrix_handle_t C_handle,
bool is_alpha_host_accessible, bool is_beta_host_accessible) {
matrix_view A_view, matrix_handle_t A_handle, dense_matrix_handle_t B_handle,
dense_matrix_handle_t C_handle, bool is_alpha_host_accessible,
bool is_beta_host_accessible) {
auto internal_A_handle = detail::get_internal_handle(A_handle);
detail::check_valid_spmm_common(function_name, A_view, internal_A_handle, B_handle, C_handle,
is_alpha_host_accessible, is_beta_host_accessible);
Expand All @@ -59,7 +59,7 @@ void check_valid_spmm(const std::string &function_name, oneapi::mkl::transpose o
if ((data_type == detail::data_type::complex_fp32 ||
data_type == detail::data_type::complex_fp64) &&
opA == oneapi::mkl::transpose::conjtrans &&
internal_A_handle->has_matrix_property(oneapi::mkl::sparse::matrix_property::symmetric)) {
internal_A_handle->has_matrix_property(matrix_property::symmetric)) {
throw mkl::unimplemented(
"sparse_blas", function_name,
"The backend does not support spmm using conjtrans and the symmetric property.");
Expand All @@ -70,13 +70,10 @@ void check_valid_spmm(const std::string &function_name, oneapi::mkl::transpose o
}

void spmm_buffer_size(sycl::queue &queue, oneapi::mkl::transpose opA,
oneapi::mkl::transpose /*opB*/, const void *alpha,
oneapi::mkl::sparse::matrix_view A_view,
oneapi::mkl::sparse::matrix_handle_t A_handle,
oneapi::mkl::sparse::dense_matrix_handle_t B_handle, const void *beta,
oneapi::mkl::sparse::dense_matrix_handle_t C_handle,
oneapi::mkl::sparse::spmm_alg /*alg*/,
oneapi::mkl::sparse::spmm_descr_t spmm_descr, std::size_t &temp_buffer_size) {
oneapi::mkl::transpose /*opB*/, const void *alpha, matrix_view A_view,
matrix_handle_t A_handle, dense_matrix_handle_t B_handle, const void *beta,
dense_matrix_handle_t C_handle, spmm_alg /*alg*/, spmm_descr_t spmm_descr,
std::size_t &temp_buffer_size) {
// TODO: Add support for external workspace once the close-source oneMKL backend supports it.
bool is_alpha_host_accessible = detail::is_ptr_accessible_on_host(queue, alpha);
bool is_beta_host_accessible = detail::is_ptr_accessible_on_host(queue, beta);
Expand All @@ -86,12 +83,11 @@ void spmm_buffer_size(sycl::queue &queue, oneapi::mkl::transpose opA,
spmm_descr->buffer_size_called = true;
}

inline void common_spmm_optimize(
sycl::queue &queue, oneapi::mkl::transpose opA, oneapi::mkl::transpose opB, const void *alpha,
oneapi::mkl::sparse::matrix_view A_view, oneapi::mkl::sparse::matrix_handle_t A_handle,
oneapi::mkl::sparse::dense_matrix_handle_t B_handle, const void *beta,
oneapi::mkl::sparse::dense_matrix_handle_t C_handle, oneapi::mkl::sparse::spmm_alg alg,
oneapi::mkl::sparse::spmm_descr_t spmm_descr) {
inline void common_spmm_optimize(sycl::queue &queue, oneapi::mkl::transpose opA,
oneapi::mkl::transpose opB, const void *alpha, matrix_view A_view,
matrix_handle_t A_handle, dense_matrix_handle_t B_handle,
const void *beta, dense_matrix_handle_t C_handle, spmm_alg alg,
spmm_descr_t spmm_descr) {
bool is_alpha_host_accessible = detail::is_ptr_accessible_on_host(queue, alpha);
bool is_beta_host_accessible = detail::is_ptr_accessible_on_host(queue, beta);
check_valid_spmm("spmm_optimize", opA, A_view, A_handle, B_handle, C_handle,
Expand All @@ -111,41 +107,36 @@ inline void common_spmm_optimize(
}

void spmm_optimize(sycl::queue &queue, oneapi::mkl::transpose opA, oneapi::mkl::transpose opB,
const void *alpha, oneapi::mkl::sparse::matrix_view A_view,
oneapi::mkl::sparse::matrix_handle_t A_handle,
oneapi::mkl::sparse::dense_matrix_handle_t B_handle, const void *beta,
oneapi::mkl::sparse::dense_matrix_handle_t C_handle,
oneapi::mkl::sparse::spmm_alg alg, oneapi::mkl::sparse::spmm_descr_t spmm_descr,
const void *alpha, matrix_view A_view, matrix_handle_t A_handle,
dense_matrix_handle_t B_handle, const void *beta, dense_matrix_handle_t C_handle,
spmm_alg alg, spmm_descr_t spmm_descr,
sycl::buffer<std::uint8_t, 1> /*workspace*/) {
auto internal_A_handle = detail::get_internal_handle(A_handle);
if (!internal_A_handle->all_use_buffer()) {
detail::throw_incompatible_container(__func__);
}
common_spmm_optimize(queue, opA, opB, alpha, A_view, A_handle, B_handle, beta, C_handle, alg,
spmm_descr);
if (alg == oneapi::mkl::sparse::spmm_alg::no_optimize_alg) {
if (alg == spmm_alg::no_optimize_alg) {
return;
}
internal_A_handle->can_be_reset = false;
// TODO: Add support for spmm_optimize once the close-source oneMKL backend supports it.
}

sycl::event spmm_optimize(sycl::queue &queue, oneapi::mkl::transpose opA,
oneapi::mkl::transpose opB, const void *alpha,
oneapi::mkl::sparse::matrix_view A_view,
oneapi::mkl::sparse::matrix_handle_t A_handle,
oneapi::mkl::sparse::dense_matrix_handle_t B_handle, const void *beta,
oneapi::mkl::sparse::dense_matrix_handle_t C_handle,
oneapi::mkl::sparse::spmm_alg alg,
oneapi::mkl::sparse::spmm_descr_t spmm_descr, void * /*workspace*/,
oneapi::mkl::transpose opB, const void *alpha, matrix_view A_view,
matrix_handle_t A_handle, dense_matrix_handle_t B_handle,
const void *beta, dense_matrix_handle_t C_handle, spmm_alg alg,
spmm_descr_t spmm_descr, void * /*workspace*/,
const std::vector<sycl::event> &dependencies) {
auto internal_A_handle = detail::get_internal_handle(A_handle);
if (internal_A_handle->all_use_buffer()) {
detail::throw_incompatible_container(__func__);
}
common_spmm_optimize(queue, opA, opB, alpha, A_view, A_handle, B_handle, beta, C_handle, alg,
spmm_descr);
if (alg == oneapi::mkl::sparse::spmm_alg::no_optimize_alg) {
if (alg == spmm_alg::no_optimize_alg) {
return detail::collapse_dependencies(queue, dependencies);
}
internal_A_handle->can_be_reset = false;
Expand All @@ -154,13 +145,12 @@ sycl::event spmm_optimize(sycl::queue &queue, oneapi::mkl::transpose opA,
}

template <typename T>
sycl::event internal_spmm(
sycl::queue &queue, oneapi::mkl::transpose opA, oneapi::mkl::transpose opB, const void *alpha,
oneapi::mkl::sparse::matrix_view /*A_view*/, oneapi::mkl::sparse::matrix_handle_t A_handle,
oneapi::mkl::sparse::dense_matrix_handle_t B_handle, const void *beta,
oneapi::mkl::sparse::dense_matrix_handle_t C_handle, oneapi::mkl::sparse::spmm_alg /*alg*/,
oneapi::mkl::sparse::spmm_descr_t /*spmm_descr*/, const std::vector<sycl::event> &dependencies,
bool is_alpha_host_accessible, bool is_beta_host_accessible) {
sycl::event internal_spmm(sycl::queue &queue, oneapi::mkl::transpose opA,
oneapi::mkl::transpose opB, const void *alpha, matrix_view /*A_view*/,
matrix_handle_t A_handle, dense_matrix_handle_t B_handle,
const void *beta, dense_matrix_handle_t C_handle, spmm_alg /*alg*/,
spmm_descr_t /*spmm_descr*/, const std::vector<sycl::event> &dependencies,
bool is_alpha_host_accessible, bool is_beta_host_accessible) {
T host_alpha =
detail::get_scalar_on_host(queue, static_cast<const T *>(alpha), is_alpha_host_accessible);
T host_beta =
Expand All @@ -187,11 +177,9 @@ sycl::event internal_spmm(
}

sycl::event spmm(sycl::queue &queue, oneapi::mkl::transpose opA, oneapi::mkl::transpose opB,
const void *alpha, oneapi::mkl::sparse::matrix_view A_view,
oneapi::mkl::sparse::matrix_handle_t A_handle,
oneapi::mkl::sparse::dense_matrix_handle_t B_handle, const void *beta,
oneapi::mkl::sparse::dense_matrix_handle_t C_handle,
oneapi::mkl::sparse::spmm_alg alg, oneapi::mkl::sparse::spmm_descr_t spmm_descr,
const void *alpha, matrix_view A_view, matrix_handle_t A_handle,
dense_matrix_handle_t B_handle, const void *beta, dense_matrix_handle_t C_handle,
spmm_alg alg, spmm_descr_t spmm_descr,
const std::vector<sycl::event> &dependencies) {
bool is_alpha_host_accessible = detail::is_ptr_accessible_on_host(queue, alpha);
bool is_beta_host_accessible = detail::is_ptr_accessible_on_host(queue, beta);
Expand Down
Loading

0 comments on commit 44dc73d

Please sign in to comment.