Tensor.h 1.2 KB
Newer Older
1 2 3 4 5 6 7
#ifndef PADDLE_OPTIMIZER_TENSOR_H_
#define PADDLE_OPTIMIZER_TENSOR_H_
/**
 * @brief tensor used by optimizer
 */

#include <string.h>
D
dzhwinter 已提交
8 9
#include "paddle/utils/Common.h"
#include "paddle/utils/Logging.h"
10 11 12 13 14

namespace paddle {
namespace optimizer {

template <class T>
D
dzhwinter 已提交
15
class TensorT {
16
public:
D
dzhwinter 已提交
17 18
  TensorT(size_t h, size_t w, T* data) : height_(h), width_(w), data_(data_) {}
  TensorT(T* data, int size) : height_(1), width_(size), data_(data) {}
D
dzhwinter 已提交
19
  TensorT(const TensorT& t)
D
dzhwinter 已提交
20
      : TensorT(1, t.size(), 0, t.get_buffer(), false, false) {}
D
dzhwinter 已提交
21
  TensorT& operator=(const TensorT& t) {
D
dzhwinter 已提交
22
    this->width_ = t.size();
D
dzhwinter 已提交
23 24
    this->data_ = t.get_buffer();
  }
25
  T* get_buffer() { return this->data_; }
26
  T& operator[](const size_t idx) {
D
dzhwinter 已提交
27
    CHECK(idx >= 0 && idx < this->width_) << "out of index range";
D
dzhwinter 已提交
28 29
    return data_[idx];
  }
30
  T& operator[](const size_t idx) const {
D
dzhwinter 已提交
31 32
    CHECK(idx >= 0 && idx < this->width_) << "out of index range";
    return data_[idx];
33
  }
34
  // TODO: replace with tensorshape
D
dzhwinter 已提交
35
  size_t size() const { return this->width_ * this->height_; }
D
dzhwinter 已提交
36 37 38 39 40

protected:
  size_t height_;
  size_t width_;
  T* data_;
41 42
};

D
dzhwinter 已提交
43
// TODO(zhihong): design problem of dynamic datatype, need to fix it
D
dzhwinter 已提交
44 45
typedef TensorT<real> Tensor;

46 47 48 49
}  // namespace optimizer
}  // namespace paddle

#endif