Skip to content

[SYCL] populate PI (and direct SYCL RT to PI) #259

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 4 commits into from
Jul 10, 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
10 changes: 10 additions & 0 deletions sycl/doc/SYCL_environment_variables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Overview

This file describes environment variables that are having effect on SYCL compiler and run-time.

# Controlling SYCL RT

| Environment variable | Description |
| ----------- | ----------- |
| SYCL_PI_TRACE | If set forces tracing of PI calls to stdout. |
| SYCL_BE={PI_OPENCL,PI_OTHER} | When SYCL RT is buils with PI this controls which plugin to use. |
6 changes: 4 additions & 2 deletions sycl/include/CL/sycl/buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,10 @@ class buffer {
event AvailableEvent = {}) {

size_t BufSize = 0;
CHECK_OCL_CODE(clGetMemObjectInfo(MemObject, CL_MEM_SIZE, sizeof(size_t),
&BufSize, nullptr));
PI_CALL(detail::RT::piMemGetInfo(
detail::pi_cast<detail::RT::PiMem>(MemObject), CL_MEM_SIZE,
sizeof(size_t), &BufSize, nullptr));

Range[0] = BufSize / sizeof(T);
MemRange[0] = BufSize / sizeof(T);
impl = std::make_shared<detail::buffer_impl<AllocatorT>>(
Expand Down
Empty file modified sycl/include/CL/sycl/detail/aligned_allocator.hpp
100644 → 100755
Empty file.
19 changes: 11 additions & 8 deletions sycl/include/CL/sycl/detail/buffer_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <CL/sycl/context.hpp>
#include <CL/sycl/detail/aligned_allocator.hpp>
#include <CL/sycl/detail/common.hpp>
#include <CL/sycl/detail/pi.hpp>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, keep includes sorted.
Usually these types of issues are handled automatically by clang-format tool.

#include <CL/sycl/detail/helpers.hpp>
#include <CL/sycl/detail/memory_manager.hpp>
#include <CL/sycl/detail/scheduler/scheduler.hpp>
Expand Down Expand Up @@ -190,13 +191,15 @@ template <typename AllocatorT> class buffer_impl : public SYCLMemObjT {
"Creation of interoperability buffer using host context is not "
"allowed");

cl_context Context = nullptr;
CHECK_OCL_CODE(clGetMemObjectInfo(MInteropMemObject, CL_MEM_CONTEXT,
sizeof(Context), &Context, nullptr));
RT::PiMem Mem = pi_cast<RT::PiMem>(MInteropMemObject);
RT::PiContext Context = nullptr;
PI_CALL(RT::piMemGetInfo(
Mem, CL_MEM_CONTEXT, sizeof(Context), &Context, nullptr));

if (MInteropContext->getHandleRef() != Context)
throw cl::sycl::invalid_parameter_error(
"Input context must be the same as the context of cl_mem");
CHECK_OCL_CODE(clRetainMemObject(MInteropMemObject));
PI_CALL(RT::piMemRetain(Mem));
}

size_t get_size() const { return MSizeInBytes; }
Expand All @@ -214,7 +217,7 @@ template <typename AllocatorT> class buffer_impl : public SYCLMemObjT {
releaseHostMem(MShadowCopy);

if (MOpenCLInterop)
CHECK_OCL_CODE_NO_EXC(clReleaseMemObject(MInteropMemObject));
PI_CALL(RT::piMemRelease(pi_cast<RT::PiMem>(MInteropMemObject)));
}

void set_final_data(std::nullptr_t) { MUploadDataFn = nullptr; }
Expand Down Expand Up @@ -251,7 +254,7 @@ template <typename AllocatorT> class buffer_impl : public SYCLMemObjT {
typename std::enable_if<std::is_pointer<Destination>::value>::type * =
0) {
static_assert(!std::is_const<Destination>::value,
"Сan not write in a constant Destination. Destination should "
"Do not write in a constant Destination. Destination should "
"not be const.");
MUploadDataFn = [this, FinalData]() mutable {

Expand All @@ -273,7 +276,7 @@ template <typename AllocatorT> class buffer_impl : public SYCLMemObjT {
typename std::enable_if<!std::is_pointer<Destination>::value>::type * =
0) {
static_assert(!std::is_const<Destination>::value,
"Сan not write in a constant Destination. Destination should "
"Do not write in a constant Destination. Destination should "
"not be const.");
MUploadDataFn = [this, FinalData]() mutable {
using FinalDataType =
Expand Down Expand Up @@ -348,7 +351,7 @@ template <typename AllocatorT> class buffer_impl : public SYCLMemObjT {
}

void *allocateMem(ContextImplPtr Context, bool InitFromUserData,
cl_event &OutEventToWait) override {
RT::PiEvent &OutEventToWait) override {

void *UserPtr = InitFromUserData ? getUserPtr() : nullptr;

Expand Down
27 changes: 2 additions & 25 deletions sycl/include/CL/sycl/detail/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,14 @@
#include <string>
#include <type_traits>

// Select underlying runtime interface in compile-time (OpenCL or PI).
// Comment the define of the FORCE_SYCL_BE_OPENCL below to switch to PI.
// As such only one path (OpenCL today) is being regularily tested.
//
// TODO: we can just remove this when switch to PI completely.
//
#define FORCE_SYCL_BE_OPENCL

#ifdef FORCE_SYCL_BE_OPENCL
#include <CL/sycl/detail/pi_opencl.hpp>
#else
#include <CL/sycl/detail/pi.hpp>
#endif
#define STRINGIFY_LINE_HELP(s) #s
#define STRINGIFY_LINE(s) STRINGIFY_LINE_HELP(s)

const char *stringifyErrorCode(cl_int error);

#define OCL_CODE_TO_STR(code) \
std::string(std::to_string(code) + " (" + stringifyErrorCode(code) + ")")

#define STRINGIFY_LINE_HELP(s) #s
#define STRINGIFY_LINE(s) STRINGIFY_LINE_HELP(s)

#define OCL_ERROR_REPORT \
"OpenCL API failed. " __FILE__ \
":" STRINGIFY_LINE(__LINE__) ": " \
Expand Down Expand Up @@ -92,15 +78,6 @@ namespace cl {
namespace sycl {
namespace detail {

// Select underlying runtime interface (RT) in compile-time (OpenCL or PI).
// As such only one path (OpenCL today) is being regularily tested.
//
#ifdef FORCE_SYCL_BE_OPENCL
using RT = cl::sycl::detail::opencl;
#else
using RT = cl::sycl::detail::pi;
#endif

// Helper function for extracting implementation from SYCL's interface objects.
// Note! This function relies on the fact that all SYCL interface classes
// contain "impl" field that points to implementation object. "impl" field
Expand Down
7 changes: 4 additions & 3 deletions sycl/include/CL/sycl/detail/context_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#pragma once
#include <CL/sycl/detail/common.hpp>
#include <CL/sycl/detail/pi.hpp>
#include <CL/sycl/exception.hpp>
#include <CL/sycl/info/info_desc.hpp>
#include <CL/sycl/platform.hpp>
Expand Down Expand Up @@ -50,13 +51,13 @@ class context_impl {
// modification. Caller must ensure the returned object lives on stack only.
// It can also be safely passed to the underlying native runtime API.
// Warning. Returned reference will be invalid if context_impl was destroyed.
cl_context &getHandleRef();
const cl_context &getHandleRef() const;
RT::PiContext &getHandleRef();
const RT::PiContext &getHandleRef() const;

private:
async_handler m_AsyncHandler;
vector_class<device> m_Devices;
cl_context m_ClContext;
RT::PiContext m_Context;
platform m_Platform;
bool m_OpenCLInterop;
bool m_HostContext;
Expand Down
8 changes: 4 additions & 4 deletions sycl/include/CL/sycl/detail/context_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ namespace cl {
namespace sycl {
namespace detail {

template <info::context param> struct get_context_info_cl {
template <info::context param> struct get_context_info {
using RetType =
typename info::param_traits<info::context, param>::return_type;

static RetType _(cl_context ctx) {
static RetType _(RT::PiContext ctx) {
RetType Result = 0;
// TODO catch an exception and put it to list of asynchronous exceptions
CHECK_OCL_CODE(clGetContextInfo(ctx, cl_context_info(param), sizeof(Result),
&Result, nullptr));
PI_CALL(RT::piContextGetInfo(ctx, pi_cast<pi_context_info>(param),
sizeof(Result), &Result, nullptr));
return Result;
}
};
Expand Down
39 changes: 16 additions & 23 deletions sycl/include/CL/sycl/detail/device_impl.hpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#pragma once

#include <CL/sycl/detail/pi.hpp>
#include <CL/sycl/detail/device_info.hpp>
#include <CL/sycl/stl.hpp>
#include <algorithm>
Expand All @@ -34,10 +35,8 @@ class device_impl {
// It can also be safely passed to the underlying native runtime API.
// Warning. Returned reference will be invalid if device_impl was destroyed.
//
// TODO: change all uses of getHandleRef to get_handle, and remove the
// getHandleRef after that.
virtual cl_device_id &getHandleRef() = 0;
virtual RT::pi_device get_handle() const = 0;
virtual RT::PiDevice &getHandleRef() = 0;
virtual const RT::PiDevice &getHandleRef() const = 0;

virtual bool is_host() const = 0;

Expand Down Expand Up @@ -68,7 +67,7 @@ class device_impl {
}
return get_device_info<
typename info::param_traits<info::device, param>::return_type,
param>::_(this->get_handle());
param>::_(this->getHandleRef());
}

bool is_partition_supported(info::partition_property Prop) const {
Expand All @@ -93,15 +92,15 @@ class device_impl {
// TODO: Make code thread-safe
class device_impl_pi : public device_impl {
public:
explicit device_impl_pi(RT::pi_device a_device) : m_device(a_device) {
explicit device_impl_pi(RT::PiDevice a_device) : m_device(a_device) {
// TODO catch an exception and put it to list of asynchronous exceptions
PI_CALL(RT::piDeviceGetInfo(
m_device, PI_DEVICE_INFO_TYPE, sizeof(RT::pi_device_type), &m_type, 0));
m_device, PI_DEVICE_INFO_TYPE, sizeof(RT::PiDeviceType), &m_type, 0));

RT::pi_device parent;
RT::PiDevice parent;
// TODO catch an exception and put it to list of asynchronous exceptions
PI_CALL(RT::piDeviceGetInfo(
m_device, PI_DEVICE_INFO_PARENT, sizeof(RT::pi_device), &parent, 0));
m_device, PI_DEVICE_INFO_PARENT, sizeof(RT::PiDevice), &parent, 0));

m_isRootDevice = (nullptr == parent);
if (!m_isRootDevice) {
Expand All @@ -126,14 +125,8 @@ class device_impl_pi : public device_impl {
return pi_cast<cl_device_id>(m_device);
}

cl_device_id &getHandleRef() override {
// TODO: check that device is an OpenCL interop one before cast, or just
// remove when all the users are moved to get_handle.
return (cl_device_id&)(m_device);
}
RT::pi_device get_handle() const override {
return m_device;
}
RT::PiDevice &getHandleRef() override { return m_device; }
const RT::PiDevice &getHandleRef() const override { return m_device; }

bool is_host() const override { return false; }

Expand All @@ -146,7 +139,7 @@ class device_impl_pi : public device_impl {
}

platform get_platform() const override {
RT::pi_platform plt;
RT::PiPlatform plt;
// TODO catch an exception and put it to list of asynchronous exceptions
PI_CALL(RT::piDeviceGetInfo(
m_device, PI_DEVICE_INFO_PLATFORM, sizeof(plt), &plt, 0));
Expand Down Expand Up @@ -178,8 +171,8 @@ class device_impl_pi : public device_impl {
create_sub_devices(info::partition_affinity_domain AffinityDomain) const override;

private:
RT::pi_device m_device = 0;
RT::pi_device_type m_type;
RT::PiDevice m_device = 0;
RT::PiDeviceType m_type;
bool m_isRootDevice = false;
}; // class device_impl_pi

Expand All @@ -192,11 +185,11 @@ class device_host : public device_impl {
cl_device_id get() const override {
throw invalid_object_error("This instance of device is a host instance");
}
cl_device_id &getHandleRef() override {
RT::PiDevice &getHandleRef() override {
throw invalid_object_error("This instance of device is a host instance");
}
RT::pi_device get_handle() const override {
pi_die("This instance of device is a host instance");
const RT::PiDevice &getHandleRef() const override {
throw invalid_object_error("This instance of device is a host instance");
}

bool is_host() const override { return true; }
Expand Down
Loading