unroll_array_ops.h 5.2 KB
Newer Older
S
sneaxiy 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
//
// 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
S
sneaxiy 已提交
16
#include <cstddef>
S
sneaxiy 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include <type_traits>
#include "paddle/fluid/platform/hostdevice.h"

namespace paddle {
namespace framework {

namespace detail {

template <size_t kStart, size_t kEnd, bool kStop>
struct UnrollFillConstant {
  template <typename T>
  HOSTDEVICE inline static void Run(T *data, T val) {
    data[kStart] = val;
    UnrollFillConstant<kStart + 1, kEnd, kStart + 1 == kEnd>::Run(data, val);
  }
};

template <size_t kStart, size_t kEnd>
struct UnrollFillConstant<kStart, kEnd, true> {
  template <typename T>
  HOSTDEVICE inline static void Run(T *data, T val) {}
};

template <size_t kStart, size_t kEnd, bool kStop>
struct UnrollAssign {
  template <typename Tin, typename Tout>
  HOSTDEVICE inline static void Run(const Tin *d1, Tout *d2) {
    d2[kStart] = static_cast<Tout>(d1[kStart]);
    UnrollAssign<kStart + 1, kEnd, kStart + 1 == kEnd>::Run(d1, d2);
  }
};

template <size_t kStart, size_t kEnd>
struct UnrollAssign<kStart, kEnd, true> {
  template <typename Tin, typename Tout>
  HOSTDEVICE inline static void Run(const Tin *d1, Tout *d2) {}
};

template <typename T, size_t kStart, size_t kEnd, bool kStop>
S
sneaxiy 已提交
56
struct UnrollVarArgsAssignImpl {
S
sneaxiy 已提交
57 58 59 60
  template <typename... Args>
  HOSTDEVICE inline static void Run(T *d, T val, Args... args) {
    static_assert(sizeof...(args) + 1 == kEnd - kStart, "Wrong argument");
    d[kStart] = val;
S
sneaxiy 已提交
61 62
    UnrollVarArgsAssignImpl<T, kStart + 1, kEnd, kStart + 1 == kEnd>::Run(
        d, args...);
S
sneaxiy 已提交
63 64 65 66
  }
};

template <typename T, size_t kStart, size_t kEnd>
S
sneaxiy 已提交
67
struct UnrollVarArgsAssignImpl<T, kStart, kEnd, true> {
S
sneaxiy 已提交
68 69 70
  HOSTDEVICE inline static void Run(T *d) {}
};

S
sneaxiy 已提交
71 72 73 74 75 76 77 78 79
template <typename T>
struct UnrollVarArgsAssign {
  template <typename... Args>
  HOSTDEVICE inline static void Run(T *d, Args... args) {
    UnrollVarArgsAssignImpl<T, 0, sizeof...(Args), sizeof...(Args) == 0>::Run(
        d, args...);
  }
};

S
sneaxiy 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
template <size_t kStart, size_t kEnd, bool kStop>
struct UnrollCompare {
  template <typename T>
  HOSTDEVICE inline static bool Run(const T *d1, const T *d2) {
    return d1[kStart] == d2[kStart] &&
           UnrollCompare<kStart + 1, kEnd, kStart + 1 == kEnd>::Run(d1, d2);
  }
};

template <size_t kStart, size_t kEnd>
struct UnrollCompare<kStart, kEnd, true> {
  template <typename T>
  HOSTDEVICE inline constexpr static bool Run(const T *d1, const T *d2) {
    return true;
  }
};

template <size_t kStart, size_t kEnd, bool kStop>
struct UnrollAdd {
  template <typename T>
  HOSTDEVICE inline static void Run(const T *d1, const T *d2, T *d3) {
    d3[kStart] = d1[kStart] + d2[kStart];
    UnrollAdd<kStart + 1, kEnd, kStart + 1 == kEnd>::Run(d1, d2, d3);
  }
};

template <size_t kStart, size_t kEnd>
struct UnrollAdd<kStart, kEnd, true> {
  template <typename T>
  HOSTDEVICE inline static void Run(const T *d1, const T *d2, T *d3) {}
};

template <size_t kStart, size_t kEnd, bool kStop>
struct UnrollMul {
  template <typename T>
  HOSTDEVICE inline static void Run(const T *d1, const T *d2, T *d3) {
    d3[kStart] = d1[kStart] * d2[kStart];
    UnrollMul<kStart + 1, kEnd, kStart + 1 == kEnd>::Run(d1, d2, d3);
  }
};

template <size_t kStart, size_t kEnd>
struct UnrollMul<kStart, kEnd, true> {
  template <typename T>
  HOSTDEVICE inline static void Run(const T *d1, const T *d2, T *d3) {}
};

template <size_t kStart, size_t kEnd, bool kStop>
struct UnrollProduct {
  template <typename T>
  HOSTDEVICE inline static T Run(const T *d) {
    return d[kStart] *
           UnrollProduct<kStart + 1, kEnd, kStart + 1 == kEnd>::Run(d);
  }

  template <typename T>
  HOSTDEVICE inline static T Run(const T *d1, const T *d2) {
    return d1[kStart] * d2[kStart] +
           UnrollProduct<kStart + 1, kEnd, kStart + 1 == kEnd>::Run(d1, d2);
  }
};

template <size_t kStart, size_t kEnd>
struct UnrollProduct<kStart, kEnd, true> {
  template <typename T>
  HOSTDEVICE inline constexpr static T Run(const T *d) {
    return 1;
  }

  template <typename T>
  HOSTDEVICE inline constexpr static T Run(const T *d1, const T *d2) {
    return 0;
  }
};

}  // namespace detail

template <size_t N>
using UnrollFillConstant = detail::UnrollFillConstant<0, N, N == 0>;

template <size_t N>
using UnrollAssign = detail::UnrollAssign<0, N, N == 0>;

S
sneaxiy 已提交
163 164
template <typename T>
using UnrollVarArgsAssign = detail::UnrollVarArgsAssign<T>;
S
sneaxiy 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179

template <size_t N>
using UnrollCompare = detail::UnrollCompare<0, N, N == 0>;

template <size_t N>
using UnrollAdd = detail::UnrollAdd<0, N, N == 0>;

template <size_t N>
using UnrollMul = detail::UnrollMul<0, N, N == 0>;

template <size_t N>
using UnrollProduct = detail::UnrollProduct<0, N, N == 0>;

}  // namespace framework
}  // namespace paddle