Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
a5cf565c
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
a5cf565c
编写于
10月 10, 2018
作者:
S
sneaxiy
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix auto_increment_allocator thread-safety bug
上级
bb04b54e
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
38 addition
and
20 deletion
+38
-20
paddle/fluid/memory/allocation/auto_increment_allocator.h
paddle/fluid/memory/allocation/auto_increment_allocator.h
+38
-20
未找到文件。
paddle/fluid/memory/allocation/auto_increment_allocator.h
浏览文件 @
a5cf565c
...
@@ -14,6 +14,7 @@
...
@@ -14,6 +14,7 @@
#pragma once
#pragma once
#include <atomic> // NOLINT
#include <functional>
#include <functional>
#include <memory>
#include <memory>
#include <thread> // NOLINT
#include <thread> // NOLINT
...
@@ -55,44 +56,61 @@ class AutoIncrementAllocator : public ManagedAllocator {
...
@@ -55,44 +56,61 @@ class AutoIncrementAllocator : public ManagedAllocator {
template
<
typename
Callback
>
template
<
typename
Callback
>
inline
typename
std
::
result_of
<
Callback
(
ManagedAllocator
&
)
>::
type
inline
typename
std
::
result_of
<
Callback
(
ManagedAllocator
&
)
>::
type
InvokeOrCreateUnderlyingAllocator
(
Callback
callback
)
{
InvokeOrCreateUnderlyingAllocator
(
Callback
callback
)
{
size_t
retry_count
=
underlying_allocators_
.
size
();
std
::
shared_ptr
<
std
::
vector
<
AllocatorCreator
::
result_type
>>
auto
cur
=
prev_success_allocator_
;
underlying_allocators
=
underlying_allocators_
;
size_t
retry_count
=
underlying_allocators
->
size
();
size_t
allocator_num
=
retry_count
;
auto
cur
=
prev_success_allocator_
.
load
();
while
(
retry_count
--
>
0
)
{
// until there retry count is zero
while
(
retry_count
--
>
0
)
{
// until there retry count is zero
try
{
try
{
auto
res
=
callback
(
*
underlying_allocators_
[
cur
]);
auto
res
=
callback
(
*
((
*
underlying_allocators
)[
cur
]));
{
prev_success_allocator_
.
store
(
cur
);
std
::
lock_guard
<
std
::
mutex
>
guard
(
mtx_
);
prev_success_allocator_
=
cur
;
}
return
std
::
move
(
res
);
return
std
::
move
(
res
);
}
catch
(
BadAlloc
&
)
{
}
catch
(
BadAlloc
&
)
{
++
cur
;
if
(
++
cur
>=
allocator_num
)
{
if
(
cur
>=
underlying_allocators_
.
size
())
{
cur
=
0
;
cur
=
0
;
}
}
}
catch
(...)
{
}
catch
(...)
{
// if there is another type of allocation, just rethrow it.
// if there is another type of allocation, just rethrow it.
throw
;
std
::
rethrow_exception
(
std
::
current_exception
())
;
}
}
}
}
// No suitable allocator
// No suitable allocator
ManagedAllocator
*
new_allocator
;
{
{
std
::
lock_guard
<
std
::
mutex
>
guard
(
mtx_
);
std
::
lock_guard
<
std
::
mutex
>
guard
(
mtx_
);
underlying_allocators_
.
emplace_back
(
creator_
()
);
auto
old_size
=
underlying_allocators_
->
size
(
);
prev_success_allocator_
=
underlying_allocators_
.
size
()
-
1
;
decltype
(
underlying_allocators_
)
new_allocators
(
PADDLE_ENFORCE
(
new
std
::
vector
<
AllocatorCreator
::
result_type
>
(
old_size
+
1
));
underlying_allocators_
[
prev_success_allocator_
]
->
IsAllocThreadSafe
(),
for
(
size_t
i
=
0
;
i
<
old_size
;
++
i
)
{
"the underlying allocator must be thread safe. This is a program "
(
*
new_allocators
)[
i
]
=
(
*
underlying_allocators_
)[
i
];
"bug."
);
}
return
callback
(
*
underlying_allocators_
[
prev_success_allocator_
]);
(
*
new_allocators
)[
old_size
]
=
creator_
();
new_allocator
=
(
*
new_allocators
)[
old_size
].
get
();
underlying_allocators_
=
new_allocators
;
prev_success_allocator_
.
store
(
old_size
);
}
}
PADDLE_ENFORCE
(
new_allocator
->
IsAllocThreadSafe
(),
"the underlying allocator must be thread safe. This is a program "
"bug."
);
return
callback
(
*
new_allocator
);
}
}
AllocatorCreator
creator_
;
AllocatorCreator
creator_
;
std
::
vector
<
AllocatorCreator
::
result_type
>
underlying_allocators_
;
size_t
prev_success_allocator_
{
0
};
// Use std::shared_ptr to ensure thread-safety
std
::
mutex
mtx_
;
// NOLINT
std
::
shared_ptr
<
std
::
vector
<
AllocatorCreator
::
result_type
>>
underlying_allocators_
;
// Use std::atomic rather than std::mutex, since std::atomic is usually
// lock-free
std
::
atomic
<
size_t
>
prev_success_allocator_
{
0
};
std
::
mutex
mtx_
;
};
};
}
// namespace allocation
}
// namespace allocation
}
// namespace memory
}
// namespace memory
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录