Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
booniebears
Paddle
提交
7411df34
P
Paddle
项目概览
booniebears
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
7411df34
编写于
1月 03, 2018
作者:
Y
Yang Yang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add multi thread
上级
8ee17e96
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
31 addition
and
18 deletion
+31
-18
paddle/operators/parallel_do_op.cc
paddle/operators/parallel_do_op.cc
+29
-16
python/paddle/v2/fluid/tests/test_parallel_op.py
python/paddle/v2/fluid/tests/test_parallel_op.py
+2
-2
未找到文件。
paddle/operators/parallel_do_op.cc
浏览文件 @
7411df34
...
...
@@ -12,6 +12,7 @@
See the License for the specific language governing permissions and
limitations under the License. */
#include <thread>
#include <vector>
#include "paddle/framework/executor.h"
...
...
@@ -44,7 +45,7 @@ void SplitTensorAndMoveTensorToScopes(
auto
lod_tensors
=
tensor
.
SplitLoDTensor
(
places
);
for
(
auto
&
lod
:
lod_tensors
)
{
LOG
(
INFO
)
<<
lod
.
dims
();
VLOG
(
3
)
<<
lod
.
dims
();
}
for
(
size_t
i
=
0
;
i
<
sub_scopes
.
size
();
++
i
)
{
...
...
@@ -84,6 +85,7 @@ class ParallelDoOp : public framework::OperatorBase {
SplitTensorAndMoveTensorToScopes
(
scope
,
sub_scopes
,
places
,
Inputs
(
kInputs
));
std
::
vector
<
std
::
thread
>
workers
;
for
(
size_t
place_idx
=
0
;
place_idx
<
places
.
size
();
++
place_idx
)
{
VLOG
(
3
)
<<
"Run "
<<
place_idx
;
...
...
@@ -96,9 +98,14 @@ class ParallelDoOp : public framework::OperatorBase {
}
// execute
auto
executor
=
framework
::
Executor
(
place
);
executor
.
Run
(
*
program
,
cur_scope
,
block
->
ID
(),
false
/*create_local_scope*/
);
workers
.
push_back
(
std
::
thread
([
program
,
cur_scope
,
place
,
block
]
{
auto
executor
=
framework
::
Executor
(
place
);
executor
.
Run
(
*
program
,
cur_scope
,
block
->
ID
(),
false
/*create_local_scope*/
);
}));
}
for
(
auto
&
worker
:
workers
)
{
worker
.
join
();
}
// merge output
...
...
@@ -162,14 +169,15 @@ class ParallelDoGradOp : public OperatorBase {
Inputs
(
framework
::
GradVarName
(
kOutputs
)));
for
(
auto
&
s
:
Inputs
(
framework
::
GradVarName
(
kOutputs
)))
{
LOG
(
INFO
)
<<
s
;
LOG
(
INFO
)
<<
scope
.
FindVar
(
s
)
->
Get
<
LoDTensor
>
();
VLOG
(
3
)
<<
s
;
VLOG
(
3
)
<<
scope
.
FindVar
(
s
)
->
Get
<
LoDTensor
>
();
for
(
auto
*
sub_scope
:
sub_scopes
)
{
LOG
(
INFO
)
<<
sub_scope
->
FindVar
(
s
)
->
Get
<
LoDTensor
>
();
VLOG
(
3
)
<<
sub_scope
->
FindVar
(
s
)
->
Get
<
LoDTensor
>
();
}
}
// exe run
std
::
vector
<
std
::
thread
>
workers
;
for
(
size_t
place_idx
=
0
;
place_idx
<
places
.
size
();
++
place_idx
)
{
VLOG
(
3
)
<<
"Run "
<<
place_idx
;
...
...
@@ -177,25 +185,30 @@ class ParallelDoGradOp : public OperatorBase {
auto
*
cur_scope
=
sub_scopes
[
place_idx
];
// execute
auto
executor
=
framework
::
Executor
(
place
);
executor
.
Run
(
*
program
,
cur_scope
,
block
->
ID
(),
false
/*create_local_scope*/
);
workers
.
push_back
(
std
::
thread
([
program
,
cur_scope
,
place
,
block
]
{
auto
executor
=
framework
::
Executor
(
place
);
executor
.
Run
(
*
program
,
cur_scope
,
block
->
ID
(),
false
/*create_local_scope*/
);
}));
}
for
(
auto
&
worker
:
workers
)
{
worker
.
join
();
}
// merge grad
for
(
auto
&
s
:
Outputs
(
framework
::
GradVarName
(
kParameters
)))
{
LOG
(
INFO
)
<<
s
;
VLOG
(
3
)
<<
s
;
auto
&
t
=
sub_scopes
[
0
]
->
FindVar
(
s
)
->
Get
<
LoDTensor
>
();
LOG
(
INFO
)
<<
t
;
VLOG
(
3
)
<<
t
;
std
::
string
s_buf
=
s
+
"@BUF"
;
auto
*
t_buf
=
sub_scopes
[
0
]
->
Var
(
s_buf
)
->
GetMutable
<
LoDTensor
>
();
for
(
size_t
place_idx
=
1
;
place_idx
<
places
.
size
();
++
place_idx
)
{
auto
&
tt
=
sub_scopes
[
place_idx
]
->
FindVar
(
s
)
->
Get
<
LoDTensor
>
();
LOG
(
INFO
)
<<
place_idx
;
LOG
(
INFO
)
<<
tt
;
VLOG
(
3
)
<<
place_idx
;
VLOG
(
3
)
<<
tt
;
framework
::
CopyFrom
(
tt
,
places
[
0
],
t_buf
);
auto
sum_op
=
framework
::
OpRegistry
::
CreateOp
(
...
...
@@ -204,7 +217,7 @@ class ParallelDoGradOp : public OperatorBase {
sum_op
->
Run
(
*
sub_scopes
[
0
],
place
);
}
LOG
(
INFO
)
<<
t
;
VLOG
(
3
)
<<
t
;
framework
::
CopyFrom
(
t
,
place
,
scope
.
FindVar
(
s
)
->
GetMutable
<
LoDTensor
>
());
}
}
...
...
@@ -219,7 +232,7 @@ class ParallelDoGradOpDescMaker : public framework::SingleGradOpDescMaker {
auto
*
grad
=
new
framework
::
OpDesc
();
grad
->
SetType
(
"parallel_do_grad"
);
for
(
auto
&
input_param
:
this
->
InputNames
())
{
LOG
(
INFO
)
<<
input_param
;
VLOG
(
3
)
<<
input_param
;
grad
->
SetInput
(
input_param
,
this
->
Input
(
input_param
));
grad
->
SetOutput
(
framework
::
GradVarName
(
input_param
),
this
->
InputGrad
(
input_param
,
false
));
...
...
python/paddle/v2/fluid/tests/test_parallel_op.py
浏览文件 @
7411df34
...
...
@@ -12,7 +12,7 @@ import paddle.v2.fluid.core as core
class
ParallelOpTest
(
unittest
.
TestCase
):
def
setUp
(
self
):
x
=
layers
.
data
(
shape
=
[
-
1
,
3
,
4
],
shape
=
[
-
1
,
3
0
,
40
],
dtype
=
'float32'
,
name
=
'x'
,
append_batch_size
=
False
,
...
...
@@ -35,7 +35,7 @@ class ParallelOpTest(unittest.TestCase):
exe
.
run
(
fluid
.
default_main_program
(),
feed
=
{
x
.
name
:
np
.
random
.
uniform
(
0.1
,
0.6
,
(
2
,
3
,
4
)).
astype
(
"float32"
)
(
2
0
,
30
,
40
)).
astype
(
"float32"
)
})
def
test_forward
(
self
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录