dim.h 2.6 KB
Newer Older
1
//  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2 3 4 5 6 7 8 9 10 11 12 13
//
// 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.
F
fengjiayi 已提交
14 15 16 17 18
#pragma once

#include <iostream>
#include <sstream>
#include <stdexcept>
S
sneaxiy 已提交
19
#include <string>
F
fengjiayi 已提交
20 21
#include <type_traits>

S
sneaxiy 已提交
22 23
#include "paddle/fluid/framework/array.h"
#include "paddle/fluid/platform/enforce.h"
Y
Yi Wang 已提交
24
#include "paddle/fluid/platform/hostdevice.h"
F
fengjiayi 已提交
25

26 27
namespace paddle {
namespace framework {
F
fengjiayi 已提交
28 29

// Statically sized, statically indexed dimension
S
sneaxiy 已提交
30 31
template <int D>
class Dim : public Array<int64_t, D> {
S
sneaxiy 已提交
32
 public:
S
sneaxiy 已提交
33
  static_assert(D >= 0, "D must be not less than 0");
F
fengjiayi 已提交
34

S
sneaxiy 已提交
35 36
  static constexpr int kRank = D;
  using BaseClass = Array<int64_t, D>;
F
fengjiayi 已提交
37

S
sneaxiy 已提交
38
  inline Dim(int64_t head, const Dim<D - 1>& tail) {
S
sneaxiy 已提交
39
    (*this)[0] = head;
S
sneaxiy 已提交
40
    new (this->GetMutable() + 1) Dim<D - 1>(tail);
S
sneaxiy 已提交
41
  }
F
fengjiayi 已提交
42

S
sneaxiy 已提交
43 44 45
  template <typename... Args>
  HOSTDEVICE explicit Dim(int64_t head, Args... args)
      : BaseClass(head, args...) {}
F
fengjiayi 已提交
46 47

  /** Construct a Dim with each dimension set to the given index */
S
sneaxiy 已提交
48
  HOSTDEVICE explicit Dim(int64_t idx) { this->Fill(idx); }
F
fengjiayi 已提交
49

S
sneaxiy 已提交
50
  HOSTDEVICE Dim() = default;
F
fengjiayi 已提交
51 52 53 54 55

  HOST std::string to_string() const;
};

// Product of a Dim
S
sneaxiy 已提交
56 57 58
template <int D>
HOSTDEVICE inline int64_t product(const Dim<D>& a) {
  return UnrollProduct<D>::Run(a.Get());
F
fengjiayi 已提交
59 60 61 62 63 64 65 66 67 68
}

/**
 * Helper function to create a Dim
 *
 * \param idxes The type of Dim constructed depends on the number of params
 *
 */

template <typename... Args>
S
sneaxiy 已提交
69
HOSTDEVICE inline Dim<sizeof...(Args)> make_dim(Args... idxes) {
F
fengjiayi 已提交
70 71 72 73
  return Dim<sizeof...(Args)>(idxes...);
}

// Allows us to output a Dim
S
sneaxiy 已提交
74 75
template <int D>
inline std::ostream& operator<<(std::ostream& os, const Dim<D>& d) {
S
sneaxiy 已提交
76
  os << d[0];
S
sneaxiy 已提交
77
  for (int i = 1; i < D; ++i) {
S
sneaxiy 已提交
78 79
    os << ", " << d[i];
  }
F
fengjiayi 已提交
80 81 82
  return os;
}

X
xuwei06 已提交
83 84 85 86
inline std::ostream& operator<<(std::ostream& os, const Dim<0>& d) {
  return os;
}

S
sneaxiy 已提交
87 88
template <int D>
HOST std::string Dim<D>::to_string() const {
F
fengjiayi 已提交
89 90 91 92 93
  std::stringstream stream;
  stream << *this;
  return stream.str();
}

S
sneaxiy 已提交
94 95 96 97 98
template <int D, typename T1, typename T2>
inline void static_dim_assign(const T1* in, T2* out) {
  UnrollAssign<D>::Run(in, out);
}

99 100
}  // namespace framework
}  // namespace paddle