Skip to content

Commit e5c4cf6

Browse files
committed
Polish allocation
Clean allocation->Deleter test=develop
1 parent 0d6718f commit e5c4cf6

22 files changed

+111
-107
lines changed

paddle/fluid/memory/allocation/aligned_allocator.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,12 @@ template <size_t kAlignment>
8686
class AlignedAllocator : public ThinAlignedAllocator {
8787
public:
8888
using ThinAlignedAllocator::ThinAlignedAllocator;
89-
AllocationPtr Allocate(size_t size, Attr attr) override {
89+
90+
protected:
91+
Allocation* AllocateImpl(size_t size, Allocator::Attr attr) override {
9092
auto raw_allocation =
9193
underlying_allocator_->Allocate(size + kAlignment, attr);
92-
return AllocationPtr(
93-
new AlignedAllocation<kAlignment>(std::move(raw_allocation), size));
94+
return new AlignedAllocation<kAlignment>(std::move(raw_allocation), size);
9495
}
9596
};
9697

paddle/fluid/memory/allocation/underlying_manual_allocation.h renamed to paddle/fluid/memory/allocation/allocation_with_underlying.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ namespace paddle {
2020
namespace memory {
2121
namespace allocation {
2222

23-
class UnderlyingManualAllocation : public Allocation {
23+
class AllocationWithUnderlying : public Allocation {
2424
public:
25-
explicit UnderlyingManualAllocation(AllocationPtr allocation)
25+
explicit AllocationWithUnderlying(AllocationPtr allocation)
2626
: Allocation(allocation->ptr(), allocation->size(), allocation->place()),
2727
allocation_(std::move(allocation)) {}
2828
AllocationPtr allocation_;

paddle/fluid/memory/allocation/allocator.cc

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

1515
#include "paddle/fluid/memory/allocation/allocator.h"
16+
1617
#include <functional>
1718

1819
namespace paddle {
@@ -24,23 +25,20 @@ Allocator::~Allocator() {}
2425

2526
bool Allocator::IsAllocThreadSafe() const { return false; }
2627

28+
AllocationPtr Allocator::Allocate(size_t size, Allocator::Attr attr) {
29+
auto ptr = AllocateImpl(size, attr);
30+
ptr->set_allocator(this);
31+
return AllocationPtr(ptr);
32+
}
33+
34+
void Allocator::Free(Allocation* allocation) { delete allocation; }
35+
2736
const char* BadAlloc::what() const noexcept { return msg_.c_str(); }
2837

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-
}
3638
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-
}
39+
allocation->allocator()->Free(allocation);
4340
}
41+
4442
} // namespace allocation
4543
} // namespace memory
4644
} // namespace paddle

paddle/fluid/memory/allocation/allocator.h

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ class BadAlloc : public std::exception {
3232
};
3333

3434
class Allocation;
35-
struct AllocationDeleter {
35+
class AllocationDeleter {
36+
public:
3637
void operator()(Allocation* allocation) const;
3738
};
3839

40+
class Allocator;
3941
// Allocation is the object holding the actually pointer. Use
4042
// `Allocation::ptr()` will returns the pointer that allocated.
4143
//
@@ -45,7 +47,7 @@ struct AllocationDeleter {
4547
class Allocation {
4648
public:
4749
Allocation(void* ptr, size_t size, platform::Place place)
48-
: ptr_(ptr), size_(size), place_(place) {}
50+
: allocator_(nullptr), ptr_(ptr), size_(size), place_(place) {}
4951

5052
Allocation(const Allocation& o) = delete;
5153
Allocation& operator=(const Allocation& o) = delete;
@@ -70,11 +72,14 @@ class Allocation {
7072

7173
const platform::Place& place() const { return place_; }
7274

73-
virtual ~Allocation();
75+
Allocator* allocator() { return allocator_; }
7476

75-
std::function<void(Allocation*)> Deleter;
77+
void set_allocator(Allocator* allocator) { allocator_ = allocator; }
78+
79+
virtual ~Allocation();
7680

7781
private:
82+
Allocator* allocator_;
7883
void* ptr_;
7984
size_t size_;
8085
platform::Place place_;
@@ -121,25 +126,18 @@ class Allocator {
121126

122127
virtual ~Allocator();
123128

124-
// Allocate an allocation. Note the return allocation might need to be freed
125-
// manually if the Allocator is an `UnmanagedAllocator`.
126-
virtual AllocationPtr Allocate(size_t size,
127-
Allocator::Attr attr = kDefault) = 0;
129+
// Allocate an allocation.
130+
AllocationPtr Allocate(size_t size, Allocator::Attr attr = kDefault);
128131

129132
// True if the `Allocate` is thread safe.
130133
virtual bool IsAllocThreadSafe() const;
131-
};
132-
133-
// User need to invoke `Free` or `FreeUniquePtr` manually if allocated by
134-
// a manally managed allocator.
135-
class MannualFreeAllocator : public Allocator {
136-
public:
137-
AllocationPtr Allocate(size_t size, Attr attr) final;
138134

139135
protected:
140-
virtual void Free(Allocation* allocation) = 0;
136+
virtual void Free(Allocation* allocation);
141137
virtual Allocation* AllocateImpl(size_t size, Allocator::Attr attr) = 0;
142-
friend class MannualFreeAllocation;
138+
139+
private:
140+
friend class AllocationDeleter;
143141
};
144142

145143
} // namespace allocation

paddle/fluid/memory/allocation/allocator_facade.cc

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

52-
AllocationPtr Allocate(size_t size, Attr attr) override {
53-
return normal_allocator_->Allocate(size, attr);
54-
}
55-
5652
bool IsAllocThreadSafe() const override { return true; }
5753

54+
protected:
55+
Allocation* AllocateImpl(size_t size, Allocator::Attr attr) override {
56+
return normal_allocator_->Allocate(size, attr).release();
57+
}
58+
5859
private:
5960
std::shared_ptr<Allocator> normal_allocator_;
6061
};
@@ -103,10 +104,6 @@ class ChunkedManagedAllocator : public Allocator {
103104
raw_allocator_.reset();
104105
}
105106

106-
AllocationPtr Allocate(size_t size, Attr attr) override {
107-
return default_allocator_->Allocate(size, attr);
108-
}
109-
110107
std::shared_ptr<Allocator> BestFitAllocatorCreator() {
111108
chunks_.emplace_back(raw_allocator_->Allocate(max_chunk_size_));
112109
auto* allocation = chunks_.back().get();
@@ -128,6 +125,11 @@ class ChunkedManagedAllocator : public Allocator {
128125

129126
bool IsAllocThreadSafe() const override { return true; }
130127

128+
protected:
129+
Allocation* AllocateImpl(size_t size, Allocator::Attr attr) override {
130+
return default_allocator_->Allocate(size, attr).release();
131+
}
132+
131133
protected:
132134
size_t max_chunk_size_;
133135
int64_t retry_time_;

paddle/fluid/memory/allocation/auto_increment_allocator.cc

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,34 @@
1717
namespace paddle {
1818
namespace memory {
1919
namespace allocation {
20+
bool AutoIncrementAllocator::IsAllocThreadSafe() const { return true; }
2021

21-
AllocationPtr AutoIncrementAllocator::Allocate(size_t size,
22-
Allocator::Attr attr) {
22+
std::shared_ptr<Allocator> AutoIncrementAllocator::CreateNewAllocator() {
23+
std::lock_guard<std::mutex> guard(mtx_);
24+
auto old_size = allocator_num_.load();
25+
PADDLE_ENFORCE_LT(old_size, underlying_allocators_.size(),
26+
"Allocator number exceeds capacity %d",
27+
underlying_allocators_.size());
28+
underlying_allocators_[old_size] = creator_();
29+
prev_success_allocator_ = old_size;
30+
++allocator_num_;
31+
PADDLE_ENFORCE(
32+
underlying_allocators_[old_size]->IsAllocThreadSafe(),
33+
"the underlying allocator must be thread safe. This is a program "
34+
"bug.");
35+
return underlying_allocators_[old_size];
36+
}
37+
Allocation *AutoIncrementAllocator::AllocateImpl(size_t size,
38+
Allocator::Attr attr) {
2339
auto cur = prev_success_allocator_.load();
2440
size_t retry_count = allocator_num_.load();
2541
size_t allocator_num = retry_count;
2642
while (retry_count-- > 0) { // until there retry count is zero
2743
try {
2844
auto res = underlying_allocators_[cur]->Allocate(size, attr);
2945
prev_success_allocator_ = cur;
30-
return res;
31-
} catch (BadAlloc&) {
46+
return res.release();
47+
} catch (BadAlloc &) {
3248
if (++cur >= allocator_num) {
3349
cur = 0;
3450
}
@@ -47,32 +63,14 @@ AllocationPtr AutoIncrementAllocator::Allocate(size_t size,
4763
try {
4864
auto ret = underlying_allocators_[cur]->Allocate(size, attr);
4965
prev_success_allocator_ = cur;
50-
return ret;
51-
} catch (BadAlloc&) {
66+
return ret.release();
67+
} catch (BadAlloc &) {
5268
} catch (...) {
5369
throw;
5470
}
5571
}
5672
// No suitable allocator
57-
return CreateNewAllocator()->Allocate(size, attr);
58-
}
59-
60-
bool AutoIncrementAllocator::IsAllocThreadSafe() const { return true; }
61-
62-
std::shared_ptr<Allocator> AutoIncrementAllocator::CreateNewAllocator() {
63-
std::lock_guard<std::mutex> guard(mtx_);
64-
auto old_size = allocator_num_.load();
65-
PADDLE_ENFORCE_LT(old_size, underlying_allocators_.size(),
66-
"Allocator number exceeds capacity %d",
67-
underlying_allocators_.size());
68-
underlying_allocators_[old_size] = creator_();
69-
prev_success_allocator_ = old_size;
70-
++allocator_num_;
71-
PADDLE_ENFORCE(
72-
underlying_allocators_[old_size]->IsAllocThreadSafe(),
73-
"the underlying allocator must be thread safe. This is a program "
74-
"bug.");
75-
return underlying_allocators_[old_size];
73+
return CreateNewAllocator()->Allocate(size, attr).release();
7674
}
7775

7876
} // namespace allocation

paddle/fluid/memory/allocation/auto_increment_allocator.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@ class AutoIncrementAllocator : public Allocator {
5454
explicit AutoIncrementAllocator(AllocatorCreator&& creator, size_t capacity)
5555
: creator_(std::move(creator)), underlying_allocators_(capacity) {}
5656

57-
AllocationPtr Allocate(size_t size, Attr attr) override;
58-
5957
bool IsAllocThreadSafe() const override;
6058

6159
private:
6260
std::shared_ptr<Allocator> CreateNewAllocator();
6361

62+
protected:
63+
Allocation* AllocateImpl(size_t size, Allocator::Attr attr) override;
64+
65+
private:
6466
AllocatorCreator creator_;
6567

6668
std::vector<AllocatorCreator::result_type> underlying_allocators_;

paddle/fluid/memory/allocation/best_fit_allocator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class BestFitAllocation : public Allocation {
9898
//
9999
// To free an allocation, it will set the chunk of allocation to free and merge
100100
// the prev-chunk and the next-chunk when possible.
101-
class BestFitAllocator : public MannualFreeAllocator {
101+
class BestFitAllocator : public Allocator {
102102
public:
103103
explicit BestFitAllocator(Allocation* allocation);
104104

paddle/fluid/memory/allocation/buffered_allocator.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include <algorithm>
1717
#include <limits>
1818
#include <utility>
19-
#include "paddle/fluid/memory/allocation/underlying_manual_allocation.h"
19+
#include "paddle/fluid/memory/allocation/allocation_with_underlying.h"
2020

2121
namespace paddle {
2222
namespace memory {
@@ -60,16 +60,16 @@ Allocation *BufferedAllocator::AllocateImpl(size_t size, Allocator::Attr attr) {
6060
if (it != allocations_.end() && it->first < size * 2) {
6161
AllocationPtr result(std::move(it->second));
6262
allocations_.erase(it);
63-
return new UnderlyingManualAllocation(std::move(result));
63+
return new AllocationWithUnderlying(std::move(result));
6464
}
6565
}
6666

6767
try {
68-
return new UnderlyingManualAllocation(
68+
return new AllocationWithUnderlying(
6969
underlying_allocator_->Allocate(size, attr));
7070
} catch (BadAlloc &) {
7171
FreeCache(size);
72-
return new UnderlyingManualAllocation(
72+
return new AllocationWithUnderlying(
7373
underlying_allocator_->Allocate(size, attr));
7474
}
7575
}

paddle/fluid/memory/allocation/buffered_allocator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace allocation {
2929
// memory allocation and reuse memory.
3030
// BufferedAllocator provides the same thread-safety level as
3131
// underlying_allocator_
32-
class BufferedAllocator : public MannualFreeAllocator {
32+
class BufferedAllocator : public Allocator {
3333
public:
3434
explicit BufferedAllocator(std::unique_ptr<Allocator> &&allocator);
3535

0 commit comments

Comments
 (0)