Skip to content

[SYCL] Add the notion of default contexts. #2805

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 52 commits into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
7d53b82
Add the notion of a default context to a platform. Queue creation wi…
jbrodman Nov 19, 2020
664e6e1
Update ABI symbols
jbrodman Nov 19, 2020
4292706
Update tests to the new behavior
jbrodman Nov 19, 2020
8ef4616
Update unit test for the new behavior
jbrodman Nov 19, 2020
8d5bc04
Make contexts in interop test explicit
jbrodman Nov 20, 2020
0bb715e
Change tests to use explicit contexts to keep exercising the original…
jbrodman Nov 20, 2020
f13c910
Add on-device tests and error when popping default context stack woul…
jbrodman Nov 20, 2020
ef1592b
Merge branch 'sycl' into defaultctxts
jbrodman Jan 22, 2021
634af84
Formatting/style changes
jbrodman Jan 22, 2021
5e55c1c
clang-format tests
jbrodman Jan 22, 2021
76c94be
Add extension doc for platform contexts
jbrodman Jan 25, 2021
ea071c9
Add mutex and update ABI
jbrodman Jan 27, 2021
6db087f
Update spec to have a table
jbrodman Jan 27, 2021
bedea74
Merge branch 'sycl' into defaultctxts
jbrodman Mar 17, 2021
5546f9b
Remove push/pop methods
jbrodman Mar 17, 2021
76d9322
Remove tests for push/pop
jbrodman Mar 17, 2021
1b98a82
Address feedback. Change vector of contexts to a single one.
jbrodman May 18, 2021
774a0be
Add Platform Context extension to README.md
jbrodman May 18, 2021
275ff5c
Merge branch 'sycl' into defaultctxts
jbrodman Jun 9, 2021
fc86ff0
Fix logic bug. Returning nullptr all the time wasn't the intended beh…
jbrodman Jun 9, 2021
c5d9ae7
Remove auto in a function
jbrodman Jun 9, 2021
e14ccad
Merge branch 'sycl' into defaultctxts
jbrodman Jun 10, 2021
c576a08
Change to using shared_ptr of context_impl, but keep context() logic
jbrodman Jun 10, 2021
82ab84a
Better conform to extension guidelines.
jbrodman Jun 11, 2021
dd790df
Merge branch 'sycl' into defaultctxts
jbrodman Jun 14, 2021
c191b8a
Handle host default context slightly specially
jbrodman Jun 30, 2021
dbfc5e3
Merge remote-tracking branch 'public/sycl' into defaultctxts
Jul 2, 2021
6654c0a
Fix issue
Jul 2, 2021
b9f9b66
Merge remote-tracking branch 'public/sycl' into HEAD
romanovvlad Jul 7, 2021
53d11ea
draft
romanovvlad Jul 8, 2021
1a6a082
Merge remote-tracking branch 'public/sycl' into private/vromanov/FixC…
romanovvlad Jul 11, 2021
5ec61b0
remove leftovers
romanovvlad Jul 11, 2021
9b4f707
Apply comments
romanovvlad Jul 11, 2021
914710d
Add missing include
romanovvlad Jul 11, 2021
6d3868c
clang-format
romanovvlad Jul 11, 2021
f6f1806
Avoid using default context when creating host queue
romanovvlad Jul 12, 2021
ac3aa59
Do not use default contexts for sub-devices
romanovvlad Jul 12, 2021
b3012a3
Release default context cache before releasing plugins because cache
romanovvlad Jul 13, 2021
3463c1e
Workaround CUDA fails during shutdown
romanovvlad Jul 14, 2021
ca6640e
Fix workaround for cuda
romanovvlad Jul 14, 2021
78cc85b
Test treating CUDA_ERROR_DEINITIALIZED as succsess in all cases
romanovvlad Jul 19, 2021
32462b5
Make default contexts optional
Aug 25, 2021
def28c0
Add docs
Aug 25, 2021
9adb576
Merge branch 'sycl' into defaultctxts
Aug 25, 2021
91a4882
Address test failures and review comments
Aug 25, 2021
9ef815e
fix env var cache
Aug 26, 2021
a976215
Update sycl/unittests/misc/CMakeLists.txt
alexbatashev Aug 26, 2021
9f4afb7
Fix some issues
Aug 30, 2021
c283796
Merge branch 'defaultctxts' of github.com:jbrodman/llvm into defaultc…
Aug 30, 2021
12504a1
fix rest of the issues
Aug 30, 2021
cc6cbc8
Merge remote-tracking branch 'upstream/sycl' into defaultctxts
Sep 1, 2021
08c72e7
Update sycl/source/detail/queue_impl.hpp
alexbatashev Sep 8, 2021
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/doc/EnvironmentVariables.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ subject to change. Do not rely on these variables in production code.
| `SYCL_CACHE_THRESHOLD` | Positive integer | Cache eviction threshold in days (default value is 7 for 1 week). Set to 0 for disabling time-based cache eviction. |
| `SYCL_CACHE_MIN_DEVICE_IMAGE_SIZE` | Positive integer | Minimum size of device code image in bytes which is reasonable to cache on disk because disk access operation may take more time than do JIT compilation for it. Default value is 0 to cache all images. |
| `SYCL_CACHE_MAX_DEVICE_IMAGE_SIZE` | Positive integer | Maximum size of device image in bytes which is cached. Too big kernels may overload disk too fast. Default value is 1 GB. |
| `SYCL_ENABLE_DEFAULT_CONTEXTS` | '1' or '0' | Enable ('1') or disable ('0') creation of default platform contexts in SYCL runtime. The default context for each platform contains all devices in the platform. Refer to [Platform Default Contexts](extensions/PlatformContext/PlatformContext.adoc) extension to learn more. Enabled by default on Linux and disabled on Windows. |

