Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
323d55a7
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
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看板
未验证
提交
323d55a7
编写于
3月 23, 2022
作者:
L
liutiexing
提交者:
GitHub
3月 23, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
AddAwaitableTask (#40770)
* AddAwaitableTask for WorkQueue Co-authored-by:
N
liutiexing
<
liutiexing@google.com
>
上级
7e3752bb
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
64 addition
and
0 deletion
+64
-0
paddle/fluid/framework/new_executor/workqueue/workqueue.h
paddle/fluid/framework/new_executor/workqueue/workqueue.h
+58
-0
paddle/fluid/framework/new_executor/workqueue/workqueue_test.cc
.../fluid/framework/new_executor/workqueue/workqueue_test.cc
+6
-0
未找到文件。
paddle/fluid/framework/new_executor/workqueue/workqueue.h
浏览文件 @
323d55a7
...
...
@@ -15,9 +15,12 @@
#pragma once
#include <functional>
#include <future>
#include <memory>
#include <string>
#include <type_traits>
#include <vector>
#include "paddle/fluid/platform/enforce.h"
namespace
paddle
{
namespace
framework
{
...
...
@@ -25,6 +28,29 @@ namespace framework {
constexpr
const
char
*
kQueueEmptyEvent
=
"QueueEmpty"
;
constexpr
const
char
*
kQueueDestructEvent
=
"QueueDestruct"
;
// For std::function
// https://stackoverflow.com/questions/25421346/how-to-create-an-stdfunction-from-a-move-capturing-lambda-expression
template
<
typename
OnlyMovable
>
class
FakeCopyable
{
public:
explicit
FakeCopyable
(
OnlyMovable
&&
obj
)
:
obj_
(
std
::
move
(
obj
))
{
static_assert
(
std
::
is_copy_constructible
<
OnlyMovable
>::
value
==
false
,
"Need not to use FakeCopyable"
);
}
FakeCopyable
(
FakeCopyable
&&
other
)
:
obj_
(
std
::
move
(
other
.
obj_
))
{}
FakeCopyable
(
const
FakeCopyable
&
other
)
{
PADDLE_THROW
(
platform
::
errors
::
Unavailable
(
"Never use the copy constructor of FakeCopyable."
));
}
OnlyMovable
&
Get
()
{
return
obj_
;
}
private:
OnlyMovable
obj_
;
};
class
EventsWaiter
;
struct
WorkQueueOptions
{
...
...
@@ -78,6 +104,22 @@ class WorkQueue {
virtual
void
AddTask
(
std
::
function
<
void
()
>
fn
)
=
0
;
// Higher cost than AddTask
template
<
typename
F
,
typename
...
Args
>
std
::
future
<
typename
std
::
result_of
<
F
(
Args
...)
>::
type
>
AddAwaitableTask
(
F
&&
f
,
Args
&&
...
args
)
{
using
ReturnType
=
typename
std
::
result_of
<
F
(
Args
...)
>::
type
;
std
::
function
<
ReturnType
()
>
task
=
std
::
bind
(
std
::
forward
<
F
>
(
f
),
std
::
forward
<
Args
>
(
args
)...);
std
::
promise
<
ReturnType
>
prom
;
std
::
future
<
ReturnType
>
res
=
prom
.
get_future
();
AddTask
([
t
=
std
::
move
(
task
),
p
=
FakeCopyable
<
std
::
promise
<
ReturnType
>>
(
std
::
move
(
prom
))
]()
mutable
{
p
.
Get
().
set_value
(
t
());
});
return
res
;
}
// See WorkQueueOptions.track_task for details
// virtual void WaitQueueEmpty() = 0;
...
...
@@ -102,6 +144,22 @@ class WorkQueueGroup {
virtual
void
AddTask
(
size_t
queue_idx
,
std
::
function
<
void
()
>
fn
)
=
0
;
// Higher cost than AddTask
template
<
typename
F
,
typename
...
Args
>
std
::
future
<
typename
std
::
result_of
<
F
(
Args
...)
>::
type
>
AddAwaitableTask
(
size_t
queue_idx
,
F
&&
f
,
Args
&&
...
args
)
{
using
ReturnType
=
typename
std
::
result_of
<
F
(
Args
...)
>::
type
;
std
::
function
<
ReturnType
()
>
task
=
std
::
bind
(
std
::
forward
<
F
>
(
f
),
std
::
forward
<
Args
>
(
args
)...);
std
::
promise
<
ReturnType
>
prom
;
std
::
future
<
ReturnType
>
res
=
prom
.
get_future
();
AddTask
(
queue_idx
,
[
t
=
std
::
move
(
task
),
p
=
FakeCopyable
<
std
::
promise
<
ReturnType
>>
(
std
::
move
(
prom
))
]()
mutable
{
p
.
Get
().
set_value
(
t
());
});
return
res
;
}
// See WorkQueueOptions.track_task for details
// virtual void WaitQueueGroupEmpty() = 0;
...
...
paddle/fluid/framework/new_executor/workqueue/workqueue_test.cc
浏览文件 @
323d55a7
...
...
@@ -60,11 +60,13 @@ TEST(WorkQueue, TestSingleThreadedWorkQueue) {
}
finished
=
true
;
});
auto
handle
=
work_queue
->
AddAwaitableTask
([]()
{
return
1234
;
});
// WaitQueueEmpty
EXPECT_EQ
(
finished
.
load
(),
false
);
events_waiter
.
WaitEvent
();
EXPECT_EQ
(
finished
.
load
(),
true
);
EXPECT_EQ
(
counter
.
load
(),
kLoopNum
);
EXPECT_EQ
(
handle
.
get
(),
1234
);
}
TEST
(
WorkQueue
,
TestMultiThreadedWorkQueue
)
{
...
...
@@ -146,6 +148,9 @@ TEST(WorkQueue, TestWorkQueueGroup) {
++
counter
;
}
});
int
random_num
=
123456
;
auto
handle
=
queue_group
->
AddAwaitableTask
(
1
,
[
random_num
]()
{
return
random_num
;
});
// WaitQueueGroupEmpty
events_waiter
.
WaitEvent
();
EXPECT_EQ
(
counter
.
load
(),
kLoopNum
*
kExternalLoopNum
+
kLoopNum
);
...
...
@@ -154,4 +159,5 @@ TEST(WorkQueue, TestWorkQueueGroup) {
events_waiter
.
WaitEvent
();
queue_group
.
reset
();
EXPECT_EQ
(
events_waiter
.
WaitEvent
(),
paddle
::
framework
::
kQueueDestructEvent
);
EXPECT_EQ
(
handle
.
get
(),
random_num
);
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录