Skip to content

Commit 124f1df

Browse files
committed
Add flags for init and re-alloc gpu
test=develop
2 parents 2271548 + d3acf68 commit 124f1df

File tree

12 files changed

+296
-72
lines changed

12 files changed

+296
-72
lines changed

paddle/fluid/memory/allocation/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,6 @@ nv_test(allocation_and_eigen_test SRCS allocation_and_eigen_test.cu DEPS allocat
6161

6262
cc_test(retry_allocator_test SRCS retry_allocator_test.cc DEPS retry_allocator best_fit_allocator locked_allocator cpu_allocator)
6363

64-
cc_test(allocator_facade_test SRCS allocator_facade_test.cc DEPS allocator_facade)
64+
cc_test(allocator_facade_abs_flags_test SRCS allocator_facade_abs_flags_test.cc DEPS allocator_facade)
65+
66+
cc_test(allocator_facade_frac_flags_test SRCS allocator_facade_frac_flags_test.cc DEPS allocator_facade)

paddle/fluid/memory/allocation/allocator_facade_test.cc renamed to paddle/fluid/memory/allocation/allocator_facade_abs_flags_test.cc

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
#ifdef PADDLE_WITH_CUDA
2020
DECLARE_double(fraction_of_gpu_memory_to_use);
2121
DECLARE_double(fraction_of_cuda_pinned_memory_to_use);
22-
DECLARE_uint64(gpu_init_memory_in_mb);
23-
DECLARE_uint64(gpu_reallocate_memory_in_mb);
22+
DECLARE_uint64(initial_gpu_memory_in_mb);
23+
DECLARE_uint64(reallocate_gpu_memory_in_mb);
2424
DECLARE_int64(gpu_allocator_retry_time);
2525
#endif
2626

@@ -79,25 +79,15 @@ void AllocateTestCases() {
7979
#endif
8080
}
8181

82-
TEST(allocator, allocator) {
82+
TEST(Allocator, SpecifyGpuMemory) {
8383
#ifdef PADDLE_WITH_CUDA
84-
FLAGS_fraction_of_gpu_memory_to_use = 0.01;
85-
FLAGS_gpu_allocator_retry_time = 500;
86-
FLAGS_fraction_of_cuda_pinned_memory_to_use = 0.5;
87-
#endif
88-
89-
AllocateTestCases();
90-
}
91-
92-
TEST(allocator, specify_gpu_memory) {
93-
#ifdef PADDLE_WITH_CUDA
94-
// Set to 0.0 to test FLAGS_gpu_init_memory_in_mb and
95-
// FLAGS_gpu_reallocate_memory_in_mb
84+
// Set to 0.0 to test FLAGS_initial_gpu_memory_in_mb and
85+
// FLAGS_reallocate_gpu_memory_in_mb
9686
FLAGS_fraction_of_gpu_memory_to_use = 0.0;
9787
// 512 MB
98-
FLAGS_gpu_init_memory_in_mb = 512;
88+
FLAGS_initial_gpu_memory_in_mb = 512;
9989
// 4 MB
100-
FLAGS_gpu_reallocate_memory_in_mb = 4;
90+
FLAGS_reallocate_gpu_memory_in_mb = 4;
10191
FLAGS_gpu_allocator_retry_time = 500;
10292
FLAGS_fraction_of_cuda_pinned_memory_to_use = 0.5;
10393
#endif
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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_facade.h"
16+
#include <gflags/gflags.h>
17+
#include <gtest/gtest.h>
18+
19+
#ifdef PADDLE_WITH_CUDA
20+
DECLARE_double(fraction_of_gpu_memory_to_use);
21+
DECLARE_double(fraction_of_cuda_pinned_memory_to_use);
22+
DECLARE_uint64(initial_gpu_memory_in_mb);
23+
DECLARE_uint64(reallocate_gpu_memory_in_mb);
24+
DECLARE_int64(gpu_allocator_retry_time);
25+
#endif
26+
27+
namespace paddle {
28+
namespace memory {
29+
namespace allocation {
30+
31+
//! Run allocate test cases for different places
32+
void AllocateTestCases() {
33+
auto &instance = AllocatorFacade::Instance();
34+
platform::Place place;
35+
size_t size = 1024;
36+
37+
{
38+
place = platform::CPUPlace();
39+
size = 1024;
40+
auto cpu_allocation = instance.Alloc(place, size);
41+
ASSERT_NE(cpu_allocation, nullptr);
42+
ASSERT_NE(cpu_allocation->ptr(), nullptr);
43+
ASSERT_EQ(cpu_allocation->place(), place);
44+
ASSERT_EQ(cpu_allocation->size(), size);
45+
}
46+
47+
#ifdef PADDLE_WITH_CUDA
48+
{
49+
place = platform::CUDAPlace(0);
50+
size = 1024;
51+
auto gpu_allocation = instance.Alloc(place, size);
52+
ASSERT_NE(gpu_allocation, nullptr);
53+
ASSERT_NE(gpu_allocation->ptr(), nullptr);
54+
ASSERT_EQ(gpu_allocation->place(), place);
55+
ASSERT_GE(gpu_allocation->size(), size);
56+
}
57+
58+
{
59+
// Allocate 2GB gpu memory
60+
place = platform::CUDAPlace(0);
61+
size = 2 * static_cast<size_t>(1 << 30);
62+
auto gpu_allocation = instance.Alloc(place, size);
63+
ASSERT_NE(gpu_allocation, nullptr);
64+
ASSERT_NE(gpu_allocation->ptr(), nullptr);
65+
ASSERT_EQ(gpu_allocation->place(), place);
66+
ASSERT_GE(gpu_allocation->size(), size);
67+
}
68+
69+
{
70+
place = platform::CUDAPinnedPlace();
71+
size = (1 << 20);
72+
auto cuda_pinned_allocation =
73+
instance.Alloc(platform::CUDAPinnedPlace(), 1 << 20);
74+
ASSERT_NE(cuda_pinned_allocation, nullptr);
75+
ASSERT_NE(cuda_pinned_allocation->ptr(), nullptr);
76+
ASSERT_EQ(cuda_pinned_allocation->place(), place);
77+
ASSERT_GE(cuda_pinned_allocation->size(), size);
78+
}
79+
#endif
80+
}
81+
82+
TEST(Allocator, Allocator) {
83+
#ifdef PADDLE_WITH_CUDA
84+
FLAGS_fraction_of_gpu_memory_to_use = 0.01;
85+
FLAGS_gpu_allocator_retry_time = 500;
86+
FLAGS_fraction_of_cuda_pinned_memory_to_use = 0.5;
87+
#endif
88+
89+
AllocateTestCases();
90+
}
91+
92+
} // namespace allocation
93+
} // namespace memory
94+
} // namespace paddle

paddle/fluid/memory/allocation/legacy_allocator.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ DEFINE_bool(init_allocated_mem, false,
3737
"that initializing the allocated memory with a small value "
3838
"during unit testing.");
3939
DECLARE_double(fraction_of_gpu_memory_to_use);
40-
DECLARE_uint64(gpu_init_memory_in_mb);
41-
DECLARE_uint64(gpu_reallocate_memory_in_mb);
40+
DECLARE_uint64(initial_gpu_memory_in_mb);
41+
DECLARE_uint64(reallocate_gpu_memory_in_mb);
4242
DECLARE_bool(benchmark);
4343

4444
namespace paddle {
@@ -158,15 +158,15 @@ BuddyAllocator *GetGPUBuddyAllocator(int gpu_id) {
158158
VLOG(10) << "\n\nNOTE:\n"
159159
<< "You can set GFlags environment variable "
160160
<< "'FLAGS_fraction_of_gpu_memory_to_use' "
161-
<< "or 'FLAGS_gpu_init_memory_in_mb' "
162-
<< "or 'FLAGS_gpu_reallocate_memory_in_mb' "
161+
<< "or 'FLAGS_initial_gpu_memory_in_mb' "
162+
<< "or 'FLAGS_reallocate_gpu_memory_in_mb' "
163163
<< "to change the memory size for GPU usage.\n"
164164
<< "Current 'FLAGS_fraction_of_gpu_memory_to_use' value is "
165165
<< FLAGS_fraction_of_gpu_memory_to_use
166-
<< ". Current 'FLAGS_gpu_init_memory_in_mb' value is "
167-
<< FLAGS_gpu_init_memory_in_mb
168-
<< ". Current 'FLAGS_gpu_reallocate_memory_in_mb' value is "
169-
<< FLAGS_gpu_reallocate_memory_in_mb << "\n\n";
166+
<< ". Current 'FLAGS_initial_gpu_memory_in_mb' value is "
167+
<< FLAGS_initial_gpu_memory_in_mb
168+
<< ". Current 'FLAGS_reallocate_gpu_memory_in_mb' value is "
169+
<< FLAGS_reallocate_gpu_memory_in_mb << "\n\n";
170170
}
171171
});
172172

paddle/fluid/memory/detail/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ endif(${WITH_GPU})
99
cc_test(system_allocator_test SRCS system_allocator_test.cc DEPS system_allocator)
1010

1111
cc_library(buddy_allocator SRCS buddy_allocator.cc DEPS memory_block system_allocator glog)
12+
13+
cc_test(buddy_allocator_test SRCS buddy_allocator_test.cc DEPS buddy_allocator)

paddle/fluid/memory/detail/buddy_allocator.cc

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ limitations under the License. */
1414

1515
#include "paddle/fluid/memory/detail/buddy_allocator.h"
1616

17-
#include <utility> // for std::move
17+
#include <algorithm>
18+
#include <utility>
1819

1920
#include "glog/logging.h"
2021

@@ -39,9 +40,10 @@ BuddyAllocator::~BuddyAllocator() {
3940
"have actually been freed";
4041
while (!pool_.empty()) {
4142
auto block = static_cast<MemoryBlock*>(std::get<2>(*pool_.begin()));
42-
VLOG(10) << "Free from block (" << block << ", " << max_chunk_size_ << ")";
43+
VLOG(10) << "Free from block (" << block << ", " << block->size(cache_)
44+
<< ")";
4345

44-
system_allocator_->Free(block, max_chunk_size_, block->index(cache_));
46+
system_allocator_->Free(block, block->size(cache_), block->index(cache_));
4547
cache_.invalidate(block);
4648
pool_.erase(pool_.begin());
4749
}
@@ -74,7 +76,7 @@ void* BuddyAllocator::Alloc(size_t unaligned_size) {
7476

7577
// refill the pool if failure
7678
if (it == pool_.end()) {
77-
it = RefillPool();
79+
it = RefillPool(size);
7880
// if still failure, fail fatally
7981
if (it == pool_.end()) {
8082
return nullptr;
@@ -187,22 +189,22 @@ void* BuddyAllocator::SystemAlloc(size_t size) {
187189
return static_cast<MemoryBlock*>(p)->data();
188190
}
189191

190-
BuddyAllocator::PoolSet::iterator BuddyAllocator::RefillPool() {
192+
BuddyAllocator::PoolSet::iterator BuddyAllocator::RefillPool(
193+
size_t request_bytes) {
191194
size_t allocate_bytes = max_chunk_size_;
192195
size_t index = 0;
193196

194197
#ifdef PADDLE_WITH_CUDA
195198
if (system_allocator_->UseGpu()) {
196199
if ((total_used_ + total_free_) == 0) {
197200
// Compute the allocation size for gpu for the first allocation.
198-
max_chunk_size_ = platform::GpuMaxChunkSize();
199-
allocate_bytes = platform::GpuInitAllocSize();
201+
allocate_bytes = std::max(platform::GpuInitAllocSize(), request_bytes);
200202
} else {
201203
// Reallocation size
202204
if (realloc_size_ == 0) {
203205
realloc_size_ = platform::GpuReallocSize();
204206
}
205-
allocate_bytes = realloc_size_;
207+
allocate_bytes = std::max(realloc_size_, request_bytes);
206208
}
207209
}
208210
#endif
@@ -298,12 +300,12 @@ void BuddyAllocator::CleanIdleFallBackAlloc() {
298300

299301
VLOG(10) << "Return block " << block << " to fallback allocator.";
300302

301-
system_allocator_->Free(block, max_chunk_size_, block->index(cache_));
303+
system_allocator_->Free(block, block->size(cache_), block->index(cache_));
302304
cache_.invalidate(block);
303305

304306
pool = PoolSet::reverse_iterator(pool_.erase(std::next(pool).base()));
305307

306-
total_free_ -= max_chunk_size_;
308+
total_free_ -= block->size(cache_);
307309
fallback_alloc_count_--;
308310

309311
// If no fall allocation exists, return directly
@@ -334,12 +336,12 @@ void BuddyAllocator::CleanIdleNormalAlloc() {
334336

335337
VLOG(10) << "Return block " << block << " to base allocator.";
336338

337-
system_allocator_->Free(block, max_chunk_size_, block->index(cache_));
339+
system_allocator_->Free(block, block->size(cache_), block->index(cache_));
338340
cache_.invalidate(block);
339341

340342
pool = PoolSet::reverse_iterator(pool_.erase(std::next(pool).base()));
341343

342-
total_free_ -= max_chunk_size_;
344+
total_free_ -= block->size(cache_);
343345

344346
if (!shall_free_alloc()) return;
345347
}

paddle/fluid/memory/detail/buddy_allocator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class BuddyAllocator {
6060
void* SystemAlloc(size_t size);
6161

6262
/*! \brief If existing chunks are not suitable, refill pool */
63-
PoolSet::iterator RefillPool();
63+
PoolSet::iterator RefillPool(size_t request_bytes);
6464

6565
/**
6666
* \brief Find the suitable chunk from existing pool and split

0 commit comments

Comments
 (0)