Skip to content

[SYCL][CUDA] Expose context extended deleters on PI API #1483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sycl/include/CL/sycl/detail/pi.def
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ _PI_API(piContextCreate)
_PI_API(piContextGetInfo)
_PI_API(piContextRetain)
_PI_API(piContextRelease)
_PI_API(piextContextSetExtendedDeleter)
// Queue
_PI_API(piQueueCreate)
_PI_API(piQueueGetInfo)
Expand Down
6 changes: 6 additions & 0 deletions sycl/include/CL/sycl/detail/pi.h
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,12 @@ pi_result piContextRetain(pi_context context);

pi_result piContextRelease(pi_context context);

typedef void (*pi_context_extended_deleter)(void *user_data);

pi_result piextContextSetExtendedDeleter(pi_context context,
pi_context_extended_deleter func,
void *user_data);

//
// Queue
//
Expand Down
7 changes: 7 additions & 0 deletions sycl/include/CL/sycl/detail/pi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ struct trace_event_data_t;

__SYCL_INLINE_NAMESPACE(cl) {
namespace sycl {

class context;

namespace detail {

enum class PiApiKind {
Expand Down Expand Up @@ -95,6 +98,10 @@ using PiMemObjectType = ::pi_mem_type;
using PiMemImageChannelOrder = ::pi_image_channel_order;
using PiMemImageChannelType = ::pi_image_channel_type;

void contextSetExtendedDeleter(const cl::sycl::context &constext,
pi_context_extended_deleter func,
void *user_data);

// Function to load the shared library
// Implementation is OS dependent.
void *loadOsLibrary(const std::string &Library);
Expand Down
9 changes: 8 additions & 1 deletion sycl/plugins/cuda/pi_cuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,12 @@ pi_result cuda_piContextRetain(pi_context context) {
return PI_SUCCESS;
}

pi_result cuda_piextContextSetExtendedDeleter(
pi_context context, pi_context_extended_deleter function, void *user_data) {
context->set_extended_deleter(function, user_data);
return PI_SUCCESS;
}

/// Not applicable to CUDA, devices cannot be partitioned.
///
pi_result cuda_piDevicePartition(
Expand Down Expand Up @@ -1462,7 +1468,7 @@ pi_result cuda_piContextRelease(pi_context ctxt) {
if (ctxt->decrement_reference_count() > 0) {
return PI_SUCCESS;
}
ctxt->invoke_callback();
ctxt->invoke_extended_deleters();

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

Expand Down Expand Up @@ -3586,6 +3592,7 @@ pi_result piPluginInit(pi_plugin *PluginInit) {
_PI_CL(piextDeviceSelectBinary, cuda_piextDeviceSelectBinary)
_PI_CL(piextGetDeviceFunctionPointer, cuda_piextGetDeviceFunctionPointer)
// Context
_PI_CL(piextContextSetExtendedDeleter, cuda_piextContextSetExtendedDeleter)
_PI_CL(piContextCreate, cuda_piContextCreate)
_PI_CL(piContextGetInfo, cuda_piContextGetInfo)
_PI_CL(piContextRetain, cuda_piContextRetain)
Expand Down
25 changes: 15 additions & 10 deletions sycl/plugins/cuda/pi_cuda.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ class _pi_device {
/// See proposal for details.
///
struct _pi_context {

struct deleter_data {
pi_context_extended_deleter function;
void *user_data;

void operator()() { function(user_data); }
};

using native_type = CUcontext;

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

~_pi_context() { cuda_piDeviceRelease(deviceId_); }

void invoke_callback()
{
void invoke_extended_deleters() {
std::lock_guard<std::mutex> guard(mutex_);
for(const auto& callback : destruction_callbacks_)
{
callback();
for (auto &deleter : extended_deleters_) {
deleter();
}
}

template<typename Func>
void register_callback(Func&& callback)
{
void set_extended_deleter(pi_context_extended_deleter function,
void *user_data) {
std::lock_guard<std::mutex> guard(mutex_);
destruction_callbacks_.emplace_back(std::forward<Func>(callback));
extended_deleters_.emplace_back(deleter_data{function, user_data});
}

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

private:
std::mutex mutex_;
std::vector<std::function<void(void)>> destruction_callbacks_;
std::vector<deleter_data> extended_deleters_;
};

/// PI Mem mapping to a CUDA memory allocation
Expand Down
12 changes: 12 additions & 0 deletions sycl/source/detail/pi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
///
/// \ingroup sycl_pi

#include "context_impl.hpp"
#include <CL/sycl/context.hpp>
#include <CL/sycl/detail/common.hpp>
#include <CL/sycl/detail/pi.hpp>
#include <detail/plugin.hpp>
Expand Down Expand Up @@ -53,6 +55,16 @@ namespace pi {

bool XPTIInitDone = false;

void contextSetExtendedDeleter(const cl::sycl::context &context,
pi_context_extended_deleter func,
void *user_data) {
auto impl = getSyclObjImpl(context);
auto contextHandle = reinterpret_cast<pi_context>(impl->getHandleRef());
auto plugin = impl->getPlugin();
plugin.call_nocheck<PiApiKind::piextContextSetExtendedDeleter>(
contextHandle, func, user_data);
}

std::string platformInfoToString(pi_platform_info info) {
switch (info) {
case PI_PLATFORM_INFO_PROFILE:
Expand Down