Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
ab87a882
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
695
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看板
提交
ab87a882
编写于
10月 22, 2018
作者:
Y
Yu Yang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Polish retry allocator
上级
2002e71d
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
40 addition
and
36 deletion
+40
-36
paddle/fluid/memory/allocation/retry_allocator.cc
paddle/fluid/memory/allocation/retry_allocator.cc
+31
-31
paddle/fluid/memory/allocation/retry_allocator.h
paddle/fluid/memory/allocation/retry_allocator.h
+9
-5
未找到文件。
paddle/fluid/memory/allocation/retry_allocator.cc
浏览文件 @
ab87a882
...
...
@@ -20,67 +20,67 @@ namespace allocation {
RetryAllocation
::~
RetryAllocation
()
{
auto
allocator
=
retry_allocator_
.
lock
();
{
// release allocation first
// Allocator is destroyed before allocation. Should not happened usually.
if
(
UNLIKELY
(
allocator
==
nullptr
))
return
;
allocator
->
underlying_allocator_
->
Free
(
underlying_allocation_
.
release
());
}
{
// notify all waited allocators
std
::
lock_guard
<
std
::
mutex
>
lock
(
allocator
->
mutex_
);
allocator
->
cv_
.
notify_all
();
}
allocator
->
FreeUnderlyingAllocation
(
std
::
move
(
underlying_allocation_
));
}
bool
RetryAllocator
::
IsAllocThreadSafe
()
const
{
return
true
;
}
std
::
shared_ptr
<
Allocation
>
RetryAllocator
::
AllocateShared
(
size_t
size
,
Allocator
::
Attr
attr
)
{
return
std
::
shared_ptr
<
Allocation
>
(
Allocate
(
size
,
attr
));
return
std
::
shared_ptr
<
Allocation
>
(
Allocate
Impl
(
size
,
attr
));
}
std
::
unique_ptr
<
Allocation
>
RetryAllocator
::
Allocate
(
size_t
size
,
Allocator
::
Attr
attr
)
{
return
std
::
unique_ptr
<
Allocation
>
(
AllocateImpl
(
size
,
attr
));
}
Allocation
*
RetryAllocator
::
AllocateImpl
(
size_t
size
,
Allocator
::
Attr
attr
)
{
auto
alloc_func
=
[
&
,
this
]()
{
return
new
RetryAllocation
(
underlying_allocator_
->
Allocate
(
size
,
attr
),
this
->
shared_from_this
());
};
// In fact, we can unify the code of allocation success and failure
// But it would add lock even when allocation success at the first time
std
::
unique_ptr
<
Allocation
>
ret
;
try
{
ret
.
reset
(
alloc_func
()
);
}
catch
(
BadAlloc
&
)
{
ret
urn
alloc_func
(
);
}
catch
(
BadAlloc
&
bad_alloc
)
{
{
// We can just write allocation retry inside the predicate function of
// wait_until
// But it needs to acquire the lock when executing predicate function
// For better performance, we use loop here
std
::
exception_ptr
ex
;
auto
end_time
=
std
::
chrono
::
high_resolution_clock
::
now
()
+
retry_time_
;
std
::
cv_status
status
;
do
{
{
auto
wait_until
=
[
&
,
this
]
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex_
);
status
=
cv_
.
wait_until
(
lock
,
end_time
);
}
return
cv_
.
wait_until
(
lock
,
end_time
);
};
while
(
wait_until
()
!=
std
::
cv_status
::
timeout
)
{
try
{
ret
.
reset
(
alloc_func
()
);
}
catch
(
BadAlloc
&
)
{
ex
=
std
::
current_exception
()
;
ret
urn
alloc_func
(
);
}
catch
(
BadAlloc
&
ex
)
{
bad_alloc
=
ex
;
}
catch
(...)
{
std
::
rethrow_exception
(
std
::
current_exception
());
throw
;
}
}
}
while
(
ret
==
nullptr
&&
status
!=
std
::
cv_status
::
timeout
);
if
(
ret
==
nullptr
)
std
::
rethrow_exception
(
ex
);
throw
;
// rethrow the original exception or throw the internal bad_alloc
}
}
catch
(...)
{
std
::
rethrow_exception
(
std
::
current_exception
());
throw
;
}
}
void
RetryAllocator
::
FreeUnderlyingAllocation
(
std
::
unique_ptr
<
Allocation
>&&
allocation
)
{
underlying_allocator_
->
Free
(
allocation
.
get
());
{
// notify all waited allocators, they can try to allocate memory after free.
std
::
lock_guard
<
std
::
mutex
>
lock
(
mutex_
);
cv_
.
notify_all
();
}
return
ret
;
}
}
// namespace allocation
...
...
paddle/fluid/memory/allocation/retry_allocator.h
浏览文件 @
ab87a882
...
...
@@ -35,7 +35,7 @@ class RetryAllocation : public Allocation {
underlying_allocation_
(
std
::
move
(
underlying_allocation
)),
retry_allocator_
(
retry_allocator
)
{}
~
RetryAllocation
();
~
RetryAllocation
()
final
;
private:
std
::
unique_ptr
<
Allocation
>
underlying_allocation_
;
...
...
@@ -61,13 +61,17 @@ class RetryAllocator : public ManagedAllocator,
bool
IsAllocThreadSafe
()
const
override
;
std
::
unique_ptr
<
Allocation
>
Allocate
(
size_t
size
,
Allocator
::
Attr
attr
=
kDefault
)
override
;
std
::
unique_ptr
<
Allocation
>
Allocate
(
size_t
size
,
Allocator
::
Attr
attr
)
override
;
std
::
shared_ptr
<
Allocation
>
AllocateShared
(
size_t
size
,
Allocator
::
Attr
attr
=
kDefault
)
override
;
std
::
shared_ptr
<
Allocation
>
AllocateShared
(
size_t
size
,
Allocator
::
Attr
attr
)
override
;
void
FreeUnderlyingAllocation
(
std
::
unique_ptr
<
Allocation
>&&
allocation
);
private:
Allocation
*
AllocateImpl
(
size_t
size
,
Allocator
::
Attr
attr
);
void
EnforceCheck
()
{
PADDLE_ENFORCE_NOT_NULL
(
underlying_allocator_
.
get
(),
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录