Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
47ebe435
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
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看板
未验证
提交
47ebe435
编写于
2月 01, 2018
作者:
D
dzhwinter
提交者:
GitHub
2月 01, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix/vector (#8045)
* "clean code" * "clean code"
上级
9b83462a
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
29 addition
and
48 deletion
+29
-48
paddle/framework/mixed_vector.h
paddle/framework/mixed_vector.h
+29
-48
未找到文件。
paddle/framework/mixed_vector.h
浏览文件 @
47ebe435
...
...
@@ -34,18 +34,6 @@ namespace framework {
template
<
typename
T
>
class
Vector
:
public
std
::
vector
<
T
>
{
public:
/* NOTE(dzhwinter):
* Data always store and modified on Host.
* If the data is modified when use cuda_data interface,
* You need to call the CopyFromCUDA explicitly to synchronize data.
*
*/
enum
class
kDataPosition
{
kDataOnHost
=
0
,
kDataOnDevice
=
1
,
};
public:
using
std
::
vector
<
T
>::
vector
;
...
...
@@ -55,11 +43,12 @@ class Vector : public std::vector<T> {
virtual
~
Vector
()
{
#ifdef PADDLE_WITH_CUDA
if
(
cuda_ptr_
!=
nullptr
)
{
memory
::
Free
<
platform
::
CUDAPlace
>
(
place_
,
static_cast
<
void
*>
(
cuda_ptr_
)
);
memory
::
Free
<
platform
::
CUDAPlace
>
(
place_
,
cuda_ptr_
);
}
#endif
}
/* Get device vector */
T
*
cuda_data
()
{
CopyToCUDA
();
PADDLE_ENFORCE_NOT_NULL
(
...
...
@@ -67,81 +56,73 @@ class Vector : public std::vector<T> {
return
static_cast
<
T
*>
(
cuda_ptr_
);
}
/* Get host vector */
T
*
data
()
{
return
std
::
vector
<
T
>::
data
();
}
const
T
*
data
()
const
{
return
std
::
vector
<
T
>::
data
();
}
/* Synchronize host vector to device vector */
void
CopyToCUDA
();
/* Synchronize device vector to host vector */
void
CopyFromCUDA
();
/* Switch device vector location */
void
CopyToPeer
(
platform
::
Place
);
private:
void
*
cuda_ptr_
=
nullptr
;
size_t
cuda_size_
=
0
;
/*The DataPosition is unused now,
if we want support random access from cpu and cuda,
we need to overload all the vector method */
kDataPosition
position_
=
kDataPosition
::
kDataOnHost
;
size_t
cuda_size_
=
0
;
// device vector numel
platform
::
CUDAPlace
place_
;
};
template
<
typename
T
>
void
Vector
<
T
>::
CopyToCUDA
()
{
#ifdef PADDLE_WITH_CUDA
if
(
cuda_ptr_
==
nullptr
)
{
if
(
cuda_size_
<
this
->
size
())
{
if
(
cuda_ptr_
!=
nullptr
)
{
memory
::
Free
<
platform
::
CUDAPlace
>
(
place_
,
cuda_ptr_
);
}
cuda_ptr_
=
memory
::
Alloc
<
platform
::
CUDAPlace
>
(
place_
,
this
->
size
()
*
sizeof
(
T
));
}
cuda_size_
=
this
->
size
();
platform
::
DeviceContextPool
&
pool
=
platform
::
DeviceContextPool
::
Instance
();
auto
*
cuda_ctx
=
pool
.
GetByPlace
(
place_
);
memory
::
Copy
(
place_
,
static_cast
<
void
*>
(
cuda_ptr_
),
platform
::
CPUPlace
(),
auto
*
ctx
=
pool
.
GetByPlace
(
place_
);
memory
::
Copy
(
place_
,
cuda_ptr_
,
platform
::
CPUPlace
(),
static_cast
<
const
void
*>
(
this
->
data
()),
this
->
size
()
*
sizeof
(
T
),
cuda_ctx
->
stream
());
cuda_ctx
->
Wait
();
cuda_size_
=
this
->
size
();
this
->
size
()
*
sizeof
(
T
),
ctx
->
stream
());
ctx
->
Wait
();
#endif
}
template
<
typename
T
>
void
Vector
<
T
>::
CopyFromCUDA
()
{
#ifdef PADDLE_WITH_CUDA
platform
::
DeviceContextPool
&
pool
=
platform
::
DeviceContextPool
::
Instance
();
auto
*
cuda_ctx
=
pool
.
GetByPlace
(
place_
);
if
(
cuda_ptr_
==
nullptr
)
{
LOG
(
WARNING
)
<<
"No uncommited cuda data."
;
LOG
(
WARNING
)
<<
"No uncommit
t
ed cuda data."
;
return
;
}
this
->
resize
(
cuda_size_
);
platform
::
DeviceContextPool
&
pool
=
platform
::
DeviceContextPool
::
Instance
();
auto
*
ctx
=
pool
.
GetByPlace
(
place_
);
memory
::
Copy
(
platform
::
CPUPlace
(),
static_cast
<
void
*>
(
this
->
data
()),
place_
,
static_cast
<
const
void
*>
(
cuda_ptr_
),
this
->
size
()
*
sizeof
(
T
),
cuda_ctx
->
stream
());
cuda_ctx
->
Wait
();
ctx
->
stream
());
ctx
->
Wait
();
#endif
}
template
<
typename
T
>
void
Vector
<
T
>::
CopyToPeer
(
platform
::
Place
peer_place
)
{
if
(
platform
::
is_cpu_place
(
peer_place
))
{
return
;
}
#ifdef PADDLE_WITH_CUDA
auto
*
c
uda_c
tx
=
platform
::
DeviceContextPool
::
Instance
().
GetByPlace
(
place_
);
void
*
peer_cuda_ptr
_
=
memory
::
Alloc
<
platform
::
CUDAPlace
>
(
auto
*
ctx
=
platform
::
DeviceContextPool
::
Instance
().
GetByPlace
(
place_
);
void
*
peer_cuda_ptr
=
memory
::
Alloc
<
platform
::
CUDAPlace
>
(
boost
::
get
<
platform
::
CUDAPlace
>
(
peer_place
),
this
->
size
()
*
sizeof
(
T
));
memory
::
Copy
(
boost
::
get
<
platform
::
CUDAPlace
>
(
peer_place
),
static_cast
<
void
*>
(
peer_cuda_ptr_
),
place_
,
static_cast
<
const
void
*>
(
cuda_ptr_
),
this
->
size
()
*
sizeof
(
T
),
cuda_ctx
->
stream
());
cuda_ctx
->
Wait
();
memory
::
Free
<
platform
::
CUDAPlace
>
(
place_
,
static_cast
<
void
*>
(
cuda_ptr_
));
memory
::
Copy
(
boost
::
get
<
platform
::
CUDAPlace
>
(
peer_place
),
peer_cuda_ptr
,
place_
,
cuda_ptr_
,
this
->
size
()
*
sizeof
(
T
),
ctx
->
stream
());
ctx
->
Wait
();
memory
::
Free
<
platform
::
CUDAPlace
>
(
place_
,
cuda_ptr_
);
place_
=
boost
::
get
<
platform
::
CUDAPlace
>
(
peer_place
);
cuda_ptr_
=
peer_cuda_ptr
_
;
cuda_ptr_
=
peer_cuda_ptr
;
#endif
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录