Skip to content

[SYCL][NFC] Use Impl objects instead of SYCL objects #941

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 1 commit into from
Dec 18, 2019
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
6 changes: 6 additions & 0 deletions sycl/include/CL/sycl/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ class context {
vector_class<device> get_devices() const;

private:
/// Constructs a SYCL context object from a valid context_impl instance.
context(shared_ptr_class<detail::context_impl> Impl);

shared_ptr_class<detail::context_impl> impl;
template <class Obj>
friend decltype(Obj::impl) detail::getSyclObjImpl(const Obj &SyclObject);
Expand All @@ -130,6 +133,9 @@ class context {
friend
typename std::add_pointer<typename decltype(T::impl)::element_type>::type
detail::getRawSyclObjImpl(const T &SyclObject);

template <class T>
friend T detail::createSyclObjFromImpl(decltype(T::impl) ImplObj);
};

} // namespace sycl
Expand Down
5 changes: 3 additions & 2 deletions sycl/include/CL/sycl/detail/context_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
#include <CL/sycl/detail/device_impl.hpp>
#include <CL/sycl/detail/os_util.hpp>
#include <CL/sycl/detail/pi.hpp>
#include <CL/sycl/detail/platform_impl.hpp>
#include <CL/sycl/detail/usm_dispatch.hpp>
#include <CL/sycl/exception_list.hpp>
#include <CL/sycl/info/info_desc.hpp>
#include <CL/sycl/platform.hpp>
#include <CL/sycl/stl.hpp>

