transform.h 2.5 KB
Newer Older
Y
Yu Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License. */

#pragma once

Y
Yu Yang 已提交
17
#include "paddle/platform/device_context.h"
Y
Yu Yang 已提交
18 19 20 21 22
#include "paddle/platform/enforce.h"
#include "paddle/platform/hostdevice.h"
#include "paddle/platform/place.h"

#include <algorithm>
Y
Yu Yang 已提交
23
#include <type_traits>
Y
Yu Yang 已提交
24
#ifdef __NVCC__
Y
Yu Yang 已提交
25
#include <thrust/execution_policy.h>
Y
Yu Yang 已提交
26
#include <thrust/transform.h>
27
#include "paddle/platform/details/device_ptr_cast.h"
Y
Yu Yang 已提交
28 29 30 31 32
#endif

namespace paddle {
namespace platform {
// Transform on host or device. It provides the same API in std library.
Y
Yu Yang 已提交
33 34 35 36
template <typename InputIter, typename OutputIter, typename UnaryOperation>
void Transform(const DeviceContext& context, InputIter first, InputIter last,
               OutputIter result, UnaryOperation op) {
  auto place = context.GetPlace();
Y
Yu Yang 已提交
37 38 39 40
  if (is_cpu_place(place)) {
    std::transform(first, last, result, op);
  } else {
#ifdef __NVCC__
Y
Yu Yang 已提交
41
    auto& ctx = reinterpret_cast<const CUDADeviceContext&>(context);
42
    using namespace details;
Y
Yu Yang 已提交
43 44
    thrust::transform(thrust::cuda::par.on(ctx.stream()), DevPtrCast(first),
                      DevPtrCast(last), DevPtrCast(result), op);
Y
Yu Yang 已提交
45 46 47 48 49 50
#else
    PADDLE_THROW("Do not invoke `Transform<GPUPlace>` in .cc file");
#endif
  }
}

Y
Yu Yang 已提交
51 52 53 54 55 56
template <typename InputIter1, typename InputIter2, typename OutputIter,
          typename BinaryOperation>
void Transform(const DeviceContext& context, InputIter1 first1,
               InputIter1 last1, InputIter2 first2, OutputIter result,
               BinaryOperation op) {
  auto place = context.GetPlace();
Y
Yu Yang 已提交
57 58 59 60
  if (is_cpu_place(place)) {
    std::transform(first1, last1, first2, result, op);
  } else {
#ifdef __NVCC__
Y
Yu Yang 已提交
61
    auto& ctx = reinterpret_cast<const CUDADeviceContext&>(context);
62
    using namespace details;
Y
Yu Yang 已提交
63 64 65
    thrust::transform(thrust::cuda::par.on(ctx.stream()), DevPtrCast(first1),
                      DevPtrCast(last1), DevPtrCast(first2), DevPtrCast(result),
                      op);
Y
Yu Yang 已提交
66 67 68 69 70 71 72 73
#else
    PADDLE_THROW("Do not invoke `Transform<GPUPlace>` in .cc file");
#endif
  }
};

}  // namespace platform
}  // namespace paddle