Skip to content

Commit 1420c3b

Browse files
committed
Add enum AllocatorStrategy
test=develop
1 parent b59a9bf commit 1420c3b

File tree

4 files changed

+76
-10
lines changed

4 files changed

+76
-10
lines changed

paddle/fluid/memory/allocation/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ cc_library(aligned_allocator SRCS aligned_allocator.cc DEPS allocator)
4343
cc_library(auto_increment_allocator SRCS auto_increment_allocator.cc DEPS allocator)
4444
cc_library(zero_size_allocator SRCS zero_size_allocator.cc DEPS allocator)
4545
cc_library(conditional_allocator SRCS conditional_allocator.cc DEPS allocator)
46+
cc_library(allocator_strategy SRCS allocator_strategy.cc DEPS gflags)
4647
cc_library(allocator_facade SRCS allocator_facade.cc DEPS
4748
${AllocatorFacadeDeps}
4849
cpu_allocator
@@ -54,7 +55,9 @@ cc_library(allocator_facade SRCS allocator_facade.cc DEPS
5455
zero_size_allocator
5556
conditional_allocator
5657
retry_allocator
57-
buffered_allocator)
58+
buffered_allocator
59+
allocator_strategy
60+
)
5861

5962
nv_test(allocation_and_eigen_test SRCS allocation_and_eigen_test.cu DEPS allocator_facade)
6063

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "paddle/fluid/memory/allocation/allocator_strategy.h"
16+
#include "gflags/gflags.h"
17+
18+
DEFINE_string(
19+
allocator_strategy, "legacy",
20+
"The allocation strategy. Legacy means the original allocator of Fluid."
21+
"New means the experimental allocators of Fluid. in [legacy, new]");
22+
23+
namespace paddle {
24+
namespace memory {
25+
namespace allocation {
26+
27+
static AllocatorStrategy GetStrategyFromFlag() {
28+
return FLAGS_allocator_strategy == "legacy"
29+
? AllocatorStrategy::kLegacy
30+
: AllocatorStrategy::kNaiveBestFit;
31+
}
32+
33+
AllocatorStrategy GetAllocatorStrategy() {
34+
static AllocatorStrategy strategy = GetStrategyFromFlag();
35+
return strategy;
36+
}
37+
} // namespace allocation
38+
} // namespace memory
39+
} // namespace paddle
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
namespace paddle {
18+
namespace memory {
19+
namespace allocation {
20+
21+
enum class AllocatorStrategy { kLegacy, kNaiveBestFit };
22+
23+
extern AllocatorStrategy GetAllocatorStrategy();
24+
25+
} // namespace allocation
26+
} // namespace memory
27+
} // namespace paddle

paddle/fluid/memory/malloc.cc

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ limitations under the License. */
1616

1717
#include "glog/logging.h"
1818
#include "paddle/fluid/memory/allocation/allocator_facade.h"
19-
#include "paddle/fluid/memory/malloc.h"
20-
19+
#include "paddle/fluid/memory/allocation/allocator_strategy.h"
2120
#include "paddle/fluid/memory/detail/buddy_allocator.h"
2221
#include "paddle/fluid/memory/detail/system_allocator.h"
22+
#include "paddle/fluid/memory/malloc.h"
2323
#include "paddle/fluid/platform/gpu_info.h"
2424

2525
DEFINE_bool(init_allocated_mem, false,
@@ -30,11 +30,6 @@ DEFINE_bool(init_allocated_mem, false,
3030
"during unit testing.");
3131
DECLARE_double(fraction_of_gpu_memory_to_use);
3232

33-
DEFINE_string(
34-
allocator_strategy, "legacy",
35-
"The allocation strategy. Legacy means the original allocator of Fluid."
36-
"New means the experimental allocators of Fluid. in [legacy, new]");
37-
3833
namespace paddle {
3934
namespace memory {
4035

@@ -288,7 +283,8 @@ class LegacyAllocation : public Allocation {
288283

289284
std::shared_ptr<Allocation> AllocShared(const platform::Place& place,
290285
size_t size, Allocator::Attr attr) {
291-
if (FLAGS_allocator_strategy == "legacy") {
286+
if (allocation::GetAllocatorStrategy() ==
287+
allocation::AllocatorStrategy::kLegacy) {
292288
void* p = boost::apply_visitor(legacy::AllocVisitor(size), place);
293289
return std::shared_ptr<Allocation>(
294290
new legacy::LegacyAllocation(p, size, place));
@@ -300,7 +296,8 @@ std::shared_ptr<Allocation> AllocShared(const platform::Place& place,
300296

301297
std::unique_ptr<Allocation> Alloc(const platform::Place& place, size_t size,
302298
Allocator::Attr attr) {
303-
if (FLAGS_allocator_strategy == "legacy") {
299+
if (allocation::GetAllocatorStrategy() ==
300+
allocation::AllocatorStrategy::kLegacy) {
304301
void* p = boost::apply_visitor(legacy::AllocVisitor(size), place);
305302
return std::unique_ptr<Allocation>(
306303
new legacy::LegacyAllocation(p, size, place));

0 commit comments

Comments
 (0)