Skip to content

Commit 19e669a

Browse files
committed
Add legacy_allocator
test=develop
1 parent 1cb7e7d commit 19e669a

File tree

9 files changed

+374
-324
lines changed

9 files changed

+374
-324
lines changed

paddle/fluid/memory/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
add_subdirectory(detail)
22
add_subdirectory(allocation)
3-
cc_library(malloc SRCS malloc.cc DEPS buddy_allocator place enforce allocator_facade)
3+
cc_library(malloc SRCS malloc.cc DEPS place enforce allocator_facade)
44
cc_library(memcpy SRCS memcpy.cc DEPS place)
55

66
cc_library(memory

paddle/fluid/memory/allocation/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ cc_library(cpu_allocator SRCS cpu_allocator.cc DEPS allocator)
33
cc_library(best_fit_allocator SRCS best_fit_allocator.cc DEPS allocator)
44
cc_library(locked_allocator SRCS locked_allocator.cc DEPS allocator)
55
cc_library(buffered_allocator SRCS buffered_allocator.cc DEPS allocator)
6+
cc_library(legacy_allocator SRCS legacy_allocator.cc DEPS allocator buddy_allocator)
67
cc_test(buffered_allocator_test SRCS buffered_allocator_test.cc DEPS best_fit_allocator locked_allocator buffered_allocator cpu_allocator)
78

89
if (WITH_GPU)
@@ -53,6 +54,7 @@ cc_library(allocator_facade SRCS allocator_facade.cc DEPS
5354
retry_allocator
5455
buffered_allocator
5556
allocator_strategy
57+
legacy_allocator
5658
)
5759

5860
nv_test(allocation_and_eigen_test SRCS allocation_and_eigen_test.cu DEPS allocator_facade)

paddle/fluid/memory/allocation/allocator.cc

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,7 @@ const char* BadAlloc::what() const noexcept { return msg_.c_str(); }
3737

3838
void AllocationDeleter::operator()(Allocation* allocation) const {
3939
auto* allocator = allocation->allocator();
40-
if (allocator) {
41-
allocator->Free(allocation);
42-
} else {
43-
delete allocation; // Compatible for legacy allocation.
44-
}
40+
allocator->Free(allocation);
4541
}
4642

4743
} // namespace allocation

paddle/fluid/memory/allocation/allocator_facade.cc

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
#include <vector>
2020
#include "paddle/fluid/memory/allocation/aligned_allocator.h"
2121
#include "paddle/fluid/memory/allocation/allocator_facade.h"
22+
#include "paddle/fluid/memory/allocation/allocator_strategy.h"
2223
#include "paddle/fluid/memory/allocation/auto_increment_allocator.h"
2324
#include "paddle/fluid/memory/allocation/best_fit_allocator.h"
2425
#include "paddle/fluid/memory/allocation/conditional_allocator.h"
2526
#include "paddle/fluid/memory/allocation/cpu_allocator.h"
27+
#include "paddle/fluid/memory/allocation/legacy_allocator.h"
2628
#include "paddle/fluid/memory/allocation/locked_allocator.h"
2729
#include "paddle/fluid/memory/allocation/retry_allocator.h"
2830
#include "paddle/fluid/memory/allocation/zero_size_allocator.h"
@@ -190,13 +192,29 @@ class AllocatorFacadePrivate {
190192
~AllocatorFacadePrivate() = default;
191193

192194
AllocatorFacadePrivate() {
193-
InitCPUAllocator();
194-
InitCUDAAllocator();
195-
InitCUDAPinnedAllocator();
196-
WrapZeroSizeAllocator();
195+
if (GetAllocatorStrategy() == AllocatorStrategy::kLegacy) {
196+
InitLegacyAllocator();
197+
} else {
198+
InitCPUAllocator();
199+
InitCUDAAllocator();
200+
InitCUDAPinnedAllocator();
201+
WrapZeroSizeAllocator();
202+
}
197203
}
198204

199205
private:
206+
void InitLegacyAllocator() {
207+
std::vector<platform::Place> places{platform::CPUPlace()};
208+
#ifdef PADDLE_WITH_CUDA
209+
for (int dev_id = 0; dev_id < platform::GetCUDADeviceCount(); ++dev_id) {
210+
places.emplace_back(platform::CUDAPlace(dev_id));
211+
}
212+
#endif
213+
for (auto& p : places) {
214+
allocators_[p] = std::make_shared<LegacyAllocator>(p);
215+
}
216+
}
217+
200218
void InitCPUAllocator() {
201219
allocators_[platform::CPUPlace()] = std::make_shared<CPUManagedAllocator>();
202220
}

paddle/fluid/memory/allocation/buffered_allocator.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@ class BufferedAllocator : public Allocator {
3535

3636
~BufferedAllocator();
3737

38-
// std::unique_ptr<Allocation> Allocate(
39-
// size_t size, Allocator::Attr attr = Allocator::Attr::kDefault)
40-
// override;
41-
//
42-
// void FreeUniquePtr(std::unique_ptr<Allocation> allocation) override;
43-
4438
bool IsAllocThreadSafe() const override;
4539

4640
// only used in unittest

0 commit comments

Comments
 (0)