Skip to content

Commit 88a7ece

Browse files
committed
Expose context extended deleters on PI API
Signed-off-by: Stuart Adams [email protected]
1 parent 3238b3f commit 88a7ece

File tree

6 files changed

+49
-11
lines changed

6 files changed

+49
-11
lines changed

sycl/include/CL/sycl/detail/pi.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ _PI_API(piContextCreate)
3131
_PI_API(piContextGetInfo)
3232
_PI_API(piContextRetain)
3333
_PI_API(piContextRelease)
34+
_PI_API(piextContextSetExtendedDeleter)
3435
// Queue
3536
_PI_API(piQueueCreate)
3637
_PI_API(piQueueGetInfo)

sycl/include/CL/sycl/detail/pi.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,12 @@ pi_result piContextRetain(pi_context context);
824824

825825
pi_result piContextRelease(pi_context context);
826826

827+
typedef void (*pi_context_extended_deleter)(void *user_data);
828+
829+
pi_result piextContextSetExtendedDeleter(pi_context context,
830+
pi_context_extended_deleter func,
831+
void *user_data);
832+
827833
//
828834
// Queue
829835
//

sycl/include/CL/sycl/detail/pi.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ struct trace_event_data_t;
3030

3131
__SYCL_INLINE_NAMESPACE(cl) {
3232
namespace sycl {
33+
34+
class context;
35+
3336
namespace detail {
3437

3538
enum class PiApiKind {
@@ -95,6 +98,10 @@ using PiMemObjectType = ::pi_mem_type;
9598
using PiMemImageChannelOrder = ::pi_image_channel_order;
9699
using PiMemImageChannelType = ::pi_image_channel_type;
97100

101+
void contextSetExtendedDeleter(const cl::sycl::context& constext,
102+
pi_context_extended_deleter func,
103+
void *user_data);
104+
98105
// Function to load the shared library
99106
// Implementation is OS dependent.
100107
void *loadOsLibrary(const std::string &Library);

sycl/plugins/cuda/pi_cuda.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,12 @@ pi_result cuda_piContextRetain(pi_context context) {
777777
return PI_SUCCESS;
778778
}
779779

780+
pi_result cuda_piextContextSetExtendedDeleter(
781+
pi_context context, pi_context_extended_deleter function, void *user_data) {
782+
context->set_extended_deleter(function, user_data);
783+
return PI_SUCCESS;
784+
}
785+
780786
/// Not applicable to CUDA, devices cannot be partitioned.
781787
///
782788
pi_result cuda_piDevicePartition(
@@ -1462,7 +1468,7 @@ pi_result cuda_piContextRelease(pi_context ctxt) {
14621468
if (ctxt->decrement_reference_count() > 0) {
14631469
return PI_SUCCESS;
14641470
}
1465-
ctxt->invoke_callback();
1471+
ctxt->invoke_extended_deleters();
14661472

14671473
std::unique_ptr<_pi_context> context{ctxt};
14681474

@@ -3586,6 +3592,7 @@ pi_result piPluginInit(pi_plugin *PluginInit) {
35863592
_PI_CL(piextDeviceSelectBinary, cuda_piextDeviceSelectBinary)
35873593
_PI_CL(piextGetDeviceFunctionPointer, cuda_piextGetDeviceFunctionPointer)
35883594
// Context
3595+
_PI_CL(piextContextSetExtendedDeleter, cuda_piextContextSetExtendedDeleter)
35893596
_PI_CL(piContextCreate, cuda_piContextCreate)
35903597
_PI_CL(piContextGetInfo, cuda_piContextGetInfo)
35913598
_PI_CL(piContextRetain, cuda_piContextRetain)

sycl/plugins/cuda/pi_cuda.hpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,14 @@ class _pi_device {
121121
/// See proposal for details.
122122
///
123123
struct _pi_context {
124+
125+
struct deleter_data {
126+
pi_context_extended_deleter function;
127+
void *user_data;
128+
129+
void operator()() { function(user_data); }
130+
};
131+
124132
using native_type = CUcontext;
125133

126134
enum class kind { primary, user_defined } kind_;
@@ -138,20 +146,17 @@ struct _pi_context {
138146

139147
~_pi_context() { cuda_piDeviceRelease(deviceId_); }
140148

141-
void invoke_callback()
142-
{
149+
void invoke_extended_deleters() {
143150
std::lock_guard<std::mutex> guard(mutex_);
144-
for(const auto& callback : destruction_callbacks_)
145-
{
146-
callback();
151+
for (auto &deleter : extended_deleters_) {
152+
deleter();
147153
}
148154
}
149155

150-
template<typename Func>
151-
void register_callback(Func&& callback)
152-
{
156+
void set_extended_deleter(pi_context_extended_deleter function,
157+
void *user_data) {
153158
std::lock_guard<std::mutex> guard(mutex_);
154-
destruction_callbacks_.emplace_back(std::forward<Func>(callback));
159+
extended_deleters_.emplace_back(deleter_data{function, user_data});
155160
}
156161

157162
pi_device get_device() const noexcept { return deviceId_; }
@@ -168,7 +173,7 @@ struct _pi_context {
168173

169174
private:
170175
std::mutex mutex_;
171-
std::vector<std::function<void(void)>> destruction_callbacks_;
176+
std::vector<deleter_data> extended_deleters_;
172177
};
173178

174179
/// PI Mem mapping to a CUDA memory allocation

sycl/source/detail/pi.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
///
1212
/// \ingroup sycl_pi
1313

14+
#include "context_impl.hpp"
15+
#include <CL/sycl/context.hpp>
1416
#include <CL/sycl/detail/common.hpp>
1517
#include <CL/sycl/detail/pi.hpp>
1618
#include <detail/plugin.hpp>
@@ -53,6 +55,16 @@ namespace pi {
5355

5456
bool XPTIInitDone = false;
5557

58+
void contextSetExtendedDeleter(const cl::sycl::context& context,
59+
pi_context_extended_deleter func,
60+
void *user_data) {
61+
auto impl = getSyclObjImpl(context);
62+
auto contextHandle = reinterpret_cast<pi_context>(impl->getHandleRef());
63+
auto plugin = impl->getPlugin();
64+
plugin.call_nocheck<PiApiKind::piextContextSetExtendedDeleter>(
65+
contextHandle, func, user_data);
66+
}
67+
5668
std::string platformInfoToString(pi_platform_info info) {
5769
switch (info) {
5870
case PI_PLATFORM_INFO_PROFILE:

0 commit comments

Comments
 (0)