`(*) Note: Any means this environment variable is effective when set to any non-null value.`

Expand Down
34 changes: 34 additions & 0 deletions sycl/doc/extensions/PlatformContext/PlatformContext.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
= SYCL(TM) Proposals: Platform Default Contexts
James Brodman <james.brodman@intel.com>
v0.1
:source-highlighter: pygments
:icons: font
:y: icon:check[role="green"]
:n: icon:times[role="red"]

== Platform Default Contexts

This extension adds the notion of a default SYCL context per SYCL platform. The default context for each platform contains all devices in the platform.

The platform class gains one new method:

[cols="^60a,40"]
|===
| Member Function | Description

|
[source,c++]
----
context ext_oneapi_get_default_context()
----

| Returns the current default context for this `platform`

|===

This extension also modifies the behavior of `queue` constructors. Queues will no longer create a new `context` upon construction. Instead, they will use the default context from the device's platform.

== Feature Test Macro

This extension defines the macro `SYCL_EXT_ONEAPI_DEFAULT_CONTEXT` to `1` to indicate that it is enabled.

1 change: 1 addition & 0 deletions sycl/doc/extensions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ DPC++ extensions status:
| [Use Pinned Memory Property](UsePinnedMemoryProperty/UsePinnedMemoryPropery.adoc) | Supported | |
| [Level-Zero backend specification](LevelZeroBackend/LevelZeroBackend.md) | Supported | |
| [ITT annotations support](ITTAnnotations/ITTAnnotations.rst) | Supported | |
| [Platform Context](PlatformContext/PlatformContext.adoc) | Proposal | |
| [SYCL_EXT_ONEAPI_DEVICE_IF](DeviceIf/device_if.asciidoc) | Proposal | |
| [SYCL_INTEL_group_sort](GroupAlgorithms/SYCL_INTEL_group_sort.asciidoc) | Proposal | |
| [Invoke SIMD](InvokeSIMD/InvokeSIMD.asciidoc) | Proposal | |
Expand Down
10 changes: 10 additions & 0 deletions sycl/include/CL/sycl/platform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
//===----------------------------------------------------------------------===//

#pragma once

#include <CL/sycl/aspects.hpp>
#include <CL/sycl/context.hpp>
#include <CL/sycl/detail/common.hpp>
#include <CL/sycl/detail/export.hpp>
#include <CL/sycl/stl.hpp>
Expand All @@ -25,6 +27,9 @@ namespace detail {
class platform_impl;
}

// Feature test macro for Default Context
#define SYCL_EXT_ONEAPI_DEFAULT_CONTEXT 1

