Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
47609ab2
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看板
未验证
提交
47609ab2
编写于
4月 13, 2018
作者:
Y
Yi Wang
提交者:
GitHub
4月 13, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Document transform.h and fix cpplint errors (#9913)
上级
b43d87c9
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
50 addition
and
18 deletion
+50
-18
paddle/fluid/platform/details/cuda_transform_iterator_cast.h
paddle/fluid/platform/details/cuda_transform_iterator_cast.h
+24
-9
paddle/fluid/platform/transform.h
paddle/fluid/platform/transform.h
+26
-9
未找到文件。
paddle/fluid/platform/details/
device_pt
r_cast.h
→
paddle/fluid/platform/details/
cuda_transform_iterato
r_cast.h
浏览文件 @
47609ab2
...
...
@@ -18,16 +18,22 @@ limitations under the License. */
#error device_ptr_cast must be include by .cu file
#endif
#include <thrust/device_ptr.h>
#include <type_traits> // For std::remove_pointer and std::is_pointer.
#include "thrust/device_ptr.h"
namespace
paddle
{
namespace
platform
{
namespace
details
{
// PointerToThrustDevicePtr has two speicalizations, one casts a (CUDA
// device) pointer into thrust::device_ptr, the other keeps rest types
// un-casted.
template
<
typename
T
,
bool
is_ptr
>
struct
DevicePtrCast
;
struct
PointerToThrustDevicePtr
;
template
<
typename
T
>
struct
DevicePtrCast
<
T
,
true
>
{
struct
PointerToThrustDevicePtr
<
T
,
true
>
{
using
ELEM
=
typename
std
::
remove_pointer
<
T
>::
type
;
using
RTYPE
=
thrust
::
device_ptr
<
ELEM
>
;
...
...
@@ -37,17 +43,26 @@ struct DevicePtrCast<T, true> {
};
template
<
typename
T
>
struct
DevicePtrCast
<
T
,
false
>
{
struct
PointerToThrustDevicePtr
<
T
,
false
>
{
using
RTYPE
=
T
;
inline
RTYPE
operator
()(
RTYPE
it
)
const
{
return
it
;
}
};
// Cast T to thrust::device_ptr if T is a pointer.
// Otherwise, e.g., T is a iterator, return T itself.
// CastToCUDATransformIterator casts a pointer to thrust::device_ptr
// so it could be used as the iterator of thrust::transform. It
// doesn't cast other types.
//
// We need CastToCUDATransformIterator because it is often that we
// want to use device memory pointers as transform iterators, e.g., to
// transform a block of float32 to float16. In this case, we want
// CastToCUDATransformIterator to cast float16/32 pointers to
// thrust::device_ptr, otherwise they cannot work as the iterator
// required by thrust::transform. At the same time, we don't want to
// cast thrust::device_ptr to thrust::device_ptr repeatedly.
template
<
typename
T
>
auto
DevPtrCast
(
T
t
)
->
typename
DevicePtrCast
<
T
,
std
::
is_pointer
<
T
>::
value
>::
RTYPE
{
DevicePtrCast
<
T
,
std
::
is_pointer
<
T
>::
value
>
cast
;
auto
CastToCUDATransformIterator
(
T
t
)
->
typename
PointerToThrustDevicePtr
<
T
,
std
::
is_pointer
<
T
>::
value
>::
RTYPE
{
PointerToThrustDevicePtr
<
T
,
std
::
is_pointer
<
T
>::
value
>
cast
;
return
cast
(
t
);
}
...
...
paddle/fluid/platform/transform.h
浏览文件 @
47609ab2
...
...
@@ -14,29 +14,44 @@ limitations under the License. */
#pragma once
#include <algorithm>
#include <type_traits>
#include "paddle/fluid/platform/device_context.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/hostdevice.h"
#include "paddle/fluid/platform/place.h"
#include <algorithm>
#include <type_traits>
#ifdef __NVCC__
#include <thrust/execution_policy.h>
#include <thrust/transform.h>
#include "paddle/fluid/platform/details/
device_pt
r_cast.h"
#include "paddle/fluid/platform/details/
cuda_transform_iterato
r_cast.h"
#endif
namespace
paddle
{
namespace
platform
{
// Transform on host or device. It provides the same API in std library.
// Transform applys a unary or a binary functor on each element in a
// range defined by a pair of iterators.
//
// - The specialization for CPU calls std::transform.
// - The specialization for CUDA calls thrust::tranform.
//
// NOTE: We need to define InputIter and OutputIter defined as
// different types, because the InputIter points op's inputs and
// OutputIter pints to op's outputs.
//
// NOTE: We don't assume that InputIter to be const InputType* and
// OutputIter to be OutputType*, because we might use a iterator
// class, paddle::fluid::operators::RowwiseTRansformIterator.
template
<
typename
DeviceContext
>
struct
Transform
{
// The unary version.
template
<
typename
InputIter
,
typename
OutputIter
,
typename
UnaryOperation
>
void
operator
()(
const
DeviceContext
&
context
,
InputIter
first
,
InputIter
last
,
OutputIter
result
,
UnaryOperation
op
);
// The binary version.
template
<
typename
InputIter1
,
typename
InputIter2
,
typename
OutputIter
,
typename
BinaryOperation
>
void
operator
()(
const
DeviceContext
&
context
,
InputIter1
first1
,
...
...
@@ -70,8 +85,9 @@ struct Transform<platform::CUDADeviceContext> {
auto
place
=
context
.
GetPlace
();
PADDLE_ENFORCE
(
is_gpu_place
(
place
),
"It must use GPU place."
);
thrust
::
transform
(
thrust
::
cuda
::
par
.
on
(
context
.
stream
()),
details
::
DevPtrCast
(
first
),
details
::
DevPtrCast
(
last
),
details
::
DevPtrCast
(
result
),
op
);
details
::
CastToCUDATransformIterator
(
first
),
details
::
CastToCUDATransformIterator
(
last
),
details
::
CastToCUDATransformIterator
(
result
),
op
);
}
template
<
typename
InputIter1
,
typename
InputIter2
,
typename
OutputIter
,
...
...
@@ -82,9 +98,10 @@ struct Transform<platform::CUDADeviceContext> {
auto
place
=
context
.
GetPlace
();
PADDLE_ENFORCE
(
is_gpu_place
(
place
),
"It must use GPU place."
);
thrust
::
transform
(
thrust
::
cuda
::
par
.
on
(
context
.
stream
()),
details
::
DevPtrCast
(
first1
),
details
::
DevPtrCast
(
last1
),
details
::
DevPtrCast
(
first2
),
details
::
DevPtrCast
(
result
),
op
);
details
::
CastToCUDATransformIterator
(
first1
),
details
::
CastToCUDATransformIterator
(
last1
),
details
::
CastToCUDATransformIterator
(
first2
),
details
::
CastToCUDATransformIterator
(
result
),
op
);
}
};
#endif
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录