Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
e576345f
P
Paddle
项目概览
Crayon鑫
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
e576345f
编写于
7月 16, 2018
作者:
Y
yuyang18
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Try to speed up buffered reader
上级
61b3a597
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
43 addition
and
25 deletion
+43
-25
paddle/fluid/operators/reader/buffered_reader.cc
paddle/fluid/operators/reader/buffered_reader.cc
+37
-23
paddle/fluid/operators/reader/buffered_reader.h
paddle/fluid/operators/reader/buffered_reader.h
+6
-2
未找到文件。
paddle/fluid/operators/reader/buffered_reader.cc
浏览文件 @
e576345f
...
@@ -18,10 +18,7 @@
...
@@ -18,10 +18,7 @@
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
namespace
reader
{
namespace
reader
{
BufferedReader
::~
BufferedReader
()
{
BufferedReader
::~
BufferedReader
()
{
reader_
->
Shutdown
();
}
reader_
->
Shutdown
();
buffer_
.
clear
();
}
BufferedReader
::
BufferedReader
(
BufferedReader
::
BufferedReader
(
const
std
::
shared_ptr
<
framework
::
ReaderBase
>
&
reader
,
const
std
::
shared_ptr
<
framework
::
ReaderBase
>
&
reader
,
const
platform
::
Place
&
place
,
size_t
buffer_size
)
const
platform
::
Place
&
place
,
size_t
buffer_size
)
...
@@ -29,43 +26,60 @@ BufferedReader::BufferedReader(
...
@@ -29,43 +26,60 @@ BufferedReader::BufferedReader(
thread_pool_
(
1
),
thread_pool_
(
1
),
place_
(
place
),
place_
(
place
),
buffer_size_
(
buffer_size
)
{
buffer_size_
(
buffer_size
)
{
cpu_buffer_
.
resize
(
buffer_size
);
gpu_buffer_
.
resize
(
buffer_size
);
AppendFutureToBatchSize
();
AppendFutureToBatchSize
();
}
}
void
BufferedReader
::
AppendFutureToBatchSize
()
{
void
BufferedReader
::
AppendFutureToBatchSize
()
{
while
(
buffer_
.
size
()
<
buffer_size_
)
{
PADDLE_ENFORCE_EQ
(
position_
.
size
(),
0U
);
AppendFuture
();
for
(
size_t
i
=
0
;
i
<
buffer_size_
;
++
i
)
{
AppendFuture
(
i
);
}
}
}
}
void
BufferedReader
::
AppendFuture
()
{
void
BufferedReader
::
AppendFuture
(
size_t
i
)
{
buffer_
.
emplace_back
(
thread_pool_
.
enqueue
([
this
]
{
position_
.
emplace
(
thread_pool_
.
enqueue
([
this
,
i
]()
->
size_t
{
TensorVec
cpu_buffer
;
TensorVec
&
cpu
=
cpu_buffer_
[
i
];
reader_
->
ReadNext
(
&
cpu_buffer
);
reader_
->
ReadNext
(
&
cpu
);
if
(
platform
::
is_gpu_place
(
place_
))
{
TensorVec
gpu_buffer
;
for
(
size_t
i
=
0
;
i
<
cpu_buffer
.
size
();
++
i
)
{
if
(
cpu
.
empty
())
{
gpu_buffer
.
emplace_back
();
return
-
1UL
;
framework
::
TensorCopySync
(
cpu_buffer
[
i
],
place_
,
&
gpu_buffer
.
back
());
}
}
cpu_buffer
=
gpu_buffer
;
if
(
platform
::
is_gpu_place
(
place_
))
{
TensorVec
&
gpu
=
gpu_buffer_
[
i
];
gpu
.
resize
(
cpu
.
size
());
for
(
size_t
i
=
0
;
i
<
cpu
.
size
();
++
i
)
{
framework
::
TensorCopySync
(
cpu
[
i
],
place_
,
&
gpu
[
i
]);
}
}
}
return
cpu_buffer
;
return
i
;
}));
}));
}
}
void
BufferedReader
::
ShutdownImpl
()
{
void
BufferedReader
::
ShutdownImpl
()
{
reader_
->
Shutdown
();
reader_
->
Shutdown
();
buffer_
.
clear
();
while
(
!
position_
.
empty
())
{
position_
.
pop
();
}
}
}
void
BufferedReader
::
StartImpl
()
{
void
BufferedReader
::
StartImpl
()
{
reader_
->
Start
();
reader_
->
Start
();
AppendFutureToBatchSize
();
AppendFutureToBatchSize
();
}
}
void
BufferedReader
::
ReadNextImpl
(
std
::
vector
<
framework
::
LoDTensor
>
*
out
)
{
void
BufferedReader
::
ReadNextImpl
(
std
::
vector
<
framework
::
LoDTensor
>
*
out
)
{
PADDLE_ENFORCE_EQ
(
buffer_
.
size
(),
buffer_size_
);
if
(
position_
.
empty
())
{
*
out
=
buffer_
.
front
().
get
();
out
->
clear
();
buffer_
.
pop_front
();
return
;
AppendFuture
();
}
size_t
i
=
position_
.
front
().
get
();
position_
.
pop
();
if
(
i
==
-
1UL
)
{
ReadNextImpl
(
out
);
return
;
}
*
out
=
platform
::
is_gpu_place
(
place_
)
?
gpu_buffer_
[
i
]
:
cpu_buffer_
[
i
];
AppendFuture
(
i
);
}
}
}
// namespace reader
}
// namespace reader
...
...
paddle/fluid/operators/reader/buffered_reader.h
浏览文件 @
e576345f
...
@@ -15,6 +15,7 @@
...
@@ -15,6 +15,7 @@
#pragma once
#pragma once
#include <list>
#include <list>
#include <queue>
#include <vector>
#include <vector>
#include "ThreadPool.h"
#include "ThreadPool.h"
#include "paddle/fluid/framework/reader.h"
#include "paddle/fluid/framework/reader.h"
...
@@ -36,7 +37,7 @@ class BufferedReader : public framework::DecoratedReader {
...
@@ -36,7 +37,7 @@ class BufferedReader : public framework::DecoratedReader {
private:
private:
void
AppendFutureToBatchSize
();
void
AppendFutureToBatchSize
();
void
AppendFuture
();
void
AppendFuture
(
size_t
i
);
protected:
protected:
void
ShutdownImpl
()
override
;
void
ShutdownImpl
()
override
;
...
@@ -47,7 +48,10 @@ class BufferedReader : public framework::DecoratedReader {
...
@@ -47,7 +48,10 @@ class BufferedReader : public framework::DecoratedReader {
ThreadPool
thread_pool_
;
ThreadPool
thread_pool_
;
platform
::
Place
place_
;
platform
::
Place
place_
;
const
size_t
buffer_size_
;
const
size_t
buffer_size_
;
std
::
list
<
VecFuture
>
buffer_
;
std
::
queue
<
std
::
future
<
size_t
>>
position_
;
std
::
vector
<
TensorVec
>
cpu_buffer_
;
std
::
vector
<
TensorVec
>
gpu_buffer_
;
};
};
}
// namespace reader
}
// namespace reader
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录