-
Notifications
You must be signed in to change notification settings - Fork 769
[SYCL] Adding caching when using interop constructor #3327
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
Changes from 5 commits
45e9dd8
a20af7e
b5e45fc
1088259
6fdff7e
a88ccbd
5c014bd
b72b4cf
e010395
89179d0
3221fc2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
#include <CL/sycl/platform.hpp> | ||
#include <CL/sycl/stl.hpp> | ||
|
||
#include <memory> | ||
#include <unordered_map> | ||
#include <utility> | ||
|
||
__SYCL_INLINE_NAMESPACE(cl) { | ||
|
@@ -177,7 +177,10 @@ class __SYCL_EXPORT device { | |
/// \return a native handle, the type of which defined by the backend. | ||
template <backend BackendName> | ||
auto get_native() const -> typename interop<BackendName, device>::type { | ||
return (typename interop<BackendName, device>::type)getNative(); | ||
auto cl_device = (typename interop<BackendName, device>::type)getNative(); | ||
std::lock_guard<std::mutex> lock(device_mutex); | ||
device_impls[cl_device] = impl; | ||
return cl_device; | ||
} | ||
|
||
/// Indicates if the SYCL device has the given feature. | ||
|
@@ -192,6 +195,10 @@ class __SYCL_EXPORT device { | |
shared_ptr_class<detail::device_impl> impl; | ||
device(shared_ptr_class<detail::device_impl> impl) : impl(impl) {} | ||
|
||
static std::unordered_map<cl_device_id, std::weak_ptr<detail::device_impl>> | ||
device_impls; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to reuse already existing cache, see platform_impl.hpp:199 (MDeviceCache) ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it is possible, testing it. I didn't see that when trying to implement caching with map There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I pushed another solution with reusing an existing code |
||
static std::mutex device_mutex; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my comment for the same piece in |
||
|
||
pi_native_handle getNative() const; | ||
|
||
template <class Obj> | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -12,6 +12,7 @@ | |||||||||||||||||||||||||
#include <CL/sycl/stl.hpp> | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// 4.6.2 Platform class | ||||||||||||||||||||||||||
#include <unordered_map> | ||||||||||||||||||||||||||
#include <utility> | ||||||||||||||||||||||||||
__SYCL_INLINE_NAMESPACE(cl) { | ||||||||||||||||||||||||||
namespace sycl { | ||||||||||||||||||||||||||
|
@@ -112,8 +113,12 @@ class __SYCL_EXPORT platform { | |||||||||||||||||||||||||
/// \return a native handle, the type of which defined by the backend. | ||||||||||||||||||||||||||
template <backend BackendName> | ||||||||||||||||||||||||||
auto get_native() const -> typename interop<BackendName, platform>::type { | ||||||||||||||||||||||||||
return reinterpret_cast<typename interop<BackendName, platform>::type>( | ||||||||||||||||||||||||||
getNative()); | ||||||||||||||||||||||||||
auto cl_platform = | ||||||||||||||||||||||||||
reinterpret_cast<typename interop<BackendName, platform>::type>( | ||||||||||||||||||||||||||
getNative()); | ||||||||||||||||||||||||||
std::lock_guard<std::mutex> lock(platform_mutex); | ||||||||||||||||||||||||||
platform_impls[cl_platform] = impl; | ||||||||||||||||||||||||||
return cl_platform; | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit
Suggested change
|
||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
/// Indicates if all of the SYCL devices on this platform have the | ||||||||||||||||||||||||||
|
@@ -132,6 +137,11 @@ class __SYCL_EXPORT platform { | |||||||||||||||||||||||||
shared_ptr_class<detail::platform_impl> impl; | ||||||||||||||||||||||||||
platform(shared_ptr_class<detail::platform_impl> impl) : impl(impl) {} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
static std::unordered_map<cl_platform_id, | ||||||||||||||||||||||||||
std::weak_ptr<detail::platform_impl>> | ||||||||||||||||||||||||||
platform_impls; | ||||||||||||||||||||||||||
static std::mutex platform_mutex; | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't going to work properly. Both unordered_map and mutex are non-trivial classes. See this document for more info. |
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
template <class T> | ||||||||||||||||||||||||||
friend T detail::createSyclObjFromImpl(decltype(T::impl) ImplObj); | ||||||||||||||||||||||||||
template <class Obj> | ||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -29,12 +29,28 @@ void force_type(info::device_type &t, const info::device_type &ft) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
device::device() : impl(detail::device_impl::getHostDeviceImpl()) {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
device::device(cl_device_id deviceId) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
: impl(std::make_shared<detail::device_impl>( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
detail::pi::cast<pi_native_handle>(deviceId), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
RT::getPlugin<backend::opencl>())) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
std::unordered_map<cl_device_id, std::weak_ptr<detail::device_impl>> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
device::device_impls; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
std::mutex device::device_mutex; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
device::device(cl_device_id deviceId) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
// The implementation constructor takes ownership of the native handle so we | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
// must retain it in order to adhere to SYCL 1.2.1 spec (Rev6, section 4.3.1.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
std::lock_guard<std::mutex> lock(device_mutex); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
auto it = device_impls.find(deviceId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (it != device_impls.end() && !it->second.expired()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
impl = it->second.lock(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
impl = std::make_shared<detail::device_impl>( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
detail::pi::cast<pi_native_handle>(deviceId), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
RT::getPlugin<backend::opencl>()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (it == device_impls.end()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
device_impls[deviceId] = impl; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
it->second = impl; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
clRetainDevice(deviceId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
memory
was actually used forshared_ptr
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was deleted when I tested map. But memory is included in stl.hpp, that included higher. Do I need to return it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. You can't rely on
CL/sycl/stl.hpp
to includememory
header for you.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returned it