Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
ada1c20b
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
ada1c20b
编写于
7月 05, 2017
作者:
L
liaogang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
FIX: Buddy Allocator Free with Merge feature
上级
d0ad0314
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
42 addition
and
12 deletion
+42
-12
paddle/memory/detail/buddy_allocator.cc
paddle/memory/detail/buddy_allocator.cc
+29
-4
paddle/memory/detail/buddy_allocator.h
paddle/memory/detail/buddy_allocator.h
+10
-5
paddle/memory/detail/memory_block.cc
paddle/memory/detail/memory_block.cc
+2
-2
paddle/platform/CMakeLists.txt
paddle/platform/CMakeLists.txt
+1
-1
未找到文件。
paddle/memory/detail/buddy_allocator.cc
浏览文件 @
ada1c20b
...
...
@@ -89,9 +89,8 @@ void BuddyAllocator::Free(void* p) {
block
->
index
(
cache_
));
// Invalidate GPU allocation from cache
if
(
system_allocator_
->
UseGpu
())
{
cache_
.
invalidate
(
block
);
}
cache_
.
invalidate
(
block
);
return
;
}
...
...
@@ -104,12 +103,35 @@ void BuddyAllocator::Free(void* p) {
if
(
block
->
has_right_buddy
(
cache_
))
{
DLOG
(
INFO
)
<<
"Merging this block "
<<
block
<<
" with its right buddy "
<<
block
->
right_buddy
(
cache_
);
auto
right_buddy
=
block
->
right_buddy
(
cache_
);
if
(
right_buddy
->
type
(
cache_
)
==
MemoryBlock
::
FREE_CHUNK
)
{
// Take away right buddy from pool
pool_
.
erase
({
right_buddy
->
index
(
cache_
),
right_buddy
->
total_size
(
cache_
),
right_buddy
});
// merge its right buddy to the block
block
->
merge
(
cache_
,
right_buddy
);
}
}
// Trying to merge the left buddy
if
(
block
->
has_left_buddy
(
cache_
))
{
DLOG
(
INFO
)
<<
"Merging this block "
<<
block
<<
" with its left buddy "
<<
block
->
left_buddy
(
cache_
);
auto
left_buddy
=
block
->
left_buddy
(
cache_
);
if
(
left_buddy
->
type
(
cache_
)
==
MemoryBlock
::
FREE_CHUNK
)
{
// Take away right buddy from pool
pool_
.
erase
({
left_buddy
->
index
(
cache_
),
left_buddy
->
total_size
(
cache_
),
left_buddy
});
// merge the block to its left buddy
left_buddy
->
merge
(
cache_
,
block
);
block
=
left_buddy
;
}
}
// Dumping this block into pool
...
...
@@ -167,13 +189,16 @@ BuddyAllocator::PoolSet::iterator BuddyAllocator::FindExistChunk(size_t size) {
while
(
1
)
{
auto
it
=
pool_
.
lower_bound
({
index
,
size
,
nullptr
});
// no match chunk memory
if
(
it
==
pool_
.
end
())
return
it
;
if
(
std
::
get
<
0
>
(
*
it
)
>
index
)
{
// find suitable one
if
(
std
::
get
<
1
>
(
*
it
)
>=
size
)
{
return
it
;
}
// update and continue
index
=
std
::
get
<
0
>
(
*
it
);
continue
;
}
...
...
paddle/memory/detail/buddy_allocator.h
浏览文件 @
ada1c20b
...
...
@@ -42,14 +42,14 @@ class BuddyAllocator {
void
Free
(
void
*
);
size_t
Used
();
p
ublic
:
p
rivate
:
// Disable copy and assignment.
BuddyAllocator
(
const
BuddyAllocator
&
)
=
delete
;
BuddyAllocator
&
operator
=
(
const
BuddyAllocator
&
)
=
delete
;
private:
// Tuple type: allocator index, memory size, memory address
// Tuple (allocator index, memory size, memory address)
using
IndexSizeAddress
=
std
::
tuple
<
size_t
,
size_t
,
void
*>
;
// Each element in PoolSet is a free allocation
using
PoolSet
=
std
::
set
<
IndexSizeAddress
>
;
/*! \brief Allocate fixed-size memory from system */
...
...
@@ -57,7 +57,6 @@ class BuddyAllocator {
/*! \brief If existing chunks are not suitable, refill pool */
PoolSet
::
iterator
RefillPool
();
/**
* \brief Find the suitable chunk from existing pool
*
...
...
@@ -77,13 +76,19 @@ class BuddyAllocator {
size_t
max_chunk_size_
;
// the maximum size of each chunk
private:
/**
* \brief A list of free allocation
*
* \note Only store free chunk memory in pool
*/
PoolSet
pool_
;
private:
/
/ Unify the metadata format between GPU and CPU allocations
/
*! Unify the metadata format between GPU and CPU allocations */
MetadataCache
cache_
;
private:
/*! Allocate CPU/GPU memory from system */
SystemAllocator
*
system_allocator_
;
std
::
mutex
mutex_
;
};
...
...
paddle/memory/detail/memory_block.cc
浏览文件 @
ada1c20b
...
...
@@ -91,8 +91,8 @@ void MemoryBlock::split(MetadataCache& cache, size_t size) {
void
MemoryBlock
::
merge
(
MetadataCache
&
cache
,
MemoryBlock
*
right_buddy
)
{
// only free blocks can be merged
PADDLE_ASSERT
(
type
(
cache
)
==
FREE_
MEMORY
);
PADDLE_ASSERT
(
right_buddy
->
type
(
cache
)
==
FREE_
MEMORY
);
PADDLE_ASSERT
(
type
(
cache
)
==
FREE_
CHUNK
);
PADDLE_ASSERT
(
right_buddy
->
type
(
cache
)
==
FREE_
CHUNK
);
auto
metadata
=
cache
.
load
(
this
);
...
...
paddle/platform/CMakeLists.txt
浏览文件 @
ada1c20b
...
...
@@ -6,4 +6,4 @@ nv_library(gpu_info SRCS gpu_info.cc DEPS gflags)
cc_library
(
place SRCS place.cc
)
cc_test
(
place_test SRCS place_test.cc DEPS place glog gflags
)
cc_library
(
dynamic_loader SRCS dynload/dynamic_loader.cc
)
cc_library
(
dynamic_loader SRCS dynload/dynamic_loader.cc
DEPS gflags
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录