strided_memcpy.h 3.2 KB
Newer Older
Y
Yu Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* 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
#include "paddle/framework/ddim.h"
#include "paddle/memory/memcpy.h"
#include "paddle/platform/device_context.h"

namespace paddle {
namespace operators {
namespace detail {

template <typename T, int Rank>
25
struct StridedMemcpyFunctor;
Y
Yu Yang 已提交
26 27

template <typename T>
28
struct StridedMemcpyFunctor<T, 1> {
Y
Yu Yang 已提交
29 30 31 32 33 34 35 36
  void operator()(const platform::DeviceContext& dev_ctx, const T* src,
                  framework::Dim<1> src_stride, framework::Dim<1> dst_dim,
                  framework::Dim<1> dst_stride, T* dst) const {
    auto place = dev_ctx.GetPlace();
    if (platform::is_cpu_place(place)) {
      auto& cpu_place = boost::get<platform::CPUPlace>(place);
      memory::Copy(cpu_place, dst, cpu_place, src, sizeof(T) * dst_dim.head);
    } else {
37
#ifdef PADDLE_WITH_CUDA
D
dzhwinter 已提交
38
      auto& gpu_place = boost::get<platform::CUDAPlace>(place);
Y
Yu Yang 已提交
39 40 41 42 43 44 45 46 47 48 49 50
      auto& cuda_ctx =
          reinterpret_cast<const platform::CUDADeviceContext&>(dev_ctx);
      memory::Copy(gpu_place, dst, gpu_place, src, sizeof(T) * dst_dim.head,
                   cuda_ctx.stream());
#else
      PADDLE_THROW("Paddle is not compiled with GPU");
#endif
    }
  }
};

template <typename T, int Rank>
51
struct StridedMemcpyFunctor {
Y
Yu Yang 已提交
52 53 54 55
  void operator()(const platform::DeviceContext& dev_ctx, const T* src,
                  framework::Dim<Rank> src_stride, framework::Dim<Rank> dst_dim,
                  framework::Dim<Rank> dst_stride, T* dst) const {
    for (int64_t i = 0; i < dst_dim.head; ++i) {
56
      StridedMemcpyFunctor<T, Rank - 1> func;
Y
Yu Yang 已提交
57 58 59 60 61 62 63 64
      func(dev_ctx, src, src_stride.tail, dst_dim.tail, dst_stride.tail, dst);
      src += src_stride.head;
      dst += dst_stride.head;
    }
  }
};

template <typename T>
65 66 67 68
struct StridedCopyDimVisitor : public boost::static_visitor<void> {
  StridedCopyDimVisitor(const platform::DeviceContext& dev_ctx, const T* src,
                        const framework::DDim& src_stride,
                        const framework::DDim& dst_stride, T* dst)
Y
Yu Yang 已提交
69 70 71 72 73 74 75 76 77 78 79
      : dev_ctx_(dev_ctx),
        src_(src),
        src_stride_(src_stride),
        dst_stride_(dst_stride),
        dst_(dst) {}

  template <typename Dim>
  void operator()(Dim dst_dim) const {
    Dim src_stride = boost::get<Dim>(src_stride_);
    Dim dst_stride = boost::get<Dim>(dst_stride_);
    constexpr int dim = Dim::dimensions;
80
    StridedMemcpyFunctor<T, dim> functor;
Y
Yu Yang 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93
    functor(dev_ctx_, src_, src_stride, dst_dim, dst_stride, dst_);
  }

  const platform::DeviceContext& dev_ctx_;
  const T* src_;
  const framework::DDim& src_stride_;
  const framework::DDim& dst_stride_;
  T* dst_;
};

}  // namespace detail
}  // namespace operators
}  // namespace paddle