Skip to content

Commit a1dedb0

Browse files
authored
Replace sycl::free with sycl_free_noexcept (#2058)
* Replace sycl::free with sycl_free_noexcept * Update changelog
1 parent 3f20511 commit a1dedb0

24 files changed

+123
-59
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ In addition, this release completes implementation of `dpnp.fft` module and adds
112112
* Updated `Array Manipulation Routines` page in documentation to add missing functions and to remove duplicate entries [#2033](https://github.com/IntelPython/dpnp/pull/2033)
113113
* `dpnp` uses pybind11 2.13.6 [#2041](https://github.com/IntelPython/dpnp/pull/2041)
114114
* 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)
115+
* 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)
115116

116117
### Fixed
117118

dpnp/backend/extensions/elementwise_functions/elementwise_functions.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "utils/memory_overlap.hpp"
4242
#include "utils/offset_utils.hpp"
4343
#include "utils/output_validation.hpp"
44+
#include "utils/sycl_alloc_utils.hpp"
4445
#include "utils/type_dispatch.hpp"
4546

4647
namespace py = pybind11;
@@ -232,8 +233,9 @@ std::pair<sycl::event, sycl::event>
232233
auto ctx = q.get_context();
233234
sycl::event tmp_cleanup_ev = q.submit([&](sycl::handler &cgh) {
234235
cgh.depends_on(strided_fn_ev);
236+
using dpctl::tensor::alloc_utils::sycl_free_noexcept;
235237
cgh.host_task(
236-
[ctx, shape_strides]() { sycl::free(shape_strides, ctx); });
238+
[ctx, shape_strides]() { sycl_free_noexcept(shape_strides, ctx); });
237239
});
238240
host_tasks.push_back(tmp_cleanup_ev);
239241

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

559561
sycl::event tmp_cleanup_ev = exec_q.submit([&](sycl::handler &cgh) {
560562
cgh.depends_on(strided_fn_ev);
563+
using dpctl::tensor::alloc_utils::sycl_free_noexcept;
561564
cgh.host_task(
562-
[ctx, shape_strides]() { sycl::free(shape_strides, ctx); });
565+
[ctx, shape_strides]() { sycl_free_noexcept(shape_strides, ctx); });
563566
});
564567

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

811814
sycl::event tmp_cleanup_ev = exec_q.submit([&](sycl::handler &cgh) {
812815
cgh.depends_on(strided_fn_ev);
816+
using dpctl::tensor::alloc_utils::sycl_free_noexcept;
813817
cgh.host_task(
814-
[ctx, shape_strides]() { sycl::free(shape_strides, ctx); });
818+
[ctx, shape_strides]() { sycl_free_noexcept(shape_strides, ctx); });
815819
});
816820

817821
host_tasks.push_back(tmp_cleanup_ev);

dpnp/backend/extensions/lapack/common_helpers.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
#include <cstring>
3232
#include <stdexcept>
3333