/// Encapsulates a SYCL platform on which kernels may be executed.
///
/// \ingroup sycl_api
Expand Down Expand Up @@ -131,6 +136,11 @@ class __SYCL_EXPORT platform {
/// given feature.
bool has(aspect Aspect) const;

/// Return this platform's default context
///
/// \return the default context
context ext_oneapi_get_default_context() const;

private:
pi_native_handle getNative() const;

Expand Down
2 changes: 1 addition & 1 deletion sycl/plugins/cuda/pi_cuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pi_result forLatestEvents(const pi_event *event_wait_list,
///
pi_result check_error(CUresult result, const char *function, int line,
const char *file) {
if (result == CUDA_SUCCESS) {
if (result == CUDA_SUCCESS || result == CUDA_ERROR_DEINITIALIZED) {
return PI_SUCCESS;
}

Expand Down
1 change: 1 addition & 0 deletions sycl/source/detail/config.def
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ CONFIG(SYCL_OVERRIDE_PI_OPENCL, 1024, __SYCL_OVERRIDE_PI_OPENCL)
CONFIG(SYCL_OVERRIDE_PI_LEVEL_ZERO, 1024, __SYCL_OVERRIDE_PI_LEVEL_ZERO)
CONFIG(SYCL_OVERRIDE_PI_CUDA, 1024, __SYCL_OVERRIDE_PI_CUDA)
CONFIG(SYCL_OVERRIDE_PI_ROCM, 1024, __SYCL_OVERRIDE_PI_ROCM)
CONFIG(SYCL_ENABLE_DEFAULT_CONTEXTS, 1, __SYCL_ENABLE_DEFAULT_CONTEXTS)
32 changes: 32 additions & 0 deletions sycl/source/detail/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,38 @@ template <> class SYCLConfig<SYCL_DEVICE_FILTER> {
}
};

template <> class SYCLConfig<SYCL_ENABLE_DEFAULT_CONTEXTS> {
using BaseT = SYCLConfigBase<SYCL_ENABLE_DEFAULT_CONTEXTS>;

public:
static bool get() {
#ifdef WIN32
constexpr bool DefaultValue = false;
#else
constexpr bool DefaultValue = true;
#endif

const char *ValStr = getCachedValue();

if (!ValStr)
return DefaultValue;

return ValStr[0] == '1';
}

static void reset() { (void)getCachedValue(/*ResetCache=*/true); }

static const char *getName() { return BaseT::MConfigName; }

private:
static const char *getCachedValue(bool ResetCache = false) {
static const char *ValStr = BaseT::getRawValue();
if (ResetCache)
ValStr = BaseT::getRawValue();
return ValStr;
}
};

} // namespace detail
} // namespace sycl
} // __SYCL_INLINE_NAMESPACE(cl)
19 changes: 19 additions & 0 deletions sycl/source/detail/global_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ ProgramManager &GlobalHandler::getProgramManager() {
return getOrCreate(MProgramManager);
}

std::unordered_map<PlatformImplPtr, ContextImplPtr> &
GlobalHandler::getPlatformToDefaultContextCache() {
return getOrCreate(MPlatformToDefaultContextCache);
}

std::mutex &GlobalHandler::getPlatformToDefaultContextCacheMutex() {
return getOrCreate(MPlatformToDefaultContextCacheMutex);
}

Sync &GlobalHandler::getSync() { return getOrCreate(MSync); }

