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
#include "paddle/fluid/framework/array.h"
Y
Yi Wang 已提交
23
#include "paddle/fluid/platform/assert.h"
S
sneaxiy 已提交
24
#include "paddle/fluid/platform/enforce.h"
Y
Yi Wang 已提交
25
#include "paddle/fluid/platform/hostdevice.h"
F
fengjiayi 已提交
26

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

100 101
}  // namespace framework
}  // namespace paddle