Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
d5109130
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
d5109130
编写于
7月 25, 2017
作者:
Q
qijun
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
set default cpu place for tensor alloc
上级
e2ba1337
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
28 addition
and
7 deletion
+28
-7
paddle/framework/tensor.h
paddle/framework/tensor.h
+12
-5
paddle/pybind/pybind.cc
paddle/pybind/pybind.cc
+8
-0
paddle/pybind/tensor_bind.h
paddle/pybind/tensor_bind.h
+8
-2
未找到文件。
paddle/framework/tensor.h
浏览文件 @
d5109130
...
...
@@ -19,6 +19,7 @@ limitations under the License. */
#include <memory>
#include <typeindex>
#include "paddle/framework/ddim.h"
#include "paddle/memory/memcpy.h"
#include "paddle/memory/memory.h"
#include "paddle/platform/enforce.h"
#include "paddle/platform/place.h"
...
...
@@ -104,15 +105,21 @@ class Tensor {
template
<
typename
T
>
void
CopyFrom
(
const
Tensor
&
src
,
platform
::
Place
dst_place
)
{
PADDLE_ENFORCE
(
platform
::
is_cpu_place
(
src
.
holder_
->
place
())
&&
platform
::
is_cpu_place
(
dst_place
),
"Tensor::CopyFrom only support CPU now."
);
src
.
EnforceSufficientMemory
<
T
>
();
PADDLE_ENFORCE
(
platform
::
is_cpu_place
(
dst_place
),
"Tensor::CopyFrom only support dst CPU now."
);
size_t
size
=
product
(
src
.
dims_
)
*
sizeof
(
T
);
Resize
(
src
.
dims
());
const
void
*
src_ptr
=
static_cast
<
const
void
*>
(
src
.
data
<
T
>
());
void
*
dst_ptr
=
static_cast
<
void
*>
(
mutable_data
<
T
>
(
dst_place
));
memcpy
(
dst_ptr
,
src_ptr
,
size
);
if
(
paddle
::
platform
::
is_cpu_place
(
holder_
->
place
()))
{
std
::
memcpy
(
dst_ptr
,
src_ptr
,
size
);
}
else
if
(
paddle
::
platform
::
is_gpu_place
(
holder_
->
place
()))
{
#ifdef PADDLE_ONLY_CPU
PADDLE_THROW
(
"'GPUPlace' is not supported in CPU only device."
);
#else
GpuMemcpySync
(
dst_ptr
,
src_ptr
,
size
,
cudaMemcpyDeviceToHost
);
#endif
}
}
template
<
typename
T
>
...
...
paddle/pybind/pybind.cc
浏览文件 @
d5109130
...
...
@@ -66,10 +66,18 @@ PYBIND11_PLUGIN(core) {
[](
pd
::
Tensor
&
self
,
paddle
::
platform
::
Place
&
place
)
{
self
.
mutable_data
<
float
>
(
place
);
})
.
def
(
"alloc_float"
,
[](
pd
::
Tensor
&
self
)
{
self
.
mutable_data
<
float
>
(
paddle
::
platform
::
CPUPlace
());
})
.
def
(
"alloc_int"
,
[](
pd
::
Tensor
&
self
,
paddle
::
platform
::
Place
&
place
)
{
self
.
mutable_data
<
int
>
(
place
);
})
.
def
(
"alloc_int"
,
[](
pd
::
Tensor
&
self
)
{
self
.
mutable_data
<
int
>
(
paddle
::
platform
::
CPUPlace
());
})
.
def
(
"set"
,
paddle
::
pybind
::
PyTensorSetFromArray
<
float
>
)
.
def
(
"set"
,
paddle
::
pybind
::
PyTensorSetFromArray
<
int
>
)
.
def
(
"shape"
,
...
...
paddle/pybind/tensor_bind.h
浏览文件 @
d5109130
...
...
@@ -57,11 +57,17 @@ struct CastToPyBufferImpl<true, I, ARGS...> {
strides
[
i
-
1
]
=
sizeof
(
CUR_TYPE
)
*
prod
;
prod
*=
dims_outside
[
i
-
1
];
}
Tensor
dst_tensor
;
if
(
paddle
::
platform
::
is_gpu_place
(
tensor
.
holder_
->
place
()))
{
dst_tensor
.
CopyFrom
(
tensor
,
platform
::
CPUPlace
());
}
else
if
(
paddle
::
platform
::
is_gpu_place
(
tensor
.
holder_
->
place
()))
{
dst_tensor
=
tensor
;
}
return
py
::
buffer_info
(
tensor
.
mutable_data
<
CUR_TYPE
>
(
tensor
.
holder_
->
place
()),
dst_tensor
.
mutable_data
<
CUR_TYPE
>
(
dst_
tensor
.
holder_
->
place
()),
sizeof
(
CUR_TYPE
),
py
::
format_descriptor
<
CUR_TYPE
>::
format
(),
(
size_t
)
framework
::
arity
(
tensor
.
dims
()),
(
size_t
)
framework
::
arity
(
dst_
tensor
.
dims
()),
dims_outside
,
strides
);
}
else
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录