#include <map>
Expand All @@ -25,6 +25,7 @@ namespace sycl {
// Forward declaration
class device;
namespace detail {
using PlatformImplPtr = std::shared_ptr<detail::platform_impl>;
class context_impl {
public:
/// Constructs a context_impl using a single SYCL devices.
Expand Down Expand Up @@ -125,7 +126,7 @@ class context_impl {
async_handler MAsyncHandler;
vector_class<device> MDevices;
RT::PiContext MContext;
platform MPlatform;
PlatformImplPtr MPlatform;
bool MPluginInterop;
bool MHostContext;
std::map<KernelSetId, RT::PiProgram> MCachedPrograms;
Expand Down
20 changes: 11 additions & 9 deletions sycl/include/CL/sycl/detail/kernel_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

#pragma once

#include <CL/sycl/context.hpp>
#include <CL/sycl/detail/common.hpp>
#include <CL/sycl/detail/context_impl.hpp>
#include <CL/sycl/detail/device_impl.hpp>
#include <CL/sycl/detail/pi.hpp>
#include <CL/sycl/device.hpp>
Expand All @@ -26,6 +26,8 @@ class program;
namespace detail {
class program_impl;

using ContextImplPtr = std::shared_ptr<detail::context_impl>;
using ProgramImplPtr = std::shared_ptr<program_impl>;
class kernel_impl {
public:
/// Constructs a SYCL kernel instance from a PiKernel
Expand All @@ -36,7 +38,7 @@ class kernel_impl {
///
/// @param Kernel is a valid PiKernel instance
/// @param SyclContext is a valid SYCL context
kernel_impl(RT::PiKernel Kernel, const context &SyclContext);
kernel_impl(RT::PiKernel Kernel, ContextImplPtr Context);

/// Constructs a SYCL kernel instance from a SYCL program and a PiKernel
///
Expand All @@ -50,16 +52,16 @@ class kernel_impl {
/// @param ProgramImpl is a valid instance of program_impl
/// @param IsCreatedFromSource is a flag that indicates whether program
/// is created from source code
kernel_impl(RT::PiKernel Kernel, const context &SyclContext,
std::shared_ptr<program_impl> ProgramImpl,
kernel_impl(RT::PiKernel Kernel, ContextImplPtr ContextImpl,
ProgramImplPtr ProgramImpl,
bool IsCreatedFromSource);

/// Constructs a SYCL kernel for host device
///
/// @param SyclContext is a valid SYCL context
/// @param ProgramImpl is a valid instance of program_impl
kernel_impl(const context &SyclContext,
std::shared_ptr<program_impl> ProgramImpl);
kernel_impl(ContextImplPtr Context,
ProgramImplPtr ProgramImpl);

~kernel_impl();

Expand All @@ -80,7 +82,7 @@ class kernel_impl {
/// Check if the associated SYCL context is a SYCL host context.
///
/// @return true if this SYCL kernel is a host kernel.
bool is_host() const { return MContext.is_host(); }
bool is_host() const { return MContext->is_host(); }

/// Query information from the kernel object using the info::kernel_info
/// descriptor.
Expand Down Expand Up @@ -138,8 +140,8 @@ class kernel_impl {

private:
RT::PiKernel MKernel;
context MContext;
std::shared_ptr<program_impl> MProgramImpl;
const ContextImplPtr MContext;
const ProgramImplPtr MProgramImpl;
bool MCreatedFromSource = true;
};

Expand Down
38 changes: 21 additions & 17 deletions sycl/include/CL/sycl/detail/program_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <CL/sycl/context.hpp>
#include <CL/sycl/detail/common.hpp>
#include <CL/sycl/detail/common_info.hpp>
#include <CL/sycl/detail/context_impl.hpp>
#include <CL/sycl/detail/kernel_desc.hpp>
#include <CL/sycl/detail/kernel_impl.hpp>
#include <CL/sycl/detail/program_manager/program_manager.hpp>
Expand All @@ -30,6 +29,8 @@ enum class program_state { none, compiled, linked };

namespace detail {

using ContextImplPtr = std::shared_ptr<detail::context_impl>;

// Used to identify the module the user code, which included this header,
// belongs to. Incurs some marginal inefficiency - there will be one copy
// per '#include "program_impl.hpp"'
Expand All @@ -39,10 +40,10 @@ class program_impl {
public:
program_impl() = delete;

explicit program_impl(const context &Context)
: program_impl(Context, Context.get_devices()) {}
explicit program_impl(ContextImplPtr Context)
: program_impl(Context, Context->get_info<info::context::devices>()) {}

program_impl(const context &Context, vector_class<device> DeviceList)
program_impl(ContextImplPtr Context, vector_class<device> DeviceList)
: Context(Context), Devices(DeviceList) {}

// Kernels caching for linked programs won't be allowed due to only compiled
Expand Down Expand Up @@ -90,14 +91,14 @@ class program_impl {
Programs.push_back(Prg->Program);
}
PI_CALL_THROW(piProgramLink, compile_program_error)(
detail::getSyclObjImpl(Context)->getHandleRef(), Devices.size(),
Context->getHandleRef(), Devices.size(),
Devices.data(), LinkOptions.c_str(), Programs.size(), Programs.data(),
nullptr, nullptr, &Program);
}
}

// Kernel caching for programs created by interoperability c-tor isn't allowed
program_impl(const context &Context, RT::PiProgram Program)
program_impl(ContextImplPtr Context, RT::PiProgram Program)
: Program(Program), Context(Context), IsLinkable(true) {

// TODO handle the case when cl_program build is in progress
Expand All @@ -108,7 +109,8 @@ class program_impl {
PI_CALL(piProgramGetInfo)(Program, CL_PROGRAM_DEVICES,
sizeof(RT::PiDevice) * NumDevices,
PiDevices.data(), nullptr);
vector_class<device> SyclContextDevices = Context.get_devices();
vector_class<device> SyclContextDevices =
Context->get_info<info::context::devices>();

// Keep only the subset of the devices (associated with context) that
// were actually used to create the program.
Expand Down Expand Up @@ -153,7 +155,7 @@ class program_impl {
PI_CALL(piProgramRetain)(Program);
}

program_impl(const context &Context, RT::PiKernel Kernel)
program_impl(ContextImplPtr Context, RT::PiKernel Kernel)
: program_impl(
Context,
ProgramManager::getInstance().getClProgramFromClKernel(Kernel)) {}
Expand All @@ -177,7 +179,7 @@ class program_impl {
RT::PiProgram &getHandleRef() { return Program; }
const RT::PiProgram &getHandleRef() const { return Program; }

bool is_host() const { return Context.is_host(); }
bool is_host() const { return Context->is_host(); }

template <typename KernelT>
void compile_with_kernel_type(string_class CompileOptions = "") {
Expand Down Expand Up @@ -210,7 +212,7 @@ class program_impl {
if (is_cacheable_with_options(BuildOptions)) {
IsProgramAndKernelCachingAllowed = true;
Program = ProgramManager::getInstance().getBuiltPIProgram(
M, Context, KernelInfo<KernelT>::getName());
M, get_context(), KernelInfo<KernelT>::getName());
PI_CALL(piProgramRetain)(Program);
} else {
create_pi_program_with_kernel_name(M, KernelInfo<KernelT>::getName());
Expand All @@ -237,7 +239,7 @@ class program_impl {
check_device_feature_support<info::device::is_linker_available>(Devices);
vector_class<RT::PiDevice> Devices(get_pi_devices());
PI_CALL_THROW(piProgramLink, compile_program_error)(
detail::getSyclObjImpl(Context)->getHandleRef(), Devices.size(),
Context->getHandleRef(), Devices.size(),
Devices.data(), LinkOptions.c_str(), 1, &Program, nullptr, nullptr,
&Program);
this->LinkOptions = LinkOptions;
Expand Down Expand Up @@ -321,7 +323,9 @@ class program_impl {
return Result;
}

context get_context() const { return Context; }
context get_context() const {
return createSyclObjFromImpl<context>(Context);
}

vector_class<device> get_devices() const { return Devices; }

Expand All @@ -348,16 +352,16 @@ class program_impl {
const string_class &KernelName) {
assert(!Program && "This program already has an encapsulated PI program");
ProgramManager &PM = ProgramManager::getInstance();
DeviceImage &Img = PM.getDeviceImage(M, KernelName, Context);
Program = PM.createPIProgram(Img, Context);
DeviceImage &Img = PM.getDeviceImage(M, KernelName, get_context());
Program = PM.createPIProgram(Img, get_context());
}

void create_cl_program_with_source(const string_class &Source) {
assert(!Program && "This program already has an encapsulated cl_program");
const char *Src = Source.c_str();
size_t Size = Source.size();
PI_CALL(piclProgramCreateWithSource)(
detail::getSyclObjImpl(Context)->getHandleRef(), 1, &Src, &Size,
Context->getHandleRef(), 1, &Src, &Size,
&Program);
}

Expand Down Expand Up @@ -428,7 +432,7 @@ class program_impl {
if (is_cacheable()) {
OSModuleHandle M = OSUtil::getOSModuleHandle(AddressInThisModule);

Kernel = ProgramManager::getInstance().getOrCreateKernel(M, Context,
Kernel = ProgramManager::getInstance().getOrCreateKernel(M, get_context(),
KernelName);
} else {
RT::PiResult Err =
Expand Down Expand Up @@ -467,7 +471,7 @@ class program_impl {

RT::PiProgram Program = nullptr;
program_state State = program_state::none;
context Context;
ContextImplPtr Context;
bool IsLinkable = false;
vector_class<device> Devices;
string_class CompileOptions;
Expand Down
38 changes: 21 additions & 17 deletions sycl/include/CL/sycl/detail/queue_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,37 @@ namespace cl {
namespace sycl {
namespace detail {

using ContextImplPtr = std::shared_ptr<detail::context_impl>;
using DeviceImplPtr = std::shared_ptr<detail::device_impl>;

// Set max number of queues supported by FPGA RT.
const size_t MaxNumQueues = 256;

enum QueueOrder { Ordered, OOO };

class queue_impl {
public:
queue_impl(const device &SyclDevice, async_handler AsyncHandler,
queue_impl(DeviceImplPtr Device, async_handler AsyncHandler,
QueueOrder Order, const property_list &PropList)
: queue_impl(SyclDevice, context(SyclDevice), AsyncHandler, Order,
PropList){};
: queue_impl(Device,
detail::getSyclObjImpl(
context(createSyclObjFromImpl<device>(Device))),
AsyncHandler, Order, PropList){};

queue_impl(const device &SyclDevice, const context &Context,
queue_impl(DeviceImplPtr Device, ContextImplPtr Context,
async_handler AsyncHandler, QueueOrder Order,
const property_list &PropList)
: m_Device(SyclDevice), m_Context(Context), m_AsyncHandler(AsyncHandler),
m_PropList(PropList), m_HostQueue(m_Device.is_host()),
: m_Device(Device), m_Context(Context), m_AsyncHandler(AsyncHandler),
m_PropList(PropList), m_HostQueue(m_Device->is_host()),
m_OpenCLInterop(!m_HostQueue) {
if (!m_HostQueue) {
m_CommandQueue = createQueue(Order);
}
}

queue_impl(cl_command_queue CLQueue, const context &SyclContext,
queue_impl(cl_command_queue CLQueue, ContextImplPtr Context,
const async_handler &AsyncHandler)
: m_Context(SyclContext), m_AsyncHandler(AsyncHandler),
: m_Context(Context), m_AsyncHandler(AsyncHandler),
m_HostQueue(false), m_OpenCLInterop(true) {

m_CommandQueue = pi::cast<RT::PiQueue>(CLQueue);
Expand All @@ -58,8 +63,7 @@ class queue_impl {
// TODO catch an exception and put it to list of asynchronous exceptions
PI_CALL(piQueueGetInfo)(m_CommandQueue, PI_QUEUE_INFO_DEVICE,
sizeof(Device), &Device, nullptr);
m_Device =
createSyclObjFromImpl<device>(std::make_shared<device_impl>(Device));
m_Device = std::make_shared<device_impl>(Device);

// TODO catch an exception and put it to list of asynchronous exceptions
PI_CALL(piQueueRetain)(m_CommandQueue);
Expand All @@ -81,13 +85,13 @@ class queue_impl {
"This instance of queue doesn't support OpenCL interoperability");
}

context get_context() const { return m_Context; }
context get_context() const { return createSyclObjFromImpl<context>(m_Context); }

ContextImplPtr get_context_impl() const {
return detail::getSyclObjImpl(m_Context);
return m_Context;
}

device get_device() const { return m_Device; }
device get_device() const { return createSyclObjFromImpl<device>(m_Device); }

bool is_host() const { return m_HostQueue; }

Expand Down Expand Up @@ -153,8 +157,8 @@ class queue_impl {
CreationFlags |= PI_QUEUE_PROFILING_ENABLE;
}
RT::PiQueue Queue;
RT::PiContext Context = detail::getSyclObjImpl(m_Context)->getHandleRef();
RT::PiDevice Device = detail::getSyclObjImpl(m_Device)->getHandleRef();
RT::PiContext Context = m_Context->getHandleRef();
RT::PiDevice Device = m_Device->getHandleRef();
RT::PiResult Error =
PI_CALL_NOCHECK(piQueueCreate)(Context, Device, CreationFlags, &Queue);

Expand Down Expand Up @@ -241,8 +245,8 @@ class queue_impl {
// Protects all the fields that can be changed by class' methods
mutex_class m_Mutex;

device m_Device;
const context m_Context;
DeviceImplPtr m_Device;
const ContextImplPtr m_Context;
vector_class<event> m_Events;
exception_list m_Exceptions;
const async_handler m_AsyncHandler;
Expand Down
9 changes: 6 additions & 3 deletions sycl/include/CL/sycl/program.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ class program {
program() = delete;

explicit program(const context &context)
: impl(std::make_shared<detail::program_impl>(context)) {}
: impl(std::make_shared<detail::program_impl>(
detail::getSyclObjImpl(context))) {}

program(const context &context, vector_class<device> deviceList)
: impl(std::make_shared<detail::program_impl>(context, deviceList)) {}
: impl(std::make_shared<detail::program_impl>(
detail::getSyclObjImpl(context), deviceList)) {}

program(vector_class<program> programList, string_class linkOptions = "") {
std::vector<std::shared_ptr<detail::program_impl>> impls;
Expand All @@ -47,7 +49,8 @@ class program {

program(const context &context, cl_program clProgram)
: impl(std::make_shared<detail::program_impl>(
context, detail::pi::cast<detail::RT::PiProgram>(clProgram))) {}
detail::getSyclObjImpl(context),
detail::pi::cast<detail::RT::PiProgram>(clProgram))) {}

program(const program &rhs) = default;

Expand Down
2 changes: 2 additions & 0 deletions sycl/source/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,7 @@ vector_class<device> context::get_devices() const {
return impl->get_info<info::context::devices>();
}

context::context(shared_ptr_class<detail::context_impl> Impl) : impl(Impl) {}

} // namespace sycl
} // namespace cl
Loading