Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
a2730d1e
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
694
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
a2730d1e
编写于
4月 07, 2018
作者:
Y
Yi Wang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Rename Metadata into MemoryBlock::Desc
上级
eebb2053
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
47 addition
and
44 deletion
+47
-44
paddle/fluid/memory/detail/CMakeLists.txt
paddle/fluid/memory/detail/CMakeLists.txt
+1
-1
paddle/fluid/memory/detail/memory_block.cc
paddle/fluid/memory/detail/memory_block.cc
+16
-12
paddle/fluid/memory/detail/memory_block.h
paddle/fluid/memory/detail/memory_block.h
+24
-25
paddle/fluid/memory/detail/memory_block_desc.cc
paddle/fluid/memory/detail/memory_block_desc.cc
+6
-6
未找到文件。
paddle/fluid/memory/detail/CMakeLists.txt
浏览文件 @
a2730d1e
cc_library
(
memory_block SRCS memory_block.cc me
ta_data
.cc meta_cache.cc
)
cc_library
(
memory_block SRCS memory_block.cc me
mory_block_desc
.cc meta_cache.cc
)
if
(
${
WITH_GPU
}
)
nv_library
(
system_allocator SRCS system_allocator.cc DEPS gflags cpu_info gpu_info
)
...
...
paddle/fluid/memory/detail/memory_block.cc
浏览文件 @
a2730d1e
...
...
@@ -21,9 +21,10 @@ namespace detail {
void
MemoryBlock
::
init
(
MetadataCache
*
cache
,
Type
t
,
size_t
index
,
size_t
size
,
void
*
left_buddy
,
void
*
right_buddy
)
{
cache
->
save
(
this
,
Metadata
(
t
,
index
,
size
-
sizeof
(
Metadata
),
size
,
static_cast
<
MemoryBlock
*>
(
left_buddy
),
static_cast
<
MemoryBlock
*>
(
right_buddy
)));
cache
->
save
(
this
,
MemoryBlock
::
Desc
(
t
,
index
,
size
-
sizeof
(
MemoryBlock
::
Desc
),
size
,
static_cast
<
MemoryBlock
*>
(
left_buddy
),
static_cast
<
MemoryBlock
*>
(
right_buddy
)));
}
MemoryBlock
::
Type
MemoryBlock
::
type
(
MetadataCache
&
cache
)
const
{
...
...
@@ -63,7 +64,7 @@ void MemoryBlock::split(MetadataCache* cache, size_t size) {
PADDLE_ASSERT
(
total_size
(
*
cache
)
>=
size
);
// bail out if there is no room for another partition
if
(
total_size
(
*
cache
)
-
size
<=
sizeof
(
Me
tadata
))
{
if
(
total_size
(
*
cache
)
-
size
<=
sizeof
(
Me
moryBlock
::
Desc
))
{
return
;
}
...
...
@@ -78,13 +79,13 @@ void MemoryBlock::split(MetadataCache* cache, size_t size) {
// Write the metadata for the new block
auto
new_block_right_buddy
=
metadata
.
right_buddy
;
cache
->
save
(
static_cast
<
MemoryBlock
*>
(
right_partition
),
Metadata
(
FREE_CHUNK
,
index
(
*
cache
),
remaining_size
-
sizeof
(
Metadata
),
remaining_size
,
this
,
new_block_right_buddy
));
cache
->
save
(
static_cast
<
MemoryBlock
*>
(
right_partition
),
MemoryBlock
::
Desc
(
FREE_CHUNK
,
index
(
*
cache
),
remaining_size
-
sizeof
(
MemoryBlock
::
Desc
),
remaining_size
,
this
,
new_block_right_buddy
));
metadata
.
right_buddy
=
static_cast
<
MemoryBlock
*>
(
right_partition
);
metadata
.
size
=
size
-
sizeof
(
Me
tadata
);
metadata
.
size
=
size
-
sizeof
(
Me
moryBlock
::
Desc
);
metadata
.
total_size
=
size
;
cache
->
save
(
this
,
metadata
);
...
...
@@ -122,7 +123,8 @@ void MemoryBlock::merge(MetadataCache* cache, MemoryBlock* right_buddy) {
metadata
.
total_size
+=
right_buddy
->
total_size
(
*
cache
);
cache
->
save
(
this
,
metadata
);
cache
->
save
(
right_buddy
,
Metadata
(
INVALID_CHUNK
,
0
,
0
,
0
,
nullptr
,
nullptr
));
cache
->
save
(
right_buddy
,
MemoryBlock
::
Desc
(
INVALID_CHUNK
,
0
,
0
,
0
,
nullptr
,
nullptr
));
}
void
MemoryBlock
::
mark_as_free
(
MetadataCache
*
cache
)
{
...
...
@@ -139,12 +141,14 @@ void MemoryBlock::set_type(MetadataCache* cache, Type t) {
}
void
*
MemoryBlock
::
data
()
const
{
return
const_cast
<
Metadata
*>
(
reinterpret_cast
<
const
Metadata
*>
(
this
))
+
1
;
return
const_cast
<
MemoryBlock
::
Desc
*>
(
reinterpret_cast
<
const
MemoryBlock
::
Desc
*>
(
this
))
+
1
;
}
MemoryBlock
*
MemoryBlock
::
metadata
()
const
{
return
const_cast
<
MemoryBlock
*>
(
reinterpret_cast
<
const
MemoryBlock
*>
(
reinterpret_cast
<
const
Me
tadata
*>
(
this
)
-
1
));
reinterpret_cast
<
const
Me
moryBlock
::
Desc
*>
(
this
)
-
1
));
}
}
// namespace detail
...
...
paddle/fluid/memory/detail/memory_block.h
浏览文件 @
a2730d1e
...
...
@@ -23,9 +23,8 @@ namespace detail {
class
MetadataCache
;
// MemoryBlock represents Each allocated memory block, which contains
// Metadata and the payload.
class
MemoryBlock
{
public:
// MemoryBlock::Desc and the payload.
struct
MemoryBlock
{
enum
Type
{
FREE_CHUNK
,
// memory is free and idle
ARENA_CHUNK
,
// memory is being occupied
...
...
@@ -33,17 +32,17 @@ class MemoryBlock {
INVALID_CHUNK
// memory is invalid
};
// init saves the Me
tadata
of the memory block in a MetadataCache.
// init saves the Me
moryBlock::Desc
of the memory block in a MetadataCache.
// If it is a CPU memory block, the MetadataCache writes the
// Me
tadata
to the beginning of the block; or, if it is a GPU memory
// Me
moryBlock::Desc
to the beginning of the block; or, if it is a GPU memory
// block, the MetadataCache writes the Meatadata to a std::map in
// the CPU.
void
init
(
Me
tadata
Cache
*
cache
,
Type
t
,
size_t
index
,
size_t
size
,
void
init
(
Me
moryBlock
::
Desc
Cache
*
cache
,
Type
t
,
size_t
index
,
size_t
size
,
void
*
left_buddy
,
void
*
right_buddy
);
// All these accessors returns fields in the Me
tadata
of the memory
// All these accessors returns fields in the Me
moryBlock::Desc
of the memory
// block. They all need a MetadataCache instance as their first
// parameter because they read the Me
tadata
from the cache.
// parameter because they read the Me
moryBlock::Desc
from the cache.
Type
type
(
const
MetadataCache
&
cache
)
const
;
size_t
size
(
const
MetadataCache
&
cache
)
const
;
size_t
index
(
const
MetadataCache
&
cache
)
const
;
...
...
@@ -68,12 +67,11 @@ class MemoryBlock {
void
*
data
()
const
;
MemoryBlock
*
metadata
()
const
;
private:
// Metadata describes a MemoryBlock.
struct
Metadata
{
Metadata
(
MemoryBlock
::
Type
t
,
size_t
i
,
size_t
s
,
size_t
ts
,
MemoryBlock
*
l
,
MemoryBlock
*
r
);
Metadata
();
// MemoryBlock::Desc describes a MemoryBlock.
struct
Desc
{
Desc
(
MemoryBlock
::
Type
t
,
size_t
i
,
size_t
s
,
size_t
ts
,
MemoryBlock
*
l
,
MemoryBlock
*
r
);
Desc
();
// Updates guard_begin and guard_end by hashes of the Metadata object.
void
update_guards
();
...
...
@@ -94,9 +92,10 @@ class MemoryBlock {
};
// A cache for accessing memory block meta-data that may be expensive
// to access directly. This class exists to unify the metadata format
// between GPU and CPU allocations. It should be removed when the CPU
// can access all GPU allocations directly via UVM.
// to access directly. This class exists to unify the
// MemoryBlock::Desc format between GPU and CPU allocations. It should
// be removed when the CPU can access all GPU allocations directly via
// UVM.
class
MetadataCache
{
public:
explicit
MetadataCache
(
bool
uses_gpu
);
...
...
@@ -105,22 +104,22 @@ class MetadataCache {
MetadataCache
(
const
MetadataCache
&
)
=
delete
;
MetadataCache
&
operator
=
(
const
MetadataCache
&
)
=
delete
;
// Returns the Me
tadata
for a memory block. When MetadataCache is
// used to manage CPU memory, the Me
tadata
resides at the beginning
// Returns the Me
moryBlock::Desc
for a memory block. When MetadataCache is
// used to manage CPU memory, the Me
moryBlock::Desc
resides at the beginning
// of the memory block; when used to manage GPU memory, the
// Meatadata resides in CPU memory indexed by cache_.
Me
tadata
load
(
const
MemoryBlock
*
memory_block
)
const
;
Me
moryBlock
::
Desc
load
(
const
MemoryBlock
*
memory_block
)
const
;
// Saves the Me
tadata
of a memory block into the cache. For CPU
// memory block, writes the Me
tadata
to the beginning of the memory
// Saves the Me
moryBlock::Desc
of a memory block into the cache. For CPU
// memory block, writes the Me
moryBlock::Desc
to the beginning of the memory
// block; whereas for GPU memory, writes it to cache_.
void
save
(
MemoryBlock
*
memory_block
,
const
Me
tadata
&
meta_data
);
void
save
(
MemoryBlock
*
memory_block
,
const
Me
moryBlock
::
Desc
&
meta_data
);
// For GPU memory block, erases its Me
tadata
from cache_.
// For GPU memory block, erases its Me
moryBlock::Desc
from cache_.
void
invalidate
(
MemoryBlock
*
memory_block
);
private:
typedef
std
::
unordered_map
<
const
MemoryBlock
*
,
Me
tadata
>
MetadataMap
;
typedef
std
::
unordered_map
<
const
MemoryBlock
*
,
Me
moryBlock
::
Desc
>
MetadataMap
;
MetadataMap
cache_
;
bool
uses_gpu_
;
};
...
...
paddle/fluid/memory/detail/me
ta_data
.cc
→
paddle/fluid/memory/detail/me
mory_block_desc
.cc
浏览文件 @
a2730d1e
...
...
@@ -20,8 +20,8 @@ namespace paddle {
namespace
memory
{
namespace
detail
{
Me
tadata
::
Metadata
(
MemoryBlock
::
Type
t
,
size_t
i
,
size_t
s
,
size_t
ts
,
MemoryBlock
*
l
,
MemoryBlock
*
r
)
Me
moryBlock
::
Desc
::
Desc
(
MemoryBlock
::
Type
t
,
size_t
i
,
size_t
s
,
size_t
ts
,
MemoryBlock
*
l
,
MemoryBlock
*
r
)
:
type
(
t
),
index
(
i
),
size
(
s
),
...
...
@@ -29,7 +29,7 @@ Metadata::Metadata(MemoryBlock::Type t, size_t i, size_t s, size_t ts,
left_buddy
(
l
),
right_buddy
(
r
)
{}
Me
tadata
::
Metadata
()
Me
moryBlock
::
Desc
::
Desc
()
:
type
(
MemoryBlock
::
INVALID_CHUNK
),
index
(
0
),
size
(
0
),
...
...
@@ -45,7 +45,7 @@ inline void hash_combine(std::size_t* seed, const T& v) {
(
*
seed
)
^=
hasher
(
v
)
+
0x9e3779b9
+
((
*
seed
)
<<
6
)
+
((
*
seed
)
>>
2
);
}
inline
size_t
hash
(
const
Me
tadata
&
metadata
,
size_t
initial_seed
)
{
inline
size_t
hash
(
const
Me
moryBlock
::
Desc
&
metadata
,
size_t
initial_seed
)
{
size_t
seed
=
initial_seed
;
hash_combine
(
&
seed
,
static_cast
<
size_t
>
(
metadata
.
type
));
...
...
@@ -60,12 +60,12 @@ inline size_t hash(const Metadata& metadata, size_t initial_seed) {
}
// namespace
void
Me
tadata
::
update_guards
()
{
void
Me
moryBlock
::
Desc
::
update_guards
()
{
guard_begin
=
hash
(
this
,
1
);
guard_end
=
hash
(
this
,
2
);
}
bool
Me
tadata
::
check_guards
()
const
{
bool
Me
moryBlock
::
Desc
::
check_guards
()
const
{
return
guard_begin
==
hash
(
this
,
1
)
&&
guard_end
==
hash
(
this
,
2
);
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录