Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
e9583166
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
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看板
未验证
提交
e9583166
编写于
7月 29, 2021
作者:
S
seemingwang
提交者:
GitHub
7月 29, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix block_queue problem (#34461)
上级
92d8fed8
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
50 addition
and
20 deletion
+50
-20
paddle/fluid/distributed/service/communicator.h
paddle/fluid/distributed/service/communicator.h
+50
-20
未找到文件。
paddle/fluid/distributed/service/communicator.h
浏览文件 @
e9583166
...
...
@@ -61,38 +61,65 @@ using Variable = framework::Variable;
template
<
typename
T
>
class
BlockingQueue
{
public:
explicit
BlockingQueue
(
size_t
capacity
)
:
capacity_
(
capacity
)
{
PADDLE_ENFORCE_GT
(
capacity_
,
0
,
platform
::
errors
::
InvalidArgument
(
"The capacity must be greater than 0."
));
}
explicit
BlockingQueue
(
size_t
capacity
)
:
capacity_
(
capacity
)
{}
bool
Push
(
const
T
&
elem
)
{
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex_
);
cv_
.
wait
(
lock
,
[
&
]
{
return
queue_
.
size
()
<
capacity_
;
});
queue_
.
push_back
(
elem
);
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex_
);
WaitForWrite
(
lock
);
queue_
.
push_back
(
elem
);
Notify
();
return
true
;
}
bool
WaitForWrite
(
std
::
unique_lock
<
std
::
mutex
>
&
lock
)
{
// NOLINT
while
(
FullUnlocked
())
{
if
(
empty_waiters_
!=
0
)
{
empty_cond_
.
notify_one
();
}
full_waiters_
++
;
full_cond_
.
wait
(
lock
);
full_waiters_
--
;
}
cv_
.
notify_one
();
return
true
;
}
bool
Push
(
T
&&
elem
)
{
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex_
);
cv_
.
wait
(
lock
,
[
&
]
{
return
queue_
.
size
()
<
capacity_
;
});
queue_
.
emplace_back
(
std
::
move
(
elem
));
bool
WaitForRead
(
std
::
unique_lock
<
std
::
mutex
>
&
lock
)
{
// NOLINT
while
(
EmptyUnlocked
())
{
if
(
full_waiters_
!=
0
)
{
full_cond_
.
notify_one
();
}
empty_waiters_
++
;
empty_cond_
.
wait
(
lock
);
empty_waiters_
--
;
}
cv_
.
notify_one
();
return
true
;
}
bool
EmptyUnlocked
()
{
return
queue_
.
empty
();
}
bool
FullUnlocked
()
{
return
queue_
.
size
()
>=
capacity_
;
}
void
Notify
()
{
if
(
empty_waiters_
!=
0
&&
(
!
EmptyUnlocked
()))
{
empty_cond_
.
notify_one
();
}
if
(
full_waiters_
!=
0
&&
(
!
FullUnlocked
()))
{
full_cond_
.
notify_one
();
}
}
bool
Push
(
T
&&
elem
)
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex_
);
WaitForWrite
(
lock
);
queue_
.
emplace_back
(
std
::
move
(
elem
));
Notify
();
return
true
;
}
T
Pop
()
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
mutex_
);
cv_
.
wait
(
lock
,
[
=
]
{
return
!
queue_
.
empty
();
}
);
WaitForRead
(
lock
);
T
rc
(
std
::
move
(
queue_
.
front
()));
queue_
.
pop_front
();
cv_
.
notify_one
();
Notify
();
return
rc
;
}
...
...
@@ -107,11 +134,14 @@ class BlockingQueue {
}
private:
int
empty_waiters_
=
0
;
int
full_waiters_
=
0
;
std
::
condition_variable
empty_cond_
;
std
::
condition_variable
full_cond_
;
const
size_t
capacity_
;
std
::
deque
<
T
>
queue_
;
mutable
std
::
mutex
mutex_
;
std
::
condition_variable
cv_
;
};
template
<
typename
T
,
int
MajorType
=
Eigen
::
RowMajor
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录