From f11da1d83b64f66252dcce17447c63bda2c663b7 Mon Sep 17 00:00:00 2001 From: Benoit Steiner Date: Sun, 20 Nov 2016 13:17:08 -0800 Subject: Made the QueueInterface thread safe --- unsupported/Eigen/CXX11/src/Tensor/TensorDeviceSycl.h | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'unsupported/Eigen/CXX11/src/Tensor/TensorDeviceSycl.h') diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorDeviceSycl.h b/unsupported/Eigen/CXX11/src/Tensor/TensorDeviceSycl.h index a78350bf3..05459f1d2 100644 --- a/unsupported/Eigen/CXX11/src/Tensor/TensorDeviceSycl.h +++ b/unsupported/Eigen/CXX11/src/Tensor/TensorDeviceSycl.h @@ -23,6 +23,8 @@ struct QueueInterface { /// class members: bool exception_caught_ = false; + mutable std::mutex mutex_; + /// std::map is the container used to make sure that we create only one buffer /// per pointer. The lifespan of the buffer now depends on the lifespan of SyclDevice. /// If a non-read-only pointer is needed to be accessed on the host we should manually deallocate it. @@ -81,6 +83,7 @@ struct QueueInterface { auto buf = cl::sycl::buffer(cl::sycl::range<1>(num_bytes)); auto ptr =buf.get_access().get_pointer(); buf.set_final_data(nullptr); + std::lock_guard lock(mutex_); buffer_map.insert(std::pair>(ptr,buf)); return static_cast(ptr); } @@ -88,6 +91,7 @@ struct QueueInterface { /// This is used to deallocate the device pointer. p is used as a key inside /// the map to find the device buffer and delete it. EIGEN_STRONG_INLINE void deallocate(const void *p) const { + std::lock_guard lock(mutex_); auto it = buffer_map.find(static_cast(p)); if (it != buffer_map.end()) { buffer_map.erase(it); @@ -95,10 +99,12 @@ struct QueueInterface { } EIGEN_STRONG_INLINE void deallocate_all() const { + std::lock_guard lock(mutex_); buffer_map.clear(); } EIGEN_STRONG_INLINE std::map>::iterator find_buffer(const void* ptr) const { + std::lock_guard lock(mutex_); auto it1 = buffer_map.find(static_cast(ptr)); if (it1 != buffer_map.end()){ return it1; -- cgit v1.2.3