Skip to content

Commit 0f94dcd

Browse files
Simplify implementation by use lambda as a deleter per @AlexanderKalistratov suggestion
1 parent fc9371f commit 0f94dcd

File tree

1 file changed

+9
-25
lines changed

1 file changed

+9
-25
lines changed

examples/pybind11/external_usm_allocation/external_usm_allocation/_usm_alloc_example.cpp

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -121,37 +121,21 @@ py::list tolist(DMatrix &m)
121121
return rows;
122122
}
123123

124-
class USMDeleter
125-
{
126-
public:
127-
USMDeleter() = delete;
128-
USMDeleter(const USMDeleter &) = default;
129-
USMDeleter(USMDeleter &&) = default;
130-
USMDeleter(const ::sycl::queue &queue) : _context(queue.get_context()) {}
131-
USMDeleter(const ::sycl::context &context) : _context(context) {}
132-
133-
/*! @brief call operator to delete resources */
134-
template <typename T> void operator()(T *ptr) const
135-
{
136-
try {
137-
::sycl::free(ptr, _context);
138-
} catch (const std::exception &e) {
139-
std::cout << "Call to sycl::free caught an exception: " << e.what()
140-
<< std::endl;
141-
}
142-
}
143-
144-
private:
145-
::sycl::context _context;
146-
};
147-
148124
dpctl::memory::usm_memory make_zeroed_device_memory(size_t nbytes,
149125
sycl::queue &q)
150126
{
151127
char *data = sycl::malloc_device<char>(nbytes, q);
152128
q.memset(data, 0, nbytes).wait();
153129

154-
USMDeleter _deleter(q);
130+
const sycl::context &ctx = q.get_context();
131+
auto _deleter = [ctx](void *ptr) {
132+
try {
133+
::sycl::free(ptr, ctx);
134+
} catch (const std::exception &e) {
135+
std::cout << "Call to sycl::free caught an exception: " << e.what()
136+
<< std::endl;
137+
}
138+
};
155139
auto shptr = std::shared_ptr<void>(data, _deleter);
156140

157141
return dpctl::memory::usm_memory(data, nbytes, q, shptr);

0 commit comments

Comments
 (0)