Skip to content

Commit e285258

Browse files
authored
[NPU] add lock for npu_pinned_allocator (#34700)
* add lock * fix typo
1 parent 8009257 commit e285258

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

paddle/fluid/memory/allocation/npu_pinned_allocator.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ void NPUPinnedAllocator::ProcessEventsAndFree() {
3939
}
4040

4141
Allocation *NPUPinnedAllocator::AllocateImpl(size_t size) {
42+
std::lock_guard<std::mutex> lock(mtx_);
4243
ProcessEventsAndFree();
4344
void *ptr;
4445
int error = posix_memalign(&ptr, kAlignment, size);
@@ -50,6 +51,7 @@ Allocation *NPUPinnedAllocator::AllocateImpl(size_t size) {
5051
}
5152

5253
void NPUPinnedAllocator::FreeImpl(Allocation *allocation) {
54+
std::lock_guard<std::mutex> lock(mtx_);
5355
void *ptr = allocation->ptr();
5456
auto iter = npu_events_.find(allocation);
5557
aclrtEvent event = iter->second;
@@ -65,11 +67,14 @@ void NPUPinnedAllocator::FreeImpl(Allocation *allocation) {
6567
}
6668

6769
uint64_t NPUPinnedAllocator::ReleaseImpl(const platform::Place &place) {
70+
std::lock_guard<std::mutex> lock(mtx_);
71+
// Empty implementation
6872
return static_cast<uint64_t>(0);
6973
}
7074

7175
void NPUPinnedAllocator::RecordEvent(Allocation *allocation,
7276
aclrtStream stream) {
77+
std::lock_guard<std::mutex> lock(mtx_);
7378
aclrtEvent event = nullptr;
7479
PADDLE_ENFORCE_NPU_SUCCESS(aclrtCreateEvent(&event));
7580
PADDLE_ENFORCE_NPU_SUCCESS(aclrtRecordEvent(event, stream));

paddle/fluid/memory/allocation/npu_pinned_allocator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class NPUPinnedAllocator : public Allocator {
4242

4343
private:
4444
std::unordered_map<Allocation *, aclrtEvent> npu_events_;
45+
mutable std::mutex mtx_;
4546
};
4647

4748
} // namespace allocation

0 commit comments

Comments
 (0)