Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions paddle/fluid/memory/allocation/npu_pinned_allocator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ void NPUPinnedAllocator::ProcessEventsAndFree() {
}

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

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

uint64_t NPUPinnedAllocator::ReleaseImpl(const platform::Place &place) {
std::lock_guard<std::mutex> lock(mtx_);
// Empty implementation
return static_cast<uint64_t>(0);
}

void NPUPinnedAllocator::RecordEvent(Allocation *allocation,
aclrtStream stream) {
std::lock_guard<std::mutex> lock(mtx_);
aclrtEvent event = nullptr;
PADDLE_ENFORCE_NPU_SUCCESS(aclrtCreateEvent(&event));
PADDLE_ENFORCE_NPU_SUCCESS(aclrtRecordEvent(event, stream));
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/memory/allocation/npu_pinned_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class NPUPinnedAllocator : public Allocator {

private:
std::unordered_map<Allocation *, aclrtEvent> npu_events_;
mutable std::mutex mtx_;
};

} // namespace allocation
Expand Down