std::vector<PlatformImplPtr> &GlobalHandler::getPlatformCache() {
Expand Down Expand Up @@ -84,6 +93,16 @@ void shutdown() {
// First, release resources, that may access plugins.
GlobalHandler::instance().MScheduler.Inst.reset(nullptr);
GlobalHandler::instance().MProgramManager.Inst.reset(nullptr);
#ifndef _WIN32
GlobalHandler::instance().MPlatformToDefaultContextCache.Inst.reset(nullptr);
#else
// Windows does not maintain dependencies between dynamically loaded libraries
// and can unload SYCL runtime dependencies before sycl.dll's DllMain has
// finished. To avoid calls to nowhere, intentionally leak platform to device
// cache. This will prevent destructors from being called, thus no PI cleanup
// routines will be called in the end.
GlobalHandler::instance().MPlatformToDefaultContextCache.Inst.release();
#endif
GlobalHandler::instance().MPlatformCache.Inst.reset(nullptr);

// Call to GlobalHandler::instance().getPlugins() initializes plugins. If
Expand Down
11 changes: 11 additions & 0 deletions sycl/source/detail/global_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
#include <CL/sycl/detail/util.hpp>

#include <memory>
#include <unordered_map>

__SYCL_INLINE_NAMESPACE(cl) {
namespace sycl {
namespace detail {
class platform_impl;
class context_impl;
class Scheduler;
class ProgramManager;
class Sync;
Expand All @@ -25,6 +27,7 @@ class device_filter_list;
class XPTIRegistry;

using PlatformImplPtr = std::shared_ptr<platform_impl>;
using ContextImplPtr = std::shared_ptr<context_impl>;

/// Wrapper class for global data structures with non-trivial destructors.
///
Expand Down Expand Up @@ -53,6 +56,11 @@ class GlobalHandler {
ProgramManager &getProgramManager();
Sync &getSync();
std::vector<PlatformImplPtr> &getPlatformCache();

std::unordered_map<PlatformImplPtr, ContextImplPtr> &
getPlatformToDefaultContextCache();

std::mutex &getPlatformToDefaultContextCacheMutex();
std::mutex &getPlatformMapMutex();
std::mutex &getFilterMutex();
std::vector<plugin> &getPlugins();
Expand Down Expand Up @@ -80,6 +88,9 @@ class GlobalHandler {
InstWithLock<ProgramManager> MProgramManager;
InstWithLock<Sync> MSync;
InstWithLock<std::vector<PlatformImplPtr>> MPlatformCache;
InstWithLock<std::unordered_map<PlatformImplPtr, ContextImplPtr>>
MPlatformToDefaultContextCache;
InstWithLock<std::mutex> MPlatformToDefaultContextCacheMutex;
InstWithLock<std::mutex> MPlatformMapMutex;
InstWithLock<std::mutex> MFilterMutex;
InstWithLock<std::vector<plugin>> MPlugins;
Expand Down
1 change: 1 addition & 0 deletions sycl/source/detail/platform_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <algorithm>
#include <cstring>
#include <mutex>
#include <string>
#include <vector>

Expand Down
1 change: 1 addition & 0 deletions sycl/source/detail/platform_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#pragma once

#include <CL/sycl/detail/common.hpp>
#include <CL/sycl/detail/pi.hpp>
#include <CL/sycl/info/info_desc.hpp>
Expand Down
26 changes: 18 additions & 8 deletions sycl/source/detail/queue_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <CL/sycl/properties/queue_properties.hpp>
#include <CL/sycl/property_list.hpp>
#include <CL/sycl/stl.hpp>
#include <detail/config.hpp>
#include <detail/context_impl.hpp>
#include <detail/device_impl.hpp>
#include <detail/event_impl.hpp>
Expand Down Expand Up @@ -50,6 +51,22 @@ enum QueueOrder { Ordered, OOO };

class queue_impl {
public:
// \return a default context for the platform if it includes the device
// passed and default contexts are enabled, a new context otherwise.
static ContextImplPtr getDefaultOrNew(const DeviceImplPtr &Device) {
if (!SYCLConfig<SYCL_ENABLE_DEFAULT_CONTEXTS>::get())
return detail::getSyclObjImpl(
context{createSyclObjFromImpl<device>(Device), {}, {}});

ContextImplPtr DefaultContext = detail::getSyclObjImpl(
Device->get_platform().ext_oneapi_get_default_context());

if (DefaultContext->hasDevice(Device))
return DefaultContext;

return detail::getSyclObjImpl(
context{createSyclObjFromImpl<device>(Device), {}, {}});
}
/// Constructs a SYCL queue from a device using an async_handler and
/// property_list provided.
///
Expand All @@ -59,14 +76,7 @@ class queue_impl {
/// \param PropList is a list of properties to use for queue construction.
queue_impl(const DeviceImplPtr &Device, const async_handler &AsyncHandler,
const property_list &PropList)
: queue_impl(Device,
detail::getSyclObjImpl(
context(createSyclObjFromImpl<device>(Device), {},
(DefaultContextType == CUDAContextT::primary)
? property_list{property::context::cuda::
use_primary_context()}
: property_list{})),
AsyncHandler, PropList){};
: queue_impl(Device, getDefaultOrNew(Device), AsyncHandler, PropList){};

/// Constructs a SYCL queue with an async_handler and property_list provided
/// form a device and a context.
Expand Down
4 changes: 3 additions & 1 deletion sycl/source/detail/scheduler/scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,10 @@ void Scheduler::deallocateStreamBuffers(stream_impl *Impl) {

Scheduler::Scheduler() {
sycl::device HostDevice;
sycl::context HostContext{HostDevice};
DefaultHostQueue = QueueImplPtr(
new queue_impl(detail::getSyclObjImpl(HostDevice), /*AsyncHandler=*/{},
new queue_impl(detail::getSyclObjImpl(HostDevice),
detail::getSyclObjImpl(HostContext), /*AsyncHandler=*/{},
/*PropList=*/{}));
}

Expand Down
23 changes: 23 additions & 0 deletions sycl/source/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
#include <CL/sycl/info/info_desc.hpp>
#include <CL/sycl/platform.hpp>
#include <detail/backend_impl.hpp>
#include <detail/config.hpp>
#include <detail/force_device.hpp>
#include <detail/global_handler.hpp>
#include <detail/platform_impl.hpp>

__SYCL_INLINE_NAMESPACE(cl) {
Expand Down Expand Up @@ -65,5 +67,26 @@ bool platform::has(aspect Aspect) const { return impl->has(Aspect); }

#undef __SYCL_PARAM_TRAITS_SPEC

context platform::ext_oneapi_get_default_context() const {
if (!detail::SYCLConfig<detail::SYCL_ENABLE_DEFAULT_CONTEXTS>::get())
throw std::runtime_error("SYCL default contexts are not enabled");

// Keeping the default context for platforms in the global cache to avoid
// shared_ptr based circular dependency between platform and context classes
std::unordered_map<detail::PlatformImplPtr, detail::ContextImplPtr>
&PlatformToDefaultContextCache =
detail::GlobalHandler::instance().getPlatformToDefaultContextCache();

std::lock_guard Lock{detail::GlobalHandler::instance()
.getPlatformToDefaultContextCacheMutex()};

auto It = PlatformToDefaultContextCache.find(impl);
if (PlatformToDefaultContextCache.end() == It)
std::tie(It, std::ignore) = PlatformToDefaultContextCache.insert(
{impl, detail::getSyclObjImpl(context{get_devices()})});

return detail::createSyclObjFromImpl<context>(It->second);
}

} // namespace sycl
} // __SYCL_INLINE_NAMESPACE(cl)
1 change: 1 addition & 0 deletions sycl/test/abi/sycl_symbols_linux.dump
Original file line number Diff line number Diff line change
Expand Up @@ -4325,6 +4325,7 @@ _ZNK2cl4sycl7samplerneERKS1_
_ZNK2cl4sycl8platform11get_backendEv
_ZNK2cl4sycl8platform11get_devicesENS0_4info11device_typeE
_ZNK2cl4sycl8platform13has_extensionERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
_ZNK2cl4sycl8platform30ext_oneapi_get_default_contextEv
_ZNK2cl4sycl8platform3getEv
_ZNK2cl4sycl8platform3hasENS0_6aspectE
_ZNK2cl4sycl8platform7is_hostEv
Expand Down
1 change: 1 addition & 0 deletions sycl/test/abi/sycl_symbols_windows.dump
Original file line number Diff line number Diff line change
Expand Up @@ -1741,6 +1741,7 @@
?extractArgsAndReqs@handler@sycl@cl@@AEAAXXZ
?extractArgsAndReqsFromLambda@handler@sycl@cl@@AEAAXPEAD_KPEBUkernel_param_desc_t@detail@23@@Z
?extractArgsAndReqsFromLambda@handler@sycl@cl@@AEAAXPEAD_KPEBUkernel_param_desc_t@detail@23@_N@Z
?ext_oneapi_get_default_context@platform@sycl@cl@@QEBA?AVcontext@23@XZ
?fabs@__host_std@cl@@YA?AV?$vec@M$00@sycl@2@V342@@Z
?fabs@__host_std@cl@@YA?AV?$vec@M$01@sycl@2@V342@@Z
?fabs@__host_std@cl@@YA?AV?$vec@M$02@sycl@2@V342@@Z
Expand Down
1 change: 1 addition & 0 deletions sycl/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ add_subdirectory(SYCL2020)
add_subdirectory(thread_safety)
add_subdirectory(program_manager)
add_subdirectory(assert)
add_subdirectory(Extensions)
8 changes: 8 additions & 0 deletions sycl/unittests/Extensions/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
set(CMAKE_CXX_EXTENSIONS OFF)

# Enable exception handling for these unit tests
set(LLVM_REQUIRES_EH 1)
add_sycl_unittest(ExtensionsTests OBJECT
DefaultContext.cpp
)

Loading