diff --git a/paddle/fluid/platform/device_context.cc b/paddle/fluid/platform/device_context.cc index d0a108f905f46135bcd2b68be19ab396ab897272..146a205832fd31faf01effdce01eb2884f9a9884 100644 --- a/paddle/fluid/platform/device_context.cc +++ b/paddle/fluid/platform/device_context.cc @@ -120,15 +120,24 @@ class EigenCudaStreamDevice : public Eigen::StreamInterface { } void* allocate(size_t num_bytes) const override { + if (UNLIKELY(num_bytes == 0)) { + return nullptr; + } auto buf = paddle::memory::Alloc(place_, num_bytes, memory::Allocator::kScratchpad); void* retv = buf->ptr(); - allocations_[buf->ptr()] = std::move(buf); + { + std::lock_guard lock(mtx_); + allocations_.emplace(retv, std::move(buf)); + } return retv; } void deallocate(void* buffer) const override { - allocations_.erase(allocations_.find(buffer)); + if (LIKELY(buffer)) { + std::lock_guard lock(mtx_); + allocations_.erase(buffer); + } } void* scratchpad() const override { @@ -155,6 +164,7 @@ class EigenCudaStreamDevice : public Eigen::StreamInterface { const cudaDeviceProp* device_prop_; // not owned; mutable void* scratch_; mutable unsigned int* semaphore_; + mutable std::mutex mtx_; // to protect allocations_ mutable std::unordered_map allocations_; };