Skip to content

Commit

Permalink
Replace sycl::free with sycl_free_noexcept (#2058) (#2060)
Browse files Browse the repository at this point in the history
* Replace sycl::free with sycl_free_noexcept

* Update changelog
  • Loading branch information
antonwolfy authored Sep 18, 2024
1 parent a013325 commit e2a6c6d
Show file tree
Hide file tree
Showing 24 changed files with 123 additions and 59 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ In addition, this release completes implementation of `dpnp.fft` module and adds
* Updated `Array Manipulation Routines` page in documentation to add missing functions and to remove duplicate entries [#2033](https://github.com/IntelPython/dpnp/pull/2033)
* `dpnp` uses pybind11 2.13.6 [#2041](https://github.com/IntelPython/dpnp/pull/2041)
* Updated `dpnp.fft` backend to depend on `INTEL_MKL_VERSION` flag to ensures that the appropriate code segment is executed based on the version of OneMKL [#2035](https://github.com/IntelPython/dpnp/pull/2035)
* Use `dpctl::tensor::alloc_utils::sycl_free_noexcept` instead of `sycl::free` in `host_task` tasks associated with life-time management of temporary USM allocations [#2058](https://github.com/IntelPython/dpnp/pull/2058)

### Fixed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "utils/memory_overlap.hpp"
#include "utils/offset_utils.hpp"
#include "utils/output_validation.hpp"
#include "utils/sycl_alloc_utils.hpp"
#include "utils/type_dispatch.hpp"

namespace py = pybind11;
Expand Down Expand Up @@ -232,8 +233,9 @@ std::pair<sycl::event, sycl::event>
auto ctx = q.get_context();
sycl::event tmp_cleanup_ev = q.submit([&](sycl::handler &cgh) {
cgh.depends_on(strided_fn_ev);
using dpctl::tensor::alloc_utils::sycl_free_noexcept;
cgh.host_task(
[ctx, shape_strides]() { sycl::free(shape_strides, ctx); });
[ctx, shape_strides]() { sycl_free_noexcept(shape_strides, ctx); });
});
host_tasks.push_back(tmp_cleanup_ev);

Expand Down Expand Up @@ -558,8 +560,9 @@ std::pair<sycl::event, sycl::event> py_binary_ufunc(

sycl::event tmp_cleanup_ev = exec_q.submit([&](sycl::handler &cgh) {
cgh.depends_on(strided_fn_ev);
using dpctl::tensor::alloc_utils::sycl_free_noexcept;
cgh.host_task(
[ctx, shape_strides]() { sycl::free(shape_strides, ctx); });
[ctx, shape_strides]() { sycl_free_noexcept(shape_strides, ctx); });
});

host_tasks.push_back(tmp_cleanup_ev);
Expand Down Expand Up @@ -810,8 +813,9 @@ std::pair<sycl::event, sycl::event>

sycl::event tmp_cleanup_ev = exec_q.submit([&](sycl::handler &cgh) {
cgh.depends_on(strided_fn_ev);
using dpctl::tensor::alloc_utils::sycl_free_noexcept;
cgh.host_task(
[ctx, shape_strides]() { sycl::free(shape_strides, ctx); });
[ctx, shape_strides]() { sycl_free_noexcept(shape_strides, ctx); });
});

host_tasks.push_back(tmp_cleanup_ev);
Expand Down
7 changes: 5 additions & 2 deletions dpnp/backend/extensions/lapack/common_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
#include <cstring>
#include <stdexcept>

// dpctl tensor headers
#include "utils/sycl_alloc_utils.hpp"

namespace dpnp::extensions::lapack::helper
{
namespace py = pybind11;
Expand Down Expand Up @@ -78,7 +81,7 @@ inline std::int64_t *alloc_ipiv(const std::int64_t n, sycl::queue &exec_q)
}
} catch (sycl::exception const &e) {
if (ipiv != nullptr)
sycl::free(ipiv, exec_q);
dpctl::tensor::alloc_utils::sycl_free_noexcept(ipiv, exec_q);
throw std::runtime_error(
std::string(
"Unexpected SYCL exception caught during ipiv allocation: ") +
Expand Down Expand Up @@ -122,7 +125,7 @@ inline T *alloc_scratchpad(std::int64_t scratchpad_size, sycl::queue &exec_q)
}
} catch (sycl::exception const &e) {
if (scratchpad != nullptr) {
sycl::free(scratchpad, exec_q);
dpctl::tensor::alloc_utils::sycl_free_noexcept(scratchpad, exec_q);
}
throw std::runtime_error(std::string("Unexpected SYCL exception caught "
"during scratchpad allocation: ") +
Expand Down
7 changes: 5 additions & 2 deletions dpnp/backend/extensions/lapack/geqrf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

// dpctl tensor headers
#include "utils/memory_overlap.hpp"
#include "utils/sycl_alloc_utils.hpp"
#include "utils/type_utils.hpp"

#include "geqrf.hpp"
Expand Down Expand Up @@ -117,15 +118,17 @@ static sycl::event geqrf_impl(sycl::queue &exec_q,
if (is_exception_caught) // an unexpected error occurs
{
if (scratchpad != nullptr) {
sycl::free(scratchpad, exec_q);
dpctl::tensor::alloc_utils::sycl_free_noexcept(scratchpad, exec_q);
}
throw std::runtime_error(error_msg.str());
}

sycl::event clean_up_event = exec_q.submit([&](sycl::handler &cgh) {
cgh.depends_on(geqrf_event);
auto ctx = exec_q.get_context();
cgh.host_task([ctx, scratchpad]() { sycl::free(scratchpad, ctx); });
cgh.host_task([ctx, scratchpad]() {
dpctl::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx);
});
});
host_task_events.push_back(clean_up_event);

Expand Down
7 changes: 5 additions & 2 deletions dpnp/backend/extensions/lapack/geqrf_batch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

// dpctl tensor headers
#include "utils/memory_overlap.hpp"
#include "utils/sycl_alloc_utils.hpp"
#include "utils/type_utils.hpp"

#include "geqrf.hpp"
Expand Down Expand Up @@ -138,7 +139,7 @@ static sycl::event geqrf_batch_impl(sycl::queue &exec_q,
if (is_exception_caught) // an unexpected error occurs
{
if (scratchpad != nullptr) {
sycl::free(scratchpad, exec_q);
dpctl::tensor::alloc_utils::sycl_free_noexcept(scratchpad, exec_q);
}

throw std::runtime_error(error_msg.str());
Expand All @@ -147,7 +148,9 @@ static sycl::event geqrf_batch_impl(sycl::queue &exec_q,
sycl::event clean_up_event = exec_q.submit([&](sycl::handler &cgh) {
cgh.depends_on(geqrf_batch_event);
auto ctx = exec_q.get_context();
cgh.host_task([ctx, scratchpad]() { sycl::free(scratchpad, ctx); });
cgh.host_task([ctx, scratchpad]() {
dpctl::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx);
});
});
host_task_events.push_back(clean_up_event);
return geqrf_batch_event;
Expand Down
12 changes: 7 additions & 5 deletions dpnp/backend/extensions/lapack/gesv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ namespace mkl_lapack = oneapi::mkl::lapack;
namespace py = pybind11;
namespace type_utils = dpctl::tensor::type_utils;

using dpctl::tensor::alloc_utils::sycl_free_noexcept;

typedef sycl::event (*gesv_impl_fn_ptr_t)(sycl::queue &,
const std::int64_t,
const std::int64_t,
Expand Down Expand Up @@ -93,7 +95,7 @@ static sycl::event gesv_impl(sycl::queue &exec_q,
ipiv = helper::alloc_ipiv(n, exec_q);
} catch (const std::exception &e) {
if (scratchpad != nullptr)
sycl::free(scratchpad, exec_q);
sycl_free_noexcept(scratchpad, exec_q);
throw;
}

Expand Down Expand Up @@ -178,18 +180,18 @@ static sycl::event gesv_impl(sycl::queue &exec_q,
if (is_exception_caught) // an unexpected error occurs
{
if (scratchpad != nullptr)
sycl::free(scratchpad, exec_q);
sycl_free_noexcept(scratchpad, exec_q);
if (ipiv != nullptr)
sycl::free(ipiv, exec_q);
sycl_free_noexcept(ipiv, exec_q);
throw std::runtime_error(error_msg.str());
}

sycl::event ht_ev = exec_q.submit([&](sycl::handler &cgh) {
cgh.depends_on(comp_event);
auto ctx = exec_q.get_context();
cgh.host_task([ctx, scratchpad, ipiv]() {
sycl::free(scratchpad, ctx);
sycl::free(ipiv, ctx);
sycl_free_noexcept(scratchpad, ctx);
sycl_free_noexcept(ipiv, ctx);
});
});

Expand Down
18 changes: 10 additions & 8 deletions dpnp/backend/extensions/lapack/gesv_batch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ namespace mkl_lapack = oneapi::mkl::lapack;
namespace py = pybind11;
namespace type_utils = dpctl::tensor::type_utils;

using dpctl::tensor::alloc_utils::sycl_free_noexcept;

typedef sycl::event (*gesv_batch_impl_fn_ptr_t)(
sycl::queue &,
const std::int64_t,
Expand Down Expand Up @@ -106,7 +108,7 @@ static sycl::event gesv_batch_impl(sycl::queue &exec_q,
ipiv = helper::alloc_ipiv(batch_size * n, exec_q);
} catch (const std::exception &e) {
if (scratchpad != nullptr)
sycl::free(scratchpad, exec_q);
sycl_free_noexcept(scratchpad, exec_q);
throw;
}

Expand Down Expand Up @@ -172,9 +174,9 @@ static sycl::event gesv_batch_impl(sycl::queue &exec_q,
error_msg << ".";

if (scratchpad != nullptr)
sycl::free(scratchpad, exec_q);
sycl_free_noexcept(scratchpad, exec_q);
if (ipiv != nullptr)
sycl::free(ipiv, exec_q);
sycl_free_noexcept(ipiv, exec_q);

throw LinAlgError(error_msg.str().c_str());
} catch (mkl_lapack::exception const &e) {
Expand Down Expand Up @@ -218,7 +220,7 @@ static sycl::event gesv_batch_impl(sycl::queue &exec_q,
ipiv = helper::alloc_ipiv_batch<T>(n, n_linear_streams, exec_q);
} catch (const std::exception &e) {
if (scratchpad != nullptr)
sycl::free(scratchpad, exec_q);
sycl_free_noexcept(scratchpad, exec_q);
throw;
}

Expand Down Expand Up @@ -281,9 +283,9 @@ static sycl::event gesv_batch_impl(sycl::queue &exec_q,
if (is_exception_caught) // an unexpected error occurs
{
if (scratchpad != nullptr)
sycl::free(scratchpad, exec_q);
sycl_free_noexcept(scratchpad, exec_q);
if (ipiv != nullptr)
sycl::free(ipiv, exec_q);
sycl_free_noexcept(ipiv, exec_q);
throw std::runtime_error(error_msg.str());
}

Expand All @@ -297,8 +299,8 @@ static sycl::event gesv_batch_impl(sycl::queue &exec_q,
#endif // USE_ONEMKL_INTERFACES
auto ctx = exec_q.get_context();
cgh.host_task([ctx, scratchpad, ipiv]() {
sycl::free(scratchpad, ctx);
sycl::free(ipiv, ctx);
sycl_free_noexcept(scratchpad, ctx);
sycl_free_noexcept(ipiv, ctx);
});
});

Expand Down
7 changes: 5 additions & 2 deletions dpnp/backend/extensions/lapack/gesv_common_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
// dpctl tensor headers
#include "utils/memory_overlap.hpp"
#include "utils/output_validation.hpp"
#include "utils/sycl_alloc_utils.hpp"
#include "utils/type_dispatch.hpp"

#include "common_helpers.hpp"
Expand Down Expand Up @@ -159,10 +160,12 @@ inline void handle_lapack_exc(sycl::queue &exec_q,
const auto threshold =
std::numeric_limits<ThresholdType>::epsilon() * 100;
if (std::abs(host_U) < threshold) {
using dpctl::tensor::alloc_utils::sycl_free_noexcept;

if (scratchpad != nullptr)
sycl::free(scratchpad, exec_q);
sycl_free_noexcept(scratchpad, exec_q);
if (ipiv != nullptr)
sycl::free(ipiv, exec_q);
sycl_free_noexcept(ipiv, exec_q);
throw LinAlgError("The input coefficient matrix is singular.");
}
else {
Expand Down
6 changes: 4 additions & 2 deletions dpnp/backend/extensions/lapack/gesvd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,17 @@ static sycl::event gesvd_impl(sycl::queue &exec_q,
if (is_exception_caught) // an unexpected error occurs
{
if (scratchpad != nullptr) {
sycl::free(scratchpad, exec_q);
dpctl::tensor::alloc_utils::sycl_free_noexcept(scratchpad, exec_q);
}
throw std::runtime_error(error_msg.str());
}

sycl::event ht_ev = exec_q.submit([&](sycl::handler &cgh) {
cgh.depends_on(gesvd_event);
auto ctx = exec_q.get_context();
cgh.host_task([ctx, scratchpad]() { sycl::free(scratchpad, ctx); });
cgh.host_task([ctx, scratchpad]() {
dpctl::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx);
});
});

return ht_ev;
Expand Down
6 changes: 4 additions & 2 deletions dpnp/backend/extensions/lapack/gesvd_batch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ static sycl::event gesvd_batch_impl(sycl::queue &exec_q,
if (is_exception_caught) // an unexpected error occurs
{
if (scratchpad != nullptr) {
sycl::free(scratchpad, exec_q);
dpctl::tensor::alloc_utils::sycl_free_noexcept(scratchpad, exec_q);
}
throw std::runtime_error(error_msg.str());
}
Expand All @@ -199,7 +199,9 @@ static sycl::event gesvd_batch_impl(sycl::queue &exec_q,
cgh.depends_on(ev);
}
auto ctx = exec_q.get_context();
cgh.host_task([ctx, scratchpad]() { sycl::free(scratchpad, ctx); });
cgh.host_task([ctx, scratchpad]() {
dpctl::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx);
});
});

return ht_ev;
Expand Down
7 changes: 5 additions & 2 deletions dpnp/backend/extensions/lapack/getrf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

// dpctl tensor headers
#include "utils/memory_overlap.hpp"
#include "utils/sycl_alloc_utils.hpp"
#include "utils/type_utils.hpp"

#include "getrf.hpp"
Expand Down Expand Up @@ -126,7 +127,7 @@ static sycl::event getrf_impl(sycl::queue &exec_q,
if (is_exception_caught) // an unexpected error occurs
{
if (scratchpad != nullptr) {
sycl::free(scratchpad, exec_q);
dpctl::tensor::alloc_utils::sycl_free_noexcept(scratchpad, exec_q);
}

throw std::runtime_error(error_msg.str());
Expand All @@ -135,7 +136,9 @@ static sycl::event getrf_impl(sycl::queue &exec_q,
sycl::event clean_up_event = exec_q.submit([&](sycl::handler &cgh) {
cgh.depends_on(getrf_event);
auto ctx = exec_q.get_context();
cgh.host_task([ctx, scratchpad]() { sycl::free(scratchpad, ctx); });
cgh.host_task([ctx, scratchpad]() {
dpctl::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx);
});
});
host_task_events.push_back(clean_up_event);
return getrf_event;
Expand Down
7 changes: 5 additions & 2 deletions dpnp/backend/extensions/lapack/getrf_batch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

// dpctl tensor headers
#include "utils/memory_overlap.hpp"
#include "utils/sycl_alloc_utils.hpp"
#include "utils/type_utils.hpp"

#include "getrf.hpp"
Expand Down Expand Up @@ -152,7 +153,7 @@ static sycl::event getrf_batch_impl(sycl::queue &exec_q,
if (is_exception_caught) // an unexpected error occurs
{
if (scratchpad != nullptr) {
sycl::free(scratchpad, exec_q);
dpctl::tensor::alloc_utils::sycl_free_noexcept(scratchpad, exec_q);
}

throw std::runtime_error(error_msg.str());
Expand All @@ -161,7 +162,9 @@ static sycl::event getrf_batch_impl(sycl::queue &exec_q,
sycl::event clean_up_event = exec_q.submit([&](sycl::handler &cgh) {
cgh.depends_on(getrf_batch_event);
auto ctx = exec_q.get_context();
cgh.host_task([ctx, scratchpad]() { sycl::free(scratchpad, ctx); });
cgh.host_task([ctx, scratchpad]() {
dpctl::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx);
});
});
host_task_events.push_back(clean_up_event);
return getrf_batch_event;
Expand Down
7 changes: 5 additions & 2 deletions dpnp/backend/extensions/lapack/getri_batch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

// dpctl tensor headers
#include "utils/memory_overlap.hpp"
#include "utils/sycl_alloc_utils.hpp"
#include "utils/type_utils.hpp"

#include "getri.hpp"
Expand Down Expand Up @@ -150,7 +151,7 @@ static sycl::event getri_batch_impl(sycl::queue &exec_q,
if (is_exception_caught) // an unexpected error occurs
{
if (scratchpad != nullptr) {
sycl::free(scratchpad, exec_q);
dpctl::tensor::alloc_utils::sycl_free_noexcept(scratchpad, exec_q);
}

throw std::runtime_error(error_msg.str());
Expand All @@ -159,7 +160,9 @@ static sycl::event getri_batch_impl(sycl::queue &exec_q,
sycl::event clean_up_event = exec_q.submit([&](sycl::handler &cgh) {
cgh.depends_on(getri_batch_event);
auto ctx = exec_q.get_context();
cgh.host_task([ctx, scratchpad]() { sycl::free(scratchpad, ctx); });
cgh.host_task([ctx, scratchpad]() {
dpctl::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx);
});
});
host_task_events.push_back(clean_up_event);
return getri_batch_event;
Expand Down
Loading

0 comments on commit e2a6c6d

Please sign in to comment.