forked from intel/llvm
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontext.cpp
More file actions
136 lines (105 loc) · 4.72 KB
/
context.cpp
File metadata and controls
136 lines (105 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//==---------------- context.cpp - SYCL context ----------------------------==//
//
// 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/backend_impl.hpp>
#include <detail/context_impl.hpp>
#include <sycl/context.hpp>
#include <sycl/detail/common.hpp>
#include <sycl/detail/ur.hpp>
#include <sycl/device.hpp>
#include <sycl/device_selector.hpp>
#include <sycl/exception.hpp>
#include <sycl/exception_list.hpp>
#include <sycl/info/info_desc.hpp>
#include <sycl/platform.hpp>
#include <sycl/properties/all_properties.hpp>
#include <algorithm>
#include <memory>
#include <utility>
// 4.6.2 Context class
namespace sycl {
inline namespace _V1 {
context::context(const property_list &PropList) : context(device{}, PropList) {}
context::context(const async_handler &AsyncHandler,
const property_list &PropList)
: context(device{}, AsyncHandler, PropList) {}
context::context(const device &Device, const property_list &PropList)
: context(std::vector<device>(1, Device), PropList) {}
context::context(const device &Device, async_handler AsyncHandler,
const property_list &PropList)
: context(std::vector<device>(1, Device), AsyncHandler, PropList) {}
context::context(const platform &Platform, const property_list &PropList)
: context(Platform.get_devices(), PropList) {}
context::context(const platform &Platform, async_handler AsyncHandler,
const property_list &PropList)
: context(Platform.get_devices(), AsyncHandler, PropList) {}
context::context(const std::vector<device> &DeviceList,
const property_list &PropList)
: context(DeviceList, detail::defaultAsyncHandler, PropList) {}
context::context(const std::vector<device> &DeviceList,
async_handler AsyncHandler, const property_list &PropList) {
if (DeviceList.empty()) {
throw exception(make_error_code(errc::invalid), "DeviceList is empty.");
}
const auto &RefPlatform =
detail::getSyclObjImpl(DeviceList[0].get_platform())->getHandleRef();
if (std::any_of(DeviceList.begin(), DeviceList.end(),
[&](const device &CurrentDevice) {
return (detail::getSyclObjImpl(CurrentDevice.get_platform())
->getHandleRef() != RefPlatform);
}))
throw exception(make_error_code(errc::invalid),
"Can't add devices across platforms to a single context.");
else
impl = std::make_shared<detail::context_impl>(DeviceList, AsyncHandler,
PropList);
}
context::context(cl_context ClContext, async_handler AsyncHandler) {
const auto &Plugin = sycl::detail::ur::getPlugin<backend::opencl>();
ur_context_handle_t hContext = nullptr;
ur_native_handle_t nativeHandle =
reinterpret_cast<ur_native_handle_t>(ClContext);
Plugin->call<detail::UrApiKind::urContextCreateWithNativeHandle>(
nativeHandle, Plugin->getUrAdapter(), 0, nullptr, nullptr, &hContext);
impl = std::make_shared<detail::context_impl>(
hContext, AsyncHandler, Plugin);
}
template <typename Param>
typename detail::is_context_info_desc<Param>::return_type
context::get_info() const {
return impl->template get_info<Param>();
}
#define __SYCL_PARAM_TRAITS_SPEC(DescType, Desc, ReturnT, PiCode) \
template __SYCL_EXPORT ReturnT context::get_info<info::DescType::Desc>() \
const;
#include <sycl/info/context_traits.def>
#undef __SYCL_PARAM_TRAITS_SPEC
template <typename Param>
typename detail::is_backend_info_desc<Param>::return_type
context::get_backend_info() const {
return impl->get_backend_info<Param>();
}
#define __SYCL_PARAM_TRAITS_SPEC(DescType, Desc, ReturnT, Picode) \
template __SYCL_EXPORT ReturnT \
context::get_backend_info<info::DescType::Desc>() const;
#include <sycl/info/sycl_backend_traits.def>
#undef __SYCL_PARAM_TRAITS_SPEC
cl_context context::get() const { return impl->get(); }
backend context::get_backend() const noexcept { return impl->getBackend(); }
platform context::get_platform() const {
return impl->get_info<info::context::platform>();
}
std::vector<device> context::get_devices() const {
return impl->get_info<info::context::devices>();
}
context::context(std::shared_ptr<detail::context_impl> Impl) : impl(Impl) {}
ur_native_handle_t context::getNative() const { return impl->getNative(); }
const property_list &context::getPropList() const {
return impl->getPropList();
}
} // namespace _V1
} // namespace sycl