Skip to content

Commit 0d6718f

Browse files
committed
Pass compile
1 parent d93b2d0 commit 0d6718f

File tree

6 files changed

+65
-78
lines changed

6 files changed

+65
-78
lines changed

paddle/fluid/framework/mixed_vector.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ class Vector {
284284
bool IsInCPU() const { return flag_ & kDataInCPU; }
285285

286286
mutable std::vector<T> cpu_;
287-
mutable std::unique_ptr<memory::Allocation> gpu_;
287+
mutable memory::AllocationPtr gpu_;
288288
mutable int flag_;
289289

290290
mutable std::mutex mtx_;

paddle/fluid/memory/allocation/best_fit_allocator_test.cc

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,10 @@ class StubAllocation : public Allocation {
3232
TEST(BestFitAllocator, test_allocation) {
3333
StubAllocation stub(4UL * 1024 * 1024 * 1024);
3434
BestFitAllocator allocator(&stub);
35-
{
36-
auto allocation = allocator.Allocate(64);
37-
allocator.FreeUniquePtr(std::move(allocation));
38-
}
35+
{ auto allocation = allocator.Allocate(64, allocator.kDefault); }
3936

4037
{
41-
auto allocation = allocator.Allocate(80);
38+
auto allocation = allocator.Allocate(80, allocator.kDefault);
4239

4340
{
4441
auto best_fit_allocation =
@@ -50,51 +47,51 @@ TEST(BestFitAllocator, test_allocation) {
5047
ASSERT_EQ(allocation->ptr(), nullptr);
5148
}
5249

53-
auto allocation2 = allocator.Allocate(60);
54-
auto allocation3 = allocator.Allocate(90);
55-
allocator.FreeUniquePtr(std::move(allocation2));
56-
allocation2 = allocator.Allocate(30);
50+
auto allocation2 = allocator.Allocate(60, allocator.kDefault);
51+
auto allocation3 = allocator.Allocate(90, allocator.kDefault);
52+
allocation2.reset();
53+
allocation2 = allocator.Allocate(30, allocator.kDefault);
5754

5855
{
5956
auto best_fit_allocation =
6057
dynamic_cast<BestFitAllocation*>(allocation2.get());
6158
ASSERT_EQ(best_fit_allocation->ChunkIterator()->offset_, 80);
6259
}
63-
allocator.FreeUniquePtr(std::move(allocation2));
64-
65-
allocation2 = allocator.Allocate(60);
60+
allocation2.reset();
61+
allocation2 = allocator.Allocate(60, allocator.kDefault);
6662

6763
{
6864
auto best_fit_allocation =
6965
dynamic_cast<BestFitAllocation*>(allocation2.get());
7066
ASSERT_EQ(best_fit_allocation->ChunkIterator()->offset_, 80);
7167
}
7268

73-
allocator.FreeUniquePtr(std::move(allocation));
74-
allocator.FreeUniquePtr(std::move(allocation2));
69+
allocation.reset();
70+
allocation2.reset();
7571

76-
allocation = allocator.Allocate(80 + 60);
72+
allocation = allocator.Allocate(80 + 60, allocator.kDefault);
7773
{
7874
auto best_fit_allocation =
7975
dynamic_cast<BestFitAllocation*>(allocation.get());
8076
ASSERT_EQ(best_fit_allocation->ChunkIterator()->offset_, 0);
8177
}
8278

83-
allocator.FreeUniquePtr(std::move(allocation));
79+
allocation.reset();
8480

85-
allocation = allocator.Allocate(80);
86-
allocation2 = allocator.Allocate(60);
87-
allocator.FreeUniquePtr(std::move(allocation));
88-
allocator.FreeUniquePtr(std::move(allocation3));
89-
allocator.FreeUniquePtr(std::move(allocation2));
81+
allocation = allocator.Allocate(80, allocator.kDefault);
82+
allocation2 = allocator.Allocate(60, allocator.kDefault);
83+
allocation = nullptr;
84+
allocation2 = nullptr;
85+
allocation3 = nullptr;
9086

9187
ASSERT_EQ(allocator.NumFreeChunks(), 1U);
9288
}
9389
}
9490

9591
TEST(BestFitAllocator, test_concurrent_cpu_allocation) {
9692
CPUAllocator allocator;
97-
auto global_allocation = allocator.Allocate(256UL * 1024 * 1024);
93+
auto global_allocation =
94+
allocator.Allocate(256UL * 1024 * 1024, allocator.kDefault);
9895

9996
std::unique_ptr<Allocator> best_fit_allocator(
10097
new BestFitAllocator(global_allocation.get()));
@@ -109,8 +106,8 @@ TEST(BestFitAllocator, test_concurrent_cpu_allocation) {
109106
for (size_t i = 0; i < 128; ++i) {
110107
size_t allocate_size = dist(engine);
111108

112-
auto allocation =
113-
locked_allocator.Allocate(sizeof(size_t) * allocate_size);
109+
auto allocation = locked_allocator.Allocate(
110+
sizeof(size_t) * allocate_size, locked_allocator.kDefault);
114111

115112
size_t* data = reinterpret_cast<size_t*>(allocation->ptr());
116113

@@ -122,8 +119,6 @@ TEST(BestFitAllocator, test_concurrent_cpu_allocation) {
122119
for (size_t j = 0; j < allocate_size; ++j) {
123120
ASSERT_EQ(data[j], j);
124121
}
125-
126-
locked_allocator.FreeUniquePtr(std::move(allocation));
127122
}
128123
};
129124
{
@@ -135,8 +130,6 @@ TEST(BestFitAllocator, test_concurrent_cpu_allocation) {
135130
th.join();
136131
}
137132
}
138-
139-
allocator.FreeUniquePtr(std::move(global_allocation));
140133
}
141134

142135
} // namespace allocation

paddle/fluid/memory/allocation/best_fit_allocator_test.cu

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ struct ForEachFill {
3535
TEST(BestFitAllocator, concurrent_cuda) {
3636
CUDAAllocator allocator(platform::CUDAPlace(0));
3737
// 256 MB
38-
auto cuda_allocation = allocator.Allocate(256U * 1024 * 1024);
38+
auto cuda_allocation =
39+
allocator.Allocate(256U * 1024 * 1024, allocator.kDefault);
3940
LockedAllocator concurrent_allocator(
4041
std::unique_ptr<Allocator>(new BestFitAllocator(cuda_allocation.get())));
4142

@@ -49,8 +50,8 @@ TEST(BestFitAllocator, concurrent_cuda) {
4950
for (size_t i = 0; i < 128; ++i) {
5051
size_t allocate_size = dist(engine);
5152

52-
auto allocation =
53-
concurrent_allocator.Allocate(sizeof(size_t) * allocate_size);
53+
auto allocation = concurrent_allocator.Allocate(
54+
sizeof(size_t) * allocate_size, concurrent_allocator.kDefault);
5455

5556
size_t* data = reinterpret_cast<size_t*>(allocation->ptr());
5657

@@ -66,8 +67,7 @@ TEST(BestFitAllocator, concurrent_cuda) {
6667
for (size_t j = 0; j < allocate_size; ++j) {
6768
ASSERT_EQ(buf[j], j);
6869
}
69-
70-
concurrent_allocator.FreeUniquePtr(std::move(allocation));
70+
allocation = nullptr;
7171
}
7272
};
7373

@@ -80,7 +80,7 @@ TEST(BestFitAllocator, concurrent_cuda) {
8080
th.join();
8181
}
8282
}
83-
allocator.FreeUniquePtr(std::move(cuda_allocation));
83+
// allocator.FreeUniquePtr(std::move(cuda_allocation));
8484
}
8585

8686
} // namespace allocation

paddle/fluid/memory/allocation/buffered_allocator_test.cc

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ inline std::unique_ptr<BufferedAllocator> GetBufferedAllocator(
3535

3636
TEST(buffered_allocator, thread_safety) {
3737
std::unique_ptr<CPUAllocator> allocator(new CPUAllocator());
38-
auto chunk = allocator->Allocate(1 << 20);
38+
auto chunk = allocator->Allocate(1 << 20, allocator->kDefault);
3939
{
4040
auto buf_allocator = GetBufferedAllocator(chunk.get(), true);
4141
ASSERT_EQ(buf_allocator->IsAllocThreadSafe(), true);
@@ -45,36 +45,15 @@ TEST(buffered_allocator, thread_safety) {
4545
auto buf_allocator = GetBufferedAllocator(chunk.get(), false);
4646
ASSERT_EQ(buf_allocator->IsAllocThreadSafe(), false);
4747
}
48-
49-
allocator->FreeUniquePtr(std::move(chunk));
5048
}
5149

5250
class StubAllocation : public Allocation {
5351
public:
5452
using Allocation::Allocation;
5553
};
5654

57-
class StubAllocator : public UnmanagedAllocator {
55+
class StubAllocator : public MannualFreeAllocator {
5856
public:
59-
std::unique_ptr<Allocation> Allocate(size_t size,
60-
Allocator::Attr attr) override {
61-
++construct_count_;
62-
if (size == 0) {
63-
return std::unique_ptr<Allocation>(
64-
new StubAllocation(nullptr, 0, platform::CPUPlace()));
65-
} else {
66-
return std::unique_ptr<Allocation>(
67-
new StubAllocation(new uint8_t[size], size, platform::CPUPlace()));
68-
}
69-
}
70-
71-
void FreeUniquePtr(std::unique_ptr<Allocation> allocation) {
72-
StubAllocation *alloc = dynamic_cast<StubAllocation *>(allocation.get());
73-
PADDLE_ENFORCE_NOT_NULL(alloc);
74-
if (alloc->ptr()) delete[] static_cast<uint8_t *>(alloc->ptr());
75-
++destruct_count_;
76-
}
77-
7857
void ResetCounter() {
7958
construct_count_ = 0;
8059
destruct_count_ = 0;
@@ -84,6 +63,23 @@ class StubAllocator : public UnmanagedAllocator {
8463

8564
size_t GetFreeCount() const { return destruct_count_; }
8665

66+
protected:
67+
void Free(Allocation *allocation) override {
68+
auto *alloc = dynamic_cast<StubAllocation *>(allocation);
69+
PADDLE_ENFORCE_NOT_NULL(alloc);
70+
if (alloc->ptr()) delete[] static_cast<uint8_t *>(alloc->ptr());
71+
++destruct_count_;
72+
delete allocation;
73+
}
74+
Allocation *AllocateImpl(size_t size, Allocator::Attr attr) override {
75+
++construct_count_;
76+
if (size == 0) {
77+
return new StubAllocation(nullptr, 0, platform::CPUPlace());
78+
} else {
79+
return new StubAllocation(new uint8_t[size], size, platform::CPUPlace());
80+
}
81+
}
82+
8783
private:
8884
size_t construct_count_ = 0;
8985
size_t destruct_count_ = 0;
@@ -101,24 +97,24 @@ TEST(buffered_allocator, lazy_free) {
10197

10298
{
10399
underlying_allocator->ResetCounter();
104-
auto x = allocator->Allocate(1025);
100+
auto x = allocator->Allocate(1025, allocator->kDefault);
105101
ASSERT_EQ(underlying_allocator->GetAllocCount(), kOne);
106102
ASSERT_EQ(underlying_allocator->GetFreeCount(), kZero);
107-
allocator->FreeUniquePtr(std::move(x));
103+
x = nullptr;
108104
ASSERT_EQ(underlying_allocator->GetFreeCount(), kZero);
109105
}
110106

111107
{
112108
underlying_allocator->ResetCounter();
113-
auto x = allocator->Allocate(900);
109+
auto x = allocator->Allocate(900, allocator->kDefault);
114110
ASSERT_EQ(underlying_allocator->GetAllocCount(), kZero);
115111
ASSERT_EQ(underlying_allocator->GetFreeCount(), kZero);
116-
auto y = allocator->Allocate(2048);
112+
auto y = allocator->Allocate(2048, allocator->kDefault);
117113
ASSERT_EQ(underlying_allocator->GetAllocCount(), kOne);
118114
ASSERT_EQ(underlying_allocator->GetFreeCount(), kZero);
119-
allocator->FreeUniquePtr(std::move(x));
115+
x = nullptr;
120116
ASSERT_EQ(underlying_allocator->GetFreeCount(), kZero);
121-
allocator->FreeUniquePtr(std::move(y));
117+
y = nullptr;
122118
ASSERT_EQ(underlying_allocator->GetFreeCount(), kZero);
123119
}
124120

@@ -132,13 +128,13 @@ TEST(buffered_allocator, lazy_free) {
132128

133129
TEST(buffered_allocator, garbage_collection) {
134130
std::unique_ptr<CPUAllocator> cpu_allocator(new CPUAllocator());
135-
auto chunk = cpu_allocator->Allocate(2048);
131+
auto chunk = cpu_allocator->Allocate(2048, cpu_allocator->kDefault);
136132
auto allocator = GetBufferedAllocator(chunk.get(), false);
137-
auto x1 = allocator->Allocate(1600);
138-
auto x2 = allocator->Allocate(400);
139-
allocator->FreeUniquePtr(std::move(x1));
140-
allocator->FreeUniquePtr(std::move(x2));
141-
auto x3 = allocator->Allocate(1600);
133+
auto x1 = allocator->Allocate(1600, allocator->kDefault);
134+
auto x2 = allocator->Allocate(400, allocator->kDefault);
135+
x1 = nullptr;
136+
x2 = nullptr;
137+
auto x3 = allocator->Allocate(1600, allocator->kDefault);
142138
ASSERT_NE(x3, nullptr);
143139
ASSERT_NE(x3->ptr(), nullptr);
144140
}

paddle/fluid/memory/allocation/retry_allocator_test.cc

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ TEST(RetryAllocator, RetryAllocator) {
3232
CPUAllocator cpu_allocator;
3333

3434
size_t size = (1 << 20);
35-
auto cpu_allocation = cpu_allocator.Allocate(size);
35+
auto cpu_allocation = cpu_allocator.Allocate(size, cpu_allocator.kDefault);
3636

3737
std::unique_ptr<BestFitAllocator> best_fit_allocator(
3838
new BestFitAllocator(cpu_allocation.get()));
@@ -44,15 +44,15 @@ TEST(RetryAllocator, RetryAllocator) {
4444
size_t extra_time = 2;
4545

4646
// Reserve to perform more tests in the future
47-
std::vector<std::shared_ptr<ManagedAllocator>> allocators;
47+
std::vector<std::shared_ptr<Allocator>> allocators;
4848
{
4949
std::unique_ptr<BestFitAllocator> best_fit_allocator(
5050
new BestFitAllocator(cpu_allocation.get()));
5151
std::unique_ptr<LockedAllocator> locked_allocator(
5252
new LockedAllocator(std::move(best_fit_allocator)));
53-
allocators.push_back(
54-
RetryAllocator::Create(std::move(locked_allocator),
55-
(thread_num - 1) * (sleep_time + extra_time)));
53+
allocators.push_back(std::make_shared<RetryAllocator>(
54+
std::move(locked_allocator),
55+
(thread_num - 1) * (sleep_time + extra_time)));
5656
}
5757

5858
for (auto &allocator : allocators) {
@@ -91,8 +91,6 @@ TEST(RetryAllocator, RetryAllocator) {
9191
[val](void *p) { return p == val; });
9292
ASSERT_TRUE(is_all_equal);
9393
}
94-
95-
cpu_allocator.FreeUniquePtr(std::move(cpu_allocation));
9694
}
9795

9896
} // namespace allocation

paddle/fluid/platform/device_context.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class CudnnHolder {
110110
std::mutex& Mutex() { return mtx_; }
111111

112112
cudnnHandle_t cudnn_handle_;
113-
std::unique_ptr<memory::Allocation> workspace_;
113+
memory::AllocationPtr workspace_;
114114

115115
const cudaStream_t* stream_; // not owned;
116116
const CUDAPlace place_;

0 commit comments

Comments
 (0)