Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
a2981f5c
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
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看板
提交
a2981f5c
编写于
3月 20, 2018
作者:
F
fengjiayi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix a bug
上级
87ac675a
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
50 addition
and
29 deletion
+50
-29
paddle/fluid/operators/reader/open_files_op.cc
paddle/fluid/operators/reader/open_files_op.cc
+50
-29
未找到文件。
paddle/fluid/operators/reader/open_files_op.cc
浏览文件 @
a2981f5c
...
...
@@ -21,12 +21,10 @@ namespace reader {
class
MultipleReader
:
public
framework
::
ReaderBase
{
public:
struct
Quota
{};
MultipleReader
(
const
std
::
vector
<
std
::
string
>&
file_names
,
const
std
::
vector
<
framework
::
DDim
>&
dims
,
size_t
thread_num
)
:
file_names_
(
file_names
),
dims_
(
dims
)
,
thread_num_
(
thread_num
)
{
PADDLE_ENFORCE_GT
(
thread_num_
,
0
);
:
file_names_
(
file_names
),
dims_
(
dims
)
{
prefetchers_
.
resize
(
thread_num
);
StartNewScheduler
();
}
...
...
@@ -34,16 +32,20 @@ class MultipleReader : public framework::ReaderBase {
bool
HasNext
()
const
override
;
void
ReInit
()
override
;
~
MultipleReader
()
{
EndScheduler
();
}
private:
void
StartNewScheduler
();
void
EndScheduler
();
void
ScheduleThreadFunc
();
void
PrefetchThreadFunc
(
std
::
string
file_name
);
void
PrefetchThreadFunc
(
std
::
string
file_name
,
size_t
thread_idx
);
std
::
vector
<
std
::
string
>
file_names_
;
std
::
vector
<
framework
::
DDim
>
dims_
;
size_t
thread_num_
;
std
::
thread
scheduler_
;
std
::
vector
<
std
::
thread
>
prefetchers_
;
framework
::
Channel
<
size_t
>*
waiting_file_idx_
;
framework
::
Channel
<
Quota
>*
thread_quotas
_
;
framework
::
Channel
<
size_t
>*
available_thread_idx
_
;
framework
::
Channel
<
std
::
vector
<
framework
::
LoDTensor
>>*
buffer_
;
mutable
std
::
vector
<
framework
::
LoDTensor
>
local_buffer_
;
};
...
...
@@ -65,59 +67,76 @@ bool MultipleReader::HasNext() const {
}
void
MultipleReader
::
ReInit
()
{
buffer_
->
Close
();
thread_quotas_
->
Close
();
waiting_file_idx_
->
Close
();
EndScheduler
();
local_buffer_
.
clear
();
StartNewScheduler
();
}
void
MultipleReader
::
StartNewScheduler
()
{
size_t
thread_num
=
prefetchers_
.
size
();
waiting_file_idx_
=
framework
::
MakeChannel
<
size_t
>
(
file_names_
.
size
());
thread_quotas_
=
framework
::
MakeChannel
<
Quota
>
(
thread_num_
);
available_thread_idx_
=
framework
::
MakeChannel
<
size_t
>
(
thread_num
);
buffer_
=
framework
::
MakeChannel
<
std
::
vector
<
framework
::
LoDTensor
>>
(
thread_num
_
);
framework
::
MakeChannel
<
std
::
vector
<
framework
::
LoDTensor
>>
(
thread_num
);
for
(
size_t
i
=
0
;
i
<
file_names_
.
size
();
++
i
)
{
waiting_file_idx_
->
Send
(
&
i
);
}
waiting_file_idx_
->
Close
();
for
(
size_t
i
=
0
;
i
<
thread_num_
;
++
i
)
{
Quota
quota
;
thread_quotas_
->
Send
(
&
quota
);
for
(
size_t
i
=
0
;
i
<
thread_num
;
++
i
)
{
available_thread_idx_
->
Send
(
&
i
);
}
std
::
thread
scheduler
([
this
]
{
ScheduleThreadFunc
();
});
scheduler
.
detach
();
scheduler_
=
std
::
thread
([
this
]
{
ScheduleThreadFunc
();
});
}
void
MultipleReader
::
EndScheduler
()
{
available_thread_idx_
->
Close
();
buffer_
->
Close
();
waiting_file_idx_
->
Close
();
scheduler_
.
join
();
delete
buffer_
;
delete
available_thread_idx_
;
delete
waiting_file_idx_
;
}
void
MultipleReader
::
ScheduleThreadFunc
()
{
VLOG
(
5
)
<<
"MultipleReader schedule thread starts."
;
size_t
completed_thread_num
=
0
;
Quota
quota
;
while
(
thread_quotas_
->
Receive
(
&
quota
))
{
size_t
thread_idx
;
while
(
available_thread_idx_
->
Receive
(
&
thread_idx
))
{
std
::
thread
&
prefetcher
=
prefetchers_
[
thread_idx
];
if
(
prefetcher
.
joinable
())
{
prefetcher
.
join
();
}
size_t
file_idx
;
if
(
waiting_file_idx_
->
Receive
(
&
file_idx
))
{
// Still have files to read. Start a new prefetch thread.
std
::
string
file_name
=
file_names_
[
file_idx
];
std
::
thread
prefetcher
(
[
this
,
file_name
]
{
PrefetchThreadFunc
(
file_name
);
}
);
prefetcher
.
detach
(
);
prefetcher
=
std
::
thread
([
this
,
file_name
,
thread_idx
]
{
PrefetchThreadFunc
(
file_name
,
thread_idx
);
}
);
}
else
{
// No more file to read.
++
completed_thread_num
;
if
(
completed_thread_num
==
thread_num_
)
{
thread_quotas_
->
Close
();
buffer_
->
Close
();
if
(
completed_thread_num
==
prefetchers_
.
size
())
{
break
;
}
}
}
// If users invoke ReInit() when scheduler is running, it will close the
// 'avaiable_thread_idx_' and prefecther threads have no way to tell scheduler
// to release their resource. So a check is needed before scheduler ends.
for
(
auto
&
p
:
prefetchers_
)
{
if
(
p
.
joinable
())
{
p
.
join
();
}
}
VLOG
(
5
)
<<
"MultipleReader schedule thread terminates."
;
}
void
MultipleReader
::
PrefetchThreadFunc
(
std
::
string
file_name
)
{
void
MultipleReader
::
PrefetchThreadFunc
(
std
::
string
file_name
,
size_t
thread_idx
)
{
VLOG
(
5
)
<<
"The prefetch thread of file '"
<<
file_name
<<
"' starts."
;
std
::
unique_ptr
<
framework
::
ReaderBase
>
reader
=
CreateReaderByFileName
(
file_name
,
dims_
);
...
...
@@ -131,8 +150,10 @@ void MultipleReader::PrefetchThreadFunc(std::string file_name) {
break
;
}
}
Quota
quota
;
thread_quotas_
->
Send
(
&
quota
);
if
(
!
available_thread_idx_
->
Send
(
&
thread_idx
))
{
VLOG
(
5
)
<<
"WARNING: The available_thread_idx_ channel has been closed. "
"Fail to send thread_idx."
;
}
VLOG
(
5
)
<<
"The prefetch thread of file '"
<<
file_name
<<
"' terminates."
;
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录