Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion libsycl/docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,4 @@ TODO for added SYCL classes
* ``has_extension``: deprecated API, to implement on RT level with ``device::has``

* device selection: to add compatibility with old SYCL 1.2.1 device selectors, still part of SYCL 2020 specification

* ``context``: to implement get_info, properties & public constructors once context support is added to liboffload
93 changes: 93 additions & 0 deletions libsycl/include/sycl/__impl/context.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file contains the declaration of the SYCL context class, which
/// represents the runtime data structures and state required by a SYCL backend
/// API to interact with a group of devices associated with a platform.
///
//===----------------------------------------------------------------------===//

#ifndef _LIBSYCL___IMPL_CONTEXT_HPP
#define _LIBSYCL___IMPL_CONTEXT_HPP

#include <sycl/__impl/backend.hpp>
#include <sycl/__impl/info/desc_base.hpp>

#include <sycl/__impl/detail/config.hpp>
#include <sycl/__impl/detail/obj_utils.hpp>

#include <memory>
#include <vector>

_LIBSYCL_BEGIN_NAMESPACE_SYCL

class context;
class device;
class platform;

namespace detail {
class ContextImpl;
template <typename T>
using is_context_info_desc_t = typename is_info_desc<T, context>::return_type;
} // namespace detail

// SYCL 2020 4.6.3. Context class
class _LIBSYCL_EXPORT context {
public:
context(const context &rhs) = default;

context(context &&rhs) = default;

context &operator=(const context &rhs) = default;

context &operator=(context &&rhs) = default;

friend bool operator==(const context &lhs, const context &rhs) {
return lhs.impl == rhs.impl;
}

friend bool operator!=(const context &lhs, const context &rhs) {
return !(lhs == rhs);
}

/// \return the backend associated with this context.
backend get_backend() const noexcept;

/// \return the platform associated with this SYCL context.
platform get_platform() const;

/// \return a vector of valid SYCL devices associated with this SYCL context.
std::vector<device> get_devices() const;

/// Queries this SYCL context for information.
///
/// The return type depends on information being queried.
template <typename Param>
detail::is_context_info_desc_t<Param> get_info() const;

/// Queries this SYCL context for SYCL backend-specific information.
///
/// The return type depends on information being queried.
template <typename Param>
typename Param::return_type get_backend_info() const;

private:
context(const std::shared_ptr<detail::ContextImpl> &Impl) : impl(Impl) {}
std::shared_ptr<detail::ContextImpl> impl;

friend sycl::detail::ImplUtils;
}; // class context

_LIBSYCL_END_NAMESPACE_SYCL

template <>
struct std::hash<sycl::context> : public sycl::detail::HashBase<sycl::context> {
};