34+
// dpctl tensor headers
35+
#include "utils/sycl_alloc_utils.hpp"
36+
3437
namespace dpnp::extensions::lapack::helper
3538
{
3639
namespace py = pybind11;
@@ -78,7 +81,7 @@ inline std::int64_t *alloc_ipiv(const std::int64_t n, sycl::queue &exec_q)
7881
}
7982
} catch (sycl::exception const &e) {
8083
if (ipiv != nullptr)
81-
sycl::free(ipiv, exec_q);
84+
dpctl::tensor::alloc_utils::sycl_free_noexcept(ipiv, exec_q);
8285
throw std::runtime_error(
8386
std::string(
8487
"Unexpected SYCL exception caught during ipiv allocation: ") +
@@ -122,7 +125,7 @@ inline T *alloc_scratchpad(std::int64_t scratchpad_size, sycl::queue &exec_q)
122125
}
123126
} catch (sycl::exception const &e) {
124127
if (scratchpad != nullptr) {
125-
sycl::free(scratchpad, exec_q);
128+
dpctl::tensor::alloc_utils::sycl_free_noexcept(scratchpad, exec_q);
126129
}
127130
throw std::runtime_error(std::string("Unexpected SYCL exception caught "
128131
"during scratchpad allocation: ") +

dpnp/backend/extensions/lapack/geqrf.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
// dpctl tensor headers
2929
#include "utils/memory_overlap.hpp"
30+
#include "utils/sycl_alloc_utils.hpp"
3031
#include "utils/type_utils.hpp"
3132

3233
#include "geqrf.hpp"
@@ -117,15 +118,17 @@ static sycl::event geqrf_impl(sycl::queue &exec_q,
117118
if (is_exception_caught) // an unexpected error occurs
118119
{
119120
if (scratchpad != nullptr) {
120-
sycl::free(scratchpad, exec_q);
121+
dpctl::tensor::alloc_utils::sycl_free_noexcept(scratchpad, exec_q);
121122
}
122123
throw std::runtime_error(error_msg.str());
123124
}
124125

125126
sycl::event clean_up_event = exec_q.submit([&](sycl::handler &cgh) {
126127
cgh.depends_on(geqrf_event);
127128
auto ctx = exec_q.get_context();
128-
cgh.host_task([ctx, scratchpad]() { sycl::free(scratchpad, ctx); });
129+
cgh.host_task([ctx, scratchpad]() {
130+
dpctl::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx);
131+
});
129132
});
130133
host_task_events.push_back(clean_up_event);
131134

dpnp/backend/extensions/lapack/geqrf_batch.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
// dpctl tensor headers
2929
#include "utils/memory_overlap.hpp"
30+
#include "utils/sycl_alloc_utils.hpp"
3031
#include "utils/type_utils.hpp"
3132

3233
#include "geqrf.hpp"
@@ -138,7 +139,7 @@ static sycl::event geqrf_batch_impl(sycl::queue &exec_q,
138139
if (is_exception_caught) // an unexpected error occurs
139140
{
140141
if (scratchpad != nullptr) {
141-
sycl::free(scratchpad, exec_q);
142+
dpctl::tensor::alloc_utils::sycl_free_noexcept(scratchpad, exec_q);
142143
}
143144

144145
throw std::runtime_error(error_msg.str());
@@ -147,7 +148,9 @@ static sycl::event geqrf_batch_impl(sycl::queue &exec_q,
147148
sycl::event clean_up_event = exec_q.submit([&](sycl::handler &cgh) {
148149
cgh.depends_on(geqrf_batch_event);
149150
auto ctx = exec_q.get_context();
150-
cgh.host_task([ctx, scratchpad]() { sycl::free(scratchpad, ctx); });
151+
cgh.host_task([ctx, scratchpad]() {
152+
dpctl::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx);
153+
});
151154
});
152155
host_task_events.push_back(clean_up_event);
153156
return geqrf_batch_event;

dpnp/backend/extensions/lapack/gesv.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ namespace mkl_lapack = oneapi::mkl::lapack;
3939
namespace py = pybind11;
4040
namespace type_utils = dpctl::tensor::type_utils;
4141

42+
using dpctl::tensor::alloc_utils::sycl_free_noexcept;
43+
4244
typedef sycl::event (*gesv_impl_fn_ptr_t)(sycl::queue &,
4345
const std::int64_t,
4446
const std::int64_t,
@@ -93,7 +95,7 @@ static sycl::event gesv_impl(sycl::queue &exec_q,
9395
ipiv = helper::alloc_ipiv(n, exec_q);
9496
} catch (const std::exception &e) {
9597
if (scratchpad != nullptr)
96-
sycl::free(scratchpad, exec_q);
98+
sycl_free_noexcept(scratchpad, exec_q);
9799
throw;
98100
}
99101

@@ -178,18 +180,18 @@ static sycl::event gesv_impl(sycl::queue &exec_q,
178180
if (is_exception_caught) // an unexpected error occurs
179181
{
180182
if (scratchpad != nullptr)
181-
sycl::free(scratchpad, exec_q);
183+
sycl_free_noexcept(scratchpad, exec_q);
182184
if (ipiv != nullptr)
183-
sycl::free(ipiv, exec_q);
185+
sycl_free_noexcept(ipiv, exec_q);
184186
throw std::runtime_error(error_msg.str());
185187
}
186188

187189
sycl::event ht_ev = exec_q.submit([&](sycl::handler &cgh) {
188190
cgh.depends_on(comp_event);
189191
auto ctx = exec_q.get_context();
190192
cgh.host_task([ctx, scratchpad, ipiv]() {
191-
sycl::free(scratchpad, ctx);
192-
sycl::free(ipiv, ctx);
193+
sycl_free_noexcept(scratchpad, ctx);
194+
sycl_free_noexcept(ipiv, ctx);
193195
});
194196
});
195197

dpnp/backend/extensions/lapack/gesv_batch.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ namespace mkl_lapack = oneapi::mkl::lapack;
3939
namespace py = pybind11;
4040
namespace type_utils = dpctl::tensor::type_utils;
4141

42+
using dpctl::tensor::alloc_utils::sycl_free_noexcept;
43+
4244
typedef sycl::event (*gesv_batch_impl_fn_ptr_t)(
4345
sycl::queue &,
4446
const std::int64_t,
@@ -106,7 +108,7 @@ static sycl::event gesv_batch_impl(sycl::queue &exec_q,
106108
ipiv = helper::alloc_ipiv(batch_size * n, exec_q);
107109
} catch (const std::exception &e) {
108110
if (scratchpad != nullptr)
109-
sycl::free(scratchpad, exec_q);
111+
sycl_free_noexcept(scratchpad, exec_q);
110112
throw;
111113
}
112114

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

174176
if (scratchpad != nullptr)
175-
sycl::free(scratchpad, exec_q);
177+
sycl_free_noexcept(scratchpad, exec_q);
176178
if (ipiv != nullptr)
177-
sycl::free(ipiv, exec_q);
179+
sycl_free_noexcept(ipiv, exec_q);
178180

179181
throw LinAlgError(error_msg.str().c_str());
180182
} catch (mkl_lapack::exception const &e) {
@@ -218,7 +220,7 @@ static sycl::event gesv_batch_impl(sycl::queue &exec_q,
218220
ipiv = helper::alloc_ipiv_batch<T>(n, n_linear_streams, exec_q);
219221
} catch (const std::exception &e) {
220222
if (scratchpad != nullptr)
221-
sycl::free(scratchpad, exec_q);
223+
sycl_free_noexcept(scratchpad, exec_q);
222224
throw;
223225
}
224226

@@ -281,9 +283,9 @@ static sycl::event gesv_batch_impl(sycl::queue &exec_q,
281283
if (is_exception_caught) // an unexpected error occurs
282284
{
283285
if (scratchpad != nullptr)
284-
sycl::free(scratchpad, exec_q);
286+
sycl_free_noexcept(scratchpad, exec_q);
285287
if (ipiv != nullptr)
286-
sycl::free(ipiv, exec_q);
288+
sycl_free_noexcept(ipiv, exec_q);
287289
throw std::runtime_error(error_msg.str());
288290
}
289291

@@ -297,8 +299,8 @@ static sycl::event gesv_batch_impl(sycl::queue &exec_q,
297299
#endif // USE_ONEMKL_INTERFACES
298300
auto ctx = exec_q.get_context();
299301
cgh.host_task([ctx, scratchpad, ipiv]() {
300-
sycl::free(scratchpad, ctx);
301-
sycl::free(ipiv, ctx);
302+
sycl_free_noexcept(scratchpad, ctx);
303+
sycl_free_noexcept(ipiv, ctx);
302304
});
303305
});
304306

dpnp/backend/extensions/lapack/gesv_common_utils.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
// dpctl tensor headers
3131
#include "utils/memory_overlap.hpp"
3232
#include "utils/output_validation.hpp"
33+
#include "utils/sycl_alloc_utils.hpp"
3334
#include "utils/type_dispatch.hpp"
3435

3536
#include "common_helpers.hpp"
@@ -159,10 +160,12 @@ inline void handle_lapack_exc(sycl::queue &exec_q,
159160
const auto threshold =
160161
std::numeric_limits<ThresholdType>::epsilon() * 100;
161162
if (std::abs(host_U) < threshold) {
163+
using dpctl::tensor::alloc_utils::sycl_free_noexcept;
164+
162165
if (scratchpad != nullptr)
163-
sycl::free(scratchpad, exec_q);
166+
sycl_free_noexcept(scratchpad, exec_q);
164167
if (ipiv != nullptr)
165-
sycl::free(ipiv, exec_q);
168+
sycl_free_noexcept(ipiv, exec_q);
166169
throw LinAlgError("The input coefficient matrix is singular.");
167170
}
168171
else {

dpnp/backend/extensions/lapack/gesvd.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,17 @@ static sycl::event gesvd_impl(sycl::queue &exec_q,
125125
if (is_exception_caught) // an unexpected error occurs
126126
{
127127
if (scratchpad != nullptr) {
128-
sycl::free(scratchpad, exec_q);
128+
dpctl::tensor::alloc_utils::sycl_free_noexcept(scratchpad, exec_q);
129129
}
130130
throw std::runtime_error(error_msg.str());
131131
}
132132

133133
sycl::event ht_ev = exec_q.submit([&](sycl::handler &cgh) {
134134
cgh.depends_on(gesvd_event);
135135
auto ctx = exec_q.get_context();
136-
cgh.host_task([ctx, scratchpad]() { sycl::free(scratchpad, ctx); });
136+
cgh.host_task([ctx, scratchpad]() {
137+
dpctl::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx);
138+
});
137139
});
138140

139141
return ht_ev;

dpnp/backend/extensions/lapack/gesvd_batch.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ static sycl::event gesvd_batch_impl(sycl::queue &exec_q,
189189
if (is_exception_caught) // an unexpected error occurs
190190
{
191191
if (scratchpad != nullptr) {
192-
sycl::free(scratchpad, exec_q);
192+
dpctl::tensor::alloc_utils::sycl_free_noexcept(scratchpad, exec_q);
193193
}
194194
throw std::runtime_error(error_msg.str());
195195
}
@@ -199,7 +199,9 @@ static sycl::event gesvd_batch_impl(sycl::queue &exec_q,
199199
cgh.depends_on(ev);
200200
}
201201
auto ctx = exec_q.get_context();
202-
cgh.host_task([ctx, scratchpad]() { sycl::free(scratchpad, ctx); });
202+
cgh.host_task([ctx, scratchpad]() {
203+
dpctl::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx);
204+
});
203205
});
204206

205207
return ht_ev;

dpnp/backend/extensions/lapack/getrf.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
// dpctl tensor headers
2929
#include "utils/memory_overlap.hpp"
30+
#include "utils/sycl_alloc_utils.hpp"
3031
#include "utils/type_utils.hpp"
3132

3233
#include "getrf.hpp"
@@ -126,7 +127,7 @@ static sycl::event getrf_impl(sycl::queue &exec_q,
126127
if (is_exception_caught) // an unexpected error occurs
127128
{
128129
if (scratchpad != nullptr) {
129-
sycl::free(scratchpad, exec_q);
130+
dpctl::tensor::alloc_utils::sycl_free_noexcept(scratchpad, exec_q);
130131
}
131132

132133
throw std::runtime_error(error_msg.str());
@@ -135,7 +136,9 @@ static sycl::event getrf_impl(sycl::queue &exec_q,
135136
sycl::event clean_up_event = exec_q.submit([&](sycl::handler &cgh) {
136137
cgh.depends_on(getrf_event);
137138
auto ctx = exec_q.get_context();
138-
cgh.host_task([ctx, scratchpad]() { sycl::free(scratchpad, ctx); });
139+
cgh.host_task([ctx, scratchpad]() {
140+
dpctl::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx);
141+
});
139142
});
140143
host_task_events.push_back(clean_up_event);
141144
return getrf_event;

dpnp/backend/extensions/lapack/getrf_batch.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
// dpctl tensor headers
2929
#include "utils/memory_overlap.hpp"
30+
#include "utils/sycl_alloc_utils.hpp"
3031
#include "utils/type_utils.hpp"
3132

3233
#include "getrf.hpp"
@@ -152,7 +153,7 @@ static sycl::event getrf_batch_impl(sycl::queue &exec_q,
152153
if (is_exception_caught) // an unexpected error occurs
153154
{
154155
if (scratchpad != nullptr) {
155-
sycl::free(scratchpad, exec_q);
156+
dpctl::tensor::alloc_utils::sycl_free_noexcept(scratchpad, exec_q);
156157
}
157158

158159
throw std::runtime_error(error_msg.str());
@@ -161,7 +162,9 @@ static sycl::event getrf_batch_impl(sycl::queue &exec_q,
161162
sycl::event clean_up_event = exec_q.submit([&](sycl::handler &cgh) {
162163
cgh.depends_on(getrf_batch_event);
163164
auto ctx = exec_q.get_context();
164-
cgh.host_task([ctx, scratchpad]() { sycl::free(scratchpad, ctx); });
165+
cgh.host_task([ctx, scratchpad]() {
166+
dpctl::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx);
167+
});
165168
});
166169
host_task_events.push_back(clean_up_event);
167170
return getrf_batch_event;

dpnp/backend/extensions/lapack/getri_batch.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
// dpctl tensor headers
2929
#include "utils/memory_overlap.hpp"
30+
#include "utils/sycl_alloc_utils.hpp"
3031
#include "utils/type_utils.hpp"
3132

3233
#include "getri.hpp"
@@ -150,7 +151,7 @@ static sycl::event getri_batch_impl(sycl::queue &exec_q,
150151
if (is_exception_caught) // an unexpected error occurs
151152
{
152153
if (scratchpad != nullptr) {
153-
sycl::free(scratchpad, exec_q);
154+
dpctl::tensor::alloc_utils::sycl_free_noexcept(scratchpad, exec_q);
154155
}
155156

156157
throw std::runtime_error(error_msg.str());
@@ -159,7 +160,9 @@ static sycl::event getri_batch_impl(sycl::queue &exec_q,
159160
sycl::event clean_up_event = exec_q.submit([&](sycl::handler &cgh) {
160161
cgh.depends_on(getri_batch_event);
161162
auto ctx = exec_q.get_context();
162-
cgh.host_task([ctx, scratchpad]() { sycl::free(scratchpad, ctx); });
163+
cgh.host_task([ctx, scratchpad]() {
164+
dpctl::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx);
165+
});
163166
});
164167
host_task_events.push_back(clean_up_event);
165168
return getri_batch_event;

0 commit comments

Comments
 (0)