Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
67f2c9f7
P
Paddle
项目概览
机器未来
/
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看板
未验证
提交
67f2c9f7
编写于
3月 22, 2022
作者:
C
Chen Weihang
提交者:
GitHub
3月 22, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Phi] Fix context pool sync init (#40787)
* fix context pool sync init * add lock for insert
上级
10bab9f1
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
17 addition
and
32 deletion
+17
-32
paddle/phi/api/include/context_pool.h
paddle/phi/api/include/context_pool.h
+7
-4
paddle/phi/api/lib/context_pool.cc
paddle/phi/api/lib/context_pool.cc
+10
-28
未找到文件。
paddle/phi/api/include/context_pool.h
浏览文件 @
67f2c9f7
...
...
@@ -14,6 +14,8 @@ limitations under the License. */
#pragma once
#include <mutex>
#include "paddle/phi/common/place.h"
#include "paddle/phi/core/macros.h"
#include "paddle/utils/flat_hash_map.h"
...
...
@@ -58,21 +60,22 @@ class DeviceContextPool {
public:
static
DeviceContextPool
&
Instance
();
const
phi
::
DeviceContext
*
Get
(
const
Place
&
place
)
const
;
const
phi
::
DeviceContext
*
Get
(
const
Place
&
place
);
phi
::
DeviceContext
*
GetMutable
(
const
Place
&
place
);
template
<
AllocationType
T
>
const
typename
DefaultDeviceContextType
<
T
>::
TYPE
*
Get
(
const
Place
&
place
)
const
{
const
typename
DefaultDeviceContextType
<
T
>::
TYPE
*
Get
(
const
Place
&
place
)
{
return
reinterpret_cast
<
const
typename
DefaultDeviceContextType
<
T
>::
TYPE
*>
(
Get
(
place
));
}
private:
DeviceContextPool
();
DeviceContextPool
()
=
default
;
paddle
::
flat_hash_map
<
Place
,
const
phi
::
DeviceContext
*
,
Place
::
Hash
>
context_map_
;
std
::
mutex
mutex_
;
DISABLE_COPY_AND_ASSIGN
(
DeviceContextPool
);
};
...
...
paddle/phi/api/lib/context_pool.cc
浏览文件 @
67f2c9f7
...
...
@@ -25,12 +25,17 @@ DeviceContextPool& DeviceContextPool::Instance() {
return
g_device_context_pool
;
}
const
phi
::
DeviceContext
*
DeviceContextPool
::
Get
(
const
Place
&
place
)
const
{
const
phi
::
DeviceContext
*
DeviceContextPool
::
Get
(
const
Place
&
place
)
{
auto
it
=
context_map_
.
find
(
place
);
PADDLE_ENFORCE_NE
(
it
,
context_map_
.
end
(),
phi
::
errors
::
NotFound
(
"The DeviceContext of %s does not exists."
,
place
));
if
(
it
==
context_map_
.
end
())
{
// only when we need the specific DeviceContext, get and cache it
auto
*
dev_ctx
=
paddle
::
platform
::
DeviceContextPool
::
Instance
().
Get
(
place
);
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
mutex_
);
context_map_
[
place
]
=
dev_ctx
;
}
return
dev_ctx
;
}
return
it
->
second
;
}
...
...
@@ -38,28 +43,5 @@ phi::DeviceContext* DeviceContextPool::GetMutable(const Place& place) {
return
const_cast
<
phi
::
DeviceContext
*>
(
Get
(
place
));
}
DeviceContextPool
::
DeviceContextPool
()
{
// We need to make sure that the correct value exists
// whenever we get the DeviceContext from DeviceContextPool
const
auto
&
device_contexts
=
paddle
::
platform
::
DeviceContextPool
::
Instance
().
device_contexts
();
for
(
const
auto
&
pair
:
device_contexts
)
{
// only get CPU and GPU DeviceContext now, add other DeviceContext type
// later if needed
if
(
platform
::
is_cpu_place
(
pair
.
first
)
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
||
platform
::
is_gpu_place
(
pair
.
first
))
{
#else
)
{
#endif
const
phi
::
DeviceContext
*
dev_ctx
=
pair
.
second
.
get
().
get
();
VLOG
(
3
)
<<
"Init phi DeviceContextPool: insert {"
<<
pair
.
first
<<
", "
<<
dev_ctx
<<
"}"
;
context_map_
[
pair
.
first
]
=
dev_ctx
;
}
}
}
}
// namespace experimental
}
// namespace paddle
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录