Skip to content

Commit d93b2d0

Browse files
committed
Refine code
1 parent ea81f8e commit d93b2d0

29 files changed

+148
-168
lines changed

paddle/fluid/memory/allocation/aligned_allocator.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ class AlignedAllocation : public Allocation {
3333
"kAlignment must be 2^N");
3434

3535
public:
36-
AlignedAllocation(std::unique_ptr<Allocation>&& underlying_allocation,
37-
size_t size)
36+
AlignedAllocation(AllocationPtr&& underlying_allocation, size_t size)
3837
: Allocation(AlignedPtr(underlying_allocation->ptr()),
3938
size + kAlignment - Offset(underlying_allocation->ptr()),
4039
underlying_allocation->place()),
@@ -59,7 +58,7 @@ class AlignedAllocation : public Allocation {
5958
}
6059
}
6160

62-
std::unique_ptr<Allocation> underlying_allocation_;
61+
AllocationPtr underlying_allocation_;
6362
};
6463

6564
// Thin aligned allocator is trivial and used to generate a small size binary.
@@ -87,10 +86,10 @@ template <size_t kAlignment>
8786
class AlignedAllocator : public ThinAlignedAllocator {
8887
public:
8988
using ThinAlignedAllocator::ThinAlignedAllocator;
90-
std::unique_ptr<Allocation> Allocate(size_t size, Attr attr) override {
89+
AllocationPtr Allocate(size_t size, Attr attr) override {
9190
auto raw_allocation =
9291
underlying_allocator_->Allocate(size + kAlignment, attr);
93-
return std::unique_ptr<Allocation>(
92+
return AllocationPtr(
9493
new AlignedAllocation<kAlignment>(std::move(raw_allocation), size));
9594
}
9695
};

paddle/fluid/memory/allocation/allocator.cc

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
// limitations under the License.
1414

1515
#include "paddle/fluid/memory/allocation/allocator.h"
16+
#include <functional>
17+
1618
namespace paddle {
1719
namespace memory {
1820
namespace allocation {
@@ -24,10 +26,20 @@ bool Allocator::IsAllocThreadSafe() const { return false; }
2426

2527
const char* BadAlloc::what() const noexcept { return msg_.c_str(); }
2628

27-
MannualFreeAllocation::~MannualFreeAllocation() { allocator_->Free(this); }
28-
std::unique_ptr<Allocation> MannualFreeAllocator::Allocate(
29-
size_t size, Allocator::Attr attr) {
30-
return std::unique_ptr<Allocation>(AllocateImpl(size, attr));
29+
AllocationPtr MannualFreeAllocator::Allocate(size_t size,
30+
Allocator::Attr attr) {
31+
auto allocation = AllocateImpl(size, attr);
32+
allocation->Deleter =
33+
std::bind1st(std::mem_fn(&MannualFreeAllocator::Free), this);
34+
return AllocationPtr(allocation);
35+
}
36+
void AllocationDeleter::operator()(Allocation* allocation) const {
37+
if (allocation->Deleter) {
38+
auto deleter = std::move(allocation->Deleter);
39+
deleter(allocation);
40+
} else {
41+
delete allocation;
42+
}
3143
}
3244
} // namespace allocation
3345
} // namespace memory

paddle/fluid/memory/allocation/allocator.h

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ class BadAlloc : public std::exception {
3131
std::string msg_;
3232
};
3333

34+
class Allocation;
35+
struct AllocationDeleter {
36+
void operator()(Allocation* allocation) const;
37+
};
38+
3439
// Allocation is the object holding the actually pointer. Use
3540
// `Allocation::ptr()` will returns the pointer that allocated.
3641
//
@@ -67,12 +72,16 @@ class Allocation {
6772

6873
virtual ~Allocation();
6974

75+
std::function<void(Allocation*)> Deleter;
76+
7077
private:
7178
void* ptr_;
7279
size_t size_;
7380
platform::Place place_;
7481
};
7582

83+
using AllocationPtr = std::unique_ptr<Allocation, AllocationDeleter>;
84+
7685
// Base interface class of memory Allocator.
7786
// To allocate a memory, allocator needs two parameters:
7887
// 1. size of bytes.
@@ -114,36 +123,22 @@ class Allocator {
114123

115124
// Allocate an allocation. Note the return allocation might need to be freed
116125
// manually if the Allocator is an `UnmanagedAllocator`.
117-
virtual std::unique_ptr<Allocation> Allocate(
118-
size_t size, Allocator::Attr attr = kDefault) = 0;
126+
virtual AllocationPtr Allocate(size_t size,
127+
Allocator::Attr attr = kDefault) = 0;
119128

120129
// True if the `Allocate` is thread safe.
121130
virtual bool IsAllocThreadSafe() const;
122131
};
123132

124-
class MannualFreeAllocator;
125-
class MannualFreeAllocation : public Allocation {
126-
public:
127-
MannualFreeAllocation(MannualFreeAllocator* allocator, void* ptr, size_t size,
128-
platform::Place place)
129-
: Allocation(ptr, size, place), allocator_(allocator) {}
130-
131-
~MannualFreeAllocation();
132-
133-
private:
134-
MannualFreeAllocator* allocator_;
135-
};
136-
137133
// User need to invoke `Free` or `FreeUniquePtr` manually if allocated by
138134
// a manally managed allocator.
139135
class MannualFreeAllocator : public Allocator {
140136
public:
141-
std::unique_ptr<Allocation> Allocate(size_t size, Attr attr) final;
137+
AllocationPtr Allocate(size_t size, Attr attr) final;
142138

143139
protected:
144-
virtual void Free(MannualFreeAllocation* allocation) = 0;
145-
virtual MannualFreeAllocation* AllocateImpl(size_t size,
146-
Allocator::Attr attr) = 0;
140+
virtual void Free(Allocation* allocation) = 0;
141+
virtual Allocation* AllocateImpl(size_t size, Allocator::Attr attr) = 0;
147142
friend class MannualFreeAllocation;
148143
};
149144

paddle/fluid/memory/allocation/allocator_facade.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class CPUManagedAllocator : public Allocator {
4949
public:
5050
CPUManagedAllocator() : normal_allocator_(new CPUAllocator()) {}
5151

52-
std::unique_ptr<Allocation> Allocate(size_t size, Attr attr) override {
52+
AllocationPtr Allocate(size_t size, Attr attr) override {
5353
return normal_allocator_->Allocate(size, attr);
5454
}
5555

@@ -103,7 +103,7 @@ class ChunkedManagedAllocator : public Allocator {
103103
raw_allocator_.reset();
104104
}
105105

106-
std::unique_ptr<Allocation> Allocate(size_t size, Attr attr) override {
106+
AllocationPtr Allocate(size_t size, Attr attr) override {
107107
return default_allocator_->Allocate(size, attr);
108108
}
109109

@@ -131,7 +131,7 @@ class ChunkedManagedAllocator : public Allocator {
131131
protected:
132132
size_t max_chunk_size_;
133133
int64_t retry_time_;
134-
std::vector<std::unique_ptr<Allocation>> chunks_;
134+
std::vector<AllocationPtr> chunks_;
135135
std::shared_ptr<Allocator> raw_allocator_;
136136
std::shared_ptr<Allocator> default_allocator_;
137137
};
@@ -236,12 +236,12 @@ AllocatorFacade& AllocatorFacade::Instance() {
236236
std::shared_ptr<Allocation> AllocatorFacade::AllocShared(
237237
const platform::Place& place, size_t size, Allocator::Attr attr) {
238238
return std::shared_ptr<Allocation>(
239-
m_->allocators_.at(place)->Allocate(size, attr).release());
239+
m_->allocators_.at(place)->Allocate(size, attr).release(),
240+
AllocationDeleter());
240241
}
241242

242-
std::unique_ptr<Allocation> AllocatorFacade::Alloc(const platform::Place& place,
243-
size_t size,
244-
Allocator::Attr attr) {
243+
AllocationPtr AllocatorFacade::Alloc(const platform::Place& place, size_t size,
244+
Allocator::Attr attr) {
245245
return m_->allocators_.at(place)->Allocate(size, attr);
246246
}
247247

paddle/fluid/memory/allocation/allocator_facade.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ class AllocatorFacade {
4343
Allocator::Attr attr = Allocator::kDefault);
4444

4545
// Allocate a unique allocation.
46-
std::unique_ptr<Allocation> Alloc(const platform::Place& place, size_t size,
47-
Allocator::Attr attr = Allocator::kDefault);
46+
AllocationPtr Alloc(const platform::Place& place, size_t size,
47+
Allocator::Attr attr = Allocator::kDefault);
4848

4949
// TODO(yy): Allocate a Copy-On-Write allocation?
5050
private:

paddle/fluid/memory/allocation/auto_increment_allocator.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ namespace paddle {
1818
namespace memory {
1919
namespace allocation {
2020

21-
std::unique_ptr<Allocation> AutoIncrementAllocator::Allocate(
22-
size_t size, Allocator::Attr attr) {
21+
AllocationPtr AutoIncrementAllocator::Allocate(size_t size,
22+
Allocator::Attr attr) {
2323
auto cur = prev_success_allocator_.load();
2424
size_t retry_count = allocator_num_.load();
2525
size_t allocator_num = retry_count;

paddle/fluid/memory/allocation/auto_increment_allocator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class AutoIncrementAllocator : public Allocator {
5454
explicit AutoIncrementAllocator(AllocatorCreator&& creator, size_t capacity)
5555
: creator_(std::move(creator)), underlying_allocators_(capacity) {}
5656

57-
std::unique_ptr<Allocation> Allocate(size_t size, Attr attr) override;
57+
AllocationPtr Allocate(size_t size, Attr attr) override;
5858

5959
bool IsAllocThreadSafe() const override;
6060

paddle/fluid/memory/allocation/best_fit_allocator.cc

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ size_t BestFitAllocator::NumFreeChunks() const {
109109
}
110110
return num;
111111
}
112-
void BestFitAllocator::Free(MannualFreeAllocation* allocation) {
112+
void BestFitAllocator::Free(Allocation* allocation) {
113113
auto* bf_allocation = dynamic_cast<BestFitAllocation*>(allocation);
114114
auto chunk_it = bf_allocation->ChunkIterator();
115115
PADDLE_ENFORCE(!chunk_it->is_free);
@@ -136,9 +136,9 @@ void BestFitAllocator::Free(MannualFreeAllocation* allocation) {
136136
}
137137

138138
InsertFreeNode(chunk_it);
139+
delete allocation;
139140
}
140-
MannualFreeAllocation* BestFitAllocator::AllocateImpl(size_t size,
141-
Allocator::Attr attr) {
141+
Allocation* BestFitAllocator::AllocateImpl(size_t size, Allocator::Attr attr) {
142142
auto highest_set_bit = static_cast<size_t>(HighestBitPos(size));
143143
MapIt map_it;
144144
for (; highest_set_bit < free_chunks_.size(); ++highest_set_bit) {
@@ -158,11 +158,10 @@ MannualFreeAllocation* BestFitAllocator::AllocateImpl(size_t size,
158158
BestFitAllocation::BestFitAllocation(
159159
paddle::memory::allocation::BestFitAllocator* allocator,
160160
typename details::ChunkList::iterator chunk_it)
161-
: MannualFreeAllocation(
162-
allocator, reinterpret_cast<void*>(
163-
reinterpret_cast<uintptr_t>(allocator->BasePtr()) +
164-
chunk_it->offset_),
165-
chunk_it->size_, allocator->Place()),
161+
: Allocation(reinterpret_cast<void*>(
162+
reinterpret_cast<uintptr_t>(allocator->BasePtr()) +
163+
chunk_it->offset_),
164+
chunk_it->size_, allocator->Place()),
166165
chunk_it_(chunk_it) {}
167166
} // namespace allocation
168167
} // namespace memory

paddle/fluid/memory/allocation/best_fit_allocator.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ using FreeChunkBin =
7171
class BestFitAllocator;
7272

7373
// The BestFitAllocation maintain the List Node iterator.
74-
class BestFitAllocation : public MannualFreeAllocation {
74+
class BestFitAllocation : public Allocation {
7575
private:
7676
using ListIt = typename details::ChunkList::iterator;
7777

@@ -123,9 +123,8 @@ class BestFitAllocator : public MannualFreeAllocator {
123123
void InsertFreeNode(const ListIt& it);
124124

125125
protected:
126-
void Free(MannualFreeAllocation* allocation) override;
127-
MannualFreeAllocation* AllocateImpl(size_t size,
128-
Allocator::Attr attr) override;
126+
void Free(Allocation* allocation) override;
127+
Allocation* AllocateImpl(size_t size, Allocator::Attr attr) override;
129128

130129
private:
131130
Allocation* allocation_; // not owned

paddle/fluid/memory/allocation/buffered_allocator.cc

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,33 +49,28 @@ void BufferedAllocator::FreeCache(size_t size) {
4949
bool BufferedAllocator::IsAllocThreadSafe() const {
5050
return this->underlying_allocator_->IsAllocThreadSafe();
5151
}
52-
void BufferedAllocator::Free(MannualFreeAllocation *allocation) {
52+
void BufferedAllocator::Free(Allocation *allocation) {
5353
platform::LockGuardPtr<std::mutex> guard(mtx_);
54-
55-
std::unique_ptr<Allocation> new_allocation(new UnderlyingManualAllocation(
56-
this, std::move(reinterpret_cast<UnderlyingManualAllocation *>(allocation)
57-
->allocation_)));
58-
allocations_.emplace(allocation->size(), std::move(new_allocation));
54+
allocations_.emplace(allocation->size(), AllocationPtr(allocation));
5955
}
60-
MannualFreeAllocation *BufferedAllocator::AllocateImpl(size_t size,
61-
Allocator::Attr attr) {
56+
Allocation *BufferedAllocator::AllocateImpl(size_t size, Allocator::Attr attr) {
6257
{
6358
platform::LockGuardPtr<std::mutex> guard(mtx_);
6459
auto it = allocations_.lower_bound(size);
6560
if (it != allocations_.end() && it->first < size * 2) {
66-
std::unique_ptr<Allocation> result(std::move(it->second));
61+
AllocationPtr result(std::move(it->second));
6762
allocations_.erase(it);
68-
return new UnderlyingManualAllocation(this, std::move(result));
63+
return new UnderlyingManualAllocation(std::move(result));
6964
}
7065
}
7166

7267
try {
7368
return new UnderlyingManualAllocation(
74-
this, underlying_allocator_->Allocate(size, attr));
69+
underlying_allocator_->Allocate(size, attr));
7570
} catch (BadAlloc &) {
7671
FreeCache(size);
7772
return new UnderlyingManualAllocation(
78-
this, underlying_allocator_->Allocate(size, attr));
73+
underlying_allocator_->Allocate(size, attr));
7974
}
8075
}
8176

0 commit comments

Comments
 (0)