strided_memcpy.h 3.8 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
Yu Yang 已提交
2

L
Luo Tao 已提交
3 4 5
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
Y
Yu Yang 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
Y
Yu Yang 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
Y
Yu Yang 已提交
14 15

#pragma once
Y
Yi Wang 已提交
16 17 18
#include "paddle/fluid/framework/ddim.h"
#include "paddle/fluid/memory/memcpy.h"
#include "paddle/fluid/platform/device_context.h"
Y
Yu Yang 已提交
19 20 21 22 23 24

namespace paddle {
namespace operators {
namespace detail {

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

X
xuwei06 已提交
27 28 29
template <typename T>
struct StridedMemcpyFunctor<T, 0> {
  void operator()(const platform::DeviceContext& dev_ctx, const T* src,
S
sneaxiy 已提交
30 31
                  const int64_t* src_stride, const int64_t* dst_dim,
                  const int64_t* dst_stride, T* dst) const {
X
xuwei06 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    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));
    } else {
#ifdef PADDLE_WITH_CUDA
      auto& gpu_place = boost::get<platform::CUDAPlace>(place);
      auto& cuda_ctx =
          reinterpret_cast<const platform::CUDADeviceContext&>(dev_ctx);
      memory::Copy(gpu_place, dst, gpu_place, src, sizeof(T),
                   cuda_ctx.stream());
#else
      PADDLE_THROW("Paddle is not compiled with GPU");
#endif
    }
  }
};

Y
Yu Yang 已提交
50
template <typename T>
51
struct StridedMemcpyFunctor<T, 1> {
Y
Yu Yang 已提交
52
  void operator()(const platform::DeviceContext& dev_ctx, const T* src,
S
sneaxiy 已提交
53 54
                  const int64_t* src_stride, const int64_t* dst_dim,
                  const int64_t* dst_stride, T* dst) const {
Y
Yu Yang 已提交
55 56 57
    auto place = dev_ctx.GetPlace();
    if (platform::is_cpu_place(place)) {
      auto& cpu_place = boost::get<platform::CPUPlace>(place);
S
sneaxiy 已提交
58
      memory::Copy(cpu_place, dst, cpu_place, src, sizeof(T) * dst_dim[0]);
Y
Yu Yang 已提交
59
    } else {
60
#ifdef PADDLE_WITH_CUDA
D
dzhwinter 已提交
61
      auto& gpu_place = boost::get<platform::CUDAPlace>(place);
Y
Yu Yang 已提交
62 63
      auto& cuda_ctx =
          reinterpret_cast<const platform::CUDADeviceContext&>(dev_ctx);
S
sneaxiy 已提交
64
      memory::Copy(gpu_place, dst, gpu_place, src, sizeof(T) * dst_dim[0],
Y
Yu Yang 已提交
65 66 67 68 69 70 71 72 73
                   cuda_ctx.stream());
#else
      PADDLE_THROW("Paddle is not compiled with GPU");
#endif
    }
  }
};

template <typename T, int Rank>
74
struct StridedMemcpyFunctor {
Y
Yu Yang 已提交
75
  void operator()(const platform::DeviceContext& dev_ctx, const T* src,
S
sneaxiy 已提交
76 77 78
                  const int64_t* src_stride, const int64_t* dst_dim,
                  const int64_t* dst_stride, T* dst) const {
    for (int64_t i = 0; i < dst_dim[0]; ++i) {
79
      StridedMemcpyFunctor<T, Rank - 1> func;
S
sneaxiy 已提交
80 81 82
      func(dev_ctx, src, src_stride + 1, dst_dim + 1, dst_stride + 1, dst);
      src += src_stride[0];
      dst += dst_stride[0];
Y
Yu Yang 已提交
83 84 85 86 87
    }
  }
};

template <typename T>
S
sneaxiy 已提交
88
struct StridedCopyDimVisitor {
89 90 91
  StridedCopyDimVisitor(const platform::DeviceContext& dev_ctx, const T* src,
                        const framework::DDim& src_stride,
                        const framework::DDim& dst_stride, T* dst)
Y
Yu Yang 已提交
92 93 94 95 96 97
      : dev_ctx_(dev_ctx),
        src_(src),
        src_stride_(src_stride),
        dst_stride_(dst_stride),
        dst_(dst) {}

S
sneaxiy 已提交
98 99 100 101 102
  template <int D>
  void operator()(const framework::Dim<D>& dst_dim) const {
    StridedMemcpyFunctor<T, D> functor;
    functor(dev_ctx_, src_, src_stride_.data(), dst_dim.data(),
            dst_stride_.data(), dst_);
Y
Yu Yang 已提交
103 104 105 106 107 108 109 110 111 112 113 114
  }

  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