#endif // _LIBSYCL___IMPL_CONTEXT_HPP
2 changes: 1 addition & 1 deletion libsycl/include/sycl/__impl/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class _LIBSYCL_EXPORT device {

/// Constructs a SYCL device instance using the default device (device chosen
/// by default device selector).
device();
device() : device(default_selector_v) {}

/// Constructs a SYCL device instance using the device
/// identified by the provided device selector.
Expand Down
1 change: 1 addition & 0 deletions libsycl/include/sycl/sycl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#ifndef _LIBSYCL_SYCL_HPP
#define _LIBSYCL_SYCL_HPP

#include <sycl/__impl/context.hpp>
#include <sycl/__impl/device.hpp>
#include <sycl/__impl/device_selector.hpp>
#include <sycl/__impl/exception.hpp>
Expand Down
2 changes: 2 additions & 0 deletions libsycl/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ function(add_sycl_rt_library LIB_TARGET_NAME LIB_OBJ_NAME LIB_OUTPUT_NAME)
endfunction(add_sycl_rt_library)

set(LIBSYCL_SOURCES
"context.cpp"
"exception.cpp"
"exception_list.cpp"
"device.cpp"
"device_selector.cpp"
"platform.cpp"
"detail/context_impl.cpp"
"detail/device_impl.cpp"
"detail/global_objects.cpp"
"detail/platform_impl.cpp"
Expand Down
35 changes: 35 additions & 0 deletions libsycl/src/context.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include <sycl/__impl/device.hpp>

#include <detail/context_impl.hpp>
#include <detail/platform_impl.hpp>
Comment thread
KseniyaTikhomirova marked this conversation as resolved.

#include <cassert>

_LIBSYCL_BEGIN_NAMESPACE_SYCL

backend context::get_backend() const noexcept { return impl->getBackend(); }

platform context::get_platform() const {
return detail::createSyclObjFromImpl<platform>(impl->getPlatformImpl());
}

std::vector<device> context::get_devices() const {
std::vector<device> Devices;

impl->iterateDevices([&Devices](detail::DeviceImpl *DevImpl) {
assert(DevImpl && "Device impl can't be nullptr");
Devices.push_back(detail::createSyclObjFromImpl<device>(*DevImpl));
});

return Devices;
}

_LIBSYCL_END_NAMESPACE_SYCL
27 changes: 27 additions & 0 deletions libsycl/src/detail/context_impl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include <detail/context_impl.hpp>
#include <detail/platform_impl.hpp>

_LIBSYCL_BEGIN_NAMESPACE_SYCL

namespace detail {

void ContextImpl::iterateDevices(
const std::function<void(DeviceImpl *)> &callback) const {
// TODO: Intentionally don't store devices in context now. This class should
// be reimplemented once liboffload adds context support. Treat context as
// default context that is associated with all devices in the platform.
return MPlatform.iterateDevices(info::device_type::all, callback);
}

backend ContextImpl::getBackend() const { return MPlatform.getBackend(); }

} // namespace detail
_LIBSYCL_END_NAMESPACE_SYCL
71 changes: 71 additions & 0 deletions libsycl/src/detail/context_impl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef _LIBSYCL_CONTEXT_IMPL
#define _LIBSYCL_CONTEXT_IMPL

#include <sycl/__impl/context.hpp>
#include <sycl/__impl/detail/config.hpp>

#include <OffloadAPI.h>

#include <functional>

_LIBSYCL_BEGIN_NAMESPACE_SYCL
namespace detail {

class PlatformImpl;
class DeviceImpl;

/// Context dummy (w/o liboffload handle) that represents all devices
/// in platform.
Comment thread
bader marked this conversation as resolved.
Outdated
///
/// Presence of context object is essential for many APIs. This dummy is a way
/// to support them in case of absence of context support in liboffload. For
/// backends where context exists and participates in operations liboffload
/// plugins create and use default context that represents all devices in that
/// platform. Duplicating this logic here.
class ContextImpl : public std::enable_shared_from_this<ContextImpl> {
struct Private {
explicit Private() = default;
};

public:
/// Constructs a ContextImpl using a platform.
///
/// Newly created instance represents all devices in platform.
///
/// \param Platform is a platform to associate this context with.
ContextImpl(PlatformImpl &Platform, Private) : MPlatform(Platform) {}

/// Constructs a ContextImpl with a provided arguments. Variadic helper.
/// Restrics ways of ContextImpl creation.
template <typename... Ts>
static std::shared_ptr<ContextImpl> create(Ts &&...args) {
return std::make_shared<ContextImpl>(std::forward<Ts>(args)..., Private{});
}

/// \return the platform this context is associated with.
PlatformImpl &getPlatformImpl() const { return MPlatform; }

/// Calls "callback" with every device associated
/// with this context.
void iterateDevices(const std::function<void(DeviceImpl *)> &callback) const;

/// \return backend of the platform this context is associated with.
backend getBackend() const;

private:
PlatformImpl &MPlatform;
};

} // namespace detail

_LIBSYCL_END_NAMESPACE_SYCL

#endif // _LIBSYCL_CONTEXT_IMPL
6 changes: 3 additions & 3 deletions libsycl/src/detail/device_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ bool DeviceImpl::has(aspect Aspect) const {
case (aspect::accelerator):
return isAccelerator();
case (aspect::custom):
return false;
case (aspect::emulated):
return false;
case (aspect::host_debuggable):
return false;
default:
Expand All @@ -49,7 +47,9 @@ bool DeviceImpl::isAccelerator() const {
return getDeviceType() == info::device_type::accelerator;
}

backend DeviceImpl::getBackend() const { return MPlatform.getBackend(); }
backend DeviceImpl::getBackend() const noexcept {
return MPlatform.getBackend();
}

} // namespace detail
_LIBSYCL_END_NAMESPACE_SYCL
4 changes: 3 additions & 1 deletion libsycl/src/detail/device_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class DeviceImpl {
/// Returns the backend associated with this device.
///
/// \return the sycl::backend associated with this device.
backend getBackend() const;
backend getBackend() const noexcept;

/// Returns the implementation class object of platform associated with this
/// device.
Expand Down Expand Up @@ -115,6 +115,8 @@ class DeviceImpl {
static_assert(false && "Info descriptor is not properly supported");
}

ol_device_handle_t getOLHandle() { return MOffloadDevice; }

private:
ol_device_handle_t MOffloadDevice = {};
PlatformImpl &MPlatform;
Expand Down
4 changes: 3 additions & 1 deletion libsycl/src/detail/offload/offload_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ inline std::string formatCodeString(ol_result_t Result) {
std::string(stringifyErrorCode(Result->Code)) + ") " + Result->Details;
}

inline bool isFailed(const ol_result_t &Result) { return Result != OL_SUCCESS; }

/// Checks liboffload API call result.
///
/// Used after calling the API without check.
Expand All @@ -48,7 +50,7 @@ inline std::string formatCodeString(ol_result_t Result) {
/// \throw sycl::runtime_exception if the call was not successful.
template <sycl::errc errc = sycl::errc::runtime>
void checkAndThrow(ol_result_t Result) {
if (Result != OL_SUCCESS) {
if (isFailed(Result)) {
throw sycl::exception(sycl::make_error_code(errc),
detail::formatCodeString(Result));
}
Expand Down
9 changes: 9 additions & 0 deletions libsycl/src/detail/platform_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <sycl/__impl/detail/config.hpp>
#include <sycl/__impl/detail/obj_utils.hpp>

#include <detail/context_impl.hpp>
#include <detail/device_impl.hpp>
#include <detail/global_objects.hpp>
#include <detail/platform_impl.hpp>
Expand Down Expand Up @@ -75,6 +76,8 @@ PlatformImpl::PlatformImpl(ol_platform_handle_t Platform, size_t PlatformIndex,
MRootDevices.emplace_back(std::make_unique<DeviceImpl>(
Device, *this, DeviceImpl::PrivateTag{}));
});

MDefaultContext = ContextImpl::create(*this);
}

const std::vector<DeviceImplUPtr> &PlatformImpl::getRootDevices() const {
Expand Down Expand Up @@ -119,5 +122,11 @@ void PlatformImpl::iterateDevices(
}
}

ContextImpl &PlatformImpl::getDefaultContext() {
assert(MDefaultContext &&
"Default context for platform must be created in platform ctor");
return *MDefaultContext.get();
}

} // namespace detail
_LIBSYCL_END_NAMESPACE_SYCL
21 changes: 15 additions & 6 deletions libsycl/src/detail/platform_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ _LIBSYCL_BEGIN_NAMESPACE_SYCL
namespace detail {

class DeviceImpl;
class ContextImpl;

using PlatformImplUPtr = std::unique_ptr<PlatformImpl>;
using DeviceImplUPtr = std::unique_ptr<DeviceImpl>;
Expand All @@ -52,8 +53,6 @@ class PlatformImpl {

~PlatformImpl() = default;

/// Returns the backend associated with this platform.
///
/// \returns sycl::backend associated with this platform.
backend getBackend() const noexcept { return MBackend; }

Expand Down Expand Up @@ -121,15 +120,25 @@ class PlatformImpl {
void iterateDevices(info::device_type DeviceType,
std::function<void(DeviceImpl *)> callback) const;

// TODO: liboffload doesn't support context now, l0 plugin creates default
// context for all devices on its level. This method should be removed or
// reimplemented once native context support is added to liboffload.
/// \return the default context that represents all devices in platform.
ContextImpl &getDefaultContext();

private:
/// \return reference to collection of root devices for platform
const std::vector<DeviceImplUPtr> &getRootDevices() const;

ol_platform_handle_t MOffloadPlatform{};
size_t MOffloadPlatformIndex{};
ol_platform_backend_t MOffloadBackend{OL_PLATFORM_BACKEND_UNKNOWN};
backend MBackend{};
const ol_platform_handle_t MOffloadPlatform{};
const size_t MOffloadPlatformIndex{};

ol_platform_backend_t MOffloadBackend;
Comment thread
KseniyaTikhomirova marked this conversation as resolved.
Outdated
backend MBackend;

std::vector<DeviceImplUPtr> MRootDevices;

std::shared_ptr<ContextImpl> MDefaultContext;
};

} // namespace detail
Expand Down
Loading
Loading